Write a really dumb sample compiler.

This commit is contained in:
Alan Johnson
2010-09-05 17:19:35 -04:00
parent b2ae3c62f0
commit 370fd4c165
4 changed files with 39 additions and 1 deletions
+8
View File
@@ -0,0 +1,8 @@
This is the story of guys who work on a project
called {{project}}. Their names were {{#people}}{{firstName}} and {{/people}}
they both enjoyed working on {{project}}.
{{#people}}
{{>personPet}}
{{/people}}
+11
View File
@@ -0,0 +1,11 @@
require('./handlebars');
var fs = require('fs');
process.argv.slice(2).forEach(function(val, index, array) {
var parts = val.split("=", 2);
var tmpl = fs.readFileSync(parts[1], 'utf8');
console.log(parts[0] + " = " + Handlebars.compileToString(tmpl).toString());
});
+12 -1
View File
@@ -1,8 +1,19 @@
Handlebars = {
compile: function(string) {
var fnBody = Handlebars.compileFunctionBody(string);
return new Function("context", "fallback", fnBody);
},
compileToString: function(string) {
var fnBody = Handlebars.compileFunctionBody(string);
return "function(context, fallback) { " + fnBody + "}";
},
compileFunctionBody: function(string) {
var compiler = new Handlebars.Compiler(string);
compiler.compile();
return new Function("context", "fallback", "fallback = fallback || {}; var stack = [];" + compiler.fn);
return "fallback = fallback || {}; var stack = [];" + compiler.fn;
},
isFunction: function(fn) {
+8
View File
@@ -0,0 +1,8 @@
This is the story of guys who work on a project
called {{project}}. Their names were {{#people}}{{firstName}} and {{/people}}
they both enjoyed working on {{project}}.
{{#people}}
{{>personPet}}
{{/people}}