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
| class NunjucksTag { constructor(name, fn) { this.tags = [name]; this.fn = fn; }
parse(parser, nodes, lexer) { const node = this._parseArgs(parser, nodes, lexer);
return new nodes.CallExtension(this, 'run', node, []); }
_parseArgs(parser, nodes, lexer) { const tag = parser.nextToken(); const node = new nodes.NodeList(tag.lineno, tag.colno); const argarray = new nodes.Array(tag.lineno, tag.colno);
let token; let argitem = '';
while ((token = parser.nextToken(true))) { if (token.type === lexer.TOKEN_WHITESPACE || token.type === lexer.TOKEN_BLOCK_END) { if (argitem !== '') { const argnode = new nodes.Literal(tag.lineno, tag.colno, argitem.trim()); argarray.addChild(argnode); argitem = ''; }
if (token.type === lexer.TOKEN_BLOCK_END) { break; } } else { argitem += token.value; } }
node.addChild(argarray);
return node; }
run(context, args) { return this._run(context, args, ''); }
_run(context, args, body) { return Reflect.apply(this.fn, context.ctx, [args, body]); } }
const trimBody = body => { return stripIndent(body()).replace(/^\n?|\n?$/g, ''); };
class NunjucksBlock extends NunjucksTag { parse(parser, nodes, lexer) { const node = this._parseArgs(parser, nodes, lexer); const body = this._parseBody(parser, nodes, lexer);
return new nodes.CallExtension(this, 'run', node, [body]); }
_parseBody(parser, nodes, lexer) { const body = parser.parseUntilBlocks(`end${this.tags[0]}`);
parser.advanceAfterBlockEnd(); return body; }
run(context, args, body) { return this._run(context, args, trimBody(body)); } }
class NunjucksAsyncTag extends NunjucksTag { parse(parser, nodes, lexer) { const node = this._parseArgs(parser, nodes, lexer);
return new nodes.CallExtensionAsync(this, 'run', node, []); }
run(context, args, callback) { return this._run(context, args, '').then(result => { callback(null, result); }, callback); } }
class NunjucksAsyncBlock extends NunjucksBlock { parse(parser, nodes, lexer) { const node = this._parseArgs(parser, nodes, lexer); const body = this._parseBody(parser, nodes, lexer);
return new nodes.CallExtensionAsync(this, 'run', node, [body]); }
run(context, args, body, callback) { body((err, result) => { body = () => result || '';
this._run(context, args, trimBody(body)).then(result => { callback(err, result); }); }); } }
|