Skip to content

Commit

Permalink
Add a new api where the module itself generates a template function.
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Apr 1, 2010
1 parent d8d7492 commit e8a557e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
42 changes: 29 additions & 13 deletions lib/haml.js
@@ -1,9 +1,4 @@
var Haml = {}; var Haml;

// Bind to the exports object if it exists. (CommonJS and NodeJS)
if (typeof exports !== 'undefined') {
Haml = exports;
}


(function () { (function () {


Expand Down Expand Up @@ -347,7 +342,7 @@ if (typeof exports !== 'undefined') {


]; ];


Haml.compile = function (lines) { function compile(lines) {
var block = false, var block = false,
output = []; output = [];


Expand Down Expand Up @@ -381,7 +376,7 @@ if (typeof exports !== 'undefined') {
check_indent: new RegExp("^(?:\\s*|" + match[1] + " (.*))$"), check_indent: new RegExp("^(?:\\s*|" + match[1] + " (.*))$"),
process: matcher.process, process: matcher.process,
render_contents: function () { render_contents: function () {
return Haml.compile(this. contents); return compile(this. contents);
} }
}; };
found = true; found = true;
Expand Down Expand Up @@ -429,7 +424,7 @@ if (typeof exports !== 'undefined') {
return output.join(' + \n'); return output.join(' + \n');
}; };


Haml.optimize = function (js) { function optimize(js) {
var new_js = [], buffer = [], part, end; var new_js = [], buffer = [], part, end;


function flush() { function flush() {
Expand Down Expand Up @@ -458,16 +453,16 @@ if (typeof exports !== 'undefined') {
return new_js.join("\n"); return new_js.join("\n");
}; };


Haml.render = function(text, options) { function render(text, options) {
options = options || {}; options = options || {};
var js = Haml.compile(text); var js = compile(text);
if (options.optimize) { if (options.optimize) {
js = Haml.optimize(js); js = Haml.optimize(js);
} }
return Haml.execute(js, options.context || Haml, options.locals); return execute(js, options.context || Haml, options.locals);
}; };


Haml.execute = function (js, self, locals) { function execute(js, self, locals) {
return (function () { return (function () {
with(locals || {}) { with(locals || {}) {
try { try {
Expand All @@ -480,5 +475,26 @@ if (typeof exports !== 'undefined') {
}).call(self); }).call(self);
}; };


Haml = function (haml) {
var js = optimize(compile(haml));
return function (locals) {
with(locals || {}) {
try {
return eval("(" + js + ")");
} catch (e) {
return "\n<pre class='error'>" + html_escape(e.stack) + "</pre>\n";
}
}
}
}
Haml.compile = compile;
Haml.optimize = optimize;
Haml.render = render;
Haml.execute = execute;


}()); }());

// Hook into module system
if (typeof module !== 'undefined') {
module.exports = Haml;
}
3 changes: 2 additions & 1 deletion test/test.js
Expand Up @@ -19,8 +19,9 @@ fs.readdir('.', function (err, files) {
try { try {
var js = Haml.compile(haml); var js = Haml.compile(haml);
var js_opt = Haml.optimize(js); var js_opt = Haml.optimize(js);
var actual = Haml.execute(js_opt, scope.context, scope.locals); var actual = Haml(haml).call(scope.context, scope.locals);
assert.equal(actual, expected); assert.equal(actual, expected);

sys.puts(haml_file + " Passed") sys.puts(haml_file + " Passed")
} catch (e) { } catch (e) {
var message = e.name; var message = e.name;
Expand Down

0 comments on commit e8a557e

Please sign in to comment.