-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjinja.js
687 lines (628 loc) · 22.4 KB
/
jinja.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
(function(){var STRINGS = /'(\\.|[^'])*'|"(\\.|[^"'"])*"/g;
var IDENTS_AND_NUMS = /([$_a-z][$\w]*)|([+-]?\d+(\.\d+)?)/g;
var NUMBER = /^[+-]?\d+(\.\d+)?$/;
//non-primitive literals (array and object literals)
var NON_PRIMITIVES = /\[[@#~](,[@#~])*\]|\[\]|\{([@i]:[@#~])(,[@i]:[@#~])*\}|\{\}/g;
//bare identifiers such as variables and in object literals: {foo: 'value'}
var IDENTIFIERS = /[$_a-z][$\w]*/ig;
var VARIABLES = /i(\.i|\[[@#i]\])*/g;
var ACCESSOR = /(\.i|\[[@#i]\])/g;
var OPERATORS = /(===?|!==?|>=?|<=?|&&|\|\||[+\-\*\/%])/g;
//extended (english) operators
var EOPS = /(^|[^$\w])(and|or|not|is|isnot)([^$\w]|$)/g;
var LEADING_SPACE = /^\s+/;
var TRAILING_SPACE = /\s+$/;
var START_TOKEN = /\{\{\{|\{\{|\{%|\{#/;
var TAGS = {
'{{{': /^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}\}/,
'{{': /^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}/,
'{%': /^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?%\}/,
'{#': /^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?#\}/
};
var delimeters = {
'{%': 'directive',
'{{': 'output',
'{#': 'comment'
};
var operators = {
and: '&&',
or: '||',
not: '!',
is: '==',
isnot: '!='
};
var constants = {
'true': true,
'false': false,
'null': null
};
function Parser() {
this.nest = [];
this.compiled = [];
this.childBlocks = 0;
this.parentBlocks = 0;
this.isSilent = false;
}
Parser.prototype.push = function (line) {
if (!this.isSilent) {
this.compiled.push(line);
}
};
Parser.prototype.parse = function (src) {
var _this = this;
return _this.tokenize(src).then(function () {
return Promise.resolve(_this.compiled);
});
};
Parser.prototype.tokenize = function (src) {
var _this = this;
return new Promise(function (resolve, reject) {
var lastEnd = 0, parser = _this, trimLeading = false;
matchAll(src, START_TOKEN, function (open, index, src) {
//here we match the rest of the src against a regex for this tag
var match = src.slice(index + open.length).match(TAGS[open]);
match = (match ? match[0] : '');
//here we sub out strings so we don't get false matches
var simplified = match.replace(STRINGS, '@');
//if we don't have a close tag or there is a nested open tag
if (!match || ~simplified.indexOf(open)) {
return Promise.resolve(index + 1);
}
var inner = match.slice(0, 0 - open.length);
//check for white-space collapse syntax
if (inner.charAt(0) == '-') var wsCollapseLeft = true;
if (inner.slice(-1) == '-') var wsCollapseRight = true;
inner = inner.replace(/^-|-$/g, '').trim();
//if we're in raw mode and we are not looking at an "endraw" tag, move along
if (parser.rawMode && (open + inner) != '{%endraw') { // TODO: remove hardcoded tag
return Promise.resolve(index + 1);
}
if (parser.commentMode) {
// TODO: skip until parser.commentMode==false
}
var text = src.slice(lastEnd, index);
lastEnd = index + open.length + match.length;
if (trimLeading) text = trimLeft(text);
if (wsCollapseLeft) text = trimRight(text);
if (wsCollapseRight) trimLeading = true;
if (open == '{{{') {
//liquid-style: make {{{x}}} => {{x|safe}}
open = '{{';
inner += '|safe'; // TODO: remove hardcoded filter
} else if (open == '{#') {
return Promise.resolve(lastEnd)
}
parser.textHandler(text);
return parser.tokenHandler(open, inner);
}).then(function(){
var text = src.slice(lastEnd);
if (trimLeading) text = trimLeft(text);
_this.textHandler(text);
resolve();
});
});
};
Parser.prototype.textHandler = function (text) {
if (text) {
this.push('write(' + JSON.stringify(text) + ');');
}
};
Parser.prototype.tokenHandler = function (open, inner) {
var type = delimeters[open];
if (type == 'directive') {
return this.compileTag(inner);
} else if (type == 'output') {
var extracted = this.extractEnt(inner, STRINGS, '@');
//replace || operators with ~
extracted.src = extracted.src.replace(/\|\|/g, '~').split('|');
//put back || operators
extracted.src = extracted.src.map(function (part) {
return part.split('~').join('||');
});
var parts = this.injectEnt(extracted, '@');
if (parts.length > 1) {
var filters = parts.slice(1).map(this.parseFilter.bind(this));
this.push('filter(' + this.parseExpr(parts[0]) + ',' + filters.join(',') + ');');
} else {
this.push('filter(' + this.parseExpr(parts[0]) + ');');
}
return Promise.resolve();
} else {
return Promise.resolve();
}
};
Parser.prototype.compileTag = function (str) {
var directive = str.split(' ')[0];
var handler = jinja.tag_handlers[directive];
if (!handler) {
throw new Error('Invalid tag: ' + str);
}
var r = handler.call(this, str.slice(directive.length).trim());
if (r==undefined){
return Promise.resolve();
} else {
return r;
}
};
Parser.prototype.parseFilter = function (src) {
src = src.trim();
var match = src.match(/[:(]/);
var i = match ? match.index : -1;
if (i < 0) return JSON.stringify([src]);
var name = src.slice(0, i);
var args = src.charAt(i) == ':' ? src.slice(i + 1) : src.slice(i + 1, -1);
args = this.parseExpr(args, {terms: true});
return '[' + JSON.stringify(name) + ',' + args + ']';
};
Parser.prototype.extractEnt = function (src, regex, placeholder) {
var subs = [], isFunc = typeof placeholder == 'function';
src = src.replace(regex, function (str) {
var replacement = isFunc ? placeholder(str) : placeholder;
if (replacement) {
subs.push(str);
return replacement;
}
return str;
});
return {src: src, subs: subs};
};
Parser.prototype.injectEnt = function (extracted, placeholder) {
var src = extracted.src, subs = extracted.subs, isArr = Array.isArray(src);
var arr = (isArr) ? src : [src];
var re = new RegExp('[' + placeholder + ']', 'g'), i = 0;
arr.forEach(function (src, index) {
arr[index] = src.replace(re, function () {
return subs[i++];
});
});
return isArr ? arr : arr[0];
};
//replace complex literals without mistaking subscript notation with array literals
Parser.prototype.replaceComplex = function (s) {
var parsed = this.extractEnt(s, /i(\.i|\[[@#i]\])+/g, 'v');
parsed.src = parsed.src.replace(NON_PRIMITIVES, '~');
return this.injectEnt(parsed, 'v');
};
//parse expression containing literals (including objects/arrays) and variables (including dot and subscript notation)
//valid expressions: `a + 1 > b.c or c == null`, `a and b[1] != c`, `(a < b) or (c < d and e)`, 'a || [1]`
Parser.prototype.parseExpr = function (src, opts) {
opts = opts || {};
//extract string literals -> @
var parsed1 = this.extractEnt(src, STRINGS, '@');
//note: this will catch {not: 1} and a.is; could we replace temporarily and then check adjacent chars?
parsed1.src = parsed1.src.replace(EOPS, function (s, before, op, after) {
return (op in operators) ? before + operators[op] + after : s;
});
//sub out non-string literals (numbers/true/false/null) -> #
// the distinction is necessary because @ can be object identifiers, # cannot
var parsed2 = this.extractEnt(parsed1.src, IDENTS_AND_NUMS, function (s) {
return (s in constants || NUMBER.test(s)) ? '#' : null;
});
//sub out object/variable identifiers -> i
var parsed3 = this.extractEnt(parsed2.src, IDENTIFIERS, 'i');
//remove white-space
parsed3.src = parsed3.src.replace(/\s+/g, '');
//the rest of this is simply to boil the expression down and check validity
var simplified = parsed3.src;
//sub out complex literals (objects/arrays) -> ~
// the distinction is necessary because @ and # can be subscripts but ~ cannot
while (simplified != (simplified = this.replaceComplex(simplified)));
//now @ represents strings, # represents other primitives and ~ represents non-primitives
//replace complex variables (those with dot/subscript accessors) -> v
while (simplified != (simplified = simplified.replace(/i(\.i|\[[@#i]\])+/, 'v')));
//empty subscript or complex variables in subscript, are not permitted
simplified = simplified.replace(/[iv]\[v?\]/g, 'x');
//sub in "i" for @ and # and ~ and v (now "i" represents all literals, variables and identifiers)
simplified = simplified.replace(/[@#~v]/g, 'i');
//sub out operators
simplified = simplified.replace(OPERATORS, '%');
//allow 'not' unary operator
simplified = simplified.replace(/!+[i]/g, 'i');
var terms = opts.terms ? simplified.split(',') : [simplified];
terms.forEach(function (term) {
//simplify logical grouping
while (term != (term = term.replace(/\(i(%i)*\)/g, 'i')));
if (!term.match(/^i(%i)*$/)) {
throw new Error('Invalid expression: ' + src);
}
});
parsed3.src = parsed3.src.replace(VARIABLES, this.parseVar.bind(this));
parsed2.src = this.injectEnt(parsed3, 'i');
parsed1.src = this.injectEnt(parsed2, '#');
return this.injectEnt(parsed1, '@');
};
Parser.prototype.parseVar = function (src) {
var args = Array.prototype.slice.call(arguments);
var str = args.pop(), index = args.pop();
//quote bare object identifiers (might be a reserved word like {while: 1})
if (src == 'i' && str.charAt(index + 1) == ':') {
return '"i"';
}
var parts = ['"i"'];
src.replace(ACCESSOR, function (part) {
if (part == '.i') {
parts.push('"i"');
} else if (part == '[i]') {
parts.push('get("i")');
} else {
parts.push(part.slice(1, -1));
}
});
return 'get(' + parts.join(',') + ')';
};
//escapes a name to be used as a javascript identifier
Parser.prototype.escName = function (str) {
return str.replace(/\W/g, function (s) {
return '$' + s.charCodeAt(0).toString(16);
});
};
Parser.prototype.parseQuoted = function (str) {
if (str.charAt(0) == "'") {
str = str.slice(1, -1).replace(/\\.|"/, function (s) {
if (s == "\\'") return "'";
return s.charAt(0) == '\\' ? s : ('\\' + s);
});
str = '"' + str + '"';
}
try {
return JSON.parse(str);
} catch (e){
console.error("could not parse string "+str+"! replacing by empty")
return "";
}
};
/*!
* Helpers
*/
function trimLeft(str) {
return str.replace(LEADING_SPACE, '');
}
function trimRight(str) {
return str.replace(TRAILING_SPACE, '');
}
function matchAll(str, reg, fn) {
return new Promise(function (resolve, reject) {
//copy as global
reg = new RegExp(reg.source, 'g' + (reg.ignoreCase ? 'i' : '') + (reg.multiline ? 'm' : ''));
var match = reg.exec(str);
function recurse_match() {
if (match) {
fn(match[0], match.index, str).then(function (result) {
if (typeof result == 'number') {
reg.lastIndex = result;
}
match = reg.exec(str);
recurse_match();
});
} else {
resolve();
}
}
recurse_match();
});
}
/*!
* Jinja Templating for JavaScript with asyncronous template file loader v0.1.8
* https://github.com/kreopt/jinjajs
* Forked from https://github.com/sstur/jinja-js
*/
window.jinja = {};
//the context 'this' inside tag_handlers is the parser instance
jinja.tag_handlers = {};
jinja.filter_handlers = {};
var _toString = Object.prototype.toString;
var _hasOwnProperty = Object.prototype.hasOwnProperty;
var toString = function (val) {
if (val == null) return '';
return (typeof val.toString == 'function') ? val.toString() : _toString.call(val);
};
var getRuntime = function runtime(data, opts) {
var defaults = {autoEscape: 'html'};
var extend = function (dest, src) {
Object.keys(src).forEach(function (key) {
dest[key] = src[key];
});
return dest;
};
//get a value, lexically, starting in current context; a.b -> get("a","b")
var get = function () {
var val, n = arguments[0], c = stack.length;
while (c--) {
val = stack[c][n];
if (typeof val != 'undefined') break;
}
for (var i = 1, len = arguments.length; i < len; i++) {
if (val == null) continue;
n = arguments[i];
val = (_hasOwnProperty.call(val, n)) ? val[n] : (typeof val._get == 'function' ? (val[n] = val._get(n)) : null);
}
return (val == null) ? null : val;
};
var set = function (n, val) {
stack[stack.length - 1][n] = val;
};
var push = function (ctx) {
stack.push(ctx || {});
};
var pop = function () {
stack.pop();
};
var write = function (str) {
output.push(str);
};
var filter = function (val) {
for (var i = 1, len = arguments.length; i < len; i++) {
var arr = arguments[i], name = arr[0], filter = jinja.filter_handlers[name];
if (filter) {
arr[0] = val;
//now arr looks like [val, arg1, arg2]
val = filter.apply(data, arr);
} else {
throw new Error('Invalid filter: ' + name);
}
}
if (opts.autoEscape && name != opts.autoEscape && name != 'safe') {
//auto escape if not explicitly safe or already escaped
val = jinja.filter_handlers[opts.autoEscape].call(data, val);
}
output.push(val);
};
var each = function (obj, loopvar, fn1, fn2) {
if (obj == null) return;
var arr = Array.isArray(obj) ? obj : Object.keys(obj), len = arr.length;
var ctx = {loop: {length: len, first: arr[0], last: arr[len - 1]}};
push(ctx);
for (var i = 0; i < len; i++) {
extend(ctx.loop, {index: i + 1, index0: i});
fn1(ctx[loopvar] = arr[i]);
}
if (len == 0 && fn2) fn2();
pop();
};
var block = function (fn) {
push();
fn();
pop();
};
var render = function () {
return output.join('');
};
data = data || {};
opts = extend(defaults, opts || {});
var stack = [Object.create(data || {})], output = [];
return {get: get, set: set, push: push, pop: pop, write: write, filter: filter, each: each, block: block, render: render};
};
var runtime;
jinja.make_tag = function(name, handler){
if (typeof handler == typeof ''){
jinja.tag_handlers[name] = jinja.tag_handlers[handler];
} else {
jinja.tag_handlers[name] = handler;
}
};
jinja.make_filter = function(name, filter){
if (jinja.filter_handlers.name) {console.warn('Filter '+name+' already exists. Overriding.');}
jinja.filter_handlers[name] = filter;
};
jinja.compile = function (markup, opts) {
var _this = this;
return new Promise(function (resolve, reject) {
opts = opts || {};
var parser = new Parser();
parser.read_template_file = _this.read_template_file;
var code = [];
code.push('function render($) {');
code.push('var get = $.get, set = $.set, push = $.push, pop = $.pop, write = $.write, filter = $.filter, each = $.each, block = $.block;');
parser.parse(markup).then(function (html) {
code.push.apply(code, html);
code.push('return $.render();');
code.push('}');
code = code.join('\n');
if (opts.runtime === false) {
var fn = new Function('data', 'options', 'return (' + code + ')(runtime(data, options))');
} else {
runtime = runtime || (runtime = getRuntime.toString());
fn = new Function('data', 'options', 'return (' + code + ')((' + runtime + ')(data, options))');
}
resolve({render: fn});
});
});
};
jinja.render = function (markup, data, opts) {
return new Promise(function(resolve, reject){
var awaiting_data={};
var promises=[];
for (var d in data){
if (data[d] instanceof Promise){
awaiting_data[d] = promises.length;
promises.push(data[d]);
}
}
Promise.all(promises).then(function(values){
for (var d in awaiting_data){
data[d] = values[awaiting_data[d]];
}
jinja.compile(markup).then(function (tmpl) {
resolve(tmpl.render(data, opts));
});
}).catch(function(e){
console.error(e);
});
})
};
jinja.template_files = {};
jinja.template_url = '/templates/';
jinja.read_template_file = function (name) {
if (!jinja.loader){
throw new Error("template loader is not set!");
}
return new Promise(function (resolve, reject) {
var templateFiles = jinja.template_files || {};
var templateFile = templateFiles[name];
if (templateFile == null) {
return jinja.loader(jinja.template_url + name).then(function(data){
resolve(data);
}).catch(function (reason) {
reject('Template file "'+name+'" not found: ' + reason);
});
} else {
resolve(templateFile);
}
});
};
jinja.loader = function(path){
return fetch(path).then(function(response){
// status "0" to handle local files fetching (e.g. Cordova/Phonegap etc.)
if (response.status === 200 || response.status === 0) {
return response.text().then(function(data){
console.log(data);
jinja.template_files[name] = data;
return jinja.template_files[name];
}).catch(function(r){
"use strict";
console.log(":(")
});
} else {
return Promise.reject(new Error(response.statusText))
}
})
}
jinja.make_filter('html', function (val) {
return toString(val)
.split('&').join('&')
.split('<').join('<')
.split('>').join('>')
.split('"').join('"');
});
jinja.make_filter('safe', function (val) {
return val;
});
jinja.make_tag('comment', function () {
this.commentMode = true;
});
jinja.make_tag('endcomment', function () {
this.commentMode = false;
});
jinja.make_tag('block', function (name) {
if (this.isParent) {
++this.parentBlocks;
var blockName = 'block_' + (this.escName(name) || this.parentBlocks);
this.push('block(typeof ' + blockName + ' == "function" ? ' + blockName + ' : function() {');
} else if (this.hasParent) {
this.isSilent = false;
++this.childBlocks;
blockName = 'block_' + (this.escName(name) || this.childBlocks);
this.push('function ' + blockName + '() {');
}
this.nest.unshift('block');
});
jinja.make_tag('endblock', function () {
this.nest.shift();
if (this.isParent) {
this.push('});');
} else if (this.hasParent) {
this.push('}');
this.isSilent = true;
}
});
jinja.make_tag('extends', function (name) {
if (name[0].startsWith("'") || name[0].startsWith("\"")) {
name = this.parseQuoted(name);
}
var _this = this;
return this.read_template_file(name).then(function (parentSrc) {
_this.isParent = true;
return _this.tokenize(parentSrc).then(function(){
_this.isParent = false;
_this.hasParent = true;
//silence output until we enter a child block
_this.isSilent = true;
});
}).catch(function(e){
console.error(e);
});
});
jinja.make_tag('for', function (str) {
var i = str.indexOf(' in ');
var name = str.slice(0, i).trim();
var expr = str.slice(i + 4).trim();
this.push('each(' + this.parseExpr(expr) + ',' + JSON.stringify(name) + ',function() {');
this.nest.unshift('for');
});
jinja.make_tag('endfor', function () {
this.nest.shift();
this.push('});');
});
jinja.make_tag('if', function (expr) {
this.push('if (' + this.parseExpr(expr) + ') {');
this.nest.unshift('if');
});
jinja.make_tag('else', function () {
if (this.nest[0] == 'for') {
this.push('}, function() {');
} else {
this.push('} else {');
}
});
jinja.make_tag('elseif', function (expr) {
this.push('} else if (' + this.parseExpr(expr) + ') {');
});
jinja.make_tag('elif', 'elseif');
jinja.make_tag('endif', function () {
this.nest.shift();
this.push('}');
});
jinja.make_tag('include', function (name) {
if (name[0].startsWith("'") || name[0].startsWith("\"")) {
name = this.parseQuoted(name);
}
var _this = this;
return this.read_template_file(name).then(function (incSrc) {
_this.isInclude = true;
return _this.tokenize(incSrc).then(function(){
_this.isInclude = false;
});
}).catch(function(e){
console.error(e);
});
});
jinja.make_tag('load', function (name) {});
jinja.make_tag('raw', function () {
this.rawMode = true;
});
jinja.make_tag('endraw', function () {
this.rawMode = false;
});
jinja.make_tag('set', function (stmt) {
var i = stmt.indexOf('=');
var name = stmt.slice(0, i).trim();
var expr = stmt.slice(i + 1).trim();
this.push('set(' + JSON.stringify(name) + ',' + this.parseExpr(expr) + ');');
});
jinja.make_tag('assign', 'set');
jinja.make_tag('static',function(stmt){
stmt = stmt.trim();
this.push("write(\""+Fat.config.static_url + stmt.substr(1,stmt.length-2)+"\")");
});
jinja.make_tag('static',function(stmt){
stmt = stmt.trim();
var parts = stmt.split(/\s/);
if (parts.length < 1){
console.warn("url tag should contain 1 or more parameters. ignoring. ");
return;
}
// TODO: check quotes
var name = parts[0].substr(1,parts[0].length-2);
var args={positional:[], keyword:{}};
for (var i=1; i<parts.length; i++){
if (parts[i].indexOd('=')!=-1){
var kw=parts[i].split('=');
args.keyword[kw[0].trim()]=kw[1].trim();
} else {
args.positional.push(parts[i].trim());
}
}
this.push("write(\""+Fat.resolve_url(name, args)+"\")");
});
}());
//# sourceMappingURL=jinja.js.map