This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twig-parser.js
85 lines (73 loc) · 2.26 KB
/
twig-parser.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
const Twig = require('twig');
const crypto = require('crypto');
const pretty = require('pretty');
Twig.extend((TwigCore) => {
/**
* Remove tokens 2, 3, 4 and 8.
* This tokens are used for functions and data output.
* Since the data binding is done in Vue this could lead to syntax issues.
* We are only using the block system for template inheritance.
*
* @type {Array<any>}
*/
TwigCore.token.definitions = [
TwigCore.token.definitions[0],
TwigCore.token.definitions[1],
TwigCore.token.definitions[5],
TwigCore.token.definitions[6],
TwigCore.token.definitions[7],
TwigCore.token.definitions[9],
TwigCore.token.definitions[10],
];
/**
* Twig inheritance extension.
* The parent function is used as a statement tag.
* This is used to prevent syntax issues between Twig and Vue.
* Use `{% parent %}` to print out the parent content of a block.
*/
TwigCore.exports.extendTag({
type: 'parent',
regex: /^parent/,
next: [],
open: true,
parse(token, context, chain) {
return {
chain,
output: TwigCore.placeholders.parent,
};
},
});
/** Make the placeholders available in the exposed Twig object. */
TwigCore.exports.placeholders = TwigCore.placeholders;
/** Make the Twig template cache registry available. */
TwigCore.exports.getRegistry = function getRegistry() {
return TwigCore.Templates.registry;
};
/** Provide possibility to clear the template cache registry */
TwigCore.exports.clearRegistry = function clearRegistry() {
TwigCore.Templates.registry = {};
};
});
function generateId() {
return crypto.randomBytes(16).toString('hex');
}
module.exports = (content) => {
const id = generateId();
const twigTemplate = Twig.twig({
id: id,
data: content,
});
const templateBlocks = twigTemplate.render(
{},
{
output: 'blocks',
}
);
return Object.keys(templateBlocks).map((key) => {
const template = templateBlocks[key];
return {
name: key,
code: pretty(template, { ocd: true }),
};
});
};