1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | 'use strict';
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | var fs = require('fs');
|
48 | var path = require('path');
|
49 | var utils = require('./utils');
|
50 |
|
51 | var scopeOptionWarned = false;
|
52 | var _VERSION_STRING = require('../package.json').version;
|
53 | var _DEFAULT_DELIMITER = '%';
|
54 | var _DEFAULT_LOCALS_NAME = 'locals';
|
55 | var _REGEX_STRING = '(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
|
56 | var _OPTS = [ 'cache', 'filename', 'delimiter', 'scope', 'context',
|
57 | 'debug', 'compileDebug', 'client', '_with', 'root', 'rmWhitespace',
|
58 | 'strict', 'localsName'];
|
59 | var _TRAILING_SEMCOL = /;\s*$/;
|
60 | var _BOM = /^\uFEFF/;
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | exports.cache = utils.cache;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | exports.localsName = _DEFAULT_LOCALS_NAME;
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | exports.resolveInclude = function(name, filename, isDir) {
|
94 | var dirname = path.dirname;
|
95 | var extname = path.extname;
|
96 | var resolve = path.resolve;
|
97 | var includePath = resolve(isDir ? filename : dirname(filename), name);
|
98 | var ext = extname(name);
|
99 | if (!ext) {
|
100 | includePath += '.ejs';
|
101 | }
|
102 | return includePath;
|
103 | };
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function getIncludePath(path, options){
|
113 | var includePath;
|
114 | if (path.charAt(0) == '/') {
|
115 | includePath = exports.resolveInclude(path.replace(/^\/*/,''), options.root || '/', true);
|
116 | }
|
117 | else {
|
118 | if (!options.filename) {
|
119 | throw new Error('`include` use relative path requires the \'filename\' option.');
|
120 | }
|
121 | includePath = exports.resolveInclude(path, options.filename);
|
122 | }
|
123 | return includePath;
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function handleCache(options, template) {
|
145 | var func;
|
146 | var filename = options.filename;
|
147 | var hasTemplate = arguments.length > 1;
|
148 |
|
149 | if (options.cache) {
|
150 | if (!filename) {
|
151 | throw new Error('cache option requires a filename');
|
152 | }
|
153 | func = exports.cache.get(filename);
|
154 | if (func) {
|
155 | return func;
|
156 | }
|
157 | if (!hasTemplate) {
|
158 | template = fs.readFileSync(filename).toString().replace(_BOM, '');
|
159 | }
|
160 | }
|
161 | else if (!hasTemplate) {
|
162 |
|
163 | if (!filename) {
|
164 | throw new Error('Internal EJS error: no file name or template '
|
165 | + 'provided');
|
166 | }
|
167 | template = fs.readFileSync(filename).toString().replace(_BOM, '');
|
168 | }
|
169 | func = exports.compile(template, options);
|
170 | if (options.cache) {
|
171 | exports.cache.set(filename, func);
|
172 | }
|
173 | return func;
|
174 | }
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | function includeFile(path, options) {
|
190 | var opts = utils.shallowCopy({}, options);
|
191 | opts.filename = getIncludePath(path, opts);
|
192 | return handleCache(opts);
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | function includeSource(path, options) {
|
206 | var opts = utils.shallowCopy({}, options);
|
207 | var includePath;
|
208 | var template;
|
209 | includePath = getIncludePath(path,opts);
|
210 | template = fs.readFileSync(includePath).toString().replace(_BOM, '');
|
211 | opts.filename = includePath;
|
212 | var templ = new Template(template, opts);
|
213 | templ.generateSource();
|
214 | return {
|
215 | source: templ.source,
|
216 | filename: includePath,
|
217 | template: template
|
218 | };
|
219 | }
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | function rethrow(err, str, filename, lineno){
|
235 | var lines = str.split('\n');
|
236 | var start = Math.max(lineno - 3, 0);
|
237 | var end = Math.min(lines.length, lineno + 3);
|
238 |
|
239 | var context = lines.slice(start, end).map(function (line, i){
|
240 | var curr = i + start + 1;
|
241 | return (curr == lineno ? ' >> ' : ' ')
|
242 | + curr
|
243 | + '| '
|
244 | + line;
|
245 | }).join('\n');
|
246 |
|
247 |
|
248 | err.path = filename;
|
249 | err.message = (filename || 'ejs') + ':'
|
250 | + lineno + '\n'
|
251 | + context + '\n\n'
|
252 | + err.message;
|
253 |
|
254 | throw err;
|
255 | }
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | function cpOptsInData(data, opts) {
|
270 | _OPTS.forEach(function (p) {
|
271 | if (typeof data[p] != 'undefined') {
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | if (p == 'root') {
|
277 | return;
|
278 | }
|
279 | opts[p] = data[p];
|
280 | }
|
281 | });
|
282 | }
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | exports.compile = function compile(template, opts) {
|
297 | var templ;
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | if (opts && opts.scope) {
|
303 | if (!scopeOptionWarned){
|
304 | console.warn('`scope` option is deprecated and will be removed in EJS 3');
|
305 | scopeOptionWarned = true;
|
306 | }
|
307 | if (!opts.context) {
|
308 | opts.context = opts.scope;
|
309 | }
|
310 | delete opts.scope;
|
311 | }
|
312 | templ = new Template(template, opts);
|
313 | return templ.compile();
|
314 | };
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | exports.render = function (template, d, o) {
|
330 | var data = d || {};
|
331 | var opts = o || {};
|
332 |
|
333 |
|
334 |
|
335 | if (arguments.length == 2) {
|
336 | cpOptsInData(data, opts);
|
337 | }
|
338 |
|
339 | return handleCache(opts, template)(data);
|
340 | };
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 | exports.renderFile = function () {
|
356 | var args = Array.prototype.slice.call(arguments);
|
357 | var filename = args.shift();
|
358 | var cb = args.pop();
|
359 | var data = args.shift() || {};
|
360 | var opts = args.pop() || {};
|
361 | var result;
|
362 |
|
363 |
|
364 | opts = utils.shallowCopy({}, opts);
|
365 |
|
366 |
|
367 |
|
368 | if (arguments.length == 3) {
|
369 |
|
370 | if (data.settings && data.settings['view options']) {
|
371 | cpOptsInData(data.settings['view options'], opts);
|
372 | }
|
373 |
|
374 | else {
|
375 | cpOptsInData(data, opts);
|
376 | }
|
377 | }
|
378 | opts.filename = filename;
|
379 |
|
380 | try {
|
381 | result = handleCache(opts)(data);
|
382 | }
|
383 | catch(err) {
|
384 | return cb(err);
|
385 | }
|
386 | return cb(null, result);
|
387 | };
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | exports.clearCache = function () {
|
395 | exports.cache.reset();
|
396 | };
|
397 |
|
398 | function Template(text, opts) {
|
399 | opts = opts || {};
|
400 | var options = {};
|
401 | this.templateText = text;
|
402 | this.mode = null;
|
403 | this.truncate = false;
|
404 | this.currentLine = 1;
|
405 | this.source = '';
|
406 | this.dependencies = [];
|
407 | options.client = opts.client || false;
|
408 | options.escapeFunction = opts.escape || utils.escapeXML;
|
409 | options.compileDebug = opts.compileDebug !== false;
|
410 | options.debug = !!opts.debug;
|
411 | options.filename = opts.filename;
|
412 | options.delimiter = opts.delimiter || exports.delimiter || _DEFAULT_DELIMITER;
|
413 | options.strict = opts.strict || false;
|
414 | options.context = opts.context;
|
415 | options.cache = opts.cache || false;
|
416 | options.rmWhitespace = opts.rmWhitespace;
|
417 | options.root = opts.root;
|
418 | options.localsName = opts.localsName || exports.localsName || _DEFAULT_LOCALS_NAME;
|
419 |
|
420 | if (options.strict) {
|
421 | options._with = false;
|
422 | }
|
423 | else {
|
424 | options._with = typeof opts._with != 'undefined' ? opts._with : true;
|
425 | }
|
426 |
|
427 | this.opts = options;
|
428 |
|
429 | this.regex = this.createRegex();
|
430 | }
|
431 |
|
432 | Template.modes = {
|
433 | EVAL: 'eval',
|
434 | ESCAPED: 'escaped',
|
435 | RAW: 'raw',
|
436 | COMMENT: 'comment',
|
437 | LITERAL: 'literal'
|
438 | };
|
439 |
|
440 | Template.prototype = {
|
441 | createRegex: function () {
|
442 | var str = _REGEX_STRING;
|
443 | var delim = utils.escapeRegExpChars(this.opts.delimiter);
|
444 | str = str.replace(/%/g, delim);
|
445 | return new RegExp(str);
|
446 | },
|
447 |
|
448 | compile: function () {
|
449 | var src;
|
450 | var fn;
|
451 | var opts = this.opts;
|
452 | var prepended = '';
|
453 | var appended = '';
|
454 | var escape = opts.escapeFunction;
|
455 |
|
456 | if (!this.source) {
|
457 | this.generateSource();
|
458 | prepended += ' var __output = [], __append = __output.push.bind(__output);' + '\n';
|
459 | if (opts._with !== false) {
|
460 | prepended += ' with (' + opts.localsName + ' || {}) {' + '\n';
|
461 | appended += ' }' + '\n';
|
462 | }
|
463 | appended += ' return __output.join("");' + '\n';
|
464 | this.source = prepended + this.source + appended;
|
465 | }
|
466 |
|
467 | if (opts.compileDebug) {
|
468 | src = 'var __line = 1' + '\n'
|
469 | + ' , __lines = ' + JSON.stringify(this.templateText) + '\n'
|
470 | + ' , __filename = ' + (opts.filename ?
|
471 | JSON.stringify(opts.filename) : 'undefined') + ';' + '\n'
|
472 | + 'try {' + '\n'
|
473 | + this.source
|
474 | + '} catch (e) {' + '\n'
|
475 | + ' rethrow(e, __lines, __filename, __line);' + '\n'
|
476 | + '}' + '\n';
|
477 | }
|
478 | else {
|
479 | src = this.source;
|
480 | }
|
481 |
|
482 | if (opts.debug) {
|
483 | console.log(src);
|
484 | }
|
485 |
|
486 | if (opts.client) {
|
487 | src = 'escape = escape || ' + escape.toString() + ';' + '\n' + src;
|
488 | if (opts.compileDebug) {
|
489 | src = 'rethrow = rethrow || ' + rethrow.toString() + ';' + '\n' + src;
|
490 | }
|
491 | }
|
492 |
|
493 | if (opts.strict) {
|
494 | src = '"use strict";\n' + src;
|
495 | }
|
496 |
|
497 | try {
|
498 | fn = new Function(opts.localsName + ', escape, include, rethrow', src);
|
499 | }
|
500 | catch(e) {
|
501 |
|
502 | if (e instanceof SyntaxError) {
|
503 | if (opts.filename) {
|
504 | e.message += ' in ' + opts.filename;
|
505 | }
|
506 | e.message += ' while compiling ejs\n\n';
|
507 | e.message += 'If the above error is not helpful, you may want to try EJS-Lint:\n';
|
508 | e.message += 'https://github.com/RyanZim/EJS-Lint';
|
509 | }
|
510 | throw e;
|
511 | }
|
512 |
|
513 | if (opts.client) {
|
514 | fn.dependencies = this.dependencies;
|
515 | return fn;
|
516 | }
|
517 |
|
518 |
|
519 |
|
520 |
|
521 | var returnedFn = function (data) {
|
522 | var include = function (path, includeData) {
|
523 | var d = utils.shallowCopy({}, data);
|
524 | if (includeData) {
|
525 | d = utils.shallowCopy(d, includeData);
|
526 | }
|
527 | return includeFile(path, opts)(d);
|
528 | };
|
529 | return fn.apply(opts.context, [data || {}, escape, include, rethrow]);
|
530 | };
|
531 | returnedFn.dependencies = this.dependencies;
|
532 | return returnedFn;
|
533 | },
|
534 |
|
535 | generateSource: function () {
|
536 | var opts = this.opts;
|
537 |
|
538 | if (opts.rmWhitespace) {
|
539 |
|
540 |
|
541 | this.templateText =
|
542 | this.templateText.replace(/\r/g, '').replace(/^\s+|\s+$/gm, '');
|
543 | }
|
544 |
|
545 |
|
546 | this.templateText =
|
547 | this.templateText.replace(/[ \t]*<%_/gm, '<%_').replace(/_%>[ \t]*/gm, '_%>');
|
548 |
|
549 | var self = this;
|
550 | var matches = this.parseTemplateText();
|
551 | var d = this.opts.delimiter;
|
552 |
|
553 | if (matches && matches.length) {
|
554 | matches.forEach(function (line, index) {
|
555 | var opening;
|
556 | var closing;
|
557 | var include;
|
558 | var includeOpts;
|
559 | var includeObj;
|
560 | var includeSrc;
|
561 |
|
562 |
|
563 |
|
564 |
|
565 | if ( line.indexOf('<' + d) === 0
|
566 | && line.indexOf('<' + d + d) !== 0) {
|
567 | closing = matches[index + 2];
|
568 | if (!(closing == d + '>' || closing == '-' + d + '>' || closing == '_' + d + '>')) {
|
569 | throw new Error('Could not find matching close tag for "' + line + '".');
|
570 | }
|
571 | }
|
572 |
|
573 | if ((include = line.match(/^\s*include\s+(\S+)/))) {
|
574 | opening = matches[index - 1];
|
575 |
|
576 | if (opening && (opening == '<' + d || opening == '<' + d + '-' || opening == '<' + d + '_')) {
|
577 | includeOpts = utils.shallowCopy({}, self.opts);
|
578 | includeObj = includeSource(include[1], includeOpts);
|
579 | if (self.opts.compileDebug) {
|
580 | includeSrc =
|
581 | ' ; (function(){' + '\n'
|
582 | + ' var __line = 1' + '\n'
|
583 | + ' , __lines = ' + JSON.stringify(includeObj.template) + '\n'
|
584 | + ' , __filename = ' + JSON.stringify(includeObj.filename) + ';' + '\n'
|
585 | + ' try {' + '\n'
|
586 | + includeObj.source
|
587 | + ' } catch (e) {' + '\n'
|
588 | + ' rethrow(e, __lines, __filename, __line);' + '\n'
|
589 | + ' }' + '\n'
|
590 | + ' ; }).call(this)' + '\n';
|
591 | }else{
|
592 | includeSrc = ' ; (function(){' + '\n' + includeObj.source +
|
593 | ' ; }).call(this)' + '\n';
|
594 | }
|
595 | self.source += includeSrc;
|
596 | self.dependencies.push(exports.resolveInclude(include[1],
|
597 | includeOpts.filename));
|
598 | return;
|
599 | }
|
600 | }
|
601 | self.scanLine(line);
|
602 | });
|
603 | }
|
604 |
|
605 | },
|
606 |
|
607 | parseTemplateText: function () {
|
608 | var str = this.templateText;
|
609 | var pat = this.regex;
|
610 | var result = pat.exec(str);
|
611 | var arr = [];
|
612 | var firstPos;
|
613 |
|
614 | while (result) {
|
615 | firstPos = result.index;
|
616 |
|
617 | if (firstPos !== 0) {
|
618 | arr.push(str.substring(0, firstPos));
|
619 | str = str.slice(firstPos);
|
620 | }
|
621 |
|
622 | arr.push(result[0]);
|
623 | str = str.slice(result[0].length);
|
624 | result = pat.exec(str);
|
625 | }
|
626 |
|
627 | if (str) {
|
628 | arr.push(str);
|
629 | }
|
630 |
|
631 | return arr;
|
632 | },
|
633 |
|
634 | scanLine: function (line) {
|
635 | var self = this;
|
636 | var d = this.opts.delimiter;
|
637 | var newLineCount = 0;
|
638 |
|
639 | function _addOutput() {
|
640 | if (self.truncate) {
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 | line = line.replace(/^(?:\r\n|\r|\n)/, '');
|
647 | self.truncate = false;
|
648 | }
|
649 | else if (self.opts.rmWhitespace) {
|
650 |
|
651 |
|
652 |
|
653 | line = line.replace(/^\n/, '');
|
654 | }
|
655 | if (!line) {
|
656 | return;
|
657 | }
|
658 |
|
659 |
|
660 | line = line.replace(/\\/g, '\\\\');
|
661 |
|
662 |
|
663 | line = line.replace(/\n/g, '\\n');
|
664 | line = line.replace(/\r/g, '\\r');
|
665 |
|
666 |
|
667 |
|
668 | line = line.replace(/"/g, '\\"');
|
669 | self.source += ' ; __append("' + line + '")' + '\n';
|
670 | }
|
671 |
|
672 | newLineCount = (line.split('\n').length - 1);
|
673 |
|
674 | switch (line) {
|
675 | case '<' + d:
|
676 | case '<' + d + '_':
|
677 | this.mode = Template.modes.EVAL;
|
678 | break;
|
679 | case '<' + d + '=':
|
680 | this.mode = Template.modes.ESCAPED;
|
681 | break;
|
682 | case '<' + d + '-':
|
683 | this.mode = Template.modes.RAW;
|
684 | break;
|
685 | case '<' + d + '#':
|
686 | this.mode = Template.modes.COMMENT;
|
687 | break;
|
688 | case '<' + d + d:
|
689 | this.mode = Template.modes.LITERAL;
|
690 | this.source += ' ; __append("' + line.replace('<' + d + d, '<' + d) + '")' + '\n';
|
691 | break;
|
692 | case d + d + '>':
|
693 | this.mode = Template.modes.LITERAL;
|
694 | this.source += ' ; __append("' + line.replace(d + d + '>', d + '>') + '")' + '\n';
|
695 | break;
|
696 | case d + '>':
|
697 | case '-' + d + '>':
|
698 | case '_' + d + '>':
|
699 | if (this.mode == Template.modes.LITERAL) {
|
700 | _addOutput();
|
701 | }
|
702 |
|
703 | this.mode = null;
|
704 | this.truncate = line.indexOf('-') === 0 || line.indexOf('_') === 0;
|
705 | break;
|
706 | default:
|
707 |
|
708 | if (this.mode) {
|
709 |
|
710 | switch (this.mode) {
|
711 | case Template.modes.EVAL:
|
712 | case Template.modes.ESCAPED:
|
713 | case Template.modes.RAW:
|
714 | if (line.lastIndexOf('//') > line.lastIndexOf('\n')) {
|
715 | line += '\n';
|
716 | }
|
717 | }
|
718 | switch (this.mode) {
|
719 |
|
720 | case Template.modes.EVAL:
|
721 | this.source += ' ; ' + line + '\n';
|
722 | break;
|
723 |
|
724 | case Template.modes.ESCAPED:
|
725 | this.source += ' ; __append(escape(' +
|
726 | line.replace(_TRAILING_SEMCOL, '').trim() + '))' + '\n';
|
727 | break;
|
728 |
|
729 | case Template.modes.RAW:
|
730 | this.source += ' ; __append(' +
|
731 | line.replace(_TRAILING_SEMCOL, '').trim() + ')' + '\n';
|
732 | break;
|
733 | case Template.modes.COMMENT:
|
734 |
|
735 | break;
|
736 |
|
737 | case Template.modes.LITERAL:
|
738 | _addOutput();
|
739 | break;
|
740 | }
|
741 | }
|
742 |
|
743 | else {
|
744 | _addOutput();
|
745 | }
|
746 | }
|
747 |
|
748 | if (self.opts.compileDebug && newLineCount) {
|
749 | this.currentLine += newLineCount;
|
750 | this.source += ' ; __line = ' + this.currentLine + '\n';
|
751 | }
|
752 | }
|
753 | };
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 | exports.escapeXML = utils.escapeXML;
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 | exports.__express = exports.renderFile;
|
779 |
|
780 |
|
781 |
|
782 | if (require.extensions) {
|
783 | require.extensions['.ejs'] = function (module, flnm) {
|
784 | var filename = flnm || module.filename;
|
785 | var options = {
|
786 | filename: filename,
|
787 | client: true
|
788 | };
|
789 | var template = fs.readFileSync(filename).toString();
|
790 | var fn = exports.compile(template, options);
|
791 | module._compile('module.exports = ' + fn.toString() + ';', filename);
|
792 | };
|
793 | }
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 | exports.VERSION = _VERSION_STRING;
|
804 |
|
805 |
|
806 | if (typeof window != 'undefined') {
|
807 | window.ejs = exports;
|
808 | }
|