/*jshint latedef:false */ (function(root, factory) { if (typeof exports === 'object') { // in Node, require this file if we want to use the compiler as a standalone module module.exports = factory(require('./parser').parse, require('./dust')); } else { // in the browser, store the factory output if we want to use the compiler directly factory(root.dust.parse, root.dust); } }(this, function(parse, dust) { var compiler = {}, isArray = dust.isArray; compiler.compile = function(source, name) { // the name parameter is optional. // this can happen for templates that are rendered immediately (renderSource which calls compileFn) or // for templates that are compiled as a callable (compileFn) // // for the common case (using compile and render) a name is required so that templates will be cached by name and rendered later, by name. if (!name && name !== null) { throw new Error('Template name parameter cannot be undefined when calling dust.compile'); } try { var ast = filterAST(parse(source)); return compile(ast, name); } catch (err) { if (!err.line || !err.column) { throw err; } throw new SyntaxError(err.message + ' At line : ' + err.line + ', column : ' + err.column); } }; function filterAST(ast) { var context = {}; return compiler.filterNode(context, ast); } compiler.filterNode = function(context, node) { return compiler.optimizers[node[0]](context, node); }; compiler.optimizers = { body: compactBuffers, buffer: noop, special: convertSpecial, format: nullify, // TODO: convert format reference: visit, '#': visit, '?': visit, '^': visit, '<': visit, '+': visit, '@': visit, '%': visit, partial: visit, context: visit, params: visit, bodies: visit, param: visit, filters: noop, key: noop, path: noop, literal: noop, raw: noop, comment: nullify, line: nullify, col: nullify }; compiler.pragmas = { esc: function(compiler, context, bodies, params) { var old = compiler.auto, out; if (!context) { context = 'h'; } compiler.auto = (context === 's') ? '' : context; out = compileParts(compiler, bodies.block); compiler.auto = old; return out; } }; function visit(context, node) { var out = [node[0]], i, len, res; for (i=1, len=node.length; i