Permalink
Browse files

Parser rewrite

This commit is a complete rewrite of the core mustache.js file with two
main goals: 1) a major performance boost and 2) better compliance with
the mustache spec.

In order to improve performance templates are pre-compiled to JavaScript
functions. These compiled functions take a view, a partials object, and
an optional callback as arguments. They are cached to prevent
unnecessary re-compilation of an already compiled template. Both of
these enhancements facilitate a generous boost in performance.

A few other notes:

- The mustache.js file is now both browser and CommonJS ready without
  any modification.
- The API exposes two main methods: Mustache.compile and
  Mustache.render. The former is used to generate a function for a given
  template, while the latter is a higher-level function that is used to
  compile and render a template in one shot. Mustache.to_html is still
  available for backwards compatibility.
- The concept of pragmas is removed to conform more closely to the
  original mustache spec. The dot symbol still works to reference the
  current item in an array.
- The parser is much more strict about whitespace than it was before. The
  rule is simple: if a line contains only a non-variable tag (i.e. not
  {{tag}} or {{{tag}}}) and whitespace, that line is ignored in the
  output. Users may use the "space" option when compiling templates to
  preserve every whitespace character in the original template.
- The parser is able to provide detailed information about where errors
  occur when parsing and rendering templates, including the line number
  and surrounding code context.
  • Loading branch information...
mjackson committed Dec 17, 2011
1 parent 5df7be5 commit eb01be04702388fd3cf98e509d3d14f1f9243f09
Showing with 596 additions and 594 deletions.
  1. +0 −1 .gitignore
  2. +5 −5 README.md
  3. +1 −8 Rakefile
  4. +4 −4 TESTING.md
  5. +458 −358 mustache.js
  6. +8 −0 package.json
  7. +0 −3 spec/_files/array_of_partials_implicit_partial.js
  8. +1 −1 spec/_files/array_of_strings.mustache
  9. +0 −1 spec/_files/array_of_strings_options.js
  10. +0 −2 spec/_files/array_of_strings_options.mustache
  11. +0 −1 spec/_files/array_of_strings_options.txt
  12. +0 −5 spec/_files/array_partial.js
  13. +1 −1 spec/_files/complex.mustache
  14. +3 −3 spec/_files/complex.txt
  15. +1 −1 spec/_files/delimiters.mustache
  16. +0 −1 spec/_files/empty_partial.2.mustache
  17. +0 −3 spec/_files/empty_partial.js
  18. +1 −1 spec/_files/empty_template.txt
  19. +0 −1 spec/_files/error_not_found.txt
  20. +1 −0 spec/_files/inverted_section.txt
  21. +3 −4 spec/_files/nesting.txt
  22. +1 −0 spec/_files/null_string.js
  23. +2 −1 spec/_files/null_string.mustache
  24. +1 −0 spec/_files/null_string.txt
  25. +3 −0 spec/_files/partial_array.js
  26. 0 spec/_files/{array_partial.mustache → partial_array.mustache}
  27. +1 −1 spec/_files/{array_partial.2.mustache → partial_array.partial}
  28. +0 −1 spec/_files/{array_partial.txt → partial_array.txt}
  29. +1 −1 spec/_files/{array_of_partials_partial.js → partial_array_of_partials.js}
  30. 0 spec/_files/{array_of_partials_implicit_partial.mustache → partial_array_of_partials.mustache}
  31. 0 spec/_files/{array_of_partials_partial.2.mustache → partial_array_of_partials.partial}
  32. 0 spec/_files/{array_of_partials_implicit_partial.txt → partial_array_of_partials.txt}
  33. +3 −0 spec/_files/partial_array_of_partials_implicit.js
  34. 0 spec/_files/{array_of_partials_partial.mustache → partial_array_of_partials_implicit.mustache}
  35. 0 ...iles/{array_of_partials_implicit_partial.2.mustache → partial_array_of_partials_implicit.partial}
  36. 0 spec/_files/{array_of_partials_partial.txt → partial_array_of_partials_implicit.txt}
  37. +3 −0 spec/_files/partial_empty.js
  38. 0 spec/_files/{empty_partial.mustache → partial_empty.mustache}
  39. +1 −0 spec/_files/partial_empty.partial
  40. 0 spec/_files/{empty_partial.txt → partial_empty.txt}
  41. +1 −1 spec/_files/partial_recursion.js
  42. +1 −1 spec/_files/partial_recursion.mustache
  43. +1 −1 spec/_files/{partial_recursion.2.mustache → partial_recursion.partial}
  44. +6 −0 spec/_files/partial_template.js
  45. +1 −1 spec/_files/{template_partial.mustache → partial_template.mustache}
  46. +1 −0 spec/_files/partial_template.partial
  47. 0 spec/_files/{template_partial.txt → partial_template.txt}
  48. +16 −0 spec/_files/partial_view.js
  49. +1 −1 spec/_files/{view_partial.mustache → partial_view.mustache}
  50. 0 spec/_files/{view_partial.2.mustache → partial_view.partial}
  51. +0 −1 spec/_files/{view_partial.txt → partial_view.txt}
  52. +17 −0 spec/_files/partial_whitespace.js
  53. +1 −1 spec/_files/{whitespace_partial.mustache → partial_whitespace.mustache}
  54. 0 spec/_files/{whitespace_partial.2.mustache → partial_whitespace.partial}
  55. +0 −1 spec/_files/{whitespace_partial.txt → partial_whitespace.txt}
  56. +1 −1 spec/_files/reuse_of_enumerables.js
  57. +3 −3 spec/_files/section_as_context.txt
  58. +1 −1 spec/_files/simple.mustache
  59. +0 −1 spec/_files/template_partial.2.mustache
  60. +0 −8 spec/_files/template_partial.js
  61. +1 −1 spec/_files/two_in_a_row.mustache
  62. +0 −1 spec/_files/two_sections.txt
  63. +1 −1 spec/_files/unescaped.mustache
  64. +0 −1 spec/_files/unknown_pragma.js
  65. +0 −1 spec/_files/unknown_pragma.mustache
  66. +0 −1 spec/_files/unknown_pragma.txt
  67. +0 −19 spec/_files/view_partial.js
  68. +0 −19 spec/_files/whitespace_partial.js
  69. +40 −98 spec/mustache_spec.rb
  70. +0 −8 wrappers/commonjs/mustache.js.tpl.post
  71. +0 −6 wrappers/commonjs/mustache.js.tpl.pre
  72. +0 −8 wrappers/commonjs/package.json
