-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.bacon.js
507 lines (423 loc) · 11.3 KB
/
template.bacon.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
bacon.template = {};
bacon.template.autoEscape = true;
bacon.template.stringIfInvalid = '';
/**
* Parses a Django template using the provided data.
*
* @param string template The template, or the location to the template.
* @param object data The data to pass to the parser.
* @param function callback The function to pass the data to.
*/
bacon.template.parse = function(template, data, callback) {
template = bacon.template._lexer(template);
template = bacon.template._parser(template);
for (var endString = '', i = 0; i < template.length; i++) {
endString += template[i].parse(data);
}
if (typeof callback === 'function') {
callback(endString, data);
}
};
var TEXT = 0, VARIABLE = 1, TAG = 2;
bacon.template._lexer = function(template) {
var end = [];
template = template.split(/(\{\# [^(\#\})]+ \#\}|\{\{ [a-zA-Z0-9_\.\|:\"\-]+ \}\}|\{% [^\{\}]+ %\})/g);
for (var i = 0; i < template.length; i++) {
if (template[i].indexOf('{%') === 0) {
end.push([TAG, template[i].slice(3, -3)]);
} else if (template[i].indexOf('{{') === 0) {
end.push([VARIABLE, template[i].slice(3, -3)]);
} else if (template[i].indexOf('{#') !== 0) {
end.push([TEXT, template[i]]);
}
}
return end;
};
bacon.template._parser = function(template, nested) {
var parseItem = function(item, i) {
switch (item[0]) {
case TEXT:
return new BaconTextNode(item[1]);
case VARIABLE:
item = item[1].split('|');
return new BaconVarNode(item[0], item.slice(1));
case TAG:
var name = (item[1].indexOf(' ') < 0) ? item[1] : item[1].split(' ')[0];
var code = (item[1].indexOf(' ') < 0) ? null : item[1].split(' ').slice(1).join(' ');
if (name.indexOf('end') === 0) {
return false;
}
var contents = bacon.template._parser(template.slice(i + 1), true);
template.splice(i + 1, contents[1] + 1);
contents = contents[0];
return new BaconTagNode(name, code, contents);
}
};
var end = [], i, item;
for (i = 0; i < template.length; i++) {
item = parseItem(template[i], i);
if (item === false) {
break;
}
end.push(item);
}
if (typeof nested === 'undefined' || !nested) {
return end;
} else {
return [end, i];
}
}
function BaconTextNode(text) {
this.text = text;
}
BaconTextNode.prototype.parse = function() {
return this.text;
}
function BaconVarNode(name, filters) {
this.name = name;
this.filters = filters;
}
BaconVarNode.prototype.parse = function(data) {
data = bacon.template._getVariable(this.name, this.filters, data);
return (typeof data === 'object') ? JSON.stringify(data) : data;
};
function BaconTagNode(name, code, contents) {
this.name = name;
this.code = code;
this.contents = contents;
}
BaconTagNode.prototype.parse = function(data) {
if (typeof bacon.template.tags[this.name] === 'function') {
return bacon.template.tags[this.name].call(null, this.code, this.contents, data);
}
return '';
};
bacon.template._getVariable = function(name, filters, data) {
// Get strings
if (name.indexOf('"') === 0) {
name = name.split('"');
if (name.length === 3 && !name[0] && !name[2]) {
return name[1];
}
return bacon.template.stringIfInvalid;
}
// Get ints
if (!isNaN(parseInt(name))) {
return parseInt(name);
}
// Get variables
if (name.indexOf('.') < 0) {
var output = (typeof data[name] === 'undefined') ? bacon.template.stringIfInvalid : data[name];
} else {
var splitName = name.split('.');
var output = data[splitName[0]];
for (var i = 1; i < splitName.length; i++) {
if (typeof output !== 'object' || typeof output[splitName[i]] === 'undefined') {
output = bacon.template.stringIfInvalid;
break;
}
output = output[splitName[i]];
}
delete i;
}
if (typeof output === 'function') {
output = output.call(null, data);
}
if (!(filters instanceof Array)) {
if (bacon.template.autoEscape) {
output = bacon.template.filters.escape(output);
}
return output;
}
if (filters.indexOf('escape') < 0 && filters.indexOf('safe') < 0 && bacon.template.autoEscape) {
output = bacon.template.filters.escape(output);
}
for (var arg, i = 0; i < filters.length; i++) {
arg = null;
if (filters[i].indexOf(':') !== -1) {
arg = filters[i].split(':');
filters[i] = arg[0];
arg = bacon.template._getVariable(arg[1], false, data);
}
if (typeof bacon.template.filters[filters[i]] === 'function') {
output = bacon.template.filters[filters[i]].call(null, output, arg);
}
}
return output;
};
bacon.template._isTrue = function(code, data) {
if (code.indexOf(' or ') !== -1) {
code = code.split(' or ');
for (var e = false, i = 0; i < code.length; i++) {
if (bacon.template._isTrue(code[i], data)) {
e = true;
}
}
return e;
}
if (code.indexOf(' and ') !== -1) {
code = code.split(' and ');
for (var e = true, i = 0; i < code.length; i++) {
if (!bacon.template._isTrue(code[i], data)) {
e = false;
}
}
return e;
}
var not = (code.indexOf('not ') === 0), res;
if (not) {
code = code.slice(4);
}
if (code.indexOf(' ') < 0 && code.indexOf('|') < 0) {
res = bacon.template._getVariable(code, false, data);
} else if (code.indexOf(' ') < 0) {
code = code.split('|');
res = bacon.template._getVariable(code[0], code.slice('1'), data);
} else {
code = code.split(' ');
code[0] = bacon.template._getVariable(code[0], code.slice('1'), data);
switch (code[1]) {
case '==':
res = code[0] == code[2];
break;
case '!==':
res = code[0] != code[2];
break;
case '<':
res = code[0] < code[2];
break;
case '>':
res = code[0] > code[2];
break;
case '<=':
res = code[0] <= code[2];
break;
case '>=':
res = code[0] >= code[2];
break;
default:
var error = new TypeError('Invalid operator');
error.type = 'invalid_operator';
throw error;
}
}
return (not) ? !res : res;
}
var filters = bacon.template.filters = {};
filters.add = function(input, add) {
var add_float = parseFloat(add);
var add_input = parseFloat(input);
if (!isNaN(add_float) && !isNaN(add_input)) {
return input + add;
}
if (input instanceof Array) {
if (add instanceof Array) {
input = input.concat(add);
} else {
input.push(add);
}
return input;
}
return input + add;
};
filters.addslashes = function(input) {
if (typeof input !== 'string') {
return input;
}
return input.replace(/("|')/g, '\\$1');
};
filters.capfirst = function(input) {
return input.charAt(0).toUpperCase() + input.slice(1);
};
//hello, 9 - " hello "
filters.center = function(input, width) {
if (typeof input !== 'string' || input.length > width) {
return input;
}
// Remove existing white-space
input = input.replace(/(?:^ +| +$)/g, '');
width = (width - input.length) / 2;
var left = new Array((width % 1) ? Math.ceil(width) : width + 1).join(' ');
var right = (width % 1) ? left + ' ' : new Array(left).join(' ');
return left + input + right;
};
filters.default = filters.default_if_none = function(input, def) {
return (input) ? input : def;
};
filters.dictsort = function(input, prop) {
input = input.slice();
input.sort(function(a, b) {
if (a[prop] < b[prop]) {
return -1;
}
if (a[prop] > b[prop]) {
return 1;
}
return 0;
});
return input;
};
filters.dictsortreversed = function(input, prop) {
input = input.slice();
input.sort(function(a, b) {
if (a[prop] < b[prop]) {
return 1;
}
if (a[prop] > b[prop]) {
return -1;
}
return 0;
});
return input;
};
filters.divisibleby = function(input, prop) {
return !(input % prop);
};
filters.escape = function(input) {
if (typeof input !== 'string') {
return input;
}
return input.replace(/&/g, '&')
.replace(/\</g, '<')
.replace(/\>/g, '>')
.replace(/'/g, ''')
.replace(/"/g, '"');
};
filters.first = function(input) {
return input[0];
};
filters.fix_ampersands = function(input) {
return input.replace(/&([^ ]+;)?/g, function(full, match) {
return (typeof match === 'undefined') ? '&' : full;
});
};
filters.float_format = function(input, places) {
if (places === null) {
places = -1;
}
var exact = false;
if (places < 0) {
places = 0 - places;
exact = true;
}
if (typeof input !== 'number') {
input = parseFloat(input);
if (isNaN(input)) {
return false;
}
}
// Math.round doesn't allow us to specify number of decimal places, so we multiply
// it by a factor of ten, round it, and then divide it again.
input = String(Math.round(input * Math.pow(10, places)) / Math.pow(10, places));
// Detect whether it is short of zeros Ð as it a string, we can add additional zeros.
if (exact && (input.indexOf('.') === -1 || input.indexOf('.') + places >= input.length)) {
if (input.indexOf('.') === -1) {
input += '.';
}
input += new Array(input.indexOf('.') + places + 2 - input.length).join('0');
}
return input;
};
filters.force_escape = function(input, places) {
return filters.escape.apply(null, arguments);
};
filters.get_digit = function(input, place) {
input = String(input);
if (place > input.length || place < 0) {
return input;
}
return parseInt(input.charAt(input.length - place));
};
filters.join = function(input, join) {
if (!(input instanceof Array)) {
return input;
}
return input.join(join);
};
filters.last = function(input) {
return input[input.length - 1];
};
filters.length = function(input) {
return input.length;
};
filters.length_is = function(input, length) {
return input.length == length;
};
filters.linebreaksbr = function(input) {
return input.replace(/\n/g, '<br />');
};
filters.linenumbers = function(input) {
input = input.split('\n');
for (var i = 0; i < input.length; i++) {
input[i] = (i + 1) + '. ' + input[i];
}
return input.join('\n');
};
filters.lower = function(input) {
return input.toLowerCase();
};
filters.make_list = function(input) {
if (typeof input === 'number') {
input = String(input);
}
if (typeof input !== 'string') {
return input;
}
for (var e = [], i = 0; i < input.length; i++) {
e.push(input.charAt(i));
}
return e;
};
filters.random = function(input) {
if (!(input instanceof Array)) {
return input;
}
return input[Math.floor(Math.random() * input.length)]
};
filters.safe = function(input) {
return input;
};
filters.slugify = function(input) {
// Strip surrounding whitespace and special characters
input = input.replace(/(?:^ +| +$|[^a-zA-Z0-9 _])/g, '');
// Replace spaces with hyphens
input = input.replace(/ /g, '-');
return input.toLowerCase();
};
filters.title = function(input) {
input = input.split(' ');
for (var i = 0; i < input.length; i++) {
if (input[i].length > 0) {
input[i] = input[i].charAt(0).toUpperCase() + input[i].slice(1);
}
}
return input.join(' ');
};
filters.upper = function(input) {
return (typeof input === 'string') ? input.toUpperCase() : input;;
};
var tags = bacon.template.tags = {};
tags.if = function(code, contents, data) {
if (bacon.template._isTrue(code, data)) {
for (var endString = '', i = 0; i < contents.length; i++) {
endString += contents[i].parse(data);
}
return endString;
}
return '';
};
tags.for = function(code, contents, data) {
code = code.split(' ');
code[2] = code[2].split('|');
code[2] = bacon.template._getVariable(code[2][0], code[2].slice(1), data);
for (var endString = '', i = 0, j; i < code[2].length; i++) {
data[code[0]] = code[2][i];
for (j = 0; j < contents.length; j++) {
endString += contents[j].parse(data);
}
}
return endString;
};
tags.comment = function(code, contents, data) {
return '';
};