diff --git a/lib/handlebars.js b/lib/handlebars.js index bb40f5f6..68fa615e 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -1,30 +1,21 @@ -var ast = require("handlebars/ast"); -var Lexer = require("handlebars/handlebars_lexer").Lexer; -var printer = require("handlebars/printer"); -var parser = require("handlebars/parser").parser; +if(exports) { + var Handlebars = {}; -parser.yy = { - ProgramNode: ast.ProgramNode, - MustacheNode: ast.MustacheNode, - ContentNode: ast.ContentNode, - IdNode: ast.IdNode, - StringNode: ast.StringNode, - PartialNode: ast.PartialNode, - CommentNode: ast.CommentNode, - BlockNode: ast.BlockNode, - inspect: function(obj) { - var sys = require("sys"); - sys.print(sys.inspect(obj) + "\n"); - } + Handlebars.AST = require("handlebars/ast").AST; + Handlebars.Lexer = require("handlebars/handlebars_lexer").Lexer; + Handlebars.PrintVisitor = require("handlebars/printer").PrintVisitor; + Handlebars.Parser = require("handlebars/parser").parser; } -parser.lexer = new Lexer; - -exports.parse = function(string) { - return parser.parse(string); +Handlebars.parse = function(string) { + Handlebars.Parser.yy = Handlebars.AST; + Handlebars.Parser.lexer = new Handlebars.Lexer; + return Handlebars.Parser.parse(string); }; -exports.print = function(ast) { - var out = new PrintVisitor().accept(ast); +Handlebars.print = function(ast) { + var out = new Handlebars.PrintVisitor().accept(ast); require("sys").print(out); } + +if(exports) { exports.Handlebars = Handlebars } diff --git a/lib/handlebars/ast.js b/lib/handlebars/ast.js index d37d378c..79dfa5d0 100644 --- a/lib/handlebars/ast.js +++ b/lib/handlebars/ast.js @@ -1,53 +1,50 @@ -var ProgramNode = function(statements, inverse) { +if(exports) { var Handlebars = {} } + +Handlebars.AST = {}; + +Handlebars.AST.ProgramNode = function(statements, inverse) { this.type = "program"; this.statements = statements; this.inverse = inverse; }; -var MustacheNode = function(params) { +Handlebars.AST.MustacheNode = function(params) { this.type = "mustache"; this.id = params[0]; this.params = params.slice(1); }; -var PartialNode = function(id) { +Handlebars.AST.PartialNode = function(id) { this.type = "partial"; this.id = id; }; -var BlockNode = function(mustache, program) { +Handlebars.AST.BlockNode = function(mustache, program) { this.type = "block"; this.mustache = mustache; this.program = program; }; -var ContentNode = function(string) { +Handlebars.AST.ContentNode = function(string) { this.type = "content"; this.string = string; } -var IdNode = function(id) { +Handlebars.AST.IdNode = function(id) { this.type = "ID" this.id = id; } -StringNode = function(string) { +Handlebars.AST.StringNode = function(string) { this.type = "STRING"; this.string = string; } -CommentNode = function(comment) { +Handlebars.AST.CommentNode = function(comment) { this.type = "comment"; this.comment = comment; } if(exports) { - exports.ProgramNode = ProgramNode; - exports.MustacheNode = MustacheNode; - exports.ContentNode = ContentNode; - exports.IdNode = IdNode; - exports.StringNode = StringNode; - exports.PartialNode = PartialNode; - exports.CommentNode = CommentNode; - exports.BlockNode = BlockNode; + exports.AST = Handlebars.AST; } diff --git a/lib/handlebars/handlebars_lexer.js b/lib/handlebars/handlebars_lexer.js index a2d3e71f..08551307 100644 --- a/lib/handlebars/handlebars_lexer.js +++ b/lib/handlebars/handlebars_lexer.js @@ -1,13 +1,14 @@ -if(require) { - var Lexer = require("handlebars/jison_ext").Lexer +if(exports) { + var Handlebars = {}; + Handlebars.Lexer = require("handlebars/jison_ext").Lexer } -var HandlebarsLexer = function() { +Handlebars.HandlebarsLexer = function() { this.state = "CONTENT"; }; -HandlebarsLexer.prototype = new Lexer; +Handlebars.HandlebarsLexer.prototype = new Handlebars.Lexer; -HandlebarsLexer.prototype.lex = function() { +Handlebars.HandlebarsLexer.prototype.lex = function() { if(this.input === "") return; this.setupLex(); @@ -68,4 +69,4 @@ HandlebarsLexer.prototype.lex = function() { } }; -if(exports) { exports.Lexer = HandlebarsLexer; } +if(exports) { exports.Lexer = Handlebars.HandlebarsLexer; } diff --git a/lib/handlebars/jison_ext.js b/lib/handlebars/jison_ext.js index 0608034e..fa43d47d 100644 --- a/lib/handlebars/jison_ext.js +++ b/lib/handlebars/jison_ext.js @@ -1,6 +1,8 @@ -var Lexer = function() {}; +if(exports) { var Handlebars = {} } -Lexer.prototype = { +Handlebars.Lexer = function() {}; + +Handlebars.Lexer.prototype = { setInput: function(input) { this.input = input; this.yylineno = 0; @@ -44,15 +46,15 @@ Lexer.prototype = { } }; -var Visitor = function() {}; +Handlebars.Visitor = function() {}; -Visitor.prototype = { +Handlebars.Visitor.prototype = { accept: function(object) { return this[object.type](object); } } if(exports) { - exports.Lexer = Lexer; - exports.Visitor = Visitor; + exports.Lexer = Handlebars.Lexer; + exports.Visitor = Handlebars.Visitor; } diff --git a/lib/handlebars/printer.js b/lib/handlebars/printer.js index 542cfcf1..5db2f814 100644 --- a/lib/handlebars/printer.js +++ b/lib/handlebars/printer.js @@ -1,9 +1,12 @@ -if(exports) { var Visitor = require("handlebars/jison_ext").Visitor } +if(exports) { + var Handlebars = {}; + Handlebars.Visitor = require("handlebars/jison_ext").Visitor +} -PrintVisitor = function() { this.padding = 0; }; -PrintVisitor.prototype = new Visitor; +Handlebars.PrintVisitor = function() { this.padding = 0; }; +Handlebars.PrintVisitor.prototype = new Handlebars.Visitor; -PrintVisitor.prototype.pad = function(string, newline) { +Handlebars.PrintVisitor.prototype.pad = function(string, newline) { var out = ""; for(var i=0,l=this.padding; i " + this.accept(partial.id) + " }}"); }; -PrintVisitor.prototype.STRING = function(string) { +Handlebars.PrintVisitor.prototype.STRING = function(string) { return string.string; }; -PrintVisitor.prototype.ID = function(id) { +Handlebars.PrintVisitor.prototype.ID = function(id) { return "ID:" + id.id; }; -PrintVisitor.prototype.content = function(content) { +Handlebars.PrintVisitor.prototype.content = function(content) { return this.pad("CONTENT[ '" + content.string + "' ]"); }; -PrintVisitor.prototype.comment = function(comment) { +Handlebars.PrintVisitor.prototype.comment = function(comment) { return this.pad("{{! '" + comment.comment + "' }}"); } if(exports) { - exports.PrintVisitor = PrintVisitor; + exports.PrintVisitor = Handlebars.PrintVisitor; } diff --git a/test.js b/test.js index 7a2b3636..394d41f3 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,6 @@ require.paths.push(__dirname + "/lib"); -var Handlebars = require("handlebars"); +var Handlebars = require("handlebars").Handlebars; var string = "foo {{ bar baz \"baz\" }}baz{{! foo bar baz }}{{#foo}} bar {{^}} baz {{/foo}}{{> partial }}{{# bar }}part1 {{^}} part2{{> foo }}{{/bar}}zomg"