Rinser/node_modules/ejs/lib/ejs.js

842 lines
23 KiB
JavaScript
Raw Normal View History

2015-07-20 13:42:07 +00:00
/*
* EJS Embedded JavaScript templates
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
'use strict';
/**
2017-07-18 11:42:39 +00:00
* @file Embedded JavaScript templating engine. {@link http://ejs.co}
2015-07-20 13:42:07 +00:00
* @author Matthew Eernisse <mde@fleegix.org>
* @author Tiancheng "Timothy" Gu <timothygu99@gmail.com>
* @project EJS
* @license {@link http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0}
*/
/**
* EJS internal functions.
*
* Technically this "module" lies in the same file as {@link module:ejs}, for
* the sake of organization all the private functions re grouped into this
* module.
*
* @module ejs-internal
* @private
*/
/**
* Embedded JavaScript templating engine.
*
* @module ejs
* @public
*/
2017-07-18 11:42:39 +00:00
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 _NAME = 'ejs';
var _REGEX_STRING = '(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
var _OPTS = ['delimiter', 'scope', 'context', 'debug', 'compileDebug',
'client', '_with', 'rmWhitespace', 'strict', 'filename'];
// We don't allow 'cache' option to be passed in the data obj
// for the normal `render` call, but this is where Express puts it
// so we make an exception for `renderFile`
var _OPTS_EXPRESS = _OPTS.concat('cache');
var _BOM = /^\uFEFF/;
2015-07-20 13:42:07 +00:00
/**
* EJS template function cache. This can be a LRU object from lru-cache NPM
* module. By default, it is {@link module:utils.cache}, a simple in-process
* cache that grows continuously.
*
* @type {Cache}
*/
exports.cache = utils.cache;
2017-07-18 11:42:39 +00:00
/**
* Custom file loader. Useful for template preprocessing or restricting access
* to a certain part of the filesystem.
*
* @type {fileLoader}
*/
exports.fileLoader = fs.readFileSync;
2015-07-20 13:42:07 +00:00
/**
* Name of the object containing the locals.
*
2017-07-18 11:42:39 +00:00
* This variable is overridden by {@link Options}`.localsName` if it is not
2015-07-20 13:42:07 +00:00
* `undefined`.
*
* @type {String}
* @public
*/
exports.localsName = _DEFAULT_LOCALS_NAME;
/**
* Get the path to the included file from the parent file path and the
* specified path.
*
2017-07-18 11:42:39 +00:00
* @param {String} name specified path
* @param {String} filename parent file path
* @param {Boolean} isDir parent file path whether is directory
2015-07-20 13:42:07 +00:00
* @return {String}
*/
2017-07-18 11:42:39 +00:00
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);
2015-07-20 13:42:07 +00:00
if (!ext) {
includePath += '.ejs';
}
return includePath;
};
2017-07-18 11:42:39 +00:00
/**
* 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;
}
2015-07-20 13:42:07 +00:00
/**
* 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.
*
* If `template` is not set, the file specified in `options.filename` will be
* read.
*
* If `options.cache` is true, this function reads the file from
* `options.filename` so it must be set prior to calling this function.
*
* @memberof module:ejs-internal
* @param {Options} options compilation options
* @param {String} [template] template source
* @return {(TemplateFunction|ClientFunction)}
* Depending on the value of `options.client`, either type might be returned.
* @static
*/
function handleCache(options, template) {
2017-07-18 11:42:39 +00:00
var func;
var filename = options.filename;
var hasTemplate = arguments.length > 1;
2015-07-20 13:42:07 +00:00
if (options.cache) {
2017-07-18 11:42:39 +00:00
if (!filename) {
2015-07-20 13:42:07 +00:00
throw new Error('cache option requires a filename');
}
2017-07-18 11:42:39 +00:00
func = exports.cache.get(filename);
if (func) {
return func;
2015-07-20 13:42:07 +00:00
}
if (!hasTemplate) {
2017-07-18 11:42:39 +00:00
template = fileLoader(filename).toString().replace(_BOM, '');
2015-07-20 13:42:07 +00:00
}
}
else if (!hasTemplate) {
// istanbul ignore if: should not happen at all
2017-07-18 11:42:39 +00:00
if (!filename) {
2015-07-20 13:42:07 +00:00
throw new Error('Internal EJS error: no file name or template '
+ 'provided');
}
2017-07-18 11:42:39 +00:00
template = fileLoader(filename).toString().replace(_BOM, '');
2015-07-20 13:42:07 +00:00
}
2017-07-18 11:42:39 +00:00
func = exports.compile(template, options);
2015-07-20 13:42:07 +00:00
if (options.cache) {
2017-07-18 11:42:39 +00:00
exports.cache.set(filename, func);
2015-07-20 13:42:07 +00:00
}
2017-07-18 11:42:39 +00:00
return func;
}
/**
* Try calling handleCache with the given options and data and call the
* callback with the result. If an error occurs, call the callback with
* the error. Used by renderFile().
*
* @memberof module:ejs-internal
* @param {Options} options compilation options
* @param {Object} data template data
* @param {RenderFileCallback} cb callback
* @static
*/
function tryHandleCache(options, data, cb) {
var result;
try {
result = handleCache(options)(data);
}
catch (err) {
return cb(err);
}
return cb(null, result);
}
/**
* fileLoader is independent
*
* @param {String} filePath ejs file path.
* @return {String} The contents of the specified file.
* @static
*/
function fileLoader(filePath){
return exports.fileLoader(filePath);
2015-07-20 13:42:07 +00:00
}
/**
* Get the template function.
*
* If `options.cache` is `true`, then the template is cached.
*
* @memberof module:ejs-internal
* @param {String} path path for the specified file
* @param {Options} options compilation options
* @return {(TemplateFunction|ClientFunction)}
* Depending on the value of `options.client`, either type might be returned
* @static
*/
function includeFile(path, options) {
var opts = utils.shallowCopy({}, options);
2017-07-18 11:42:39 +00:00
opts.filename = getIncludePath(path, opts);
2015-07-20 13:42:07 +00:00
return handleCache(opts);
}
/**
* Get the JavaScript source of an included file.
*
* @memberof module:ejs-internal
* @param {String} path path for the specified file
* @param {Options} options compilation options
2017-07-18 11:42:39 +00:00
* @return {Object}
2015-07-20 13:42:07 +00:00
* @static
*/
function includeSource(path, options) {
2017-07-18 11:42:39 +00:00
var opts = utils.shallowCopy({}, options);
var includePath;
var template;
includePath = getIncludePath(path, opts);
template = fileLoader(includePath).toString().replace(_BOM, '');
2015-07-20 13:42:07 +00:00
opts.filename = includePath;
var templ = new Template(template, opts);
templ.generateSource();
2017-07-18 11:42:39 +00:00
return {
source: templ.source,
filename: includePath,
template: template
};
2015-07-20 13:42:07 +00:00
}
/**
* Re-throw the given `err` in context to the `str` of ejs, `filename`, and
* `lineno`.
*
* @implements RethrowCallback
* @memberof module:ejs-internal
* @param {Error} err Error object
* @param {String} str EJS source
* @param {String} filename file name of the EJS file
* @param {String} lineno line number of the error
* @static
*/
2017-07-18 11:42:39 +00:00
function rethrow(err, str, flnm, lineno, esc){
var lines = str.split('\n');
var start = Math.max(lineno - 3, 0);
var end = Math.min(lines.length, lineno + 3);
var filename = esc(flnm); // eslint-disable-line
2015-07-20 13:42:07 +00:00
// Error context
var context = lines.slice(start, end).map(function (line, i){
var curr = i + start + 1;
return (curr == lineno ? ' >> ' : ' ')
+ curr
+ '| '
+ line;
}).join('\n');
// Alter exception message
err.path = filename;
err.message = (filename || 'ejs') + ':'
+ lineno + '\n'
+ context + '\n\n'
+ err.message;
throw err;
}
2017-07-18 11:42:39 +00:00
function stripSemi(str){
return str.replace(/;(\s*$)/, '$1');
2015-07-20 13:42:07 +00:00
}
/**
* Compile the given `str` of ejs into a template function.
*
* @param {String} template EJS template
*
* @param {Options} opts compilation options
*
* @return {(TemplateFunction|ClientFunction)}
* Depending on the value of `opts.client`, either type might be returned.
* @public
*/
exports.compile = function compile(template, opts) {
var templ;
// v1 compat
// 'scope' is 'context'
// FIXME: Remove this in a future version
if (opts && opts.scope) {
if (!scopeOptionWarned){
console.warn('`scope` option is deprecated and will be removed in EJS 3');
scopeOptionWarned = true;
}
if (!opts.context) {
opts.context = opts.scope;
}
delete opts.scope;
}
templ = new Template(template, opts);
return templ.compile();
};
/**
* Render the given `template` of ejs.
*
* If you would like to include options but not data, you need to explicitly
* call this function with `data` being an empty object or `null`.
*
* @param {String} template EJS template
* @param {Object} [data={}] template data
* @param {Options} [opts={}] compilation and rendering options
* @return {String}
* @public
*/
2017-07-18 11:42:39 +00:00
exports.render = function (template, d, o) {
var data = d || {};
var opts = o || {};
2015-07-20 13:42:07 +00:00
// No options object -- if there are optiony names
// in the data, copy them to options
if (arguments.length == 2) {
2017-07-18 11:42:39 +00:00
utils.shallowCopyFromList(opts, data, _OPTS);
2015-07-20 13:42:07 +00:00
}
return handleCache(opts, template)(data);
};
/**
* Render an EJS file at the given `path` and callback `cb(err, str)`.
*
* If you would like to include options but not data, you need to explicitly
* call this function with `data` being an empty object or `null`.
*
* @param {String} path path to the EJS file
* @param {Object} [data={}] template data
* @param {Options} [opts={}] compilation and rendering options
* @param {RenderFileCallback} cb callback
* @public
*/
exports.renderFile = function () {
2017-07-18 11:42:39 +00:00
var filename = arguments[0];
var cb = arguments[arguments.length - 1];
var opts = {filename: filename};
var data;
if (arguments.length > 2) {
data = arguments[1];
// No options object -- if there are optiony names
// in the data, copy them to options
if (arguments.length === 3) {
// Express 4
if (data.settings && data.settings['view options']) {
utils.shallowCopyFromList(opts, data.settings['view options'], _OPTS_EXPRESS);
}
// Express 3 and lower
else {
utils.shallowCopyFromList(opts, data, _OPTS_EXPRESS);
}
2016-02-12 13:10:16 +00:00
}
else {
2017-07-18 11:42:39 +00:00
// Use shallowCopy so we don't pollute passed in opts obj with new vals
utils.shallowCopy(opts, arguments[2]);
2016-02-12 13:10:16 +00:00
}
2015-07-20 13:42:07 +00:00
2017-07-18 11:42:39 +00:00
opts.filename = filename;
2015-07-20 13:42:07 +00:00
}
2017-07-18 11:42:39 +00:00
else {
data = {};
2015-07-20 13:42:07 +00:00
}
2017-07-18 11:42:39 +00:00
return tryHandleCache(opts, data, cb);
2015-07-20 13:42:07 +00:00
};
/**
* Clear intermediate JavaScript cache. Calls {@link Cache#reset}.
* @public
*/
exports.clearCache = function () {
exports.cache.reset();
};
function Template(text, opts) {
opts = opts || {};
var options = {};
this.templateText = text;
this.mode = null;
this.truncate = false;
this.currentLine = 1;
this.source = '';
this.dependencies = [];
options.client = opts.client || false;
options.escapeFunction = opts.escape || utils.escapeXML;
options.compileDebug = opts.compileDebug !== false;
options.debug = !!opts.debug;
options.filename = opts.filename;
options.delimiter = opts.delimiter || exports.delimiter || _DEFAULT_DELIMITER;
2016-02-12 13:10:16 +00:00
options.strict = opts.strict || false;
2015-07-20 13:42:07 +00:00
options.context = opts.context;
options.cache = opts.cache || false;
options.rmWhitespace = opts.rmWhitespace;
2017-07-18 11:42:39 +00:00
options.root = opts.root;
2016-02-12 13:10:16 +00:00
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;
}
2015-07-20 13:42:07 +00:00
this.opts = options;
this.regex = this.createRegex();
}
Template.modes = {
2017-07-18 11:42:39 +00:00
EVAL: 'eval',
ESCAPED: 'escaped',
RAW: 'raw',
COMMENT: 'comment',
LITERAL: 'literal'
2015-07-20 13:42:07 +00:00
};
Template.prototype = {
createRegex: function () {
2017-07-18 11:42:39 +00:00
var str = _REGEX_STRING;
var delim = utils.escapeRegExpChars(this.opts.delimiter);
2015-07-20 13:42:07 +00:00
str = str.replace(/%/g, delim);
return new RegExp(str);
2017-07-18 11:42:39 +00:00
},
2015-07-20 13:42:07 +00:00
2017-07-18 11:42:39 +00:00
compile: function () {
var src;
var fn;
var opts = this.opts;
var prepended = '';
var appended = '';
var escapeFn = opts.escapeFunction;
2015-12-21 10:03:16 +00:00
2015-07-20 13:42:07 +00:00
if (!this.source) {
this.generateSource();
prepended += ' var __output = [], __append = __output.push.bind(__output);' + '\n';
if (opts._with !== false) {
2016-02-12 13:10:16 +00:00
prepended += ' with (' + opts.localsName + ' || {}) {' + '\n';
2015-07-20 13:42:07 +00:00
appended += ' }' + '\n';
}
appended += ' return __output.join("");' + '\n';
this.source = prepended + this.source + appended;
}
if (opts.compileDebug) {
src = 'var __line = 1' + '\n'
+ ' , __lines = ' + JSON.stringify(this.templateText) + '\n'
+ ' , __filename = ' + (opts.filename ?
JSON.stringify(opts.filename) : 'undefined') + ';' + '\n'
+ 'try {' + '\n'
+ this.source
+ '} catch (e) {' + '\n'
2017-07-18 11:42:39 +00:00
+ ' rethrow(e, __lines, __filename, __line, escapeFn);' + '\n'
2015-07-20 13:42:07 +00:00
+ '}' + '\n';
}
else {
src = this.source;
}
if (opts.debug) {
console.log(src);
}
if (opts.client) {
2017-07-18 11:42:39 +00:00
src = 'escapeFn = escapeFn || ' + escapeFn.toString() + ';' + '\n' + src;
2015-07-20 13:42:07 +00:00
if (opts.compileDebug) {
src = 'rethrow = rethrow || ' + rethrow.toString() + ';' + '\n' + src;
}
}
2016-02-12 13:10:16 +00:00
if (opts.strict) {
src = '"use strict";\n' + src;
}
2015-07-20 13:42:07 +00:00
try {
2017-07-18 11:42:39 +00:00
fn = new Function(opts.localsName + ', escapeFn, include, rethrow', src);
2015-07-20 13:42:07 +00:00
}
catch(e) {
// istanbul ignore else
if (e instanceof SyntaxError) {
if (opts.filename) {
e.message += ' in ' + opts.filename;
}
2017-07-18 11:42:39 +00:00
e.message += ' while compiling ejs\n\n';
e.message += 'If the above error is not helpful, you may want to try EJS-Lint:\n';
e.message += 'https://github.com/RyanZim/EJS-Lint';
2015-07-20 13:42:07 +00:00
}
throw e;
}
if (opts.client) {
fn.dependencies = this.dependencies;
return fn;
}
// Return a callable function which will execute the function
// created by the source-code, with the passed data as locals
// Adds a local `include` function which allows full recursive include
var returnedFn = function (data) {
var include = function (path, includeData) {
var d = utils.shallowCopy({}, data);
if (includeData) {
d = utils.shallowCopy(d, includeData);
}
return includeFile(path, opts)(d);
};
2017-07-18 11:42:39 +00:00
return fn.apply(opts.context, [data || {}, escapeFn, include, rethrow]);
2015-07-20 13:42:07 +00:00
};
returnedFn.dependencies = this.dependencies;
return returnedFn;
2017-07-18 11:42:39 +00:00
},
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, '_%>');
2015-07-20 13:42:07 +00:00
2017-07-18 11:42:39 +00:00
var self = this;
var matches = this.parseTemplateText();
var d = this.opts.delimiter;
2015-07-20 13:42:07 +00:00
if (matches && matches.length) {
matches.forEach(function (line, index) {
2017-07-18 11:42:39 +00:00
var opening;
var closing;
var include;
var includeOpts;
var includeObj;
var includeSrc;
2015-07-20 13:42:07 +00:00
// 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
// Then this can simply check against the map
if ( line.indexOf('<' + d) === 0 // If it is a tag
&& line.indexOf('<' + d + d) !== 0) { // and is not escaped
closing = matches[index + 2];
2015-12-21 10:03:16 +00:00
if (!(closing == d + '>' || closing == '-' + d + '>' || closing == '_' + d + '>')) {
2015-07-20 13:42:07 +00:00
throw new Error('Could not find matching close tag for "' + line + '".');
}
}
// HACK: backward-compat `include` preprocessor directives
if ((include = line.match(/^\s*include\s+(\S+)/))) {
opening = matches[index - 1];
// Must be in EVAL or RAW mode
2015-12-21 10:03:16 +00:00
if (opening && (opening == '<' + d || opening == '<' + d + '-' || opening == '<' + d + '_')) {
2015-07-20 13:42:07 +00:00
includeOpts = utils.shallowCopy({}, self.opts);
2017-07-18 11:42:39 +00:00
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';
}
2015-07-20 13:42:07 +00:00
self.source += includeSrc;
self.dependencies.push(exports.resolveInclude(include[1],
includeOpts.filename));
return;
}
}
self.scanLine(line);
});
}
2017-07-18 11:42:39 +00:00
},
2015-07-20 13:42:07 +00:00
2017-07-18 11:42:39 +00:00
parseTemplateText: function () {
var str = this.templateText;
var pat = this.regex;
var result = pat.exec(str);
var arr = [];
var firstPos;
2015-07-20 13:42:07 +00:00
while (result) {
firstPos = result.index;
if (firstPos !== 0) {
arr.push(str.substring(0, firstPos));
str = str.slice(firstPos);
}
arr.push(result[0]);
str = str.slice(result[0].length);
result = pat.exec(str);
}
if (str) {
arr.push(str);
}
return arr;
2017-07-18 11:42:39 +00:00
},
2015-07-20 13:42:07 +00:00
2017-07-18 11:42:39 +00:00
scanLine: function (line) {
var self = this;
var d = this.opts.delimiter;
var newLineCount = 0;
2015-07-20 13:42:07 +00:00
function _addOutput() {
if (self.truncate) {
2016-02-12 13:10:16 +00:00
// 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
2017-07-18 11:42:39 +00:00
line = line.replace(/^(?:\r\n|\r|\n)/, '');
2015-07-20 13:42:07 +00:00
self.truncate = false;
}
else if (self.opts.rmWhitespace) {
2017-07-18 11:42:39 +00:00
// rmWhitespace has already removed trailing spaces, just need
// to remove linebreaks
2015-07-20 13:42:07 +00:00
line = line.replace(/^\n/, '');
}
if (!line) {
return;
}
// Preserve literal slashes
line = line.replace(/\\/g, '\\\\');
// Convert linebreaks
line = line.replace(/\n/g, '\\n');
line = line.replace(/\r/g, '\\r');
// Escape double-quotes
// - this will be the delimiter during execution
line = line.replace(/"/g, '\\"');
self.source += ' ; __append("' + line + '")' + '\n';
}
newLineCount = (line.split('\n').length - 1);
switch (line) {
2017-07-18 11:42:39 +00:00
case '<' + d:
case '<' + d + '_':
this.mode = Template.modes.EVAL;
break;
case '<' + d + '=':
this.mode = Template.modes.ESCAPED;
break;
case '<' + d + '-':
this.mode = Template.modes.RAW;
break;
case '<' + d + '#':
this.mode = Template.modes.COMMENT;
break;
case '<' + d + d:
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 + '>':
if (this.mode == Template.modes.LITERAL) {
_addOutput();
}
2015-07-20 13:42:07 +00:00
2017-07-18 11:42:39 +00:00
this.mode = null;
this.truncate = line.indexOf('-') === 0 || line.indexOf('_') === 0;
break;
default:
2015-07-20 13:42:07 +00:00
// In script mode, depends on type of tag
2017-07-18 11:42:39 +00:00
if (this.mode) {
2015-07-20 13:42:07 +00:00
// If '//' is found without a line break, add a line break.
2017-07-18 11:42:39 +00:00
switch (this.mode) {
case Template.modes.EVAL:
case Template.modes.ESCAPED:
case Template.modes.RAW:
if (line.lastIndexOf('//') > line.lastIndexOf('\n')) {
line += '\n';
2015-07-20 13:42:07 +00:00
}
2017-07-18 11:42:39 +00:00
}
switch (this.mode) {
2015-07-20 13:42:07 +00:00
// Just executing code
2017-07-18 11:42:39 +00:00
case Template.modes.EVAL:
this.source += ' ; ' + line + '\n';
break;
2015-07-20 13:42:07 +00:00
// Exec, esc, and output
2017-07-18 11:42:39 +00:00
case Template.modes.ESCAPED:
this.source += ' ; __append(escapeFn(' + stripSemi(line) + '))' + '\n';
break;
2015-07-20 13:42:07 +00:00
// Exec and output
2017-07-18 11:42:39 +00:00
case Template.modes.RAW:
this.source += ' ; __append(' + stripSemi(line) + ')' + '\n';
break;
case Template.modes.COMMENT:
2015-07-20 13:42:07 +00:00
// Do nothing
2017-07-18 11:42:39 +00:00
break;
2015-07-20 13:42:07 +00:00
// Literal <%% mode, append as raw output
2017-07-18 11:42:39 +00:00
case Template.modes.LITERAL:
2015-07-20 13:42:07 +00:00
_addOutput();
2017-07-18 11:42:39 +00:00
break;
2015-07-20 13:42:07 +00:00
}
2017-07-18 11:42:39 +00:00
}
// In string mode, just add the output
else {
_addOutput();
}
2015-07-20 13:42:07 +00:00
}
if (self.opts.compileDebug && newLineCount) {
this.currentLine += newLineCount;
this.source += ' ; __line = ' + this.currentLine + '\n';
}
}
};
2017-07-18 11:42:39 +00:00
/**
* 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
2016-06-15 13:38:21 +00:00
* */
exports.escapeXML = utils.escapeXML;
2015-07-20 13:42:07 +00:00
/**
* Express.js support.
*
* This is an alias for {@link module:ejs.renderFile}, in order to support
* Express.js out-of-the-box.
*
* @func
*/
exports.__express = exports.renderFile;
// Add require support
/* istanbul ignore else */
if (require.extensions) {
2017-07-18 11:42:39 +00:00
require.extensions['.ejs'] = function (module, flnm) {
var filename = flnm || /* istanbul ignore next */ module.filename;
2015-07-20 13:42:07 +00:00
var options = {
2017-07-18 11:42:39 +00:00
filename: filename,
client: true
};
var template = fileLoader(filename).toString();
var fn = exports.compile(template, options);
2015-07-20 13:42:07 +00:00
module._compile('module.exports = ' + fn.toString() + ';', filename);
};
}
/**
* Version of EJS.
*
* @readonly
* @type {String}
* @public
*/
exports.VERSION = _VERSION_STRING;
2017-07-18 11:42:39 +00:00
/**
* Name for detection of EJS.
*
* @readonly
* @type {String}
* @public
*/
exports.name = _NAME;
2015-07-20 13:42:07 +00:00
/* istanbul ignore if */
if (typeof window != 'undefined') {
window.ejs = exports;
}