-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
425 lines (294 loc) · 10.6 KB
/
build.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
const { PerformanceObserver, performance } = require('perf_hooks');
let t_start = performance.now();
process.on('exit', function() {
let t_end = performance.now();
console.log("Done in " + (t_end - t_start) + "ms");
});
const fs = require('fs-extra');
const glob = require('glob-fs')({ gitignore: true });
const marked = require('marked');
var plantuml = require('plantuml');
const child_process = require('child_process');
function run(cmd, cwd, done) {
console.log(cmd);
const exec = child_process.exec;
exec(cmd, { cwd }, (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err);
} else {
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (typeof done == "function") {
done(err, stdout, stderr);
} else {}
}
});
}
const build_json = fs.readJSONSync('build.json', { throws: false }) || {};
build_json.doc_build = (build_json.doc_build || 0) + 1;
fs.outputJsonSync('build.json', build_json, { spaces: 2 });
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
const cache_json = fs.readJSONSync('./upload/puml_cache.json', { throws: false }) || {};
function cache_lookup(text, miss) {
let hash = new String(text).hashCode();
if (cache_json[hash]) { //hit
return new Promise((resolve,reject)=>resolve(cache_json[hash]));
} else {
return miss(text).then((output) => {
cache_json[hash] = output;
fs.outputJson('./upload/puml_cache.json', cache_json, { spaces: 2 });
return output;
})
}
}
let puml_count = 0;
let epuml_count = 0;
let fml_count = 0;
const renderer = new marked.Renderer();
renderer._paragraph = renderer.paragraph;
renderer.paragraph = function(text) {
//console.log(text);
if (text.trim().startsWith("{")) {
renderer.sec_depth = renderer.sec_depth || 0;
renderer.sec_depth++;
let c = text.split(".");
c.shift();
if (c.length>0) {
return `<section class="${c.join(" ")}">`;
} else {
return "<section>"
}
}
if (text.trim().startsWith("}")) {
renderer.sec_depth = renderer.sec_depth || 0;
renderer.sec_depth--;
return "</section>"
}
if (text.trim().startsWith("Pass: ") && renderer.sec_depth > 0) {
return `<section class="pass"><span>★☆☆: </span>${text.split("Pass: ")[1]}</section>`;
}
if (text.trim().startsWith("Good: ") && renderer.sec_depth > 0) {
return `<section class="ok"><span>★★☆: </span>${text.split("Good: ")[1]}</section>`;
}
if (text.trim().startsWith("Exceed: ") && renderer.sec_depth > 0) {
return `<section class="exceed"><span>★★★: </span>${text.split("Exceed: ")[1]}</section>`;
}
if (text.trim().startsWith("Hint: ") && renderer.sec_depth > 0) {
return `<section class="hint"><span>Hint: </span>${text.split("Hint: ")[1]}</section>`;
}
return renderer._paragraph(text);
}
renderer._code = renderer.code;
renderer.code = function(code, infostring, escaped) {
//console.log(code, infostring, escaped);
if (infostring) {
if (infostring.startsWith("puml")) {
let i = epuml_count++;
cache_lookup(code, ()=>plantuml(code)).then(svg=>{
fs.writeFile(renderer._out_folder+"puml_e"+i+".svg", svg)
})
return `<img src="./puml_e${i}.svg">`;
}
}
return renderer._code(code, infostring, escaped);
};
renderer._image = renderer.image;
renderer.image = function(href, title, text) {
//console.log(href, title, text);
if (href.endsWith(".puml")) {
let i = puml_count++;
fs.readFile(href.replace("./",renderer._in_folder)).then(puml=>{
return cache_lookup(puml, ()=>plantuml(puml))
//return plantuml(puml)
}).then(svg=>{
fs.writeFile(renderer._out_folder+"puml_"+i+".svg", svg)
})
return `<img src="./puml_${i}.svg" alt="${text}">`;
}
if (href.endsWith(".fml")) {
let i = fml_count++;
const fml_output = renderer._out_folder+"fml_"+i+".svg";
run("node fml " + href.replace("./",renderer._in_folder) + " " + fml_output);
return `<img src="./fml_${i}.svg" alt="${text}">`;
}
if (href.endsWith(".c5")) {
let i = fml_count++;
const fml_output = renderer._out_folder+"c5_"+i+".svg";
run("node c5 " + href.replace("./",renderer._in_folder) + " " + fml_output);
return `<img src="./c5_${i}.svg" alt="${text}">`;
}
if (href.endsWith(".madr")) {
let md = fs.readFileSync(href.replace("./",renderer._in_folder));
return `<h3>${text}</h3>` + marked.parse("" + new String(md));
}
return renderer._image(href, title, text);
}
renderer._heading = renderer.heading;
let task_count = 0;
renderer.heading = function(text, level, raw, slugger) {
// console.log(text);
if (text.indexOf("Ex -") >= 0) {
task_count++;
text = text.replace("Ex -", `<span class='task'>${task_count}.</span> `);
}
// let sec = "<section>"
// if (task_count > 1) {
// sec = "</section>" + sec;
// }
return renderer._heading(text, level, raw, slugger);
};
marked.setOptions({
renderer, //: new marked.Renderer(),
highlight: function(code) {
return code; //require('highlight').highlightAuto(code).value;
},
pedantic: false,
gfm: true,
breaks: false,
sanitize: false,
smartLists: true,
smartypants: false,
xhtml: false
});
function loadMD(f, vars = {}) {
let txt = "" + new String(fs.readFileSync(f));
if (txt.indexOf("---") >= 0) {
let parts = txt.split("\n---\n");
parts.slice(0, -1).forEach(p => {
let lines = p.split("\n");
let k = lines[0].split(":");
vars[k[0]] = lines.slice(1).join("\n");
});
txt = parts[parts.length - 1];
}
//convert strings to booleans
Object.keys(vars).forEach(k => {
if (vars[k] === "false") vars[k] = false;
if (vars[k] === "true") vars[k] = true;
})
task_count = 0;
return { md: marked.parse(txt), meta: vars };
}
let ejs = require('ejs');
let views = {}
function render_one(model, out_file, template) {
model.out_file = out_file;
views[template] = views[template] || new String(fs.readFileSync("./template/"+template+".ejs"));
let html = ejs.render(views[template], model);
fs.ensureFileSync(out_file);
fs.writeFileSync(out_file, html);
};
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var i = 0;
(function next() {
var file = list[i++];
if (!file) return done(null, results);
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
next();
});
} else {
results.push({ name: file, stat });
next();
}
});
})();
});
};
function deltaBuild() {
let old_file_list = {};
try {
old_file_list = fs.readJsonSync('upload/files.json');
} catch (e) {}
walk("src", function(err, list) {
// console.log(list);
let flat = {};
list.filter((f) => !f.name.endsWith(".DS_Store")).forEach(f => {
flat[f.name] = f.stat;
});
let todo = {};
let deleted = {};
Object.keys(old_file_list).forEach(k => { deleted[k] = old_file_list[k] });
let changed = [];
function extract_dir(k) {
let a = k.split("/")
return a.slice(1, 3).join("/");
}
function push(k) {
let d = extract_dir(k);
todo[d] = (todo[d] || 0) + 1;
changed.push(k);
}
Object.keys(flat).forEach((k) => {
if (old_file_list[k]) {
if (flat[k].mtimeMs != old_file_list[k].mtimeMs) {
push(k);
}
delete deleted[k]; //k was found so what's left over should have been deleted
} else {
push(k); //this is a new addition
}
});
fs.writeJson("upload/files.json", flat);
console.log('Changed', changed);
console.log('Deleted', Object.keys(deleted));
if (Object.keys(todo).length == 0) {
todo['sa/model'] = 1;
}
//todo['exercises/8-ws'] = 1;
if (Object.keys(todo).indexOf('templates/doc.ejs') != -1) {
todo = Array.from(new Set(Object.keys(flat).map(extract_dir))).reduce((a, c) => { a[c] = 1; return a; }, {});
delete todo['templates/doc.ejs'];
}
Object.keys(todo).filter(k => k.endsWith('meta.json')).forEach(mk => {
delete todo[mk];
Array.from(new Set(Object.keys(flat).map(extract_dir))).filter(k => k.startsWith(mk.split("/")[0])).forEach(k => {
todo[k] = 1;
})
})
console.log("Building", todo);
Object.keys(todo).forEach(buildDoc.bind(null, changed, Object.keys(deleted)));
})
}
function buildDoc(changed, deleted, slide) {
console.log("Building " + slide);
let md_path = "./src/" + slide + "/index.md";
let md = { meta: {} };
let meta_path = "./src/" + slide.split("/")[0] + "/meta.json";
try {
md.meta = fs.readJSONSync(meta_path);
} catch (e) {
//console.log(e);
}
md.meta.build = build_json.doc_build;
//md.meta.lecture = "Web Atelier 2020 ";
md.meta.timestamp = new Date().toLocaleString();
if (fs.existsSync(md_path) && fs.lstatSync(md_path).isFile()) {
renderer._out_folder = `./upload/${slide}/`;
renderer._in_folder = `./src/${slide}/`;
md = loadMD(md_path, md.meta);
md.meta.series = "";
let model = { title: slide, md: md.md, meta: md.meta, pid: slide + "." + build_json.build }
let mdout = `./upload/${slide}/index.html`;
render_one(model, mdout, 'doc');
}
}
fs.ensureDirSync("upload/");
deltaBuild();