Skip to content

Commit

Permalink
move old include code to scanLine
Browse files Browse the repository at this point in the history
  • Loading branch information
m committed Oct 23, 2017
1 parent 3f39a80 commit 7250566
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,45 +599,9 @@ Template.prototype = {

var self = this;
var matches = this.parseTemplateText();
var d = this.opts.delimiter;

if (matches && matches.length) {
matches.forEach(function (line, index) {
var opening;
var closing;
var include;
var includeOpts;
var includeObj;
var includeSrc;
// 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
if (opening && (opening == '<' + d || opening == '<' + d + '-' || opening == '<' + d + '_')) {
includeOpts = utils.shallowCopy({}, self.opts);
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, escapeFn);' + '\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));
return;
}
}
matches.forEach(function (line) {
self.scanLine(line);
});
if (this.isInTag) {
Expand Down Expand Up @@ -738,9 +702,11 @@ Template.prototype = {
isHandled = false;
}
if (isHandled && this.mode != Template.modes.LITERAL) {
if (this.isInTag)
if (this.isInTag) {
throw new Error('Could not find matching close tag for "' + this.isInTag + '".');
}
this.isInTag = line;
this.isAfterTag = true;
}
}
if (! isHandled) {
Expand All @@ -758,7 +724,7 @@ Template.prototype = {
if (this.mode == Template.modes.LITERAL) {
this._addOutput(line);
}

this.mode = null;
this.truncate = line.indexOf('-') === 0 || line.indexOf('_') === 0;
break;
Expand All @@ -772,8 +738,40 @@ Template.prototype = {
// If '//' is found without a line break, add a line break.
switch (this.mode) {
case Template.modes.EVAL:
case Template.modes.ESCAPED:
case Template.modes.RAW:
// HACK: backward-compat `include` preprocessor directives
var include;
if (this.isAfterTag && (include = line.match(/^\s*include\s+(\S+)/))) {
var includeSrc;
var includeOpts = utils.shallowCopy({}, self.opts);
var 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, escapeFn);' + '\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));
return; // TODO: debug info
}
// END HACH include
if (line.lastIndexOf('//') > line.lastIndexOf('\n')) {
line += '\n';
}
break;
case Template.modes.ESCAPED:
if (line.lastIndexOf('//') > line.lastIndexOf('\n')) {
line += '\n';
}
Expand Down Expand Up @@ -805,6 +803,7 @@ Template.prototype = {
this._addOutput(line);
}
}
this.isAfterTag = false;
}

if (self.opts.compileDebug && newLineCount) {
Expand Down

0 comments on commit 7250566

Please sign in to comment.