From 8ff19e29b6050221001c25ae110f82a74f545d79 Mon Sep 17 00:00:00 2001 From: Michael Mathews Date: Mon, 10 Jan 2011 21:38:47 +0000 Subject: [PATCH] Moved mustache.js into modules. Added shell script to run jsdoc.jar. --- jsdoc | 4 ++ {templates/lib => modules}/janl/mustache.js | 51 ++++++++++++++++++--- templates/default/publish.js | 2 +- templates/default/tmpl/index.mustache | 1 + 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100755 jsdoc rename {templates/lib => modules}/janl/mustache.js (87%) diff --git a/jsdoc b/jsdoc new file mode 100755 index 00000000..98876b26 --- /dev/null +++ b/jsdoc @@ -0,0 +1,4 @@ +#!/bin/sh + +basedir=`dirname $0` +java -jar ${basedir}/jsdoc.jar $@ \ No newline at end of file diff --git a/templates/lib/janl/mustache.js b/modules/janl/mustache.js similarity index 87% rename from templates/lib/janl/mustache.js rename to modules/janl/mustache.js index 82490274..caae3181 100644 --- a/templates/lib/janl/mustache.js +++ b/modules/janl/mustache.js @@ -1,3 +1,9 @@ +/* + * CommonJS-compatible mustache.js module + * + * See http://github.com/janl/mustache.js for more info. + */ + /* mustache.js — Logic-less templates in JavaScript @@ -13,7 +19,8 @@ var Mustache = function() { pragmas: {}, buffer: [], pragmas_implemented: { - "IMPLICIT-ITERATOR": true + "IMPLICIT-ITERATOR": true, + "ARRAY-ORDINALS": true // define #first? and #last? when looping arrays }, context: {}, @@ -122,7 +129,7 @@ var Mustache = function() { if(that.is_array(value)) { // Enumerable, Let's loop! var len = value.length; return value.map(function(row, i) { - return that.render(content, that.create_context(row, {last: i === len-1}), + return that.render(content, that.create_context(row, {first: i === 0, last: i === len-1}), partials, true); }).join(""); } else if(that.is_object(value)) { // Object, Use it as subcontext! @@ -245,11 +252,12 @@ var Mustache = function() { */ escape: function(s) { s = String(s === null ? "" : s); - return s.replace(/&(?!\w+;)|["<>\\]/g, function(s) { + return s.replace(/&(?!\w+;)|["'<>\\]/g, function(s) { switch(s) { case "&": return "&"; case "\\": return "\\\\"; - case '"': return '\"'; + case '"': return '"'; + case "'": return '''; case "<": return "<"; case ">": return ">"; default: return s; @@ -260,7 +268,10 @@ var Mustache = function() { // by @langalex, support for arrays of strings create_context: function(_context, opts) { if(this.is_object(_context)) { - if (opts){ _context['last?'] = opts.last || false; } + if (this.pragmas["ARRAY-ORDINALS"] && opts) { + _context['first?'] = opts.first || false; + _context['last?'] = opts.last || false; + } return _context; } else { var iterator = "."; @@ -269,7 +280,10 @@ var Mustache = function() { } var ctx = {}; ctx[iterator] = _context; - if (opts){ ctx['last?'] = opts.last || false; } + if (this.pragmas["ARRAY-ORDINALS"] && opts){ + ctx['first?'] = opts.first || false; + ctx['last?'] = opts.last || false; + } return ctx; } }, @@ -287,12 +301,28 @@ var Mustache = function() { */ trim: function(s) { return s.replace(/^\s*|\s*$/g, ""); + }, + + /* + Why, why, why? Because IE. Cry, cry cry. + */ + map: function(array, fn) { + if (typeof array.map == "function") { + return array.map(fn); + } else { + var r = []; + var l = array.length; + for(var i = 0; i < l; i++) { + r.push(fn(array[i])); + } + return r; + } } }; return({ name: "mustache.js", - version: "0.3.0", + version: "0.3.1-dev", /* Turns a template and view into HTML @@ -309,3 +339,10 @@ var Mustache = function() { } }); }(); + +exports.name = Mustache.name; +exports.version = Mustache.version; + +exports.to_html = function() { + return Mustache.to_html.apply(this, arguments); +}; diff --git a/templates/default/publish.js b/templates/default/publish.js index 06daf658..6120e4fe 100644 --- a/templates/default/publish.js +++ b/templates/default/publish.js @@ -1,6 +1,6 @@ (function() { - include('templates/lib/janl/mustache.js'); + var Mustache = require('janl/mustache'); /** @global diff --git a/templates/default/tmpl/index.mustache b/templates/default/tmpl/index.mustache index dc3d3f77..1c9f6452 100644 --- a/templates/default/tmpl/index.mustache +++ b/templates/default/tmpl/index.mustache @@ -1,3 +1,4 @@ +{{%ARRAY-ORDINALS}}