View
@@ -1,7 +1,6 @@
.DS_Store
.rvmrc
runner.js
lib
jquery.mustache.js
qooxdoo.mustache.js
dojox
View
@@ -3,7 +3,7 @@
> What could be more logical awesome than no logic at all?
[mustache.js](http://github.com/janl/mustache.js) is an implementation of the
[Mustache](http://mustache.github.com/) templating system in JavaScript.
[Mustache](http://mustache.github.com/) template system in JavaScript.
[Mustache](http://mustache.github.com/) is a logic-less template syntax. It can
be used for HTML, config files, source code - anything. It works by expanding
@@ -274,10 +274,11 @@ own iteration marker:
## Plugins for JavaScript Libraries
mustache.js may be built specifically for several different client libraries
and platforms, including the following:
By default mustache.js may be used in a browser or any [CommonJS](http://www.commonjs.org/)
environment, including [node](http://nodejs.org/). Additionally, mustache.js may
be built specifically for several different client libraries and platforms,
including the following:
- [node](http://nodejs.org/) (or other CommonJS platforms)
- [jQuery](http://jquery.com/)
- [Dojo](http://www.dojotoolkit.org/)
- [YUI](http://developer.yahoo.com/yui/)
@@ -287,7 +288,6 @@ and platforms, including the following:
These may be built using [Rake](http://rake.rubyforge.org/) and one of the
following commands:
$ rake commonjs
$ rake jquery
$ rake dojo
$ rake yui
View
@@ -13,7 +13,7 @@ task :spec do
end
def version
File.read("mustache.js").match('version: "([^\"]+)",$')[1]
File.read("mustache.js").match('version = "([^\"]+)";$')[1]
end
# Creates a rule that uses the .tmpl.{pre,post} stuff to make a final,
@@ -36,17 +36,10 @@ def templated_build(name, opts={})
sh "cat #{source}/#{target_js}.tpl.pre mustache.js \
#{source}/#{target_js}.tpl.post > #{opts[:location] || '.'}/#{target_js}"
# extra
if opts[:extra]
sh "sed -e 's/{{version}}/#{version}/' #{source}/#{opts[:extra]} \
> #{opts[:location]}/#{opts[:extra]}"
end
puts "Done, see #{opts[:location] || '.'}/#{target_js}"
end
end
templated_build "CommonJS", :location => "lib", :extra => "package.json"
templated_build "jQuery"
templated_build "Dojo", :location => "dojox/string"
templated_build "YUI3", :location => "yui3/mustache"
View
@@ -1,4 +1,4 @@
## Running the mustache.js Test Suite
## Running the mustache.js test suite
The mustache.js test suite uses the [RSpec](http://rspec.info/) testing
framework. In order to run the tests you'll need to install [Ruby](http://ruby-lang.org/)
@@ -57,6 +57,6 @@ suite with the following command:
All test files live in the spec/_files directory. To create a new test:
* Create a template file `somename.mustache`
* Create a javascript file with data and functions `somename.js`
* Create a file the expected result `somename.txt`
* Create a template file called `somename.mustache`
* Create a JavaScript file containing the view called `somename.js`
* Create a text file with the expected result called `somename.txt`
Oops, something went wrong.

0 comments on commit eb01be0

Please sign in to comment.