/*! Dust - Asynchronous Templating - v2.4.2 * http://linkedin.github.io/dustjs/ * Copyright (c) 2014 Aleksander Williams; Released under the MIT License */ (function(root) { var dust = {}, NONE = 'NONE', ERROR = 'ERROR', WARN = 'WARN', INFO = 'INFO', DEBUG = 'DEBUG', loggingLevels = [DEBUG, INFO, WARN, ERROR, NONE], EMPTY_FUNC = function() {}, logger = {}, originalLog, loggerContext; dust.debugLevel = NONE; // Try to find the console in global scope if (root && root.console && root.console.log) { loggerContext = root.console; originalLog = root.console.log; } // robust logger for node.js, modern browsers, and IE <= 9. logger.log = loggerContext ? function() { // Do this for normal browsers if (typeof originalLog === 'function') { logger.log = function() { originalLog.apply(loggerContext, arguments); }; } else { // Do this for IE <= 9 logger.log = function() { var message = Array.prototype.slice.apply(arguments).join(' '); originalLog(message); }; } logger.log.apply(this, arguments); } : function() { /* no op */ }; /** * Log dust debug statements, info statements, warning statements, and errors. * Filters out the messages based on the dust.debuglevel. * This default implementation will print to the console if it exists. * @param {String|Error} message the message to print/throw * @param {String} type the severity of the message(ERROR, WARN, INFO, or DEBUG) * @public */ dust.log = function(message, type) { type = type || INFO; if (dust.debugLevel !== NONE && dust.indexInArray(loggingLevels, type) >= dust.indexInArray(loggingLevels, dust.debugLevel)) { if(!dust.logQueue) { dust.logQueue = []; } dust.logQueue.push({message: message, type: type}); logger.log('[DUST ' + type + ']: ' + message); } }; dust.helpers = {}; dust.cache = {}; dust.register = function(name, tmpl) { if (!name) { return; } dust.cache[name] = tmpl; }; dust.render = function(name, context, callback) { var chunk = new Stub(callback).head; try { dust.load(name, chunk, Context.wrap(context, name)).end(); } catch (err) { chunk.setError(err); } }; dust.stream = function(name, context) { var stream = new Stream(), chunk = stream.head; dust.nextTick(function() { try { dust.load(name, stream.head, Context.wrap(context, name)).end(); } catch (err) { chunk.setError(err); } }); return stream; }; dust.renderSource = function(source, context, callback) { return dust.compileFn(source)(context, callback); }; dust.compileFn = function(source, name) { // name is optional. When name is not provided the template can only be rendered using the callable returned by this function. // If a name is provided the compiled template can also be rendered by name. name = name || null; var tmpl = dust.loadSource(dust.compile(source, name)); return function(context, callback) { var master = callback ? new Stub(callback) : new Stream(); dust.nextTick(function() { if(typeof tmpl === 'function') { tmpl(master.head, Context.wrap(context, name)).end(); } else { dust.log(new Error('Template [' + name + '] cannot be resolved to a Dust function'), ERROR); } }); return master; }; }; dust.load = function(name, chunk, context) { var tmpl = dust.cache[name]; if (tmpl) { return tmpl(chunk, context); } else { if (dust.onLoad) { return chunk.map(function(chunk) { dust.onLoad(name, function(err, src) { if (err) { return chunk.setError(err); } if (!dust.cache[name]) { dust.loadSource(dust.compile(src, name)); } dust.cache[name](chunk, context).end(); }); }); } return chunk.setError(new Error('Template Not Found: ' + name)); } }; dust.loadSource = function(source, path) { return eval(source); }; if (Array.isArray) { dust.isArray = Array.isArray; } else { dust.isArray = function(arr) { return Object.prototype.toString.call(arr) === '[object Array]'; }; } // indexOf shim for arrays for IE <= 8 // source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf dust.indexInArray = function(arr, item, fromIndex) { fromIndex = +fromIndex || 0; if (Array.prototype.indexOf) { return arr.indexOf(item, fromIndex); } else { if ( arr === undefined || arr === null ) { throw new TypeError( 'cannot call method "indexOf" of null' ); } var length = arr.length; // Hack to convert object.length to a UInt32 if (Math.abs(fromIndex) === Infinity) { fromIndex = 0; } if (fromIndex < 0) { fromIndex += length; if (fromIndex < 0) { fromIndex = 0; } } for (;fromIndex < length; fromIndex++) { if (arr[fromIndex] === item) { return fromIndex; } } return -1; } }; dust.nextTick = (function() { return function(callback) { setTimeout(callback,0); }; } )(); dust.isEmpty = function(value) { if (dust.isArray(value) && !value.length) { return true; } if (value === 0) { return false; } return (!value); }; // apply the filter chain and return the output string dust.filter = function(string, auto, filters) { if (filters) { for (var i=0, len=filters.length; i 0) { // any custom helper can blow up the stack // and store a flattened context, guard defensively if(context.stack.head) { context.stack.head['$len'] = len; } for (var i=0; i\"\']/), AMP = /&/g, LT = //g, QUOT = /\"/g, SQUOT = /\'/g; dust.escapeHtml = function(s) { if (typeof s === 'string') { if (!HCHARS.test(s)) { return s; } return s.replace(AMP,'&').replace(LT,'<').replace(GT,'>').replace(QUOT,'"').replace(SQUOT, '''); } return s; }; var BS = /\\/g, FS = /\//g, CR = /\r/g, LS = /\u2028/g, PS = /\u2029/g, NL = /\n/g, LF = /\f/g, SQ = /'/g, DQ = /"/g, TB = /\t/g; dust.escapeJs = function(s) { if (typeof s === 'string') { return s .replace(BS, '\\\\') .replace(FS, '\\/') .replace(DQ, '\\"') .replace(SQ, '\\\'') .replace(CR, '\\r') .replace(LS, '\\u2028') .replace(PS, '\\u2029') .replace(NL, '\\n') .replace(LF, '\\f') .replace(TB, '\\t'); } return s; }; if (typeof exports === 'object') { module.exports = dust; } else { root.dust = dust; } })((function(){return this;})());