summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/handlebars.js18
-rw-r--r--lib/handlebars/base.js26
-rw-r--r--lib/handlebars/compiler/ast.js34
-rw-r--r--lib/handlebars/compiler/base.js6
-rw-r--r--lib/handlebars/compiler/compiler.js14
-rw-r--r--lib/handlebars/compiler/javascript-compiler.js17
-rw-r--r--lib/handlebars/compiler/printer.js2
-rw-r--r--lib/handlebars/runtime.js45
-rw-r--r--lib/handlebars/template.js3
9 files changed, 101 insertions, 64 deletions
diff --git a/lib/handlebars.js b/lib/handlebars.js
index e051aa5..b448658 100644
--- a/lib/handlebars.js
+++ b/lib/handlebars.js
@@ -1,21 +1,21 @@
-import handlebars from "handlebars/base";
+import { base as handlebars } from "./handlebars/base";
// Each of these augment the Handlebars object. No need to setup here.
// (This is done to easily share code between commonjs and browse envs)
-import { SafeString, Exception, extend, escapeExpression, isEmpty } from "handlebars/utils";
-import compiler from "handlebars/compiler";
-import runtime from "handlebars/runtime";
+import { SafeString, Exception, extend, escapeExpression, isEmpty } from "./handlebars/utils";
+import { compile, precompile } from "./handlebars/compiler/compiler";
+import { template } from "./handlebars/runtime";
// For compatibility and usage outside of module systems, make the Handlebars object a namespace
var create = function() {
- var hb = handlebars.create();
+ var hb = handlebars();
hb.SafeString = SafeString;
hb.Exception = Exception;
- hb.utils = { extend: extend, escapeExpression: escapeExpression, isEmpty: isEmpty };
-
- compiler.attach(hb);
- runtime.attach(hb);
+ hb.Utils = { extend: extend, escapeExpression: escapeExpression, isEmpty: isEmpty };
+ hb.compile = compile;
+ hb.precompile = precompile;
+ hb.template = template;
return hb;
};
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js
index 875fe03..0e3383e 100644
--- a/lib/handlebars/base.js
+++ b/lib/handlebars/base.js
@@ -1,13 +1,13 @@
/*jshint eqnull: true */
-import { Exception, extend } from "handlebars/utils";
+import { Exception, extend } from "./utils";
var K = function() { return this; };
-export VERSION = "1.0.0";
-export COMPILER_REVISION = 4;
+export var VERSION = "1.0.0";
+export var COMPILER_REVISION = 4;
-export REVISION_CHANGES = {
+export var REVISION_CHANGES = {
1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
2: '== 1.0.0-rc.3',
3: '== 1.0.0-rc.4',
@@ -19,15 +19,17 @@ export function base(helpers, partials) {
var exports = {};
+ var helpers = helpers || {};
+ var partials = partials || {};
- helpers = helpers || {};
- partials = partials || {};
+ exports.helpers = helpers;
+ exports.partials = partials;
var toString = Object.prototype.toString,
functionType = '[object Function]',
objectType = '[object Object]';
- exports.registerHelper(name, fn, inverse) {
+ exports.registerHelper = function(name, fn, inverse) {
if (toString.call(name) === objectType) {
if (inverse || fn) { throw new Exception('Arg not supported with multiple helpers'); }
extend(helpers, name);
@@ -37,7 +39,7 @@ export function base(helpers, partials) {
}
};
- exports.registerPartial(name, str) {
+ exports.registerPartial = function(name, str) {
if (toString.call(name) === objectType) {
extend(partials, name);
} else {
@@ -83,7 +85,7 @@ export function base(helpers, partials) {
if(type === functionType) { context = context.call(this); }
if (options.data) {
- data = Handlebars.createFrame(options.data);
+ data = createFrame(options.data);
}
if(context && typeof context === 'object') {
@@ -137,7 +139,7 @@ export function base(helpers, partials) {
Handlebars.log(level, context);
});
- return Handlebars;
+ return exports;
}
var levels = {
@@ -146,7 +148,7 @@ var levels = {
var methodMap = { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' };
-export logger = {
+export var logger = {
// can be overridden in the host environment
log: function(level, obj) {
if (Handlebars.logger.level <= level) {
@@ -160,7 +162,7 @@ export logger = {
export function log(level, obj) { logger.log(level, obj); };
-export createFrame = Object.create || function(object) {
+export var createFrame = Object.create || function(object) {
K.prototype = object;
var obj = new K();
K.prototype = null;
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js
index fcf5c6e..b61fa0e 100644
--- a/lib/handlebars/compiler/ast.js
+++ b/lib/handlebars/compiler/ast.js
@@ -1,12 +1,14 @@
-import { Exception } from "handlebars/utils";
+import { Exception } from "../utils";
-export function ProgramNode(statements, inverse) {
+var exports = {};
+
+exports.ProgramNode = function ProgramNode(statements, inverse) {
this.type = "program";
this.statements = statements;
- if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); }
+ if(inverse) { this.inverse = new ProgramNode(inverse); }
};
-export function MustacheNode(rawParams, hash, unescaped) {
+exports.MustacheNode = function(rawParams, hash, unescaped) {
this.type = "mustache";
this.escaped = !unescaped;
this.hash = hash;
@@ -28,13 +30,13 @@ export function MustacheNode(rawParams, hash, unescaped) {
// pass or at runtime.
};
-export function PartialNode(partialName, context) {
+exports.PartialNode = function(partialName, context) {
this.type = "partial";
this.partialName = partialName;
this.context = context;
};
-export function BlockNode(mustache, program, inverse, close) {
+exports.BlockNode = function(mustache, program, inverse, close) {
if(mustache.id.original !== close.original) {
throw new Exception(mustache.id.original + " doesn't match " + close.original);
}
@@ -49,17 +51,17 @@ export function BlockNode(mustache, program, inverse, close) {
}
};
-export function ContentNode(string) {
+exports.ContentNode = function(string) {
this.type = "content";
this.string = string;
};
-export function HashNode(pairs) {
+exports.HashNode = function(pairs) {
this.type = "hash";
this.pairs = pairs;
};
-export function IdNode(part) {
+exports.IdNode = function(parts) {
this.type = "ID";
var original = "",
@@ -90,37 +92,39 @@ export function IdNode(part) {
this.stringModeValue = this.string;
};
-export function PartialNameNode(name) {
+exports.PartialNameNode = function(name) {
this.type = "PARTIAL_NAME";
this.name = name.original;
};
-export function DataNode(id) {
+exports.DataNode = function(id) {
this.type = "DATA";
this.id = id;
};
-export function StringNode(string) {
+exports.StringNode = function(string) {
this.type = "STRING";
this.original =
this.string =
this.stringModeValue = string;
};
-export function IntegerNode(integer) {
+exports.IntegerNode = function(integer) {
this.type = "INTEGER";
this.original =
this.integer = integer;
this.stringModeValue = Number(integer);
};
-export function BooleanNode(bool) {
+exports.BooleanNode = function(bool) {
this.type = "BOOLEAN";
this.bool = bool;
this.stringModeValue = bool === "true";
};
-export function CommentNode(comment) {
+exports.CommentNode = function(comment) {
this.type = "comment";
this.comment = comment;
};
+
+export default exports;
diff --git a/lib/handlebars/compiler/base.js b/lib/handlebars/compiler/base.js
index 8dd464d..adbf4f2 100644
--- a/lib/handlebars/compiler/base.js
+++ b/lib/handlebars/compiler/base.js
@@ -1,7 +1,7 @@
-import { parser } from "handlebars/compiler/parser";
-module AST from "handlebars/compiler/ast":
+import parser from "./parser";
+import AST from "./ast";
-export Parser = parser;
+export var parser = parser;
export function parse(input) {
// Just return if an already-compile AST was passed in.
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index 6e051b7..b779617 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -1,6 +1,8 @@
-import { Exception } from "handlebars/utils";
-import parse from "handlebars/compiler/base";
-import { template } from "handlebars/runtime";
+import { Exception } from "../utils";
+import { template } from "../runtime";
+import { parse } from "./base";
+import { JavaScriptCompiler } from "./javascript-compiler";
+import AST from "./ast";
/*jshint eqnull:true*/
@@ -414,7 +416,7 @@ Compiler.prototype = {
};
export function precompile(input, options) {
- if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
+ if (input == null || (typeof input !== 'string' && input.constructor !== AST.ProgramNode)) {
throw new Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input);
}
@@ -428,8 +430,8 @@ export function precompile(input, options) {
return new JavaScriptCompiler().compile(environment, options);
};
-export function compile = function(input, options) {
- if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
+export function compile(input, options) {
+ if (input == null || (typeof input !== 'string' && input.constructor !== AST.ProgramNode)) {
throw new Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
}
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js
index f5d70ee..a416431 100644
--- a/lib/handlebars/compiler/javascript-compiler.js
+++ b/lib/handlebars/compiler/javascript-compiler.js
@@ -1,10 +1,9 @@
-import { COMPILER_REVISION, REVISION_CHANGES } from "handlebars/base";
+import { COMPILER_REVISION, REVISION_CHANGES } from "../base";
function Literal(value) {
this.value = value;
};
-
export function JavaScriptCompiler() {};
JavaScriptCompiler.prototype = {
@@ -44,7 +43,9 @@ JavaScriptCompiler.prototype = {
this.environment = environment;
this.options = options || {};
- Handlebars.log(Handlebars.logger.DEBUG, this.environment.disassemble() + "\n\n");
+ // TODO: a module-compatible logger
+ // Handlebars.log(Handlebars.logger.DEBUG, this.environment.disassemble() + "\n\n");
+ log(this.environment.disassemble() + "\n\n");
this.name = this.environment.name;
this.isChild = !!context;
@@ -166,7 +167,11 @@ JavaScriptCompiler.prototype = {
return Function.apply(this, params);
} else {
var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + source + '}';
- Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
+
+ // TODO: a module-compatible logger
+ //Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
+ log(functionSource + "\n\n");
+
return functionSource;
}
},
@@ -841,3 +846,7 @@ JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
}
return false;
};
+
+function log(string) {
+ //console.log(string);
+}
diff --git a/lib/handlebars/compiler/printer.js b/lib/handlebars/compiler/printer.js
index c6e08bb..0280960 100644
--- a/lib/handlebars/compiler/printer.js
+++ b/lib/handlebars/compiler/printer.js
@@ -1,4 +1,4 @@
-import Visitor from "handlebars/compiler/visitor";
+import Visitor from "./visitor";
export function print(ast) {
return new PrintVisitor().accept(ast);
diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js
index 2585ef3..991bdfd 100644
--- a/lib/handlebars/runtime.js
+++ b/lib/handlebars/runtime.js
@@ -1,14 +1,33 @@
-import { escapeExpression, extend, Exceptions } from "handlebars/utils";
-import { COMPILER_REVISION, REVISION_CHANGES } from "handlebars/base";
+import { escapeExpression, extend, Exception } from "./utils";
+import { COMPILER_REVISION, REVISION_CHANGES } from "./base";
-// TODO: Deal with the fact that compile is necessary for on-the-fly partial
-// compilation but won't be present for precompiled templates.
+// TODO: Remove this line and break up compilePartial
+
+export function template(templateSpec, Hbars) {
+ // TODO: Make this less global
+ Hbars = Hbars || Handlebars;
+
+ if (Hbars.compile) {
+ var invokePartialWrapper = function(partial, name, context, helpers, partials, data) {
+ var result = invokePartial.apply(this, arguments);
+ if (result) { return result; }
+
+ var options = { helpers: helpers, partials: partials, data: data };
+ partials[name] = Hbars.compile(partial, { data: data !== undefined });
+ return partials[name](context, options);
+ };
+ } else {
+ var invokePartialWrapper = function(partial, name, context, helpers, partials, data) {
+ var result = invokePartial.apply(this, arguments);
+ if (result) { return result; }
+ throw new Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
+ };
+ }
-export function template(templateSpec) {
// Just add water
var container = {
escapeExpression: escapeExpression,
- invokePartial: invokePartial,
+ invokePartial: invokePartialWrapper,
programs: [],
program: function(i, fn, data) {
var programWrapper = this.programs[i];
@@ -36,8 +55,11 @@ export function template(templateSpec) {
return function(context, options) {
options = options || {};
+
+ Hbars = Hbars || require("handlebars");
+
// TODO: Why does templateSpec require a reference to the global Handlebars?
- var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
+ var result = templateSpec.call(container, Hbars, context, options.helpers, options.partials, options.data);
var compilerInfo = container.compilerInfo || [],
compilerRevision = compilerInfo[0] || 1,
@@ -84,8 +106,6 @@ export function program(i, fn, data) {
return program;
}
-export function noop() { return ""; }
-
export function invokePartial(partial, name, context, helpers, partials, data) {
var options = { helpers: helpers, partials: partials, data: data };
@@ -93,10 +113,7 @@ export function invokePartial(partial, name, context, helpers, partials, data) {
throw new Exception("The partial " + name + " could not be found");
} else if(partial instanceof Function) {
return partial(context, options);
- } else if (!compile) {
- throw new Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
- } else {
- partials[name] = compile(partial, {data: data !== undefined});
- return partials[name](context, options);
}
}
+
+export function noop() { return ""; }
diff --git a/lib/handlebars/template.js b/lib/handlebars/template.js
new file mode 100644
index 0000000..befba7e
--- /dev/null
+++ b/lib/handlebars/template.js
@@ -0,0 +1,3 @@
+import { escapeExpression, extend } from "./utils";
+import { COMPILER_REVISION, REVISION_CHANGES } from "./base";
+