updated node modules
This commit is contained in:
336
nodejs/node_modules/ejs/lib/ejs.js
generated
vendored
336
nodejs/node_modules/ejs/lib/ejs.js
generated
vendored
@@ -44,18 +44,20 @@
|
||||
* @public
|
||||
*/
|
||||
|
||||
var fs = require('fs')
|
||||
, utils = require('./utils')
|
||||
, scopeOptionWarned = false
|
||||
, _VERSION_STRING = require('../package.json').version
|
||||
, _DEFAULT_DELIMITER = '%'
|
||||
, _DEFAULT_LOCALS_NAME = 'locals'
|
||||
, _REGEX_STRING = '(<%%|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)'
|
||||
, _OPTS = [ 'cache', 'filename', 'delimiter', 'scope', 'context'
|
||||
, 'debug', 'compileDebug', 'client', '_with', 'rmWhitespace'
|
||||
]
|
||||
, _TRAILING_SEMCOL = /;\s*$/
|
||||
, _BOM = /^\uFEFF/;
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var utils = require('./utils');
|
||||
|
||||
var scopeOptionWarned = false;
|
||||
var _VERSION_STRING = require('../package.json').version;
|
||||
var _DEFAULT_DELIMITER = '%';
|
||||
var _DEFAULT_LOCALS_NAME = 'locals';
|
||||
var _REGEX_STRING = '(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
|
||||
var _OPTS = [ 'cache', 'filename', 'delimiter', 'scope', 'context',
|
||||
'debug', 'compileDebug', 'client', '_with', 'root', 'rmWhitespace',
|
||||
'strict', 'localsName'];
|
||||
var _TRAILING_SEMCOL = /;\s*$/;
|
||||
var _BOM = /^\uFEFF/;
|
||||
|
||||
/**
|
||||
* EJS template function cache. This can be a LRU object from lru-cache NPM
|
||||
@@ -83,24 +85,44 @@ exports.localsName = _DEFAULT_LOCALS_NAME;
|
||||
* Get the path to the included file from the parent file path and the
|
||||
* specified path.
|
||||
*
|
||||
* @param {String} name specified path
|
||||
* @param {String} filename parent file path
|
||||
* @param {String} name specified path
|
||||
* @param {String} filename parent file path
|
||||
* @param {Boolean} isDir parent file path whether is directory
|
||||
* @return {String}
|
||||
*/
|
||||
|
||||
exports.resolveInclude = function(name, filename) {
|
||||
var path = require('path')
|
||||
, dirname = path.dirname
|
||||
, extname = path.extname
|
||||
, resolve = path.resolve
|
||||
, includePath = resolve(dirname(filename), name)
|
||||
, ext = extname(name);
|
||||
exports.resolveInclude = function(name, filename, isDir) {
|
||||
var dirname = path.dirname;
|
||||
var extname = path.extname;
|
||||
var resolve = path.resolve;
|
||||
var includePath = resolve(isDir ? filename : dirname(filename), name);
|
||||
var ext = extname(name);
|
||||
if (!ext) {
|
||||
includePath += '.ejs';
|
||||
}
|
||||
return includePath;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the path to the included file by Options
|
||||
*
|
||||
* @param {String} path specified path
|
||||
* @param {Options} options compilation options
|
||||
* @return {String}
|
||||
*/
|
||||
function getIncludePath(path, options){
|
||||
var includePath;
|
||||
if (path.charAt(0) == '/') {
|
||||
includePath = exports.resolveInclude(path.replace(/^\/*/,''), options.root || '/', true);
|
||||
}
|
||||
else {
|
||||
if (!options.filename) {
|
||||
throw new Error('`include` use relative path requires the \'filename\' option.');
|
||||
}
|
||||
includePath = exports.resolveInclude(path, options.filename);
|
||||
}
|
||||
return includePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the template from a string or a file, either compiled on-the-fly or
|
||||
* read from cache (if enabled), and cache the template if needed.
|
||||
@@ -120,35 +142,35 @@ exports.resolveInclude = function(name, filename) {
|
||||
*/
|
||||
|
||||
function handleCache(options, template) {
|
||||
var fn
|
||||
, path = options.filename
|
||||
, hasTemplate = arguments.length > 1;
|
||||
var func;
|
||||
var filename = options.filename;
|
||||
var hasTemplate = arguments.length > 1;
|
||||
|
||||
if (options.cache) {
|
||||
if (!path) {
|
||||
if (!filename) {
|
||||
throw new Error('cache option requires a filename');
|
||||
}
|
||||
fn = exports.cache.get(path);
|
||||
if (fn) {
|
||||
return fn;
|
||||
func = exports.cache.get(filename);
|
||||
if (func) {
|
||||
return func;
|
||||
}
|
||||
if (!hasTemplate) {
|
||||
template = fs.readFileSync(path).toString().replace(_BOM, '');
|
||||
template = fs.readFileSync(filename).toString().replace(_BOM, '');
|
||||
}
|
||||
}
|
||||
else if (!hasTemplate) {
|
||||
// istanbul ignore if: should not happen at all
|
||||
if (!path) {
|
||||
if (!filename) {
|
||||
throw new Error('Internal EJS error: no file name or template '
|
||||
+ 'provided');
|
||||
}
|
||||
template = fs.readFileSync(path).toString().replace(_BOM, '');
|
||||
template = fs.readFileSync(filename).toString().replace(_BOM, '');
|
||||
}
|
||||
fn = exports.compile(template, options);
|
||||
func = exports.compile(template, options);
|
||||
if (options.cache) {
|
||||
exports.cache.set(path, fn);
|
||||
exports.cache.set(filename, func);
|
||||
}
|
||||
return fn;
|
||||
return func;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,10 +188,7 @@ function handleCache(options, template) {
|
||||
|
||||
function includeFile(path, options) {
|
||||
var opts = utils.shallowCopy({}, options);
|
||||
if (!opts.filename) {
|
||||
throw new Error('`include` requires the \'filename\' option.');
|
||||
}
|
||||
opts.filename = exports.resolveInclude(path, opts.filename);
|
||||
opts.filename = getIncludePath(path, opts);
|
||||
return handleCache(opts);
|
||||
}
|
||||
|
||||
@@ -179,24 +198,24 @@ function includeFile(path, options) {
|
||||
* @memberof module:ejs-internal
|
||||
* @param {String} path path for the specified file
|
||||
* @param {Options} options compilation options
|
||||
* @return {String}
|
||||
* @return {Object}
|
||||
* @static
|
||||
*/
|
||||
|
||||
function includeSource(path, options) {
|
||||
var opts = utils.shallowCopy({}, options)
|
||||
, includePath
|
||||
, template;
|
||||
if (!opts.filename) {
|
||||
throw new Error('`include` requires the \'filename\' option.');
|
||||
}
|
||||
includePath = exports.resolveInclude(path, opts.filename);
|
||||
var opts = utils.shallowCopy({}, options);
|
||||
var includePath;
|
||||
var template;
|
||||
includePath = getIncludePath(path,opts);
|
||||
template = fs.readFileSync(includePath).toString().replace(_BOM, '');
|
||||
|
||||
opts.filename = includePath;
|
||||
var templ = new Template(template, opts);
|
||||
templ.generateSource();
|
||||
return templ.source;
|
||||
return {
|
||||
source: templ.source,
|
||||
filename: includePath,
|
||||
template: template
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,10 +232,9 @@ function includeSource(path, options) {
|
||||
*/
|
||||
|
||||
function rethrow(err, str, filename, lineno){
|
||||
var lines = str.split('\n')
|
||||
, start = Math.max(lineno - 3, 0)
|
||||
, end = Math.min(lines.length, lineno + 3);
|
||||
|
||||
var lines = str.split('\n');
|
||||
var start = Math.max(lineno - 3, 0);
|
||||
var end = Math.min(lines.length, lineno + 3);
|
||||
// Error context
|
||||
var context = lines.slice(start, end).map(function (line, i){
|
||||
var curr = i + start + 1;
|
||||
@@ -301,10 +319,9 @@ exports.compile = function compile(template, opts) {
|
||||
* @public
|
||||
*/
|
||||
|
||||
exports.render = function (template, data, opts) {
|
||||
data = data || {};
|
||||
opts = opts || {};
|
||||
var fn;
|
||||
exports.render = function (template, d, o) {
|
||||
var data = d || {};
|
||||
var opts = o || {};
|
||||
|
||||
// No options object -- if there are optiony names
|
||||
// in the data, copy them to options
|
||||
@@ -329,12 +346,12 @@ exports.render = function (template, data, opts) {
|
||||
*/
|
||||
|
||||
exports.renderFile = function () {
|
||||
var args = Array.prototype.slice.call(arguments)
|
||||
, path = args.shift()
|
||||
, cb = args.pop()
|
||||
, data = args.shift() || {}
|
||||
, opts = args.pop() || {}
|
||||
, result;
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var filename = args.shift();
|
||||
var cb = args.pop();
|
||||
var data = args.shift() || {};
|
||||
var opts = args.pop() || {};
|
||||
var result;
|
||||
|
||||
// Don't pollute passed in opts obj with new vals
|
||||
opts = utils.shallowCopy({}, opts);
|
||||
@@ -342,9 +359,16 @@ exports.renderFile = function () {
|
||||
// No options object -- if there are optiony names
|
||||
// in the data, copy them to options
|
||||
if (arguments.length == 3) {
|
||||
cpOptsInData(data, opts);
|
||||
// Express 4
|
||||
if (data.settings && data.settings['view options']) {
|
||||
cpOptsInData(data.settings['view options'], opts);
|
||||
}
|
||||
// Express 3 and lower
|
||||
else {
|
||||
cpOptsInData(data, opts);
|
||||
}
|
||||
}
|
||||
opts.filename = path;
|
||||
opts.filename = filename;
|
||||
|
||||
try {
|
||||
result = handleCache(opts)(data);
|
||||
@@ -379,55 +403,54 @@ function Template(text, opts) {
|
||||
options.debug = !!opts.debug;
|
||||
options.filename = opts.filename;
|
||||
options.delimiter = opts.delimiter || exports.delimiter || _DEFAULT_DELIMITER;
|
||||
options._with = typeof opts._with != 'undefined' ? opts._with : true;
|
||||
options.strict = opts.strict || false;
|
||||
options.context = opts.context;
|
||||
options.cache = opts.cache || false;
|
||||
options.rmWhitespace = opts.rmWhitespace;
|
||||
options.root = opts.root;
|
||||
options.localsName = opts.localsName || exports.localsName || _DEFAULT_LOCALS_NAME;
|
||||
|
||||
if (options.strict) {
|
||||
options._with = false;
|
||||
}
|
||||
else {
|
||||
options._with = typeof opts._with != 'undefined' ? opts._with : true;
|
||||
}
|
||||
|
||||
this.opts = options;
|
||||
|
||||
this.regex = this.createRegex();
|
||||
}
|
||||
|
||||
Template.modes = {
|
||||
EVAL: 'eval'
|
||||
, ESCAPED: 'escaped'
|
||||
, RAW: 'raw'
|
||||
, COMMENT: 'comment'
|
||||
, LITERAL: 'literal'
|
||||
EVAL: 'eval',
|
||||
ESCAPED: 'escaped',
|
||||
RAW: 'raw',
|
||||
COMMENT: 'comment',
|
||||
LITERAL: 'literal'
|
||||
};
|
||||
|
||||
Template.prototype = {
|
||||
createRegex: function () {
|
||||
var str = _REGEX_STRING
|
||||
, delim = utils.escapeRegExpChars(this.opts.delimiter);
|
||||
var str = _REGEX_STRING;
|
||||
var delim = utils.escapeRegExpChars(this.opts.delimiter);
|
||||
str = str.replace(/%/g, delim);
|
||||
return new RegExp(str);
|
||||
}
|
||||
},
|
||||
|
||||
, compile: function () {
|
||||
var src
|
||||
, fn
|
||||
, opts = this.opts
|
||||
, prepended = ''
|
||||
, appended = ''
|
||||
, escape = opts.escapeFunction;
|
||||
|
||||
if (opts.rmWhitespace) {
|
||||
// Have to use two separate replace here as `^` and `$` operators don't
|
||||
// work well with `\r`.
|
||||
this.templateText =
|
||||
this.templateText.replace(/\r/g, '').replace(/^\s+|\s+$/gm, '');
|
||||
}
|
||||
|
||||
// Slurp spaces and tabs before <%_ and after _%>
|
||||
this.templateText =
|
||||
this.templateText.replace(/[ \t]*<%_/gm, '<%_').replace(/_%>[ \t]*/gm, '_%>');
|
||||
compile: function () {
|
||||
var src;
|
||||
var fn;
|
||||
var opts = this.opts;
|
||||
var prepended = '';
|
||||
var appended = '';
|
||||
var escape = opts.escapeFunction;
|
||||
|
||||
if (!this.source) {
|
||||
this.generateSource();
|
||||
prepended += ' var __output = [], __append = __output.push.bind(__output);' + '\n';
|
||||
if (opts._with !== false) {
|
||||
prepended += ' with (' + exports.localsName + ' || {}) {' + '\n';
|
||||
prepended += ' with (' + opts.localsName + ' || {}) {' + '\n';
|
||||
appended += ' }' + '\n';
|
||||
}
|
||||
appended += ' return __output.join("");' + '\n';
|
||||
@@ -460,8 +483,12 @@ Template.prototype = {
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.strict) {
|
||||
src = '"use strict";\n' + src;
|
||||
}
|
||||
|
||||
try {
|
||||
fn = new Function(exports.localsName + ', escape, include, rethrow', src);
|
||||
fn = new Function(opts.localsName + ', escape, include, rethrow', src);
|
||||
}
|
||||
catch(e) {
|
||||
// istanbul ignore else
|
||||
@@ -494,20 +521,34 @@ Template.prototype = {
|
||||
};
|
||||
returnedFn.dependencies = this.dependencies;
|
||||
return returnedFn;
|
||||
}
|
||||
},
|
||||
|
||||
, generateSource: function () {
|
||||
var self = this
|
||||
, matches = this.parseTemplateText()
|
||||
, d = this.opts.delimiter;
|
||||
generateSource: function () {
|
||||
var opts = this.opts;
|
||||
|
||||
if (opts.rmWhitespace) {
|
||||
// Have to use two separate replace here as `^` and `$` operators don't
|
||||
// work well with `\r`.
|
||||
this.templateText =
|
||||
this.templateText.replace(/\r/g, '').replace(/^\s+|\s+$/gm, '');
|
||||
}
|
||||
|
||||
// Slurp spaces and tabs before <%_ and after _%>
|
||||
this.templateText =
|
||||
this.templateText.replace(/[ \t]*<%_/gm, '<%_').replace(/_%>[ \t]*/gm, '_%>');
|
||||
|
||||
var self = this;
|
||||
var matches = this.parseTemplateText();
|
||||
var d = this.opts.delimiter;
|
||||
|
||||
if (matches && matches.length) {
|
||||
matches.forEach(function (line, index) {
|
||||
var opening
|
||||
, closing
|
||||
, include
|
||||
, includeOpts
|
||||
, includeSrc;
|
||||
var opening;
|
||||
var closing;
|
||||
var include;
|
||||
var includeOpts;
|
||||
var includeObj;
|
||||
var includeSrc;
|
||||
// If this is an opening tag, check for closing tags
|
||||
// FIXME: May end up with some false positives here
|
||||
// Better to store modes as k/v with '<' + delimiter as key
|
||||
@@ -525,9 +566,23 @@ Template.prototype = {
|
||||
// Must be in EVAL or RAW mode
|
||||
if (opening && (opening == '<' + d || opening == '<' + d + '-' || opening == '<' + d + '_')) {
|
||||
includeOpts = utils.shallowCopy({}, self.opts);
|
||||
includeSrc = includeSource(include[1], includeOpts);
|
||||
includeSrc = ' ; (function(){' + '\n' + includeSrc +
|
||||
' ; })()' + '\n';
|
||||
includeObj = includeSource(include[1], includeOpts);
|
||||
if (self.opts.compileDebug) {
|
||||
includeSrc =
|
||||
' ; (function(){' + '\n'
|
||||
+ ' var __line = 1' + '\n'
|
||||
+ ' , __lines = ' + JSON.stringify(includeObj.template) + '\n'
|
||||
+ ' , __filename = ' + JSON.stringify(includeObj.filename) + ';' + '\n'
|
||||
+ ' try {' + '\n'
|
||||
+ includeObj.source
|
||||
+ ' } catch (e) {' + '\n'
|
||||
+ ' rethrow(e, __lines, __filename, __line);' + '\n'
|
||||
+ ' }' + '\n'
|
||||
+ ' ; }).call(this)' + '\n';
|
||||
}else{
|
||||
includeSrc = ' ; (function(){' + '\n' + includeObj.source +
|
||||
' ; }).call(this)' + '\n';
|
||||
}
|
||||
self.source += includeSrc;
|
||||
self.dependencies.push(exports.resolveInclude(include[1],
|
||||
includeOpts.filename));
|
||||
@@ -538,19 +593,17 @@ Template.prototype = {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
, parseTemplateText: function () {
|
||||
var str = this.templateText
|
||||
, pat = this.regex
|
||||
, result = pat.exec(str)
|
||||
, arr = []
|
||||
, firstPos
|
||||
, lastPos;
|
||||
parseTemplateText: function () {
|
||||
var str = this.templateText;
|
||||
var pat = this.regex;
|
||||
var result = pat.exec(str);
|
||||
var arr = [];
|
||||
var firstPos;
|
||||
|
||||
while (result) {
|
||||
firstPos = result.index;
|
||||
lastPos = pat.lastIndex;
|
||||
|
||||
if (firstPos !== 0) {
|
||||
arr.push(str.substring(0, firstPos));
|
||||
@@ -567,20 +620,25 @@ Template.prototype = {
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
},
|
||||
|
||||
, scanLine: function (line) {
|
||||
var self = this
|
||||
, d = this.opts.delimiter
|
||||
, newLineCount = 0;
|
||||
scanLine: function (line) {
|
||||
var self = this;
|
||||
var d = this.opts.delimiter;
|
||||
var newLineCount = 0;
|
||||
|
||||
function _addOutput() {
|
||||
if (self.truncate) {
|
||||
line = line.replace('\n', '');
|
||||
// Only replace single leading linebreak in the line after
|
||||
// -%> tag -- this is the single, trailing linebreak
|
||||
// after the tag that the truncation mode replaces
|
||||
// Handle Win / Unix / old Mac linebreaks -- do the \r\n
|
||||
// combo first in the regex-or
|
||||
line = line.replace(/^(?:\r\n|\r|\n)/, '');
|
||||
self.truncate = false;
|
||||
}
|
||||
else if (self.opts.rmWhitespace) {
|
||||
// Gotta me more careful here.
|
||||
// Gotta be more careful here.
|
||||
// .replace(/^(\s*)\n/, '$1') might be more appropriate here but as
|
||||
// rmWhitespace already removes trailing spaces anyway so meh.
|
||||
line = line.replace(/^\n/, '');
|
||||
@@ -622,6 +680,10 @@ Template.prototype = {
|
||||
this.mode = Template.modes.LITERAL;
|
||||
this.source += ' ; __append("' + line.replace('<' + d + d, '<' + d) + '")' + '\n';
|
||||
break;
|
||||
case d + d + '>':
|
||||
this.mode = Template.modes.LITERAL;
|
||||
this.source += ' ; __append("' + line.replace(d + d + '>', d + '>') + '")' + '\n';
|
||||
break;
|
||||
case d + '>':
|
||||
case '-' + d + '>':
|
||||
case '_' + d + '>':
|
||||
@@ -681,6 +743,20 @@ Template.prototype = {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Escape characters reserved in XML.
|
||||
*
|
||||
* This is simply an export of {@link module:utils.escapeXML}.
|
||||
*
|
||||
* If `markup` is `undefined` or `null`, the empty string is returned.
|
||||
*
|
||||
* @param {String} markup Input string
|
||||
* @return {String} Escaped string
|
||||
* @public
|
||||
* @func
|
||||
* */
|
||||
exports.escapeXML = utils.escapeXML;
|
||||
|
||||
/**
|
||||
* Express.js support.
|
||||
*
|
||||
@@ -695,14 +771,14 @@ exports.__express = exports.renderFile;
|
||||
// Add require support
|
||||
/* istanbul ignore else */
|
||||
if (require.extensions) {
|
||||
require.extensions['.ejs'] = function (module, filename) {
|
||||
filename = filename || /* istanbul ignore next */ module.filename;
|
||||
require.extensions['.ejs'] = function (module, flnm) {
|
||||
var filename = flnm || /* istanbul ignore next */ module.filename;
|
||||
var options = {
|
||||
filename: filename
|
||||
, client: true
|
||||
}
|
||||
, template = fs.readFileSync(filename).toString()
|
||||
, fn = exports.compile(template, options);
|
||||
filename: filename,
|
||||
client: true
|
||||
};
|
||||
var template = fs.readFileSync(filename).toString();
|
||||
var fn = exports.compile(template, options);
|
||||
module._compile('module.exports = ' + fn.toString() + ';', filename);
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user