-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
242 lines (192 loc) · 4.99 KB
/
index.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
const twig = require("twig");
const twigDrupal = require("twig-drupal-filters");
const twigDrupalWithout = require("twig-drupal-filters/filters/without");
/**
* @param {Array} args
*/
function DrupalAttribute(args) {
this.args = args;
this.args.forEach((arg) => {
if (arg[0] !== "$drupal") {
this[arg[0]] = arg[1];
}
});
/**
* @returns {DrupalAttribute}
*/
this.addClass = function () {
let self = this;
let values = [];
for (let i = 0; i < arguments.length; i++) {
values.push(arguments[i]);
}
values.forEach(function (value) {
if (!Array.isArray(value)) {
value = [value];
}
if (!self.class) {
self.class = [];
}
let classes = self.class;
value.forEach(function (d) {
if (classes.indexOf(d) < 0) {
classes.push(d);
}
});
});
return this;
};
this.removeClass = function (value) {
let classes = [];
if (this.class) {
classes = this.class;
}
if (!Array.isArray(value)) {
value = [value];
}
value.forEach(function (v) {
let index = classes.indexOf(v);
if (index > -1) {
classes.splice(index, 1);
}
});
return this;
};
this.hasClass = function (value) {
let classes = [];
if (this.class) {
classes = this.class;
}
return classes.indexOf(value) > -1;
};
this.setAttribute = function (key, value) {
this.set(key, value);
return this;
};
this.removeAttribute = function (key) {
this.delete(key);
return this;
};
}
DrupalAttribute.prototype.toString = function () {
let result = "";
let components = [];
this.args.forEach(([value, key]) => {
if (Array.isArray(value)) {
value = value.join(" ");
}
if (value !== "$drupal") {
components.push([value, '"' + key + '"'].join("="));
}
});
let rendered = components.join(" ");
if (rendered) {
result += " " + rendered;
}
return result;
};
module.exports = {
engine: twig.twig,
extendEngine({ engine, init }, cache = false) {
const engineInstance = engine || twig;
engineInstance.cache(cache);
twigDrupal(engineInstance);
engineInstance.extendFilter("without", function (value, args) {
if (!value) return {};
if (typeof value === "string") {
let str = value;
args.forEach((a) => {
const pattern = `/\\s${a}="[^"]*"/`;
str = value.replace(new RegExp(pattern), "");
});
return str;
}
if (value.args && value.args.find((arg) => arg[0] === "$drupal")) {
const values = { ...value };
values.args = values.args.filter((arg) => {
return !args.includes(arg[0]);
});
return DrupalAttribute.prototype.toString.call(values);
}
return twigDrupalWithout(value, args);
});
engineInstance.extend(function (Twig) {
Twig.exports.extendTag({
type: "trans",
regex: /^trans$/,
next: ["endtrans", "plural", "variable"],
open: true,
compile: function (token) {
return token;
},
parse: function (token, context, chain) {
var html = "";
token.output.forEach((output) => {
if (output.type === "raw") {
html += output.value;
} else {
if (output.type === "output") {
html += Twig.expression.parse.apply(this, [
output.stack,
context,
]);
}
}
});
return {
chain: chain,
output: html,
};
},
});
Twig.exports.extendTag({
type: "endtrans",
regex: /^endtrans$/,
next: [],
open: false,
});
Twig.exports.extendTag({
type: "plural",
regex: /^plural\s+(.+)$/,
next: ["endtrans"],
open: false,
});
});
if (init && typeof init === "function") {
init(engineInstance);
}
return engineInstance.twig;
},
async extendTemplateData(file, engineOptions = {}, data = {}) {
if (
typeof data === "string" ||
typeof data === "number" ||
typeof data === "boolean"
) {
return data;
}
if (Array.isArray(data)) {
data.forEach(async (entry, i) => {
data[i] = await this.extendTemplateData(file, engineOptions, entry);
});
return data;
}
const o = {};
Object.entries(data).forEach(async ([attr, entries]) => {
if (!(entries === null || entries === undefined)) {
if (entries["$drupal"]) {
o[attr] = new DrupalAttribute(Object.entries(entries));
} else if (
typeof entries === "string" ||
typeof entries === "number" ||
typeof entries === "boolean"
) {
o[attr] = entries;
} else {
o[attr] = await this.extendTemplateData(file, engineOptions, entries);
}
}
});
return o;
},
};