/
compile3.js
436 lines (399 loc) · 10.7 KB
/
compile3.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
module.exports = () => {
// Libraries
const YAML = require('js-yaml');
const FS = require('fs');
let clean = require('./cleanup');
delete console;
const console = require('conslog');
// Locales input
const input = (function () { return YAML.safeLoad(FS.readFileSync('locales.yaml')) })();
const shared = input.shared;
const home = input['/'];
const names = input.names;
const descs = input.descs;
// Modules input
const mods = (function () { return YAML.safeLoad(FS.readFileSync('modules.yaml')) })();
// Easy access constants
const md = FS.mkdir;
const mf = FS.writeFile;
const no = false;
// The two 'different' pages.
let root = "";
let form = "";
String.prototype.replace$ = function (thisArg) {
let array = this.split('$');
let repls = [...arguments];
repls.shift();
let r = "";
array.forEach((el, id) => {
r += el;
if (typeof repls[id] == "undefined" || typeof repls[id] == "null" || repls[id] === false) {
r += '';
} else if (typeof repls[id] == "function") {
r += repls[id].call(thisArg);
} else {
r += repls[id];
}
});
return r;
};
console.group('root');
console.info('Started root.');
root += mods.head.replace$(
this,
"Home" + shared.htmlTitle,
shared.ogTitle,
shared.ogDescription,
shared.ogimageAlt,
shared.ogDescription,
);
root += mods.customBody.replace$(
this,
mods.navbar.replace$(
this,
false,
names.museo,
false,
false,
names.divertimento,
false,
false,
names.rivista,
false,
false,
names.libro,
false,
false,
names.film,
false,
false,
names.audio,
false,
false,
names.green,
false,
false,
names.new,
false,
),
mods.root.replace$(
this,
home.title,
home.motto,
home.description,
names.museo,
descs.museo,
names.divertimento,
descs.divertimento,
names.rivista,
descs.rivista,
names.libro,
descs.libro,
names.film,
descs.film,
names.audio,
descs.audio,
names.green,
descs.green,
),
mods.foot,
);
console.info('Finished writing root.');
console.info('Started saving root.')
mf('index.html', root, (e) => { if (e) console.fatal(e); })
console.success('Home done.');
console.ungroup();
console.group('new');
console.info('Started new.');
form += mods.head.replace$(
this,
shared.htmlTitles.new + shared.htmlTitle,
shared.ogTitle,
shared.ogDescription,
shared.ogimageAlt,
shared.ogDescription,
);
form += mods.customBody.replace$(
this,
mods.navbar.replace$(
this,
false,
names.museo,
false,
false,
names.divertimento,
false,
false,
names.rivista,
false,
false,
names.libro,
false,
false,
names.film,
false,
false,
names.audio,
false,
false,
names.green,
false,
"active",
names.new,
" (current)",
),
mods.new.replace$(
this,
names.museo,
names.divertimento,
names.rivista,
names.libro,
names.film,
names.audio,
names.green,
names.museo,
names.divertimento,
names.rivista,
names.libro,
names.film,
names.audio,
names.green,
),
mods.formScript,
);
console.info("Finished writing new.");
console.info('Started saving new.');
md('new', e => { console.catch(e); });
mf('new/index.html', form, e => { if (e) console.fatal(e); })
console.success('New done.');
console.ungroup();
console.group('list');
console.info('Started the similar list pages.')
console.group('museo');
console.info('Started museo.');
let museo = similar('museo');
console.info('Finished writing museo.');
console.info('Started saving museo.');
md('museo', e => { console.catch(e); });
mf('museo/index.html', museo, e => { if (e) console.fatal(e); });
console.success('Museo done.');
console.ungroup();
console.group('divertimento');
console.info('Started divertimento.');
let divertimento = similar('divertimento');
console.info('Finished writing divertimento');
console.info('Started saving divertimento.');
md('divertimento', e => { console.catch(e); });
mf('divertimento/index.html', divertimento, e => { if (e) console.fatal(e); });
console.success('Divertimento done.');
console.ungroup();
console.group('rivista');
console.info('Started rivista.');
let rivista = similar('rivista');
console.info('Finished writing rivista');
console.info('Started saving rivista.');
md('rivista', e => { console.catch(e); });
mf('rivista/index.html', rivista, e => { if (e) console.fatal(e); });
console.success('Rivista done.');
console.ungroup();
console.group('libro');
console.info('Started libro.');
let libro = similar('libro');
console.info('Finished writing libro');
console.info('Started saving libro.');
md('libro', e => { console.catch(e); });
mf('libro/index.html', libro, e => { if (e) console.fatal(e); });
console.success('Libro done.');
console.ungroup();
console.group('film');
console.info('Started film.');
let film = similar('film');
console.info('Finished writing film');
console.info('Started saving film.');
md('film', e => { console.catch(e); });
mf('film/index.html', film, e => { if (e) console.fatal(e); });
console.success('Film done.');
console.ungroup();
console.group('audio');
console.info('Started audio.');
let audio = similar('audio');
console.info('Finished writing audio');
console.info('Started saving audio.');
md('audio', e => { console.catch(e); });
mf('audio/index.html', audio, e => { if (e) console.fatal(e); });
console.success('Audio done.');
console.ungroup();
console.group('green');
console.info('Started green.');
let green = similar('green');
console.info('Finished writing green');
console.info('Started saving green.');
md('green', e => { console.catch(e); });
mf('green/index.html', green, e => { if (e) console.fatal(e); });
console.success('Green done.');
console.ungroup();
console.success('Done the similar list pages.')
console.ungroup();
clean.cleanup(() => {
while (console.INDENTATION != 0) {
console.ungroup();
}
console.group('Catched problems');
console.uncatch();
console.ungroup();
});
function similar(name) {
let result = "";
result += mods.head.replace$(
this,
shared.htmlTitles[name] + shared.htmlTitle,
shared.ogTitle,
shared.ogDescription,
shared.ogimageAlt,
shared.ogDescription,
);
result += mods.customBody.replace$(
this,
mods.navbar.replace$(
this,
function () {
if (name == 'museo') return 'active'
else return ''
},
names.museo,
function () {
if (name == 'museo') return ' (corrente)'
else return ''
},
function () {
if (name == 'divertimento') return 'active'
else return ''
},
names.divertimento,
function () {
if (name == 'divertimento') return ' (corrente)'
else return ''
},
function () {
if (name == 'rivista') return 'active'
else return ''
},
names.rivista,
function () {
if (name == 'rivista') return ' (corrente)'
else return ''
},
function () {
if (name == 'libro') return 'active'
else return ''
},
names.libro,
function () {
if (name == 'libro') return ' (corrente)'
else return ''
},
function () {
if (name == 'film') return 'active'
else return ''
},
names.film,
function () {
if (name == 'film') return ' (corrente)'
else return ''
},
function () {
if (name == 'audio') return 'active'
else return ''
},
names.audio,
function () {
if (name == 'audio') return ' (corrente)'
else return ''
},
function () {
if (name == 'green') return 'active'
else return ''
},
names.green,
function () {
if (name == 'green') return ' (corrente)'
else return ''
},
false,
names.new,
false,
),
mods.elementWrapper.replace$(
this,
function () {
let res = "";
let initialElements = shared.list;
initialElements = initialElements.filter(el => { return el.cat.indexOf(name) !== -1 });
initialElements.forEach((el, id) => {
initialElements[id] = mods.element.replace$(
this,
function () { if (el.ref) { return `id='${el.ref}'` } else { return '' } },
function () {
try {
FS.readFileSync(`img/${el.ref}.webp`);
return `<img src='/img/${el.ref}.webp' width=100% loading=lazy class="card-img-top" alt="Image for ${el.title}">`
} catch (e) {
console.catch(e);
return ""
};
},
el.title,
el.desc ? el.desc : "",
el.warn ? `<div class="alert alert-warning"><small>${el.warn}</small></div>` : "",
el.info ? `<div class="alert alert-info" ><small>${el.info}</small></div>` : "",
el.href ? `<div><a class="btn btn-outline-secondary" href="${el.href}">Vai al contenuto »</a></div>` : "",
el.sc ? `<div class="d-flex justify-content-between align-items-center"><div class="btn-group">${(function () {
let acc = "";
el.sc.forEach(sc => {
acc += `<span class="badge badge-secondary">#${sc.toLowerCase()}</span>`
});
return acc;
})()}</div></div>` : ''
)
});
// At this point, the array initialElements contains HTML strings.
let howManyPerCol = [0, 0, 0];
let lengthD3 = initialElements.length / 3, lengthM3 = initialElements.length % 3;
if (lengthM3 == 0) {
// The elements can be safely split in three parts.
howManyPerCol = [lengthD3, lengthD3, lengthD3];
} else if (lengthM3 == 1) {
// The remaining 1 element goes in the center.
howManyPerCol = [Math.floor(lengthD3), Math.ceil(lengthD3), Math.floor(lengthD3)];
} else if (lengthM3 == 2) {
// The remaining 2 elements go in the first two columns.
howManyPerCol = [Math.ceil(lengthD3), Math.ceil(lengthD3), Math.floor(lengthD3)];
} else {
// Something strange happened!
console.fatal(`Stupid division: ${initialElements.length} % 3 = ${lengthM3}???`.random);
}
let splitInThreeParts = [[], [], []];
splitInThreeParts[0] = initialElements.slice(0, howManyPerCol[0]);
splitInThreeParts[1] = initialElements.slice(howManyPerCol[0], howManyPerCol[0] + howManyPerCol[1]);
splitInThreeParts[2] = initialElements.slice(howManyPerCol[0] + howManyPerCol[1], howManyPerCol[0] + howManyPerCol[1] + howManyPerCol[2]);
let columnified = ["", "", ""];
columnified = ["<div class='col-lg-4'>", "<div class='col-lg-4'>", "<div class='col-lg-4'>"];
columnified.forEach((el, id) => {
columnified[id] = el + splitInThreeParts[id].reduce((acc, el) => {
return acc + el;
}, "");
});
columnified.forEach((el, id) => {
columnified[id] += "</div>";
});
res = columnified.reduce((acc, el) => {
return acc + el;
}, "");
return res;
}
),
mods.foot,
);
return result;
}
}