-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsert_warning.mjs
More file actions
141 lines (130 loc) · 4.8 KB
/
insert_warning.mjs
File metadata and controls
141 lines (130 loc) · 4.8 KB
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
import fs from 'fs';
import { readFile, rename } from 'fs/promises';
import pathlib from 'path';
import { pipeline } from 'stream/promises';
import { parseArgs } from 'util';
import { JSDOM } from 'jsdom';
import { RewritingStream } from 'parse5-html-rewriting-stream';
import tmp from 'tmp';
const { positionals: cliArgs } = parseArgs({
allowPositionals: true,
options: {},
});
if (cliArgs.length < 3) {
const self = pathlib.relative(process.cwd(), process.argv[1]);
console.error(`Usage: node ${self} <template.html> <data.json> <file.html>...
{{identifier}} substrings in template.html are replaced from data.json, then
the result is inserted into each file.html.`);
process.exit(64);
}
const main = async ([templateFile, dataFile, ...files]) => {
// Substitute data into the template.
const [
template,
data,
] = await Promise.all([
readFile(templateFile, 'utf8'),
readFile(dataFile, 'utf8').then(JSON.parse),
])
const formatErrors = [];
const placeholderPatt = /[{][{](?:([\p{ID_Start}$_][\p{ID_Continue}$]*)[}][}]|.*?(?:[}][}]|(?=[{][{])|$))/gsu;
const resolved = template.replaceAll(placeholderPatt, (m, name, i) => {
if (!name) {
const trunc = m.replace(/([^\n]{29}(?!$)|[^\n]{,29}(?=\n)).*/s, '$1…');
formatErrors.push(new SyntaxError(`bad placeholder at index ${i}: ${trunc}`));
} else if (!Object.hasOwn(data, name)) {
formatErrors.push(new ReferenceError(`no data for ${m}`));
}
return data[name];
});
if (formatErrors.length > 0) {
throw new AggregateError(formatErrors);
}
// Parse the template into DOM nodes for appending to page head (metadata such
// as <style> elements) or prepending to page body (everything else).
const jsdomOpts = { contentType: 'text/html; charset=utf-8' };
const { document } = new JSDOM(resolved, jsdomOpts).window;
const headHTML = document.head.innerHTML;
const bodyHTML = document.body.innerHTML;
// Perform the insertions.
const work = files.map(async (file) => {
await null;
const { name: tmpName, fd, removeCallback } = tmp.fileSync({
tmpdir: pathlib.dirname(file),
prefix: pathlib.basename(file),
postfix: '.tmp',
detachDescriptor: true,
});
try {
// Make a pipeline: fileReader -> inserter -> finisher -> fileWriter
const fileReader = fs.createReadStream(file, 'utf8');
const fileWriter = fs.createWriteStream('', { fd, flush: true });
// Insert headHTML at the end of a possibly implied head, and bodyHTML at
// the beginning of a possibly implied body.
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhtml
let mode = 'before html'; // | 'before head' | 'in head' | 'after head' | '$DONE'
const stayInHead = new Set([
'base',
'basefont',
'bgsound',
'head',
'link',
'meta',
'noframes',
'noscript',
'script',
'style',
'template',
'title',
]);
const inserter = new RewritingStream();
const onEndTag = function (tag) {
if (tag.tagName === 'head') {
this.emitRaw(headHTML);
mode = 'after head';
}
this.emitEndTag(tag);
};
const onStartTag = function (tag) {
const echoTag = () => this.emitStartTag(tag);
if (mode === 'before html' && tag.tagName === 'html') {
mode = 'before head';
} else if (mode !== 'after head' && stayInHead.has(tag.tagName)) {
mode = 'in head';
} else {
if (mode !== 'after head') { this.emitRaw(headHTML); }
// Emit either `${bodyTag}${bodyHTML}` or `${bodyHTML}${otherTag}`.
const emits = [echoTag, () => this.emitRaw(bodyHTML)];
if (tag.tagName !== 'body') { emits.reverse(); }
for (const emit of emits) { emit(); }
mode = '$DONE';
this.off('endTag', onEndTag).off('startTag', onStartTag);
return;
}
echoTag();
};
inserter.on('endTag', onEndTag).on('startTag', onStartTag);
// Ensure headHTML/bodyHTML insertion before EOF.
const finisher = async function* (source) {
for await (const chunk of source) { yield chunk; }
if (mode === '$DONE') { return; }
if (mode !== 'after head') { yield headHTML; }
yield bodyHTML;
};
await pipeline(fileReader, inserter, finisher, fileWriter);
// Now that the temp file is complete, overwrite the source file.
await rename(tmpName, file);
} finally {
removeCallback();
}
});
const results = await Promise.allSettled(work);
const failures = results.filter((result) => result.status !== 'fulfilled');
if (failures.length > 0) {
throw new AggregateError(failures.map((r) => r.reason));
}
};
main(cliArgs).catch((err) => {
console.error(err);
process.exit(1);
});