Skip to content

Commit

Permalink
vendor Mu
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Aug 29, 2010
1 parent b29eb6f commit 9569ea3
Show file tree
Hide file tree
Showing 57 changed files with 1,352 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Mu/.gitignore
@@ -0,0 +1 @@
.DS_Store
22 changes: 22 additions & 0 deletions lib/Mu/LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2010 Ray Christopher Morgan

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
100 changes: 100 additions & 0 deletions lib/Mu/README.markdown
@@ -0,0 +1,100 @@
Mu - Mustache template compiler for Node.js
===========================================

Mustache is a simply awesome template language inspired by
[ctemplate](http://code.google.com/p/google-ctemplate/) and
[et](http://www.ivan.fomichev.name/2008/05/erlang-template-engine-prototype.html).

Mu is a Mustache based template engine for Node.js. Mu compiles mustache
templates into an extremely fast executable function.


What makes Mu cool?
-------------------

* It is very fast
* Supports async parsing/compiling
* Rendering is streamed


Benchmarks
----------

Rendering examples/complex.html.mu 1 million times yields the following results:

Ruby Mustache - 112 secs (benchmarks/rb/complex_view.rb)
Mu - 40 secs (benchmarks/million_complex.js)

Tested on a 2.4 GHz Intel Core 2 Duo MacBook Pro

Mu sync rendering was benchmarking in at 24 secs, but I felt it was much more
important to stream the rendering. Streaming adds a pretty set overhead of
about 14µs (microseconds) to each template render to setup the event emitter.
It also adds a variable extra amount of time due to the additional function calls.
The million_complex.js caused 2µs per render addition.


Usage (from demo.js)
--------------------

var sys = require('sys');
var Mu = require('./lib/mu');

Mu.templateRoot = './examples';

var ctx = {
name: "Chris",
value: 10000,
taxed_value: function() {
return this.value - (this.value * 0.4);
},
in_ca: true
};

Mu.render('simple.html', ctx, {}, function (err, output) {
if (err) {
throw err;
}
var buffer = '';

output.addListener('data', function (c) {buffer += c; })
.addListener('end', function () { sys.puts(buffer); });
});


Which yields:

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

Using Mu.compileText
--------------------

var sys = require('sys');
var Mu = require('./lib/mu');

var tmpl = "Hello {{> part}}. Your name is: {{name}}!";
var partials = {part: "World"};
var compiled = Mu.compileText(tmpl, partials);

compiled({name: "Chris"})
.addListener('data', function (c) { sys.puts(c) });


Mustache Documentation
----------------------

See **Tag Types** section at
[http://github.com/defunkt/mustache/](http://github.com/defunkt/mustache/)
for more information on supported tags.

Todo
----

* Better parse time errors. Currently they are decent when partials are not involved
but break down once partials are involved.
* Implement some compile time optimizations. The big one is predetermining when a
enumerable actually needs to inherit the full context. Cutting this out can be huge.
* Cleanup the Preprocessor methods. They are a bit unwieldy.
36 changes: 36 additions & 0 deletions lib/Mu/benchmarks/million_complex.js
@@ -0,0 +1,36 @@
var sys = require('sys')
fs = require('fs'),
Path = require('path');
var Mu = require('../lib/mu');

Mu.templateRoot = '';

var name = Path.join(Path.dirname(__filename), "../examples/complex");
var js = fs.readFileSync(name + '.js');

js = eval('(' + js + ')');

sys.puts(name + '.html');

Mu.compile(name + '.html', function (err, compiled) {
if (err) {
throw err;
}

var buffer = '';
compiled(js).addListener('data', function (c) { buffer += c; })
.addListener('end', function () { sys.puts(buffer); });

var i = 0;
var d = new Date();

(function go() {
if (i++ < 1000000) {
compiled(js).addListener('end', function () { go(); });
}
})();

process.addListener('exit', function () {
sys.error("Time taken: " + ((new Date() - d) / 1000) + "secs");
});
});
16 changes: 16 additions & 0 deletions lib/Mu/benchmarks/rb/complex_view.mustache
@@ -0,0 +1,16 @@
<h1>{{header}}</h1>
{{#list}}
<ul>
{{#item}}
{{#current}}
<li><strong>{{name}}</strong></li>
{{/current}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/item}}
</ul>
{{/list}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
42 changes: 42 additions & 0 deletions lib/Mu/benchmarks/rb/complex_view.rb
@@ -0,0 +1,42 @@
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require 'rubygems'
require 'mustache'

class ComplexView < Mustache
self.path = File.dirname(__FILE__)

def header
"Colors"
end

def item
items = []
items << { :name => 'red', :current => true, :url => '#Red' }
items << { :name => 'green', :current => false, :url => '#Green' }
items << { :name => 'blue', :current => false, :url => '#Blue' }
items
end

def link
not self[:current]
end

def list
not item.empty?
end

def empty
item.empty?
end
end

if $0 == __FILE__
puts ComplexView.to_html

d = Time.now
1000000.times {
ComplexView.to_html
}
diff = (Time.now - d)
puts "Time taken: " + diff.to_s
end
33 changes: 33 additions & 0 deletions lib/Mu/demo.js
@@ -0,0 +1,33 @@
var sys = require('sys');
var Mu = require('./lib/mu');

Mu.templateRoot = './examples';

var ctx = {
name: "Chris",
value: 10000,
taxed_value: function() {
return this.value - (this.value * 0.4);
},
in_ca: true
};

sys.puts("Outputting examples/simple.html.mu with a chunkSize of 10\n");

Mu.render('simple.html', ctx, {chunkSize: 10}, function (err, output) {
if (err) {
throw err;
}

var buffer = '';

output
.addListener('data', function (c) {
sys.print(c); // output chunk
output.pause(); // pause for demo
setTimeout(function () { // wait 500ms and resume for demo
output.resume();
}, 500);
})
.addListener('end', function () { sys.puts("\n\nDONE"); });
});
1 change: 1 addition & 0 deletions lib/Mu/examples/comments.html.mu
@@ -0,0 +1 @@
<h1>{{title}}{{! just something interesting... or not... }}</h1>
5 changes: 5 additions & 0 deletions lib/Mu/examples/comments.js
@@ -0,0 +1,5 @@
{
title: function() {
return "A Comedy of Errors";
}
}
1 change: 1 addition & 0 deletions lib/Mu/examples/comments.txt
@@ -0,0 +1 @@
<h1>A Comedy of Errors</h1>
16 changes: 16 additions & 0 deletions lib/Mu/examples/complex.html.mu
@@ -0,0 +1,16 @@
<h1>{{header}}</h1>
{{#list}}
<ul>
{{#item}}
{{#current}}
<li><strong>{{name}}</strong></li>
{{/current}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/item}}
</ul>
{{/list}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
19 changes: 19 additions & 0 deletions lib/Mu/examples/complex.js
@@ -0,0 +1,19 @@
{
header: function() {
return "Colors";
},
item: [
{name: "red", current: true, url: "#Red"},
{name: "green", current: false, url: "#Green"},
{name: "blue", current: false, url: "#Blue"}
],
link: function() {
return this["current"] !== true;
},
list: function() {
return this.item.length !== 0;
},
empty: function() {
return this.item.length === 0;
}
}
6 changes: 6 additions & 0 deletions lib/Mu/examples/complex.txt
@@ -0,0 +1,6 @@
<h1>Colors</h1>
<ul>
<li><strong>red</strong></li>
<li><a href="#Green">green</a></li>
<li><a href="#Blue">blue</a></li>
</ul>
2 changes: 2 additions & 0 deletions lib/Mu/examples/deep_partial.html.mu
@@ -0,0 +1,2 @@
<h1>First: {{title}}</h1>
{{>partial.html}}
3 changes: 3 additions & 0 deletions lib/Mu/examples/deep_partial.js
@@ -0,0 +1,3 @@
{
title: "Welcome"
}
3 changes: 3 additions & 0 deletions lib/Mu/examples/deep_partial.txt
@@ -0,0 +1,3 @@
<h1>First: Welcome</h1>
<h1>Welcome</h1>
<p class="again">Again, Welcome!</p>
6 changes: 6 additions & 0 deletions lib/Mu/examples/delimiters.html.mu
@@ -0,0 +1,6 @@
{{=<% %>=}}* <% first %>
* <% second %>
<%=| |=%>
* | third |
|={{ }}=|
* {{ fourth }}
6 changes: 6 additions & 0 deletions lib/Mu/examples/delimiters.js
@@ -0,0 +1,6 @@
{
first: "It worked the first time.",
second: "And it worked the second time.",
third: "Then, surprisingly, it worked the third time.",
fourth: "Fourth time also fine!."
}
6 changes: 6 additions & 0 deletions lib/Mu/examples/delimiters.txt
@@ -0,0 +1,6 @@
* It worked the first time.
* And it worked the second time.

* Then, surprisingly, it worked the third time.

* Fourth time also fine!.
1 change: 1 addition & 0 deletions lib/Mu/examples/error_not_found.html.mu
@@ -0,0 +1 @@
{{foo}}
1 change: 1 addition & 0 deletions lib/Mu/examples/error_not_found.js
@@ -0,0 +1 @@
{bar: 2}
Empty file.
1 change: 1 addition & 0 deletions lib/Mu/examples/escaped.html.mu
@@ -0,0 +1 @@
<h1>{{title}}</h1>
5 changes: 5 additions & 0 deletions lib/Mu/examples/escaped.js
@@ -0,0 +1,5 @@
{
title: function() {
return "Bear > Shark";
}
}
1 change: 1 addition & 0 deletions lib/Mu/examples/escaped.txt
@@ -0,0 +1 @@
<h1>Bear &gt; Shark</h1>
3 changes: 3 additions & 0 deletions lib/Mu/examples/hash_instead_of_array.html.mu
@@ -0,0 +1,3 @@
{{#person}}
Name: {{name}}
{{/person}}
5 changes: 5 additions & 0 deletions lib/Mu/examples/hash_instead_of_array.js
@@ -0,0 +1,5 @@
{
person: {
name: "Chris"
}
}
2 changes: 2 additions & 0 deletions lib/Mu/examples/hash_instead_of_array.txt
@@ -0,0 +1,2 @@

Name: Chris
1 change: 1 addition & 0 deletions lib/Mu/examples/inner_partial.html.mu
@@ -0,0 +1 @@
<p class="again">Again, {{title}}!</p>
6 changes: 6 additions & 0 deletions lib/Mu/examples/inverted.html.mu
@@ -0,0 +1,6 @@
{{#admin}}
Admin.
{{/admin}}
{{^admin}}
Not Admin.
{{/admin}}

0 comments on commit 9569ea3

Please sign in to comment.