This repository was archived by the owner on May 10, 2026. It is now read-only.
forked from martinkadlec0/Smart-RSS
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtemplate.js
More file actions
91 lines (76 loc) · 3.18 KB
/
Copy pathtemplate.js
File metadata and controls
91 lines (76 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
define([], function () {
let templateSettings = {
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
escape: /<%-([\s\S]+?)%>/g
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
let noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
let escapes = {
'\'': '\'',
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
let escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g;
let escapeChar = function (match) {
return '\\' + escapes[match];
};
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
// NB: `oldSettings` only exists for backwards compatibility.
return function (text, settings) {
settings = Object.assign({}, settings, templateSettings);
// Combine delimiters into one regular expression via alternation.
let matcher = RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.evaluate || noMatch).source
].join('|') + '|$', 'g');
// Compile the template source, escaping string literals appropriately.
let index = 0;
let source = '__p+=\'';
text.replace(matcher, function (match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset).replace(escapeRegExp, escapeChar);
index = offset + match.length;
if (escape) {
source += '\'+\n((__t=(' + escape + '))==null?\'\':_.escape(__t))+\n\'';
} else if (interpolate) {
source += '\'+\n((__t=(' + interpolate + '))==null?\'\':__t)+\n\'';
} else if (evaluate) {
source += '\';\n' + evaluate + '\n__p+=\'';
}
// Adobe VMs need the match returned to produce the correct offset.
return match;
});
source += '\';\n';
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = 'let __t,__p=\'\',__j=Array.prototype.join,' +
'print=function(){__p+=__j.call(arguments,\'\');};\n' +
source + 'return __p;\n';
let render;
try {
render = new Function(settings.variable || 'obj', source);
} catch (e) {
e.source = source;
throw e;
}
let template = function (data) {
return render.call(this, data);
};
// Provide the compiled source as a convenience for precompilation.
let argument = settings.variable || 'obj';
template.source = 'function(' + argument + '){\n' + source + '}';
return template;
};
});