More modularization

This commit is contained in:
Yehuda Katz
2013-07-02 18:57:28 +00:00
parent 88ee4757e7
commit 12f8299eb2
5 changed files with 21 additions and 69 deletions
-2
View File
@@ -1,2 +0,0 @@
(function(undefined) {
var Handlebars = {};
-13
View File
@@ -1,13 +0,0 @@
if (typeof module === 'object' && module.exports) {
// CommonJS
module.exports = Handlebars;
} else if (typeof define === "function" && define.amd) {
// AMD modules
define(function() { return Handlebars; });
} else {
// other, i.e. browser
this.Handlebars = Handlebars;
}
}).call(this);
-18
View File
@@ -1,18 +0,0 @@
// Each of these module will augment the Handlebars object as it loads. No need to perform addition operations
module.exports.attach = function(Handlebars) {
var visitor = require("./visitor"),
printer = require("./printer"),
ast = require("./ast"),
compiler = require("./compiler"),
javascriptCompiler = require("./javascript-compiler");
visitor.attach(Handlebars);
printer.attach(Handlebars);
ast.attach(Handlebars);
compiler.attach(Handlebars);
javascriptCompiler.attach(Handlebars);
return Handlebars;
};
+18 -24
View File
@@ -1,15 +1,13 @@
exports.attach = function(Handlebars) {
import Visitor from "handlebars/compiler/visitor";
// BEGIN(BROWSER)
Handlebars.print = function(ast) {
return new Handlebars.PrintVisitor().accept(ast);
export print = function(ast) {
return new PrintVisitor().accept(ast);
};
Handlebars.PrintVisitor = function() { this.padding = 0; };
Handlebars.PrintVisitor.prototype = new Handlebars.Visitor();
export function PrintVisitor() { this.padding = 0; };
PrintVisitor.prototype = new Handlebars.Visitor();
Handlebars.PrintVisitor.prototype.pad = function(string, newline) {
PrintVisitor.prototype.pad = function(string, newline) {
var out = "";
for(var i=0,l=this.padding; i<l; i++) {
@@ -22,7 +20,7 @@ Handlebars.PrintVisitor.prototype.pad = function(string, newline) {
return out;
};
Handlebars.PrintVisitor.prototype.program = function(program) {
PrintVisitor.prototype.program = function(program) {
var out = "",
statements = program.statements,
inverse = program.inverse,
@@ -37,7 +35,7 @@ Handlebars.PrintVisitor.prototype.program = function(program) {
return out;
};
Handlebars.PrintVisitor.prototype.block = function(block) {
PrintVisitor.prototype.block = function(block) {
var out = "";
out = out + this.pad("BLOCK:");
@@ -76,13 +74,13 @@ Handlebars.PrintVisitor.prototype.mustache = function(mustache) {
return this.pad("{{ " + this.accept(mustache.id) + " " + params + hash + " }}");
};
Handlebars.PrintVisitor.prototype.partial = function(partial) {
PrintVisitor.prototype.partial = function(partial) {
var content = this.accept(partial.partialName);
if(partial.context) { content = content + " " + this.accept(partial.context); }
return this.pad("{{> " + content + " }}");
};
Handlebars.PrintVisitor.prototype.hash = function(hash) {
PrintVisitor.prototype.hash = function(hash) {
var pairs = hash.pairs;
var joinedPairs = [], left, right;
@@ -95,19 +93,19 @@ Handlebars.PrintVisitor.prototype.hash = function(hash) {
return "HASH{" + joinedPairs.join(", ") + "}";
};
Handlebars.PrintVisitor.prototype.STRING = function(string) {
PrintVisitor.prototype.STRING = function(string) {
return '"' + string.string + '"';
};
Handlebars.PrintVisitor.prototype.INTEGER = function(integer) {
PrintVisitor.prototype.INTEGER = function(integer) {
return "INTEGER{" + integer.integer + "}";
};
Handlebars.PrintVisitor.prototype.BOOLEAN = function(bool) {
PrintVisitor.prototype.BOOLEAN = function(bool) {
return "BOOLEAN{" + bool.bool + "}";
};
Handlebars.PrintVisitor.prototype.ID = function(id) {
PrintVisitor.prototype.ID = function(id) {
var path = id.parts.join("/");
if(id.parts.length > 1) {
return "PATH:" + path;
@@ -116,23 +114,19 @@ Handlebars.PrintVisitor.prototype.ID = function(id) {
}
};
Handlebars.PrintVisitor.prototype.PARTIAL_NAME = function(partialName) {
PrintVisitor.prototype.PARTIAL_NAME = function(partialName) {
return "PARTIAL:" + partialName.name;
};
Handlebars.PrintVisitor.prototype.DATA = function(data) {
PrintVisitor.prototype.DATA = function(data) {
return "@" + this.accept(data.id);
};
Handlebars.PrintVisitor.prototype.content = function(content) {
PrintVisitor.prototype.content = function(content) {
return this.pad("CONTENT[ '" + content.string + "' ]");
};
Handlebars.PrintVisitor.prototype.comment = function(comment) {
PrintVisitor.prototype.comment = function(comment) {
return this.pad("{{! '" + comment.comment + "' }}");
};
// END(BROWSER)
return Handlebars;
};
+3 -12
View File
@@ -1,18 +1,9 @@
exports.attach = function(Handlebars) {
export function Visitor() {};
// BEGIN(BROWSER)
Visitor.prototype = {
constructor: Visitor,
Handlebars.Visitor = function() {};
Handlebars.Visitor.prototype = {
accept: function(object) {
return this[object.type](object);
}
};
// END(BROWSER)
return Handlebars;
};