Write a really dumb sample compiler.
This commit is contained in:
@@ -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}}
|
||||
|
||||
@@ -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
@@ -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) {
|
||||
|
||||
@@ -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}}
|
||||
|
||||
Reference in New Issue
Block a user