diff --git a/package.json b/package.json index 98ccf43..c7bbb19 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,9 @@ "devDependencies": { "@tc39/ecma262-biblio": "^2.1.2775", "ecmarkup": "^20.0.0", - "jsdom": "^25.0.1" + "jsdom": "^25.0.1", + "parse5-html-rewriting-stream": "^7.0.0", + "tmp": "^0.2.3" }, "engines": { "node": ">= 12" diff --git a/scripts/insert_warning.mjs b/scripts/insert_warning.mjs index 8bc6ca0..d26d42b 100644 --- a/scripts/insert_warning.mjs +++ b/scripts/insert_warning.mjs @@ -1,7 +1,10 @@ import fs from 'node:fs'; import pathlib from 'node:path'; +import { pipeline } from 'node:stream/promises'; import { parseArgs } from 'node:util'; -import { JSDOM, VirtualConsole } from 'jsdom'; +import { JSDOM } from 'jsdom'; +import { RewritingStream } from 'parse5-html-rewriting-stream'; +import tmp from 'tmp'; const { positionals: cliArgs } = parseArgs({ allowPositionals: true, @@ -12,7 +15,7 @@ if (cliArgs.length < 3) { console.error(`Usage: node ${self} ... {{identifier}} substrings in template.html are replaced from data.json, then -the result is inserted at the start of the body element in each file.html.`); +the result is inserted into each file.html.`); process.exit(64); } @@ -21,58 +24,97 @@ const main = async args => { // Substitute data into the template. const template = fs.readFileSync(templateFile, 'utf8'); - const { default: data } = - await import(pathlib.resolve(dataFile), { with: { type: 'json' } }); + const data = JSON.parse(fs.readFileSync(dataFile, 'utf8')); const formatErrors = []; - const placeholderPatt = /[{][{](?:([\p{ID_Start}$_][\p{ID_Continue}$]*)[}][}]|.*?(?:[}][}]|(?=[{][{])|$))/gsu; + 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(Error(`bad placeholder at index ${i}: ${trunc}`)); + formatErrors.push(SyntaxError(`bad placeholder at index ${i}: ${trunc}`)); } else if (!Object.hasOwn(data, name)) { - formatErrors.push(Error(`no data for ${m}`)); + formatErrors.push(ReferenceError(`no data for ${m}`)); } return data[name]; }); if (formatErrors.length > 0) throw AggregateError(formatErrors); - // Parse the template into DOM nodes for appending to page s (metadata - // such as