From 7198e2c5784fb978130b6bf1653975556ac5cac0 Mon Sep 17 00:00:00 2001 From: Brian Kierstead Date: Mon, 26 Apr 2010 00:45:40 -0400 Subject: [PATCH 01/29] simple iterator adding \n bug --- examples/simple_iterator.html | 1 + examples/simple_iterator.js | 7 +++++++ examples/simple_iterator.txt | 1 + 3 files changed, 9 insertions(+) create mode 100644 examples/simple_iterator.html create mode 100644 examples/simple_iterator.js create mode 100644 examples/simple_iterator.txt diff --git a/examples/simple_iterator.html b/examples/simple_iterator.html new file mode 100644 index 0000000..4bfa59e --- /dev/null +++ b/examples/simple_iterator.html @@ -0,0 +1 @@ +{{#names}}{{name}}{{/names}} \ No newline at end of file diff --git a/examples/simple_iterator.js b/examples/simple_iterator.js new file mode 100644 index 0000000..6cbfccd --- /dev/null +++ b/examples/simple_iterator.js @@ -0,0 +1,7 @@ +var simple_iterator = { + names:[ + {name:"brian"}, + {name:"dave"}, + {name:"steve"} + ] +}; \ No newline at end of file diff --git a/examples/simple_iterator.txt b/examples/simple_iterator.txt new file mode 100644 index 0000000..4b11fb4 --- /dev/null +++ b/examples/simple_iterator.txt @@ -0,0 +1 @@ +briandavesteve \ No newline at end of file From 43587531c881f7baa7a5c7314ebed15111459f76 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 14:39:59 +0700 Subject: [PATCH 02/29] Build CommonJS just like any other target, using "pre" and "post" wrappers --- .gitignore | 1 + Rakefile | 5 +++-- mustache-commonjs/commonjs.mustache.js.tpl.post | 7 +++++++ mustache-commonjs/commonjs.mustache.js.tpl.pre | 6 ++++++ 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 mustache-commonjs/commonjs.mustache.js.tpl.post create mode 100644 mustache-commonjs/commonjs.mustache.js.tpl.pre diff --git a/.gitignore b/.gitignore index 252c5ae..a7ed15c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ runner.js jquery.mustache.js dojox yui3 +commonjs.mustache.js diff --git a/Rakefile b/Rakefile index 3298479..2946e97 100644 --- a/Rakefile +++ b/Rakefile @@ -14,8 +14,9 @@ task :spec desc "Package for CommonJS" task :commonjs do puts "Packaging for CommonJS" - `mkdir lib` - `cp mustache.js lib/mustache.js` + source = "mustache-commonjs" + target_jq = "commonjs.mustache.js" + `cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}` puts "Done." end diff --git a/mustache-commonjs/commonjs.mustache.js.tpl.post b/mustache-commonjs/commonjs.mustache.js.tpl.post new file mode 100644 index 0000000..885803f --- /dev/null +++ b/mustache-commonjs/commonjs.mustache.js.tpl.post @@ -0,0 +1,7 @@ + +exports.name = Mustache.name; +exports.version = Mustache.version; + +exports.to_html = function() { + return Mustache.to_html.apply(this, arguments); +}; diff --git a/mustache-commonjs/commonjs.mustache.js.tpl.pre b/mustache-commonjs/commonjs.mustache.js.tpl.pre new file mode 100644 index 0000000..5e33b34 --- /dev/null +++ b/mustache-commonjs/commonjs.mustache.js.tpl.pre @@ -0,0 +1,6 @@ +/* + * CommonJS-compatible mustache.js module + * + * See http://github.com/janl/mustache.js for more info. + */ + From f54312e879be6217be407b14f66296d4870200b9 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 14:43:44 +0700 Subject: [PATCH 03/29] Execute commands properly. You can see what's happening, and errors halt the build --- Rakefile | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Rakefile b/Rakefile index 2946e97..213bbda 100644 --- a/Rakefile +++ b/Rakefile @@ -16,7 +16,7 @@ task :commonjs do puts "Packaging for CommonJS" source = "mustache-commonjs" target_jq = "commonjs.mustache.js" - `cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}` + sh "cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}" puts "Done." end @@ -25,7 +25,7 @@ task :jquery do puts "Packaging for jQuery" source = "mustache-jquery" target_jq = "jquery.mustache.js" - `cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}` + sh "cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}" puts "Done, see ./#{target_jq}" end @@ -34,8 +34,8 @@ task :dojo do puts "Packaging for dojo" source = "mustache-dojo" target_js = "mustache.js" - `mkdir -p dojox; mkdir -p dojox/string` - `cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > dojox/string/#{target_js}` + sh "mkdir -p dojox; mkdir -p dojox/string" + sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > dojox/string/#{target_js}" puts "Done, see ./dojox/string/#{target_js} Include using dojo.require('dojox.string.mustache.'); " end @@ -44,12 +44,12 @@ task :yui3 do puts "Packaging for YUI3" source = "mustache-yui3" target_js = "mustache.js" - `mkdir -p yui3; mkdir -p yui3/mustache` - `cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > yui3/mustache/#{target_js}` + sh "kdir -p yui3; mkdir -p yui3/mustache" + sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > yui3/mustache/#{target_js}" puts "Done, see ./yui3/mustache/#{target_js}" end desc "Remove temporary files." task :clean do - `git clean -fdx` + sh "git clean -fdx" end From 01272005fe9e0ca9ee96ea4ce2127fd310fc86c6 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 15:08:28 +0700 Subject: [PATCH 04/29] Refactor template-style builds into a common function --- Rakefile | 54 +++++++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/Rakefile b/Rakefile index 213bbda..9e7712c 100644 --- a/Rakefile +++ b/Rakefile @@ -11,43 +11,27 @@ end desc "Run all specs" task :spec -desc "Package for CommonJS" -task :commonjs do - puts "Packaging for CommonJS" - source = "mustache-commonjs" - target_jq = "commonjs.mustache.js" - sh "cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}" - puts "Done." +def templated_build(name, opts={}) + # Create a rule that uses the .tmpl.{pre,post} stuff to make a final, wrapped, output file. + # There is some extra complexity because Dojo and YUI3 use different template files and final locations. + short = name.downcase + source = "mustache-#{short}" + dependencies = ["mustache.js"] + Dir.glob("#{source}/*.tpl.*") + + desc "Package for #{name}" + task short.to_sym => dependencies do + target_js = opts[:location] ? "mustache.js" : "#{short}.mustache.js" + + puts "Packaging for #{name}" + sh "mkdir -p #{opts[:location]}" if opts[:location] + sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > #{opts[:location] || '.'}/#{target_js}" + end end -desc "Package for jQuery" -task :jquery do - puts "Packaging for jQuery" - source = "mustache-jquery" - target_jq = "jquery.mustache.js" - sh "cat #{source}/#{target_jq}.tpl.pre mustache.js #{source}/#{target_jq}.tpl.post > #{target_jq}" - puts "Done, see ./#{target_jq}" -end - -desc "Package for dojo" -task :dojo do - puts "Packaging for dojo" - source = "mustache-dojo" - target_js = "mustache.js" - sh "mkdir -p dojox; mkdir -p dojox/string" - sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > dojox/string/#{target_js}" - puts "Done, see ./dojox/string/#{target_js} Include using dojo.require('dojox.string.mustache.'); " -end - -desc "Package for YUI3" -task :yui3 do - puts "Packaging for YUI3" - source = "mustache-yui3" - target_js = "mustache.js" - sh "kdir -p yui3; mkdir -p yui3/mustache" - sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > yui3/mustache/#{target_js}" - puts "Done, see ./yui3/mustache/#{target_js}" -end +templated_build "CommonJS" +templated_build "jQuery" +templated_build "Dojo", :location => "dojox/string" +templated_build "YUI3", :location => "yui3/mustache" desc "Remove temporary files." task :clean do From d82a1174d768c0de6e8e70e75a3366f2866b7fc4 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 15:21:31 +0700 Subject: [PATCH 05/29] Actually ignore comment tags; and correct the expected output in the test case --- examples/comments.txt | 2 +- mustache.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/comments.txt b/examples/comments.txt index 071e026..0133517 100644 --- a/examples/comments.txt +++ b/examples/comments.txt @@ -1 +1 @@ -

A Comedy of Errors{{! just something interesting... or not... }}

+

A Comedy of Errors

diff --git a/mustache.js b/mustache.js index fe87d9f..95088d2 100644 --- a/mustache.js +++ b/mustache.js @@ -157,7 +157,7 @@ var Mustache = function() { lines[i] = lines[i].replace(regex, function(match, operator, name) { switch(operator) { case "!": // ignore comments - return match; + return ""; case "=": // set new delimiters, rebuild the replace regexp that.set_delimiters(name); regex = new_regex(); From 0117a42532a834a0d0c3e9471a267e97e47a36bc Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sun, 9 May 2010 16:16:13 +0700 Subject: [PATCH 06/29] Output the "Done" message as before --- Rakefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Rakefile b/Rakefile index 9e7712c..2941527 100644 --- a/Rakefile +++ b/Rakefile @@ -25,6 +25,7 @@ def templated_build(name, opts={}) puts "Packaging for #{name}" sh "mkdir -p #{opts[:location]}" if opts[:location] sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > #{opts[:location] || '.'}/#{target_js}" + puts "Done, see #{opts[:location] || '.'}/#{target_js}" end end From a7464db3738cf3bae94e84cf6172206ff874e293 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Tue, 18 May 2010 14:27:24 +0200 Subject: [PATCH 07/29] remove lib, use rake commonjs for a commonjs package --- lib/mustache.js | 296 ------------------------------------------------ 1 file changed, 296 deletions(-) delete mode 100644 lib/mustache.js diff --git a/lib/mustache.js b/lib/mustache.js deleted file mode 100644 index ecfefcb..0000000 --- a/lib/mustache.js +++ /dev/null @@ -1,296 +0,0 @@ -/* - mustache.js — Logic-less templates in JavaScript - - See http://mustache.github.com/ for more info. -*/ - -var Mustache = function() { - var Renderer = function() {}; - - Renderer.prototype = { - otag: "{{", - ctag: "}}", - pragmas: {}, - buffer: [], - pragmas_implemented: { - "IMPLICIT-ITERATOR": true - }, - - render: function(template, context, partials, in_recursion) { - // fail fast - if(template.indexOf(this.otag) == -1) { - if(in_recursion) { - return template; - } else { - this.send(template); - return; - } - } - - if(!in_recursion) { - this.buffer = []; - } - - template = this.render_pragmas(template); - var html = this.render_section(template, context, partials); - if(in_recursion) { - return this.render_tags(html, context, partials, in_recursion); - } - - this.render_tags(html, context, partials, in_recursion); - }, - - /* - Sends parsed lines - */ - send: function(line) { - if(line != "") { - this.buffer.push(line); - } - }, - - /* - Looks for %PRAGMAS - */ - render_pragmas: function(template) { - // no pragmas - if(template.indexOf(this.otag + "%") == -1) { - return template; - } - - var that = this; - var regex = new RegExp(this.otag + "%([\\w_-]+) ?([\\w]+=[\\w]+)?" - + this.ctag); - return template.replace(regex, function(match, pragma, options) { - if(!that.pragmas_implemented[pragma]) { - throw({message: "This implementation of mustache doesn't understand the '" - + pragma + "' pragma"}); - } - that.pragmas[pragma] = {}; - if(options) { - var opts = options.split("="); - that.pragmas[pragma][opts[0]] = opts[1]; - } - return ""; - // ignore unknown pragmas silently - }); - }, - - /* - Tries to find a partial in the global scope and render it - */ - render_partial: function(name, context, partials) { - if(!partials || !partials[name]) { - throw({message: "unknown_partial '" + name + "'"}); - } - if(typeof(context[name]) != "object") { - return partials[name]; - } - return this.render(partials[name], context[name], partials, true); - }, - - /* - Renders boolean and enumerable sections - */ - render_section: function(template, context, partials) { - if(template.indexOf(this.otag + "#") == -1) { - return template; - } - var that = this; - // CSW - Added "+?" so it finds the tighest bound, not the widest - var regex = new RegExp(this.otag + "\\#(.+)" + this.ctag + - "\\s*([\\s\\S]+?)" + this.otag + "\\/\\1" + this.ctag + "\\s*", "mg"); - - // for each {{#foo}}{{/foo}} section do... - return template.replace(regex, function(match, name, content) { - var value = that.find(name, context); - if(that.is_array(value)) { // Enumerable, Let's loop! - return that.map(value, function(row) { - return that.render(content, that.merge(context, - that.create_context(row)), partials, true); - }).join(""); - } else if(value) { // boolean section - return that.render(content, context, partials, true); - } else { - return ""; - } - }); - }, - - /* - Replace {{foo}} and friends with values from our view - */ - render_tags: function(template, context, partials, in_recursion) { - // tit for tat - var that = this; - - var new_regex = function() { - return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\/#]+?)\\1?" + - that.ctag + "+", "g"); - }; - - var regex = new_regex(); - var lines = template.split("\n"); - for (var i=0; i < lines.length; i++) { - lines[i] = lines[i].replace(regex, function(match, operator, name) { - switch(operator) { - case "!": // ignore comments - return match; - case "=": // set new delimiters, rebuild the replace regexp - that.set_delimiters(name); - regex = new_regex(); - return ""; - case ">": // render partial - return that.render_partial(name, context, partials); - case "{": // the triple mustache is unescaped - return that.find(name, context); - default: // escape the value - return that.escape(that.find(name, context)); - } - }, this); - if(!in_recursion) { - this.send(lines[i]); - } - } - - if(in_recursion) { - return lines.join("\n"); - } - }, - - set_delimiters: function(delimiters) { - var dels = delimiters.split(" "); - this.otag = this.escape_regex(dels[0]); - this.ctag = this.escape_regex(dels[1]); - }, - - escape_regex: function(text) { - // thank you Simon Willison - if(!arguments.callee.sRE) { - var specials = [ - '/', '.', '*', '+', '?', '|', - '(', ')', '[', ']', '{', '}', '\\' - ]; - arguments.callee.sRE = new RegExp( - '(\\' + specials.join('|\\') + ')', 'g' - ); - } - return text.replace(arguments.callee.sRE, '\\$1'); - }, - - /* - find `name` in current `context`. That is find me a value - from the view object - */ - find: function(name, context) { - name = this.trim(name); - if(typeof context[name] === "function") { - return context[name].apply(context); - } - if(context[name] !== undefined) { - return context[name]; - } - // silently ignore unkown variables - return ""; - }, - - // Utility methods - - /* - Does away with nasty characters - */ - escape: function(s) { - return ((s == null) ? "" : s).toString().replace(/[&"<>\\]/g, function(s) { - switch(s) { - case "&": return "&"; - case "\\": return "\\\\";; - case '"': return '\"';; - case "<": return "<"; - case ">": return ">"; - default: return s; - } - }); - }, - - /* - Merges all properties of object `b` into object `a`. - `b.property` overwrites a.property` - */ - merge: function(a, b) { - var _new = {}; - for(var name in a) { - if(a.hasOwnProperty(name)) { - _new[name] = a[name]; - } - }; - for(var name in b) { - if(b.hasOwnProperty(name)) { - _new[name] = b[name]; - } - }; - return _new; - }, - - // by @langalex, support for arrays of strings - create_context: function(_context) { - if(this.is_object(_context)) { - return _context; - } else if(this.pragmas["IMPLICIT-ITERATOR"]) { - var iterator = this.pragmas["IMPLICIT-ITERATOR"].iterator || "."; - var ctx = {}; - ctx[iterator] = _context; - return ctx; - } - }, - - is_object: function(a) { - return a && typeof a == "object"; - }, - - is_array: function(a) { - return Object.prototype.toString.call(a) === '[object Array]'; - }, - - /* - Gets rid of leading and trailing whitespace - */ - 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 Date: Tue, 18 May 2010 14:43:25 +0200 Subject: [PATCH 08/29] fix rake commonjs --- mustache-commonjs/mustache.js.tpl.post | 7 +++++++ mustache-commonjs/mustache.js.tpl.pre | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 mustache-commonjs/mustache.js.tpl.post create mode 100644 mustache-commonjs/mustache.js.tpl.pre diff --git a/mustache-commonjs/mustache.js.tpl.post b/mustache-commonjs/mustache.js.tpl.post new file mode 100644 index 0000000..885803f --- /dev/null +++ b/mustache-commonjs/mustache.js.tpl.post @@ -0,0 +1,7 @@ + +exports.name = Mustache.name; +exports.version = Mustache.version; + +exports.to_html = function() { + return Mustache.to_html.apply(this, arguments); +}; diff --git a/mustache-commonjs/mustache.js.tpl.pre b/mustache-commonjs/mustache.js.tpl.pre new file mode 100644 index 0000000..5e33b34 --- /dev/null +++ b/mustache-commonjs/mustache.js.tpl.pre @@ -0,0 +1,6 @@ +/* + * CommonJS-compatible mustache.js module + * + * See http://github.com/janl/mustache.js for more info. + */ + From 88d3d9cf32c121096b1a332c3ddffc0b36290cfb Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Tue, 18 May 2010 14:47:50 +0200 Subject: [PATCH 09/29] fix rake commonjs part 2 --- Rakefile | 11 +++++++---- mustache-commonjs/commonjs.mustache.js.tpl.post | 7 ------- mustache-commonjs/commonjs.mustache.js.tpl.pre | 6 ------ 3 files changed, 7 insertions(+), 17 deletions(-) delete mode 100644 mustache-commonjs/commonjs.mustache.js.tpl.post delete mode 100644 mustache-commonjs/commonjs.mustache.js.tpl.pre diff --git a/Rakefile b/Rakefile index 2941527..e27970c 100644 --- a/Rakefile +++ b/Rakefile @@ -12,8 +12,10 @@ desc "Run all specs" task :spec def templated_build(name, opts={}) - # Create a rule that uses the .tmpl.{pre,post} stuff to make a final, wrapped, output file. - # There is some extra complexity because Dojo and YUI3 use different template files and final locations. + # Create a rule that uses the .tmpl.{pre,post} stuff to make a final, + # wrapped, output file. + # There is some extra complexity because Dojo and YUI3 use different + # template files and final locations. short = name.downcase source = "mustache-#{short}" dependencies = ["mustache.js"] + Dir.glob("#{source}/*.tpl.*") @@ -24,12 +26,13 @@ def templated_build(name, opts={}) puts "Packaging for #{name}" sh "mkdir -p #{opts[:location]}" if opts[:location] - sh "cat #{source}/#{target_js}.tpl.pre mustache.js #{source}/#{target_js}.tpl.post > #{opts[:location] || '.'}/#{target_js}" + sh "cat #{source}/#{target_js}.tpl.pre mustache.js \ + #{source}/#{target_js}.tpl.post > #{opts[:location] || '.'}/#{target_js}" puts "Done, see #{opts[:location] || '.'}/#{target_js}" end end -templated_build "CommonJS" +templated_build "CommonJS", :location => "lib" templated_build "jQuery" templated_build "Dojo", :location => "dojox/string" templated_build "YUI3", :location => "yui3/mustache" diff --git a/mustache-commonjs/commonjs.mustache.js.tpl.post b/mustache-commonjs/commonjs.mustache.js.tpl.post deleted file mode 100644 index 885803f..0000000 --- a/mustache-commonjs/commonjs.mustache.js.tpl.post +++ /dev/null @@ -1,7 +0,0 @@ - -exports.name = Mustache.name; -exports.version = Mustache.version; - -exports.to_html = function() { - return Mustache.to_html.apply(this, arguments); -}; diff --git a/mustache-commonjs/commonjs.mustache.js.tpl.pre b/mustache-commonjs/commonjs.mustache.js.tpl.pre deleted file mode 100644 index 5e33b34..0000000 --- a/mustache-commonjs/commonjs.mustache.js.tpl.pre +++ /dev/null @@ -1,6 +0,0 @@ -/* - * CommonJS-compatible mustache.js module - * - * See http://github.com/janl/mustache.js for more info. - */ - From f1f55138a4a6e6e26022a1c00ac09b1154e714c3 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Tue, 18 May 2010 14:48:40 +0200 Subject: [PATCH 10/29] fix expected output --- examples/simple_iterator.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_iterator.txt b/examples/simple_iterator.txt index 4b11fb4..57e28e6 100644 --- a/examples/simple_iterator.txt +++ b/examples/simple_iterator.txt @@ -1 +1 @@ -briandavesteve \ No newline at end of file +briandavesteve From 73b463a30e15cda8151360bb99d106a8c89f3b95 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Tue, 18 May 2010 14:58:23 +0200 Subject: [PATCH 11/29] Revert "fix expected output" This reverts commit f1f55138a4a6e6e26022a1c00ac09b1154e714c3. --- examples/simple_iterator.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_iterator.txt b/examples/simple_iterator.txt index 57e28e6..4b11fb4 100644 --- a/examples/simple_iterator.txt +++ b/examples/simple_iterator.txt @@ -1 +1 @@ -briandavesteve +briandavesteve \ No newline at end of file From 595f3fbf357a6c36bed37f3b7e63a268ad3ff60c Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Tue, 18 May 2010 15:01:45 +0200 Subject: [PATCH 12/29] Revert "Merge remote branch 'rbriank/master'" This reverts commit c2f886602af6ff2d6790b33f87445fc2a5677e0e, reversing changes made to 88d3d9cf32c121096b1a332c3ddffc0b36290cfb. --- examples/simple_iterator.html | 1 - examples/simple_iterator.js | 7 ------- examples/simple_iterator.txt | 1 - 3 files changed, 9 deletions(-) delete mode 100644 examples/simple_iterator.html delete mode 100644 examples/simple_iterator.js delete mode 100644 examples/simple_iterator.txt diff --git a/examples/simple_iterator.html b/examples/simple_iterator.html deleted file mode 100644 index 4bfa59e..0000000 --- a/examples/simple_iterator.html +++ /dev/null @@ -1 +0,0 @@ -{{#names}}{{name}}{{/names}} \ No newline at end of file diff --git a/examples/simple_iterator.js b/examples/simple_iterator.js deleted file mode 100644 index 6cbfccd..0000000 --- a/examples/simple_iterator.js +++ /dev/null @@ -1,7 +0,0 @@ -var simple_iterator = { - names:[ - {name:"brian"}, - {name:"dave"}, - {name:"steve"} - ] -}; \ No newline at end of file diff --git a/examples/simple_iterator.txt b/examples/simple_iterator.txt deleted file mode 100644 index 4b11fb4..0000000 --- a/examples/simple_iterator.txt +++ /dev/null @@ -1 +0,0 @@ -briandavesteve \ No newline at end of file From e25eeaebb82bb76ae93aefdb7b50baa5cf94c67d Mon Sep 17 00:00:00 2001 From: Aaron Gibralter Date: Wed, 12 May 2010 15:46:49 -0400 Subject: [PATCH 13/29] Allow arbitrary whitespace within tags. Closes #34. --- examples/whitespace_partial.2.html | 5 +++++ examples/whitespace_partial.html | 3 +++ examples/whitespace_partial.js | 19 +++++++++++++++++++ examples/whitespace_partial.txt | 6 ++++++ mustache.js | 5 +++-- 5 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 examples/whitespace_partial.2.html create mode 100644 examples/whitespace_partial.html create mode 100644 examples/whitespace_partial.js create mode 100644 examples/whitespace_partial.txt diff --git a/examples/whitespace_partial.2.html b/examples/whitespace_partial.2.html new file mode 100644 index 0000000..9c46084 --- /dev/null +++ b/examples/whitespace_partial.2.html @@ -0,0 +1,5 @@ +Hello {{ name}} +You have just won ${{value }}! +{{# in_ca }} +Well, ${{ taxed_value }}, after taxes. +{{/ in_ca }} diff --git a/examples/whitespace_partial.html b/examples/whitespace_partial.html new file mode 100644 index 0000000..ce43cb3 --- /dev/null +++ b/examples/whitespace_partial.html @@ -0,0 +1,3 @@ +

{{ greeting }}

+{{> partial }} +

{{ farewell }}

\ No newline at end of file diff --git a/examples/whitespace_partial.js b/examples/whitespace_partial.js new file mode 100644 index 0000000..30ade55 --- /dev/null +++ b/examples/whitespace_partial.js @@ -0,0 +1,19 @@ +var partial_context = { + greeting: function() { + return "Welcome"; + }, + + farewell: function() { + return "Fair enough, right?"; + }, + + partial: { + name: "Chris", + value: 10000, + taxed_value: function() { + return this.value - (this.value * 0.4); + }, + in_ca: true + } +}; + diff --git a/examples/whitespace_partial.txt b/examples/whitespace_partial.txt new file mode 100644 index 0000000..160b0b6 --- /dev/null +++ b/examples/whitespace_partial.txt @@ -0,0 +1,6 @@ +

Welcome

+Hello Chris +You have just won $10000! +Well, $6000, after taxes. + +

Fair enough, right?

diff --git a/mustache.js b/mustache.js index 4d48090..b6d60e4 100644 --- a/mustache.js +++ b/mustache.js @@ -84,6 +84,7 @@ var Mustache = function() { Tries to find a partial in the global scope and render it */ render_partial: function(name, context, partials) { + name = this.trim(name); if(!partials || !partials[name]) { throw({message: "unknown_partial '" + name + "'"}); } @@ -103,8 +104,8 @@ var Mustache = function() { var that = this; // CSW - Added "+?" so it finds the tighest bound, not the widest - var regex = new RegExp(this.otag + "(\\^|\\#)(.+)" + this.ctag + - "\\s*([\\s\\S]+?)" + this.otag + "\\/\\2" + this.ctag + + var regex = new RegExp(this.otag + "(\\^|\\#)\\s*(.+)\\s*" + this.ctag + + "\\s*([\\s\\S]+?)" + this.otag + "\\/\\s*\\2\\s*" + this.ctag + "\\s*", "mg"); // for each {{#foo}}{{/foo}} section do... From 53e9b2692b9ee03f6e902161421c6ff41e93a71b Mon Sep 17 00:00:00 2001 From: Aaron Gibralter Date: Wed, 12 May 2010 17:24:59 -0400 Subject: [PATCH 14/29] Partials within list sections did not seem to be functioning correctly. Since a list section pushes a sub-object of the "view" into the "current" context, a partial should recognize this altered context. The one-line change in mustache.js does exactly this. Closes #35. --- examples/array_of_partials_implicit_partial.2.html | 1 + examples/array_of_partials_implicit_partial.html | 5 +++++ examples/array_of_partials_implicit_partial.js | 3 +++ examples/array_of_partials_implicit_partial.txt | 5 +++++ examples/array_of_partials_partial.2.html | 1 + examples/array_of_partials_partial.html | 4 ++++ examples/array_of_partials_partial.js | 3 +++ examples/array_of_partials_partial.txt | 5 +++++ mustache.js | 2 +- 9 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 examples/array_of_partials_implicit_partial.2.html create mode 100644 examples/array_of_partials_implicit_partial.html create mode 100644 examples/array_of_partials_implicit_partial.js create mode 100644 examples/array_of_partials_implicit_partial.txt create mode 100644 examples/array_of_partials_partial.2.html create mode 100644 examples/array_of_partials_partial.html create mode 100644 examples/array_of_partials_partial.js create mode 100644 examples/array_of_partials_partial.txt diff --git a/examples/array_of_partials_implicit_partial.2.html b/examples/array_of_partials_implicit_partial.2.html new file mode 100644 index 0000000..12f7159 --- /dev/null +++ b/examples/array_of_partials_implicit_partial.2.html @@ -0,0 +1 @@ +{{.}} diff --git a/examples/array_of_partials_implicit_partial.html b/examples/array_of_partials_implicit_partial.html new file mode 100644 index 0000000..11537e9 --- /dev/null +++ b/examples/array_of_partials_implicit_partial.html @@ -0,0 +1,5 @@ +Here is some stuff! +{{%IMPLICIT-ITERATOR}} +{{#numbers}} +{{>partial}} +{{/numbers}} diff --git a/examples/array_of_partials_implicit_partial.js b/examples/array_of_partials_implicit_partial.js new file mode 100644 index 0000000..fcdf3b0 --- /dev/null +++ b/examples/array_of_partials_implicit_partial.js @@ -0,0 +1,3 @@ +var partial_context = { + numbers: ['1', '2', '3', '4'] +}; diff --git a/examples/array_of_partials_implicit_partial.txt b/examples/array_of_partials_implicit_partial.txt new file mode 100644 index 0000000..f622375 --- /dev/null +++ b/examples/array_of_partials_implicit_partial.txt @@ -0,0 +1,5 @@ +Here is some stuff! +1 +2 +3 +4 diff --git a/examples/array_of_partials_partial.2.html b/examples/array_of_partials_partial.2.html new file mode 100644 index 0000000..bdde77d --- /dev/null +++ b/examples/array_of_partials_partial.2.html @@ -0,0 +1 @@ +{{i}} diff --git a/examples/array_of_partials_partial.html b/examples/array_of_partials_partial.html new file mode 100644 index 0000000..1af6d68 --- /dev/null +++ b/examples/array_of_partials_partial.html @@ -0,0 +1,4 @@ +Here is some stuff! +{{#numbers}} +{{>partial}} +{{/numbers}} diff --git a/examples/array_of_partials_partial.js b/examples/array_of_partials_partial.js new file mode 100644 index 0000000..45611cf --- /dev/null +++ b/examples/array_of_partials_partial.js @@ -0,0 +1,3 @@ +var partial_context = { + numbers: [{i: '1'}, {i: '2'}, {i: '3'}, {i: '4'}] +}; diff --git a/examples/array_of_partials_partial.txt b/examples/array_of_partials_partial.txt new file mode 100644 index 0000000..f622375 --- /dev/null +++ b/examples/array_of_partials_partial.txt @@ -0,0 +1,5 @@ +Here is some stuff! +1 +2 +3 +4 diff --git a/mustache.js b/mustache.js index b6d60e4..b224249 100644 --- a/mustache.js +++ b/mustache.js @@ -89,7 +89,7 @@ var Mustache = function() { throw({message: "unknown_partial '" + name + "'"}); } if(typeof(context[name]) != "object") { - return partials[name]; + return this.render(partials[name], context, partials, true); } return this.render(partials[name], context[name], partials, true); }, From 4e01d9faf6ea738502467512ac543519fba32fc4 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Tue, 18 May 2010 15:24:02 +0200 Subject: [PATCH 15/29] update THANKS, CHANGES --- CHANGES.md | 4 ++++ THANKS.md | 1 + 2 files changed, 5 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 8f10522..cdd3858 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## 0.3.0 (??-??-????) +* CommonJS packaging is no longer a special case. +* DRY Rakefile. +* Allow whitespace around tag names. +* Fix partial scope. * Fix Comments. * Added inverted sections. * Avoid double encoding of entities. diff --git a/THANKS.md b/THANKS.md index 67cd3a0..9843374 100644 --- a/THANKS.md +++ b/THANKS.md @@ -16,3 +16,4 @@ Mustache.js wouldn't kick ass if it weren't for these fine souls: * Will Leinweber / will * dpree * Jason Smith / jhs + * Aaron Gibralter / agibralter From 885501df7df5a709d45ccc6e65295c3921ad7e54 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Tue, 18 May 2010 15:29:57 +0200 Subject: [PATCH 16/29] Add test case to close #11. No bugfix necessary. --- examples/bug_11_eating_whitespace.html | 1 + examples/bug_11_eating_whitespace.js | 3 +++ examples/bug_11_eating_whitespace.txt | 1 + 3 files changed, 5 insertions(+) create mode 100644 examples/bug_11_eating_whitespace.html create mode 100644 examples/bug_11_eating_whitespace.js create mode 100644 examples/bug_11_eating_whitespace.txt diff --git a/examples/bug_11_eating_whitespace.html b/examples/bug_11_eating_whitespace.html new file mode 100644 index 0000000..8d5cd92 --- /dev/null +++ b/examples/bug_11_eating_whitespace.html @@ -0,0 +1 @@ +{{tag}} foo diff --git a/examples/bug_11_eating_whitespace.js b/examples/bug_11_eating_whitespace.js new file mode 100644 index 0000000..78ce975 --- /dev/null +++ b/examples/bug_11_eating_whitespace.js @@ -0,0 +1,3 @@ +var bug_11_eating_whitespace = { + tag: "yo" +}; diff --git a/examples/bug_11_eating_whitespace.txt b/examples/bug_11_eating_whitespace.txt new file mode 100644 index 0000000..f5bbc85 --- /dev/null +++ b/examples/bug_11_eating_whitespace.txt @@ -0,0 +1 @@ +yo foo From dd19083d5ff1f98745417294be92397d0789eeed Mon Sep 17 00:00:00 2001 From: endor Date: Tue, 18 May 2010 18:10:07 +0300 Subject: [PATCH 17/29] tests for partial recursion --- examples/partial_recursion.2.html | 4 ++++ examples/partial_recursion.html | 4 ++++ examples/partial_recursion.js | 11 +++++++++++ examples/partial_recursion.txt | 3 +++ 4 files changed, 22 insertions(+) create mode 100644 examples/partial_recursion.2.html create mode 100644 examples/partial_recursion.html create mode 100644 examples/partial_recursion.js create mode 100644 examples/partial_recursion.txt diff --git a/examples/partial_recursion.2.html b/examples/partial_recursion.2.html new file mode 100644 index 0000000..c002d68 --- /dev/null +++ b/examples/partial_recursion.2.html @@ -0,0 +1,4 @@ +{{name}} +{{#children}} + {{>partial}} +{{/children}} \ No newline at end of file diff --git a/examples/partial_recursion.html b/examples/partial_recursion.html new file mode 100644 index 0000000..c002d68 --- /dev/null +++ b/examples/partial_recursion.html @@ -0,0 +1,4 @@ +{{name}} +{{#children}} + {{>partial}} +{{/children}} \ No newline at end of file diff --git a/examples/partial_recursion.js b/examples/partial_recursion.js new file mode 100644 index 0000000..e51c838 --- /dev/null +++ b/examples/partial_recursion.js @@ -0,0 +1,11 @@ +var partial_context = { + name: '1', + children: [ + { + name: '1.1', + children: [ + {name: '1.1.1'} + ] + } + ] +}; diff --git a/examples/partial_recursion.txt b/examples/partial_recursion.txt new file mode 100644 index 0000000..4ddfeac --- /dev/null +++ b/examples/partial_recursion.txt @@ -0,0 +1,3 @@ +1 +1.1 +1.1.1 \ No newline at end of file From 1e7fce2e7a43a9b1ae85c6246fdf87c650d6bc61 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Wed, 19 May 2010 13:23:57 +0200 Subject: [PATCH 18/29] Remove expensive merge() method. Context lookups for nested sections and partials are now performed on the current context and the top- level view object. This avoids a ton of copying and duplication of view objects and contexts. --- mustache.js | 53 ++++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/mustache.js b/mustache.js index b224249..61f6ad2 100644 --- a/mustache.js +++ b/mustache.js @@ -15,6 +15,7 @@ var Mustache = function() { pragmas_implemented: { "IMPLICIT-ITERATOR": true }, + context: {}, render: function(template, context, partials, in_recursion) { // reset buffer @@ -32,6 +33,7 @@ var Mustache = function() { } if(!in_recursion) { + this.context = context; this.buffer = []; } @@ -81,7 +83,7 @@ var Mustache = function() { }, /* - Tries to find a partial in the global scope and render it + Tries to find a partial in the cuurent scope and render it */ render_partial: function(name, context, partials) { name = this.trim(name); @@ -121,17 +123,17 @@ var Mustache = function() { } else if(type == "#") { // normal section if(that.is_array(value)) { // Enumerable, Let's loop! return that.map(value, function(row) { - return that.render(content, that.merge(context, - that.create_context(row)), partials, true); + return that.render(content, that.create_context(row), + partials, true); }).join(""); } else if(that.is_object(value)) { // Object, Use it as subcontext! - return that.render(content, - that.merge(context, that.create_context(value)), partials, true); + return that.render(content, that.create_context(value), + partials, true); } else if(typeof value === "function") { // higher order section return value.call(context, content, function(text) { return that.render(text, context, partials, true); - }) + }); } else if(value) { // boolean section return that.render(content, context, partials, true); } else { @@ -208,11 +210,23 @@ var Mustache = function() { */ find: function(name, context) { name = this.trim(name); - if(typeof context[name] === "function") { - return context[name].apply(context); + + // Checks whether a value is thruthy or false or 0 + function is_kinda_truthy(bool) { + return bool === false || bool === 0 || bool; + } + + if(is_kinda_truthy(context[name])) { + var value = context[name]; + } else if(is_kinda_truthy(this.context[name])) { + var value = this.context[name]; } - if(context[name] !== undefined) { - return context[name]; + + if(typeof value === "function") { + return value.apply(context); + } + if(value !== undefined) { + return value; } // silently ignore unkown variables return ""; @@ -241,25 +255,6 @@ var Mustache = function() { }); }, - /* - Merges all properties of object `b` into object `a`. - `b.property` overwrites a.property` - */ - merge: function(a, b) { - var _new = {}; - for(var name in a) { - if(a.hasOwnProperty(name)) { - _new[name] = a[name]; - } - }; - for(var name in b) { - if(b.hasOwnProperty(name)) { - _new[name] = b[name]; - } - }; - return _new; - }, - // by @langalex, support for arrays of strings create_context: function(_context) { if(this.is_object(_context)) { From 1f0ca2fe50c82c25baac4f3e8be4264c1fea9eb3 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Wed, 19 May 2010 13:24:35 +0200 Subject: [PATCH 19/29] =?UTF-8?q?Add=20test=20case=20for=20recursive=20par?= =?UTF-8?q?tial.=20If=20it=20weren't=20for=20complex.html=20it=20could=20b?= =?UTF-8?q?e=20neat=20and=20there'd=20be=20s/kids/children/=20=E2=80=94=20?= =?UTF-8?q?Oh=20well.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/partial_recursion.html | 4 ++-- examples/partial_recursion.js | 2 +- examples/partial_recursion.txt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/partial_recursion.html b/examples/partial_recursion.html index c002d68..8d67c38 100644 --- a/examples/partial_recursion.html +++ b/examples/partial_recursion.html @@ -1,4 +1,4 @@ {{name}} -{{#children}} +{{#kids}} {{>partial}} -{{/children}} \ No newline at end of file +{{/kids}} \ No newline at end of file diff --git a/examples/partial_recursion.js b/examples/partial_recursion.js index e51c838..ad1f2eb 100644 --- a/examples/partial_recursion.js +++ b/examples/partial_recursion.js @@ -1,6 +1,6 @@ var partial_context = { name: '1', - children: [ + kids: [ { name: '1.1', children: [ diff --git a/examples/partial_recursion.txt b/examples/partial_recursion.txt index 4ddfeac..0f70515 100644 --- a/examples/partial_recursion.txt +++ b/examples/partial_recursion.txt @@ -1,3 +1,3 @@ 1 1.1 -1.1.1 \ No newline at end of file +1.1.1 From 4466a475615f26d763e2962471cf8f048da08662 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Wed, 19 May 2010 13:26:53 +0200 Subject: [PATCH 20/29] whoopsiedupecode --- mustache.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/mustache.js b/mustache.js index 61f6ad2..3c0191d 100644 --- a/mustache.js +++ b/mustache.js @@ -18,10 +18,12 @@ var Mustache = function() { context: {}, render: function(template, context, partials, in_recursion) { - // reset buffer - // TODO: make this non-lazy - if(!in_recursion) - this.buffer = []; + // reset buffer & set context + if(!in_recursion) { + this.context = context; + this.buffer = []; // TODO: make this non-lazy + } + // fail fast if(!this.includes("", template)) { if(in_recursion) { @@ -32,11 +34,6 @@ var Mustache = function() { } } - if(!in_recursion) { - this.context = context; - this.buffer = []; - } - template = this.render_pragmas(template); var html = this.render_section(template, context, partials); if(in_recursion) { From 3abfafa553e8067f087835893cf8e678a8ab8994 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Fri, 21 May 2010 22:47:07 +0200 Subject: [PATCH 21/29] Fix typo in comment. #pedantry --- mustache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mustache.js b/mustache.js index 3c0191d..dc82698 100644 --- a/mustache.js +++ b/mustache.js @@ -80,7 +80,7 @@ var Mustache = function() { }, /* - Tries to find a partial in the cuurent scope and render it + Tries to find a partial in the curent scope and render it */ render_partial: function(name, context, partials) { name = this.trim(name); From c6fc49c7833ea2bf8bfda06834da2c550e53ac5e Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 22 May 2010 14:49:34 +0200 Subject: [PATCH 22/29] Add package.json to CommonJS package (`rake commonjs`). Use fancy regex to auto-fill the version number. Closes #37. --- Rakefile | 16 ++++++++++++++-- mustache-commonjs/package.json | 7 +++++++ package.json | 7 ------- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 mustache-commonjs/package.json delete mode 100644 package.json diff --git a/Rakefile b/Rakefile index e27970c..b32f2b0 100644 --- a/Rakefile +++ b/Rakefile @@ -28,15 +28,27 @@ def templated_build(name, opts={}) sh "mkdir -p #{opts[:location]}" if opts[:location] sh "cat #{source}/#{target_js}.tpl.pre mustache.js \ #{source}/#{target_js}.tpl.post > #{opts[:location] || '.'}/#{target_js}" - puts "Done, see #{opts[:location] || '.'}/#{target_js}" + + # extra + if opts[:extra] + sh "cat #{source}/#{opts[:extra]} | sed -e 's/{{version}}/#{version}/' > #{opts[:location]}/#{opts[:extra]}" + end + + puts "Done, see #{opts[:location] || '.'}/#{target_js}" + end end -templated_build "CommonJS", :location => "lib" +templated_build "CommonJS", :location => "lib", :extra => "package.json" templated_build "jQuery" templated_build "Dojo", :location => "dojox/string" templated_build "YUI3", :location => "yui3/mustache" +def version + File.read("mustache.js").match('version: "([^\"]+)",$')[1] +end + + desc "Remove temporary files." task :clean do sh "git clean -fdx" diff --git a/mustache-commonjs/package.json b/mustache-commonjs/package.json new file mode 100644 index 0000000..74d3aba --- /dev/null +++ b/mustache-commonjs/package.json @@ -0,0 +1,7 @@ +{ + "name": "mustache", + "author": "http://mustache.github.com/", + "description": "{{ mustache }} in JavaScript — Logic-less templates.", + "keywords": ["template"], + "version": "{{version}}" +} diff --git a/package.json b/package.json deleted file mode 100644 index 54a86f9..0000000 --- a/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "mustache", - "author": "Jan Lehnardt", - "description": "{{mustaches}} in JavaScript — shameless port from @defunkt", - "keywords": ["template"], - "version": "0.2.3" -} From 86ed681de751cc5754281557c8d75657d3994e2a Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 22 May 2010 18:28:13 +0200 Subject: [PATCH 23/29] more pedantry --- Rakefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index b32f2b0..c68f37c 100644 --- a/Rakefile +++ b/Rakefile @@ -31,7 +31,8 @@ def templated_build(name, opts={}) # extra if opts[:extra] - sh "cat #{source}/#{opts[:extra]} | sed -e 's/{{version}}/#{version}/' > #{opts[:location]}/#{opts[:extra]}" + sh "cat #{source}/#{opts[:extra]} | sed -e 's/{{version}}/#{version}/' \ + > #{opts[:location]}/#{opts[:extra]}" end puts "Done, see #{opts[:location] || '.'}/#{target_js}" From 2bab7be637da89f489518e7d219e13b754ae783e Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 22 May 2010 18:32:03 +0200 Subject: [PATCH 24/29] Less code per line, fix Rhino compat. --- CHANGES.md | 1 + THANKS.md | 1 + mustache.js | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index cdd3858..d833415 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ ## 0.3.0 (??-??-????) +* Fix Rhino compat. * CommonJS packaging is no longer a special case. * DRY Rakefile. * Allow whitespace around tag names. diff --git a/THANKS.md b/THANKS.md index 9843374..6dac939 100644 --- a/THANKS.md +++ b/THANKS.md @@ -17,3 +17,4 @@ Mustache.js wouldn't kick ass if it weren't for these fine souls: * dpree * Jason Smith / jhs * Aaron Gibralter / agibralter + * Ross Boucher / boucher diff --git a/mustache.js b/mustache.js index dc82698..545b0bd 100644 --- a/mustache.js +++ b/mustache.js @@ -240,7 +240,8 @@ var Mustache = function() { Does away with nasty characters */ escape: function(s) { - return ((s == null) ? "" : s).toString().replace(/&(?!\w+;)|["<>\\]/g, function(s) { + s = String(s == null ? "" : s); + return s.replace(/&(?!\w+;)|["<>\\]/g, function(s) { switch(s) { case "&": return "&"; case "\\": return "\\\\";; From 7be0692b2f9797b76624a7ec5fd3ce7df05a8e39 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 22 May 2010 18:33:21 +0200 Subject: [PATCH 25/29] pedantry --- CHANGES.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index d833415..f843e74 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,7 +14,6 @@ * Added higher order sections. - ## 0.2.3 (28-03-2010) * Better error message for missing partials. From 53ec2d559b91db8a1cf4512ad94e4644a91c9d9a Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 22 May 2010 18:35:36 +0200 Subject: [PATCH 26/29] less cat --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index c68f37c..4629c97 100644 --- a/Rakefile +++ b/Rakefile @@ -31,7 +31,7 @@ def templated_build(name, opts={}) # extra if opts[:extra] - sh "cat #{source}/#{opts[:extra]} | sed -e 's/{{version}}/#{version}/' \ + sh "sed -e 's/{{version}}/#{version}/' #{source}/#{opts[:extra]} \ > #{opts[:location]}/#{opts[:extra]}" end From e3911963f32a5154343c51fd9c2e0b78df4e9b39 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 22 May 2010 19:05:49 +0200 Subject: [PATCH 27/29] JSLint hurt my feelings. --- mustache.js | 58 ++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/mustache.js b/mustache.js index 545b0bd..77df727 100644 --- a/mustache.js +++ b/mustache.js @@ -62,12 +62,13 @@ var Mustache = function() { } var that = this; - var regex = new RegExp(this.otag + "%([\\w_-]+) ?([\\w]+=[\\w]+)?" - + this.ctag); + var regex = new RegExp(this.otag + "%([\\w_-]+) ?([\\w]+=[\\w]+)?" + + this.ctag); return template.replace(regex, function(match, pragma, options) { if(!that.pragmas_implemented[pragma]) { - throw({message: "This implementation of mustache doesn't understand the '" - + pragma + "' pragma"}); + throw({message: + "This implementation of mustache doesn't understand the '" + + pragma + "' pragma"}); } that.pragmas[pragma] = {}; if(options) { @@ -111,7 +112,7 @@ var Mustache = function() { return template.replace(regex, function(match, type, name, content) { var value = that.find(name, context); if(type == "^") { // inverted section - if(!value || that.is_array(value) && value.length == 0) { + if(!value || that.is_array(value) && value.length === 0) { // false or empty list, render it return that.render(content, context, partials, true); } else { @@ -148,29 +149,31 @@ var Mustache = function() { var that = this; var new_regex = function() { - return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\/#\^]+?)\\1?" + + return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\\/#\\^]+?)\\1?" + that.ctag + "+", "g"); }; var regex = new_regex(); + var tag_replace_callback = function(match, operator, name) { + switch(operator) { + case "!": // ignore comments + return ""; + case "=": // set new delimiters, rebuild the replace regexp + that.set_delimiters(name); + regex = new_regex(); + return ""; + case ">": // render partial + return that.render_partial(name, context, partials); + case "{": // the triple mustache is unescaped + return that.find(name, context); + default: // escape the value + return that.escape(that.find(name, context)); + } + }; var lines = template.split("\n"); + for (var i=0; i < lines.length; i++) { - lines[i] = lines[i].replace(regex, function(match, operator, name) { - switch(operator) { - case "!": // ignore comments - return ""; - case "=": // set new delimiters, rebuild the replace regexp - that.set_delimiters(name); - regex = new_regex(); - return ""; - case ">": // render partial - return that.render_partial(name, context, partials); - case "{": // the triple mustache is unescaped - return that.find(name, context); - default: // escape the value - return that.escape(that.find(name, context)); - } - }, this); + lines[i] = lines[i].replace(regex, tag_replace_callback, this); if(!in_recursion) { this.send(lines[i]); } @@ -213,10 +216,11 @@ var Mustache = function() { return bool === false || bool === 0 || bool; } + var value; if(is_kinda_truthy(context[name])) { - var value = context[name]; + value = context[name]; } else if(is_kinda_truthy(this.context[name])) { - var value = this.context[name]; + value = this.context[name]; } if(typeof value === "function") { @@ -240,12 +244,12 @@ var Mustache = function() { Does away with nasty characters */ escape: function(s) { - s = String(s == null ? "" : s); + s = String(s === null ? "" : 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; From e33d4f8bf89551ee06726293d0dd676608470c76 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 22 May 2010 19:13:04 +0200 Subject: [PATCH 28/29] whitespacin' --- mustache.js | 59 ++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/mustache.js b/mustache.js index 77df727..19c8417 100644 --- a/mustache.js +++ b/mustache.js @@ -156,32 +156,31 @@ var Mustache = function() { var regex = new_regex(); var tag_replace_callback = function(match, operator, name) { switch(operator) { - case "!": // ignore comments - return ""; - case "=": // set new delimiters, rebuild the replace regexp - that.set_delimiters(name); - regex = new_regex(); - return ""; - case ">": // render partial - return that.render_partial(name, context, partials); - case "{": // the triple mustache is unescaped - return that.find(name, context); - default: // escape the value - return that.escape(that.find(name, context)); + case "!": // ignore comments + return ""; + case "=": // set new delimiters, rebuild the replace regexp + that.set_delimiters(name); + regex = new_regex(); + return ""; + case ">": // render partial + return that.render_partial(name, context, partials); + case "{": // the triple mustache is unescaped + return that.find(name, context); + default: // escape the value + return that.escape(that.find(name, context)); } }; var lines = template.split("\n"); + for(var i = 0; i < lines.length; i++) { + lines[i] = lines[i].replace(regex, tag_replace_callback, this); + if(!in_recursion) { + this.send(lines[i]); + } + } - for (var i=0; i < lines.length; i++) { - lines[i] = lines[i].replace(regex, tag_replace_callback, this); - if(!in_recursion) { - this.send(lines[i]); - } - } - - if(in_recursion) { - return lines.join("\n"); - } + if(in_recursion) { + return lines.join("\n"); + } }, set_delimiters: function(delimiters) { @@ -201,7 +200,7 @@ var Mustache = function() { '(\\' + specials.join('|\\') + ')', 'g' ); } - return text.replace(arguments.callee.sRE, '\\$1'); + return text.replace(arguments.callee.sRE, '\\$1'); }, /* @@ -247,12 +246,12 @@ var Mustache = function() { s = String(s === null ? "" : s); return s.replace(/&(?!\w+;)|["<>\\]/g, function(s) { switch(s) { - case "&": return "&"; - case "\\": return "\\\\"; - case '"': return '\"'; - case "<": return "<"; - case ">": return ">"; - default: return s; + case "&": return "&"; + case "\\": return "\\\\"; + case '"': return '\"'; + case "<": return "<"; + case ">": return ">"; + default: return s; } }); }, @@ -293,7 +292,7 @@ var Mustache = function() { } else { var r = []; var l = array.length; - for(var i=0;i Date: Mon, 24 May 2010 11:12:46 +0200 Subject: [PATCH 29/29] Regex: \w includes "_". Thanks Leeoniya. --- mustache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mustache.js b/mustache.js index 19c8417..be1d7e6 100644 --- a/mustache.js +++ b/mustache.js @@ -62,7 +62,7 @@ var Mustache = function() { } var that = this; - var regex = new RegExp(this.otag + "%([\\w_-]+) ?([\\w]+=[\\w]+)?" + + var regex = new RegExp(this.otag + "%([\\w-]+) ?([\\w]+=[\\w]+)?" + this.ctag); return template.replace(regex, function(match, pragma, options) { if(!that.pragmas_implemented[pragma]) {