· 6 years ago · Feb 12, 2019, 11:10 AM
1
2 // node.js catching syntax errors
3 // node.js catching syntax errors
4 // node.js catching syntax errors
5 // node.js catching syntax errors
6 // node.js catching syntax errors
7 // node.js catching syntax errors
8
9 // template: function () {
10
11 // const args = Array.prototype.slice.call(arguments);
12
13 // try {
14
15 // const tmp = template.apply(this, args);
16
17 // return function () {
18 // try {
19
20 // return tmp.apply(this, Array.prototype.slice.call(arguments));
21 // }
22 // catch(e) {
23
24 // log.start();
25
26 // log("Template error: (execution)", args[0]);
27
28 // throw log.get();
29 // }
30 // }
31 // }
32 // catch(e) {
33
34 // log.start();
35
36 // log("Template error: (compiling)", args[0]);
37
38 // throw log.get();
39 // }
40 // }
41
42 var template = (function (t, delimiters) {
43 var escapeMap = {
44 '&': '&',
45 '<': '<',
46 '>': '>',
47 '"': '"',
48 "'": ''',
49 '`': '`'
50 };
51 var escapes = {
52 "'": "'",
53 '\\': '\\',
54 '\r': 'r',
55 '\n': 'n',
56 '\u2028': 'u2028',
57 '\u2029': 'u2029'
58 };
59 var escaper = /\\|'|\r|\n|\u2028|\u2029/g;
60 var createEscaper = function(map) {
61 var escaper = function(match) {
62 return map[match];
63 };
64 var l = [];
65 for (var i in map) {
66 l.push(i);
67 }
68 // Regexes for identifying a key that needs to be escaped
69 var source = '(?:' + l.join('|') + ')';
70 var testRegexp = RegExp(source);
71 var replaceRegexp = RegExp(source, 'g');
72 return function(string) {
73 string = string == null ? '' : '' + string;
74 return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
75 };
76 };
77 t._esc = createEscaper(escapeMap);
78 var escapeChar = function(match) {
79 return '\\' + escapes[match];
80 };
81 function isObject(obj) {
82 var type = typeof obj;
83 return type === 'function' || type === 'object' && !!obj;
84 };
85 function defaults(obj) {
86 if (!isObject(obj)) return obj;
87 for (var i = 1, length = arguments.length; i < length; i++) {
88 var source = arguments[i];
89 for (var prop in source) {
90 if (obj[prop] === void 0) obj[prop] = source[prop];
91 }
92 }
93 return obj;
94 };
95
96 // JavaScript micro-templating, similar to John Resig's implementation.
97 // Underscore templating handles arbitrary delimiters, preserves whitespace,
98 // and correctly escapes quotes within interpolated code.
99 // NB: `oldSettings` only exists for backwards compatibility.
100 return function (text, settings, oldSettings) {
101 if (!settings && oldSettings) settings = oldSettings;
102 settings = defaults({}, settings, delimiters);
103
104 // Combine delimiters into one regular expression via alternation.
105 var matcher = RegExp([
106 (settings.escape || noMatch).source,
107 (settings.interpolate || noMatch).source,
108 (settings.evaluate || noMatch).source
109 ].join('|') + '|$', 'g');
110
111 // Compile the template source, escaping string literals appropriately.
112 var index = 0;
113 var source = "__p+='";
114 text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
115 source += text.slice(index, offset).replace(escaper, escapeChar);
116 index = offset + match.length;
117
118 if (escape) {
119 source += "'+\n((__t=(" + escape + "))==null?'':_esc(__t))+\n'";
120 } else if (interpolate) {
121 source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
122 } else if (evaluate) {
123 source += "';\n" + evaluate + "\n__p+='";
124 }
125
126 // Adobe VMs need the match returned to produce the correct offest.
127 return match;
128 });
129 source += "';\n";
130
131 // If a variable is not specified, place data values in local scope.
132 if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
133
134 source = "var __t,__p='',__j=Array.prototype.join," +
135 "print=function(){__p+=__j.call(arguments,'');};\n" +
136 source + 'return __p;\n';
137
138 try {
139 var render = new Function(settings.variable || 'obj', '_', source);
140 } catch (e) {
141 e.source = source;
142 throw e;
143 }
144
145 var template = function(data) {
146 return render.call(t, data);
147 };
148
149 // Provide the compiled source as a convenience for precompilation.
150 var argument = settings.variable || 'obj';
151 template.source = 'function(' + argument + '){\n' + source + '}';
152
153 return template;
154 };
155 })(this, {
156// evaluate : /<%([\s\S]+?)%>/g,
157// interpolate : /<%=([\s\S]+?)%>/g,
158// escape : /<%-([\s\S]+?)%>/g
159 evaluate : /<#([\s\S]+?)#>/g,
160 interpolate : /<#=([\s\S]+?)#>/g,
161 escape : /<#-([\s\S]+?)#>/g
162 });
163
164 $(function () {
165 var t = template($('#tmp2').html());
166// var t = _.template($('#tmp').html());
167 $('#target').html(t( {test: ' jest ',l:{jeden:' dwa gg ',trzy:' cztery ',piec:' sesc '}} ));
168 });