-
Notifications
You must be signed in to change notification settings - Fork 19
/
milk.js
executable file
·294 lines (294 loc) · 10.5 KB
/
milk.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
(function() {
var Expand, Find, Milk, Parse, TemplateCache, key;
var __slice = Array.prototype.slice;
TemplateCache = {};
Find = function(name, stack, value) {
var ctx, i, part, parts, _i, _len, _ref, _ref2, _ref3;
if (value == null) {
value = null;
}
if (name === '.') {
return stack[stack.length - 1];
}
_ref = name.split(/\./), name = _ref[0], parts = 2 <= _ref.length ? __slice.call(_ref, 1) : [];
for (i = _ref2 = stack.length - 1, _ref3 = -1; (_ref2 <= _ref3 ? i < _ref3 : i > _ref3); (_ref2 <= _ref3 ? i += 1 : i -= 1)) {
if (stack[i] == null) {
continue;
}
if (!(typeof stack[i] === 'object' && name in (ctx = stack[i]))) {
continue;
}
value = ctx[name];
break;
}
for (_i = 0, _len = parts.length; _i < _len; _i++) {
part = parts[_i];
value = Find(part, [value]);
}
if (value instanceof Function) {
value = (function(value) {
return function() {
var val;
val = value.apply(ctx, arguments);
return (val instanceof Function) && val.apply(null, arguments) || val;
};
})(value);
}
return value;
};
Expand = function() {
var args, f, obj, tmpl;
obj = arguments[0], tmpl = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
return ((function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = tmpl.length; _i < _len; _i++) {
f = tmpl[_i];
_results.push(f.call.apply(f, [obj].concat(__slice.call(args))));
}
return _results;
})()).join('');
};
Parse = function(template, delimiters, section) {
var BuildRegex, buffer, buildInterpolationTag, buildInvertedSectionTag, buildPartialTag, buildSectionTag, cache, content, contentEnd, d, error, escape, isStandalone, match, name, parseError, pos, sectionInfo, tag, tagPattern, tmpl, type, whitespace, _name, _ref, _ref2, _ref3;
if (delimiters == null) {
delimiters = ['{{', '}}'];
}
if (section == null) {
section = null;
}
cache = (TemplateCache[_name = delimiters.join(' ')] || (TemplateCache[_name] = {}));
if (template in cache) {
return cache[template];
}
buffer = [];
BuildRegex = function() {
var tagClose, tagOpen;
tagOpen = delimiters[0], tagClose = delimiters[1];
return RegExp("([\\s\\S]*?)([" + ' ' + "\\t]*)(?:" + tagOpen + "\\s*(?:(!)\\s*([\\s\\S]+?)|(=)\\s*([\\s\\S]+?)\\s*=|({)\\s*(\\w[\\S]*?)\\s*}|([^0-9a-zA-Z._!={]?)\\s*([\\w.][\\S]*?))\\s*" + tagClose + ")", "gm");
};
tagPattern = BuildRegex();
tagPattern.lastIndex = pos = (section || {
start: 0
}).start;
parseError = function(pos, msg) {
var carets, e, endOfLine, error, indent, key, lastLine, lastTag, lineNo, parsedLines, tagStart;
(endOfLine = /$/gm).lastIndex = pos;
endOfLine.exec(template);
parsedLines = template.substr(0, pos).split('\n');
lineNo = parsedLines.length;
lastLine = parsedLines[lineNo - 1];
tagStart = contentEnd + whitespace.length;
lastTag = template.substr(tagStart + 1, pos - tagStart - 1);
indent = new Array(lastLine.length - lastTag.length + 1).join(' ');
carets = new Array(lastTag.length + 1).join('^');
lastLine = lastLine + template.substr(pos, endOfLine.lastIndex - pos);
error = new Error();
for (key in e = {
"message": "" + msg + "\n\nLine " + lineNo + ":\n" + lastLine + "\n" + indent + carets,
"error": msg,
"line": lineNo,
"char": indent.length,
"tag": lastTag
}) {
error[key] = e[key];
}
return error;
};
while (match = tagPattern.exec(template)) {
_ref = match.slice(1, 3), content = _ref[0], whitespace = _ref[1];
type = match[3] || match[5] || match[7] || match[9];
tag = match[4] || match[6] || match[8] || match[10];
contentEnd = (pos + content.length) - 1;
pos = tagPattern.lastIndex;
isStandalone = (contentEnd === -1 || template.charAt(contentEnd) === '\n') && ((_ref2 = template.charAt(pos)) === void 0 || _ref2 === '' || _ref2 === '\r' || _ref2 === '\n');
if (content) {
buffer.push((function(content) {
return function() {
return content;
};
})(content));
}
if (isStandalone && (type !== '' && type !== '&' && type !== '{')) {
if (template.charAt(pos) === '\r') {
pos += 1;
}
if (template.charAt(pos) === '\n') {
pos += 1;
}
} else if (whitespace) {
buffer.push((function(whitespace) {
return function() {
return whitespace;
};
})(whitespace));
contentEnd += whitespace.length;
whitespace = '';
}
switch (type) {
case '!':
break;
case '':
case '&':
case '{':
buildInterpolationTag = function(name, is_unescaped) {
return function(context) {
var value, _ref;
if ((value = (_ref = Find(name, context)) != null ? _ref : '') instanceof Function) {
value = Expand.apply(null, [this, Parse("" + (value()))].concat(__slice.call(arguments)));
}
if (!is_unescaped) {
value = this.escape("" + value);
}
return "" + value;
};
};
buffer.push(buildInterpolationTag(tag, type));
break;
case '>':
buildPartialTag = function(name, indentation) {
return function(context, partials) {
var partial;
partial = partials(name).toString();
if (indentation) {
partial = partial.replace(/^(?=.)/gm, indentation);
}
return Expand.apply(null, [this, Parse(partial)].concat(__slice.call(arguments)));
};
};
buffer.push(buildPartialTag(tag, whitespace));
break;
case '#':
case '^':
sectionInfo = {
name: tag,
start: pos,
error: parseError(tagPattern.lastIndex, "Unclosed section '" + tag + "'!")
};
_ref3 = Parse(template, delimiters, sectionInfo), tmpl = _ref3[0], pos = _ref3[1];
sectionInfo['#'] = buildSectionTag = function(name, delims, raw) {
return function(context) {
var parsed, result, v, value;
value = Find(name, context) || [];
tmpl = value instanceof Function ? value(raw) : raw;
if (!(value instanceof Array)) {
value = [value];
}
parsed = Parse(tmpl || '', delims);
context.push(value);
result = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = value.length; _i < _len; _i++) {
v = value[_i];
context[context.length - 1] = v;
_results.push(Expand.apply(null, [this, parsed].concat(__slice.call(arguments))));
}
return _results;
}).apply(this, arguments);
context.pop();
return result.join('');
};
};
sectionInfo['^'] = buildInvertedSectionTag = function(name, delims, raw) {
return function(context) {
var value;
value = Find(name, context) || [];
if (!(value instanceof Array)) {
value = [1];
}
value = value.length === 0 ? Parse(raw, delims) : [];
return Expand.apply(null, [this, value].concat(__slice.call(arguments)));
};
};
buffer.push(sectionInfo[type](tag, delimiters, tmpl));
break;
case '/':
if (section == null) {
error = "End Section tag '" + tag + "' found, but not in section!";
} else if (tag !== (name = section.name)) {
error = "End Section tag closes '" + tag + "'; expected '" + name + "'!";
}
if (error) {
throw parseError(tagPattern.lastIndex, error);
}
template = template.slice(section.start, (contentEnd + 1) || 9e9);
cache[template] = buffer;
return [template, pos];
case '=':
if ((delimiters = tag.split(/\s+/)).length !== 2) {
error = "Set Delimiters tags should have two and only two values!";
}
if (error) {
throw parseError(tagPattern.lastIndex, error);
}
escape = /[-[\]{}()*+?.,\\^$|#]/g;
delimiters = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = delimiters.length; _i < _len; _i++) {
d = delimiters[_i];
_results.push(d.replace(escape, "\\$&"));
}
return _results;
})();
tagPattern = BuildRegex();
break;
default:
throw parseError(tagPattern.lastIndex, "Unknown tag type -- " + type);
}
tagPattern.lastIndex = pos != null ? pos : template.length;
}
if (section != null) {
throw section.error;
}
if (template.length !== pos) {
buffer.push(function() {
return template.slice(pos);
});
}
return cache[template] = buffer;
};
Milk = {
VERSION: '1.2.0',
helpers: [],
partials: null,
escape: function(value) {
var entities;
entities = {
'&': 'amp',
'"': 'quot',
'<': 'lt',
'>': 'gt'
};
return value.replace(/[&"<>]/g, function(ch) {
return "&" + entities[ch] + ";";
});
},
render: function(template, data, partials) {
var context;
if (partials == null) {
partials = null;
}
if (!((partials || (partials = this.partials || {})) instanceof Function)) {
partials = (function(partials) {
return function(name) {
if (!(name in partials)) {
throw "Unknown partial '" + name + "'!";
}
return Find(name, [partials]);
};
})(partials);
}
context = this.helpers instanceof Array ? this.helpers : [this.helpers];
return Expand(this, Parse(template), context.concat([data]), partials);
}
};
if (typeof exports != "undefined" && exports !== null) {
for (key in Milk) {
exports[key] = Milk[key];
}
} else {
this.Milk = Milk;
}
}).call(this);