Commiting initial factory code

This commit is contained in:
Tommy Messbauer
2012-08-29 12:48:22 -05:00
parent 89f5ab8aaf
commit 7c4813b417
10 changed files with 1425 additions and 1367 deletions
+23 -5
View File
@@ -1,14 +1,32 @@
var Handlebars = require("./handlebars/base"); var handlebars = require("./handlebars/base"),
module.exports = Handlebars;
// Each of these augment the Handlebars object. No need to setup here. // Each of these augment the Handlebars object. No need to setup here.
// (This is done to easily share code between commonjs and browse envs) // (This is done to easily share code between commonjs and browse envs)
require("./handlebars/utils"); utils = require("./handlebars/utils"),
compiler = require("./handlebars/compiler"),
runtime = require("./handlebars/runtime");
require("./handlebars/compiler"); var create = function() {
require("./handlebars/runtime"); var hb = handlebars.create();
utils.attach(hb);
compiler.attach(hb);
runtime.attach(hb);
return hb;
};
var Handlebars = create();
Handlebars.create = create;
module.exports = Handlebars; // instantiate an instance
// BEGIN(BROWSER) // BEGIN(BROWSER)
// END(BROWSER) // END(BROWSER)
// USAGE:
// var handlebars = require('handlebars');
// var singleton = handlebars.Handlebars,
// local = handlebars.create();
+95 -95
View File
@@ -1,119 +1,119 @@
// BEGIN(BROWSER) module.exports.create = function() {
/*jshint eqnull:true*/ // BEGIN(BROWSER)
this.Handlebars = {};
(function(Handlebars) { var Handlebars = {};
Handlebars.VERSION = "1.0.rc.1"; Handlebars.VERSION = "1.0.rc.1";
Handlebars.helpers = {}; Handlebars.helpers = {};
Handlebars.partials = {}; Handlebars.partials = {};
Handlebars.registerHelper = function(name, fn, inverse) { Handlebars.registerHelper = function(name, fn, inverse) {
if(inverse) { fn.not = inverse; } if(inverse) { fn.not = inverse; }
this.helpers[name] = fn; this.helpers[name] = fn;
}; };
Handlebars.registerPartial = function(name, str) { Handlebars.registerPartial = function(name, str) {
this.partials[name] = str; this.partials[name] = str;
}; };
Handlebars.registerHelper('helperMissing', function(arg) { Handlebars.registerHelper('helperMissing', function(arg) {
if(arguments.length === 2) { if(arguments.length === 2) {
return undefined; return undefined;
} else { } else {
throw new Error("Could not find property '" + arg + "'"); throw new Error("Could not find property '" + arg + "'");
} }
}); });
var toString = Object.prototype.toString, functionType = "[object Function]"; var toString = Object.prototype.toString, functionType = "[object Function]";
Handlebars.registerHelper('blockHelperMissing', function(context, options) { Handlebars.registerHelper('blockHelperMissing', function(context, options) {
var inverse = options.inverse || function() {}, fn = options.fn; var inverse = options.inverse || function() {}, fn = options.fn;
var ret = ""; var ret = "";
var type = toString.call(context); var type = toString.call(context);
if(type === functionType) { context = context.call(this); } if(type === functionType) { context = context.call(this); }
if(context === true) { if(context === true) {
return fn(this); return fn(this);
} else if(context === false || context == null) { } else if(context === false || context == null) {
return inverse(this); return inverse(this);
} else if(type === "[object Array]") { } else if(type === "[object Array]") {
if(context.length > 0) { if(context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
ret = ret + fn(context[i]);
}
} else {
ret = inverse(this);
}
return ret;
} else {
return fn(context);
}
});
Handlebars.K = function() {};
Handlebars.createFrame = Object.create || function(object) {
Handlebars.K.prototype = object;
var obj = new Handlebars.K();
Handlebars.K.prototype = null;
return obj;
};
Handlebars.registerHelper('each', function(context, options) {
var fn = options.fn, inverse = options.inverse;
var ret = "", data;
if (options.data) {
data = Handlebars.createFrame(options.data);
}
if(context && context.length > 0) {
for(var i=0, j=context.length; i<j; i++) { for(var i=0, j=context.length; i<j; i++) {
ret = ret + fn(context[i]); if (data) { data.index = i; }
ret = ret + fn(context[i], { data: data });
} }
} else { } else {
ret = inverse(this); ret = inverse(this);
} }
return ret; return ret;
} else { });
return fn(context);
}
});
Handlebars.K = function() {}; Handlebars.registerHelper('if', function(context, options) {
var type = toString.call(context);
if(type === functionType) { context = context.call(this); }
if(!context || Handlebars.Utils.isEmpty(context)) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
Handlebars.registerHelper('unless', function(context, options) {
var fn = options.fn, inverse = options.inverse;
options.fn = inverse;
options.inverse = fn;
return Handlebars.helpers['if'].call(this, context, options);
});
Handlebars.registerHelper('with', function(context, options) {
return options.fn(context);
});
Handlebars.registerHelper('log', function(context) {
Handlebars.log(context);
});
// END(BROWSER)
return Handlebars;
Handlebars.createFrame = Object.create || function(object) {
Handlebars.K.prototype = object;
var obj = new Handlebars.K();
Handlebars.K.prototype = null;
return obj;
}; };
Handlebars.registerHelper('each', function(context, options) {
var fn = options.fn, inverse = options.inverse;
var ret = "", data;
if (options.data) {
data = Handlebars.createFrame(options.data);
}
if(context && context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
if (data) { data.index = i; }
ret = ret + fn(context[i], { data: data });
}
} else {
ret = inverse(this);
}
return ret;
});
Handlebars.registerHelper('if', function(context, options) {
var type = toString.call(context);
if(type === functionType) { context = context.call(this); }
if(!context || Handlebars.Utils.isEmpty(context)) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
Handlebars.registerHelper('unless', function(context, options) {
var fn = options.fn, inverse = options.inverse;
options.fn = inverse;
options.inverse = fn;
return Handlebars.helpers['if'].call(this, context, options);
});
Handlebars.registerHelper('with', function(context, options) {
return options.fn(context);
});
Handlebars.registerHelper('log', function(context) {
Handlebars.log(context);
});
}(this.Handlebars));
// END(BROWSER)
module.exports = this.Handlebars;
+4 -3
View File
@@ -1,7 +1,6 @@
var Handlebars = require('./base');
// BEGIN(BROWSER) // BEGIN(BROWSER)
(function() { exports.attach = function(Handlebars) {
Handlebars.AST = {}; Handlebars.AST = {};
@@ -118,6 +117,8 @@ var Handlebars = require('./base');
this.comment = comment; this.comment = comment;
}; };
})(); return Handlebars;
};
// END(BROWSER) // END(BROWSER)
+26 -23
View File
@@ -1,27 +1,30 @@
var handlebars = require("./parser").parser; var handlebars = require("./parser").parser;
var Handlebars = require("../base");
// BEGIN(BROWSER) exports.attach = function(Handlebars) {
Handlebars.Parser = handlebars;
Handlebars.parse = function(string) { // BEGIN(BROWSER)
Handlebars.Parser.yy = Handlebars.AST;
return Handlebars.Parser.parse(string); Handlebars.Parser = handlebars;
Handlebars.parse = function(string) {
Handlebars.Parser.yy = Handlebars.AST;
return Handlebars.Parser.parse(string);
};
Handlebars.print = function(ast) {
return new Handlebars.PrintVisitor().accept(ast);
};
Handlebars.logger = {
DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
// override in the host environment
log: function(level, str) {}
};
Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
return Handlebars;
// END(BROWSER)
}; };
Handlebars.print = function(ast) {
return new Handlebars.PrintVisitor().accept(ast);
};
Handlebars.logger = {
DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
// override in the host environment
log: function(level, str) {}
};
Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
// END(BROWSER)
module.exports = Handlebars;
File diff suppressed because it is too large Load Diff
+14 -5
View File
@@ -1,7 +1,16 @@
// Each of these module will augment the Handlebars object as it loads. No need to perform addition operations // Each of these module will augment the Handlebars object as it loads. No need to perform addition operations
module.exports = require("./base"); module.exports.attach = function(Handlebars) {
require("./visitor");
require("./printer");
require("./ast"); var visitor = require("./visitor"),
require("./compiler"); printer = require("./printer"),
ast = require("./ast"),
compiler = require("./compiler");
visitor.attach(Handlebars);
printer.attach(Handlebars);
ast.attach(Handlebars);
compiler.attach(Handlebars);
return Handlebars;
};
+116 -113
View File
@@ -1,127 +1,130 @@
var Handlebars = require("./base"); exports.attach = function(Handlebars) {
// BEGIN(BROWSER) // BEGIN(BROWSER)
Handlebars.PrintVisitor = function() { this.padding = 0; };
Handlebars.PrintVisitor.prototype = new Handlebars.Visitor(); Handlebars.PrintVisitor = function() { this.padding = 0; };
Handlebars.PrintVisitor.prototype = new Handlebars.Visitor();
Handlebars.PrintVisitor.prototype.pad = function(string, newline) { Handlebars.PrintVisitor.prototype.pad = function(string, newline) {
var out = ""; var out = "";
for(var i=0,l=this.padding; i<l; i++) { for(var i=0,l=this.padding; i<l; i++) {
out = out + " "; out = out + " ";
} }
out = out + string; out = out + string;
if(newline !== false) { out = out + "\n"; } if(newline !== false) { out = out + "\n"; }
return out; return out;
}; };
Handlebars.PrintVisitor.prototype.program = function(program) { Handlebars.PrintVisitor.prototype.program = function(program) {
var out = "", var out = "",
statements = program.statements, statements = program.statements,
inverse = program.inverse, inverse = program.inverse,
i, l; i, l;
for(i=0, l=statements.length; i<l; i++) { for(i=0, l=statements.length; i<l; i++) {
out = out + this.accept(statements[i]); out = out + this.accept(statements[i]);
} }
this.padding--;
return out;
};
Handlebars.PrintVisitor.prototype.block = function(block) {
var out = "";
out = out + this.pad("BLOCK:");
this.padding++;
out = out + this.accept(block.mustache);
if (block.program) {
out = out + this.pad("PROGRAM:");
this.padding++;
out = out + this.accept(block.program);
this.padding--; this.padding--;
}
if (block.inverse) { return out;
if (block.program) { this.padding++; } };
out = out + this.pad("{{^}}");
Handlebars.PrintVisitor.prototype.block = function(block) {
var out = "";
out = out + this.pad("BLOCK:");
this.padding++; this.padding++;
out = out + this.accept(block.inverse); out = out + this.accept(block.mustache);
if (block.program) {
out = out + this.pad("PROGRAM:");
this.padding++;
out = out + this.accept(block.program);
this.padding--;
}
if (block.inverse) {
if (block.program) { this.padding++; }
out = out + this.pad("{{^}}");
this.padding++;
out = out + this.accept(block.inverse);
this.padding--;
if (block.program) { this.padding--; }
}
this.padding--; this.padding--;
if (block.program) { this.padding--; }
}
this.padding--;
return out; return out;
};
Handlebars.PrintVisitor.prototype.mustache = function(mustache) {
var params = mustache.params, paramStrings = [], hash;
for(var i=0, l=params.length; i<l; i++) {
paramStrings.push(this.accept(params[i]));
}
params = "[" + paramStrings.join(", ") + "]";
hash = mustache.hash ? " " + this.accept(mustache.hash) : "";
return this.pad("{{ " + this.accept(mustache.id) + " " + params + hash + " }}");
};
Handlebars.PrintVisitor.prototype.partial = function(partial) {
var content = this.accept(partial.id);
if(partial.context) { content = content + " " + this.accept(partial.context); }
return this.pad("{{> " + content + " }}");
};
Handlebars.PrintVisitor.prototype.hash = function(hash) {
var pairs = hash.pairs;
var joinedPairs = [], left, right;
for(var i=0, l=pairs.length; i<l; i++) {
left = pairs[i][0];
right = this.accept(pairs[i][1]);
joinedPairs.push( left + "=" + right );
}
return "HASH{" + joinedPairs.join(", ") + "}";
};
Handlebars.PrintVisitor.prototype.STRING = function(string) {
return '"' + string.string + '"';
};
Handlebars.PrintVisitor.prototype.INTEGER = function(integer) {
return "INTEGER{" + integer.integer + "}";
};
Handlebars.PrintVisitor.prototype.BOOLEAN = function(bool) {
return "BOOLEAN{" + bool.bool + "}";
};
Handlebars.PrintVisitor.prototype.ID = function(id) {
var path = id.parts.join("/");
if(id.parts.length > 1) {
return "PATH:" + path;
} else {
return "ID:" + path;
}
};
Handlebars.PrintVisitor.prototype.DATA = function(data) {
return "@" + data.id;
};
Handlebars.PrintVisitor.prototype.content = function(content) {
return this.pad("CONTENT[ '" + content.string + "' ]");
};
Handlebars.PrintVisitor.prototype.comment = function(comment) {
return this.pad("{{! '" + comment.comment + "' }}");
};
// END(BROWSER)
return Handlebars;
}; };
Handlebars.PrintVisitor.prototype.mustache = function(mustache) {
var params = mustache.params, paramStrings = [], hash;
for(var i=0, l=params.length; i<l; i++) {
paramStrings.push(this.accept(params[i]));
}
params = "[" + paramStrings.join(", ") + "]";
hash = mustache.hash ? " " + this.accept(mustache.hash) : "";
return this.pad("{{ " + this.accept(mustache.id) + " " + params + hash + " }}");
};
Handlebars.PrintVisitor.prototype.partial = function(partial) {
var content = this.accept(partial.id);
if(partial.context) { content = content + " " + this.accept(partial.context); }
return this.pad("{{> " + content + " }}");
};
Handlebars.PrintVisitor.prototype.hash = function(hash) {
var pairs = hash.pairs;
var joinedPairs = [], left, right;
for(var i=0, l=pairs.length; i<l; i++) {
left = pairs[i][0];
right = this.accept(pairs[i][1]);
joinedPairs.push( left + "=" + right );
}
return "HASH{" + joinedPairs.join(", ") + "}";
};
Handlebars.PrintVisitor.prototype.STRING = function(string) {
return '"' + string.string + '"';
};
Handlebars.PrintVisitor.prototype.INTEGER = function(integer) {
return "INTEGER{" + integer.integer + "}";
};
Handlebars.PrintVisitor.prototype.BOOLEAN = function(bool) {
return "BOOLEAN{" + bool.bool + "}";
};
Handlebars.PrintVisitor.prototype.ID = function(id) {
var path = id.parts.join("/");
if(id.parts.length > 1) {
return "PATH:" + path;
} else {
return "ID:" + path;
}
};
Handlebars.PrintVisitor.prototype.DATA = function(data) {
return "@" + data.id;
};
Handlebars.PrintVisitor.prototype.content = function(content) {
return this.pad("CONTENT[ '" + content.string + "' ]");
};
Handlebars.PrintVisitor.prototype.comment = function(comment) {
return this.pad("{{! '" + comment.comment + "' }}");
};
// END(BROWSER)
exports.PrintVisitor = Handlebars.PrintVisitor;
+15 -8
View File
@@ -1,13 +1,20 @@
var Handlebars = require("./base");
// BEGIN(BROWSER) exports.attach = function(Handlebars) {
Handlebars.Visitor = function() {}; // BEGIN(BROWSER)
Handlebars.Visitor = function() {};
Handlebars.Visitor.prototype = {
accept: function(object) {
return this[object.type](object);
}
};
// END(BROWSER)
return Handlebars;
Handlebars.Visitor.prototype = {
accept: function(object) {
return this[object.type](object);
}
}; };
// END(BROWSER)
+61 -57
View File
@@ -1,68 +1,72 @@
var Handlebars = require("./base"); exports.attach = function(Handlebars) {
// BEGIN(BROWSER) // BEGIN(BROWSER)
Handlebars.VM = {
template: function(templateSpec) { Handlebars.VM = {
// Just add water template: function(templateSpec) {
var container = { // Just add water
escapeExpression: Handlebars.Utils.escapeExpression, var container = {
invokePartial: Handlebars.VM.invokePartial, escapeExpression: Handlebars.Utils.escapeExpression,
programs: [], invokePartial: Handlebars.VM.invokePartial,
program: function(i, fn, data) { programs: [],
var programWrapper = this.programs[i]; program: function(i, fn, data) {
if(data) { var programWrapper = this.programs[i];
return Handlebars.VM.program(fn, data); if(data) {
} else if(programWrapper) { return Handlebars.VM.program(fn, data);
return programWrapper; } else if(programWrapper) {
} else { return programWrapper;
programWrapper = this.programs[i] = Handlebars.VM.program(fn); } else {
return programWrapper; programWrapper = this.programs[i] = Handlebars.VM.program(fn);
} return programWrapper;
}, }
programWithDepth: Handlebars.VM.programWithDepth, },
noop: Handlebars.VM.noop programWithDepth: Handlebars.VM.programWithDepth,
}; noop: Handlebars.VM.noop
};
return function(context, options) { return function(context, options) {
options = options || {}; options = options || {};
return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
}; };
}, },
programWithDepth: function(fn, data, $depth) { programWithDepth: function(fn, data, $depth) {
var args = Array.prototype.slice.call(arguments, 2); var args = Array.prototype.slice.call(arguments, 2);
return function(context, options) { return function(context, options) {
options = options || {}; options = options || {};
return fn.apply(this, [context, options.data || data].concat(args)); return fn.apply(this, [context, options.data || data].concat(args));
}; };
}, },
program: function(fn, data) { program: function(fn, data) {
return function(context, options) { return function(context, options) {
options = options || {}; options = options || {};
return fn(context, options.data || data); return fn(context, options.data || data);
}; };
}, },
noop: function() { return ""; }, noop: function() { return ""; },
invokePartial: function(partial, name, context, helpers, partials, data) { invokePartial: function(partial, name, context, helpers, partials, data) {
var options = { helpers: helpers, partials: partials, data: data }; var options = { helpers: helpers, partials: partials, data: data };
if(partial === undefined) { if(partial === undefined) {
throw new Handlebars.Exception("The partial " + name + " could not be found"); throw new Handlebars.Exception("The partial " + name + " could not be found");
} else if(partial instanceof Function) { } else if(partial instanceof Function) {
return partial(context, options); return partial(context, options);
} else if (!Handlebars.compile) { } else if (!Handlebars.compile) {
throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
} else { } else {
partials[name] = Handlebars.compile(partial); partials[name] = Handlebars.compile(partial);
return partials[name](context, options); return partials[name](context, options);
}
} }
} };
};
Handlebars.template = Handlebars.VM.template; Handlebars.template = Handlebars.VM.template;
// END(BROWSER) // END(BROWSER)
return Handlebars;
};
+67 -63
View File
@@ -1,68 +1,72 @@
var Handlebars = require("./base"); exports.attach = function(Handlebars) {
// BEGIN(BROWSER) // BEGIN(BROWSER)
Handlebars.Exception = function(message) {
var tmp = Error.prototype.constructor.apply(this, arguments);
for (var p in tmp) { Handlebars.Exception = function(message) {
if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; } var tmp = Error.prototype.constructor.apply(this, arguments);
}
this.message = tmp.message; for (var p in tmp) {
}; if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
Handlebars.Exception.prototype = new Error();
// Build out our basic SafeString type
Handlebars.SafeString = function(string) {
this.string = string;
};
Handlebars.SafeString.prototype.toString = function() {
return this.string.toString();
};
(function() {
var escape = {
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#x27;",
"`": "&#x60;"
};
var badChars = /&(?!\w+;)|[<>"'`]/g;
var possible = /[&<>"'`]/;
var escapeChar = function(chr) {
return escape[chr] || "&amp;";
};
Handlebars.Utils = {
escapeExpression: function(string) {
// don't escape SafeStrings, since they're already safe
if (string instanceof Handlebars.SafeString) {
return string.toString();
} else if (string == null || string === false) {
return "";
}
if(!possible.test(string)) { return string; }
return string.replace(badChars, escapeChar);
},
isEmpty: function(value) {
if (typeof value === "undefined") {
return true;
} else if (value === null) {
return true;
} else if (value === false) {
return true;
} else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
return true;
} else {
return false;
}
} }
};
})();
// END(BROWSER)
this.message = tmp.message;
};
Handlebars.Exception.prototype = new Error();
// Build out our basic SafeString type
Handlebars.SafeString = function(string) {
this.string = string;
};
Handlebars.SafeString.prototype.toString = function() {
return this.string.toString();
};
(function() {
var escape = {
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#x27;",
"`": "&#x60;"
};
var badChars = /&(?!\w+;)|[<>"'`]/g;
var possible = /[&<>"'`]/;
var escapeChar = function(chr) {
return escape[chr] || "&amp;";
};
Handlebars.Utils = {
escapeExpression: function(string) {
// don't escape SafeStrings, since they're already safe
if (string instanceof Handlebars.SafeString) {
return string.toString();
} else if (string == null || string === false) {
return "";
}
if(!possible.test(string)) { return string; }
return string.replace(badChars, escapeChar);
},
isEmpty: function(value) {
if (typeof value === "undefined") {
return true;
} else if (value === null) {
return true;
} else if (value === false) {
return true;
} else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
return true;
} else {
return false;
}
}
};
})();
// END(BROWSER)
return Handlebars;
};