@projektemacher/vivliostyle-batch-cli 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -26
- package/dist/index.d.ts +99 -1
- package/dist/vivliostyle-batch-cli.js +990 -46
- package/package.json +23 -19
- package/scripts/convert-docs-pdf.mjs +520 -0
- package/src/metadata.ts +154 -0
- package/src/render/pdf-postprocess.ts +293 -0
- package/src/render/pdf.ts +221 -23
- package/src/render/srgb.icc +0 -0
- package/src/render/xmp.ts +183 -0
- package/src/server.ts +18 -1
- package/src/vivliostyle-cli.ts +324 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@projektemacher/vivliostyle-batch-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A vivliostyle-cli with more options for batch processing",
|
|
6
6
|
"bin": {
|
|
@@ -21,40 +21,43 @@
|
|
|
21
21
|
"site:build": "vite build site"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@vivliostyle/viewer": "^2.
|
|
25
|
-
"commander": "^15.
|
|
26
|
-
"jsdom": "^
|
|
24
|
+
"@vivliostyle/viewer": "^2.46.0",
|
|
25
|
+
"commander": "^15.1.0",
|
|
26
|
+
"jsdom": "^31.0.1",
|
|
27
27
|
"loglevel": "^1.9.2",
|
|
28
28
|
"mime-types": "^3.0.2",
|
|
29
|
+
"mupdf": "^1.28.6",
|
|
29
30
|
"pdf-lib": "^1.17.1",
|
|
30
|
-
"puppeteer-core": "^25.
|
|
31
|
+
"puppeteer-core": "^25.17.0",
|
|
32
|
+
"yargs": "^18.2.0"
|
|
31
33
|
},
|
|
32
34
|
"devDependencies": {
|
|
33
35
|
"@puppeteer/browsers": "^3.2.4",
|
|
34
36
|
"@rollup/plugin-commonjs": "^29.0.3",
|
|
35
37
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
36
|
-
"@rollup/plugin-typescript": "^12.3.
|
|
38
|
+
"@rollup/plugin-typescript": "^12.3.1",
|
|
37
39
|
"@types/express": "^5.0.6",
|
|
38
40
|
"@types/jsdom": "^30.0.0",
|
|
39
41
|
"@types/mime-types": "^3.0.1",
|
|
40
|
-
"@types/node": "^26.6.
|
|
42
|
+
"@types/node": "^26.6.6",
|
|
41
43
|
"@types/serve-static": "^2.2.0",
|
|
42
|
-
"
|
|
44
|
+
"aframe": "^1.8.0",
|
|
45
|
+
"d3": "^7.9.2",
|
|
43
46
|
"echarts": "^6.1.0",
|
|
44
|
-
"eslint": "^10.
|
|
45
|
-
"globals": "^17.
|
|
46
|
-
"marked": "^18.
|
|
47
|
+
"eslint": "^10.15.0",
|
|
48
|
+
"globals": "^17.14.0",
|
|
49
|
+
"marked": "^18.2.0",
|
|
47
50
|
"openseadragon": "^6.1.1",
|
|
48
|
-
"prettier": "^3.
|
|
49
|
-
"rimraf": "^6.1.
|
|
50
|
-
"rollup": "^4.
|
|
51
|
+
"prettier": "^3.10.1",
|
|
52
|
+
"rimraf": "^6.1.4",
|
|
53
|
+
"rollup": "^4.68.3",
|
|
51
54
|
"rollup-plugin-dts": "^6.5.1",
|
|
52
|
-
"three": "^0.
|
|
53
|
-
"tslib": "^2.
|
|
55
|
+
"three": "^0.187.0",
|
|
56
|
+
"tslib": "^2.9.1",
|
|
54
57
|
"typescript": "^6.0.3",
|
|
55
|
-
"typescript-eslint": "^8.
|
|
56
|
-
"vite": "^8.3.
|
|
57
|
-
"vitest": "^5.0
|
|
58
|
+
"typescript-eslint": "^8.72.2",
|
|
59
|
+
"vite": "^8.3.8",
|
|
60
|
+
"vitest": "^5.1.0"
|
|
58
61
|
},
|
|
59
62
|
"license": "MIT",
|
|
60
63
|
"exports": {
|
|
@@ -64,6 +67,7 @@
|
|
|
64
67
|
"files": [
|
|
65
68
|
"dist",
|
|
66
69
|
"src",
|
|
70
|
+
"scripts/convert-docs-pdf.mjs",
|
|
67
71
|
"README.md",
|
|
68
72
|
"LICENSE"
|
|
69
73
|
],
|
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
// scripts/convert-docs-pdf.mjs
|
|
2
|
+
|
|
3
|
+
// Batch-converts a built Hugo site (expects `<path>/post/<slug>/<pattern>`)
|
|
4
|
+
// to PDF using the vivliostyle-batch-cli build. Page selection is driven by
|
|
5
|
+
// per-page properties (content types, embedded media, remote references) so
|
|
6
|
+
// problem pages can be converted and debugged in isolation.
|
|
7
|
+
//
|
|
8
|
+
// Shipped with the npm package: the default CLI path resolves relative to this
|
|
9
|
+
// file, so it works from a repository checkout and an installed package alike.
|
|
10
|
+
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { cpus } from "node:os";
|
|
14
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
import yargs from "yargs";
|
|
17
|
+
import { hideBin } from "yargs/helpers";
|
|
18
|
+
|
|
19
|
+
/** Built CLI next to this script (../dist), valid in checkouts and installs. */
|
|
20
|
+
const DEFAULT_CLI = fileURLToPath(new URL("../dist/vivliostyle-batch-cli.js", import.meta.url));
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Property definitions
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
const ASSET_TAG_RE = /<(link|script|img|iframe|source|video|audio|object|embed|input|a-asset-item|model-viewer)\b[^>]*>/gi;
|
|
27
|
+
const URL_ATTR_RE = /(?:href|src|data|poster)\s*=\s*"([^"]*)"/gi;
|
|
28
|
+
const SRCSET_ATTR_RE = /srcset\s*=\s*"([^"]*)"/gi;
|
|
29
|
+
|
|
30
|
+
/** Properties a page can be selected by. Order is the display order. */
|
|
31
|
+
const PROPERTIES = [
|
|
32
|
+
"redirect",
|
|
33
|
+
"js-content",
|
|
34
|
+
"img",
|
|
35
|
+
"gallery",
|
|
36
|
+
"code",
|
|
37
|
+
"footnote",
|
|
38
|
+
"pdf-link",
|
|
39
|
+
"table",
|
|
40
|
+
"details",
|
|
41
|
+
"math",
|
|
42
|
+
"video",
|
|
43
|
+
"canvas",
|
|
44
|
+
"iframe",
|
|
45
|
+
"script",
|
|
46
|
+
"remote-refs",
|
|
47
|
+
"missing-local-assets"
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Options (yargs: <path> and <pattern> are mandatory positionals)
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
function commaList(value) {
|
|
55
|
+
if (value === undefined || value === null) return null;
|
|
56
|
+
return String(value)
|
|
57
|
+
.split(",")
|
|
58
|
+
.map((entry) => entry.trim())
|
|
59
|
+
.filter(Boolean);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function checkProperties(name, value) {
|
|
63
|
+
if (!value) return;
|
|
64
|
+
for (const prop of value) {
|
|
65
|
+
if (!PROPERTIES.includes(prop)) throw new Error(`Unknown property "${prop}" for ${name}. Known: ${PROPERTIES.join(", ")}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function parseOptions(rawArgv) {
|
|
70
|
+
const parsed = yargs(hideBin(rawArgv))
|
|
71
|
+
.scriptName("convert-docs-pdf")
|
|
72
|
+
.usage("Usage: $0 <path> <pattern> [options]")
|
|
73
|
+
.command("$0 <path> <pattern>", "Batch-convert a built Hugo site to PDF")
|
|
74
|
+
.positional("path", {
|
|
75
|
+
describe: "Site root to convert (expects a post/ directory of article folders)",
|
|
76
|
+
type: "string",
|
|
77
|
+
demandOption: true
|
|
78
|
+
})
|
|
79
|
+
.positional("pattern", { describe: "Page filename inside each post dir", type: "string", demandOption: true })
|
|
80
|
+
.option("out", { describe: "PDF output directory", type: "string", default: "pdf" })
|
|
81
|
+
.option("cli", { describe: "Built CLI entry", type: "string", default: DEFAULT_CLI })
|
|
82
|
+
.option("site-host", { describe: "Host treated as locally available", type: "string", demandOption: true })
|
|
83
|
+
.option("lang", { describe: "Document language passed to the CLI", type: "string" })
|
|
84
|
+
.option("jobs", {
|
|
85
|
+
alias: "j",
|
|
86
|
+
describe: "Parallel conversions",
|
|
87
|
+
type: "number",
|
|
88
|
+
default: Math.max(1, Math.min(4, cpus().length - 1))
|
|
89
|
+
})
|
|
90
|
+
.option("only", { describe: "Convert only pages having at least one of these properties", type: "string", coerce: commaList })
|
|
91
|
+
.option("skip", { describe: "Skip pages having any of these properties", type: "string", coerce: commaList })
|
|
92
|
+
.option("page", { describe: "Convert only these slugs (comma-separated)", type: "string", coerce: commaList })
|
|
93
|
+
.option("list", { describe: "Print slug + property matrix and exit", type: "boolean", default: false })
|
|
94
|
+
.option("limit", { describe: "Convert only the first n selected pages", type: "number" })
|
|
95
|
+
.option("report", { describe: "JSON summary path", type: "string", default: "pdf/report.json" })
|
|
96
|
+
.option("include-redirect", {
|
|
97
|
+
describe: "Also convert meta-refresh redirect stubs (skipped by default)",
|
|
98
|
+
type: "boolean",
|
|
99
|
+
default: false
|
|
100
|
+
})
|
|
101
|
+
.option("force", { describe: "Re-convert pages whose PDF already exists", type: "boolean", default: false })
|
|
102
|
+
.option("dry-run", { describe: "Print the CLI arguments per page without converting", type: "boolean", default: false })
|
|
103
|
+
.option("keep-logs", { describe: "Always write a per-page log, not only on failure", type: "boolean", default: false })
|
|
104
|
+
.option("debug", { describe: "Pass -d to the CLI (implies --jobs 1)", type: "boolean", default: false })
|
|
105
|
+
.option("fetch-missing", {
|
|
106
|
+
describe: "Let Chromium load references without a local file from the web",
|
|
107
|
+
type: "boolean",
|
|
108
|
+
default: false
|
|
109
|
+
})
|
|
110
|
+
.option("wait-for-content", {
|
|
111
|
+
describe: "Delay pagination until the page's async work finished",
|
|
112
|
+
type: "string"
|
|
113
|
+
})
|
|
114
|
+
.option("pixel-ratio", { describe: "Render at n times the output resolution (e.g. 2)", type: "string" })
|
|
115
|
+
.option("timeout", { describe: "Give up on a page after <ms> (Vivliostyle default 300000)", type: "string" })
|
|
116
|
+
.example("$0 site article.html --list", "Print slug + property matrix and exit")
|
|
117
|
+
.example("$0 site article.html --only canvas,iframe --dry-run", "Show what problem pages would convert")
|
|
118
|
+
.example("$0 site article.html -j 4", "Convert with four parallel jobs")
|
|
119
|
+
.example("$0 site article.html --only img --limit 5 --force --keep-logs", "Redo five image pages with logs")
|
|
120
|
+
.epilogue(`Properties: ${PROPERTIES.join(", ")}`)
|
|
121
|
+
.check((argv) => {
|
|
122
|
+
checkProperties("--only", argv.only);
|
|
123
|
+
checkProperties("--skip", argv.skip);
|
|
124
|
+
return true;
|
|
125
|
+
})
|
|
126
|
+
.parserConfiguration({ "populate--": true })
|
|
127
|
+
.strict()
|
|
128
|
+
.locale("en")
|
|
129
|
+
.help()
|
|
130
|
+
.parse();
|
|
131
|
+
|
|
132
|
+
const options = {
|
|
133
|
+
docs: parsed.path,
|
|
134
|
+
out: parsed.out,
|
|
135
|
+
cli: parsed.cli,
|
|
136
|
+
siteHost: parsed.siteHost,
|
|
137
|
+
pageFile: parsed.pattern,
|
|
138
|
+
language: parsed.lang ?? null,
|
|
139
|
+
jobs: Math.max(1, parsed.jobs || 1),
|
|
140
|
+
only: parsed.only,
|
|
141
|
+
skip: parsed.skip,
|
|
142
|
+
pages: parsed.page,
|
|
143
|
+
includeRedirect: parsed.includeRedirect,
|
|
144
|
+
list: parsed.list,
|
|
145
|
+
limit: parsed.limit ?? null,
|
|
146
|
+
force: parsed.force,
|
|
147
|
+
dryRun: parsed.dryRun,
|
|
148
|
+
keepLogs: parsed.keepLogs,
|
|
149
|
+
debug: parsed.debug,
|
|
150
|
+
fetchMissing: parsed.fetchMissing,
|
|
151
|
+
waitForContent: parsed.waitForContent ?? null,
|
|
152
|
+
timeout: parsed.timeout ?? null,
|
|
153
|
+
pixelRatio: parsed.pixelRatio ?? null,
|
|
154
|
+
report: parsed.report,
|
|
155
|
+
extraArgs: parsed["--"] ?? []
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
if (options.debug) options.jobs = 1;
|
|
159
|
+
return options;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ---------------------------------------------------------------------------
|
|
163
|
+
// HTML inspection
|
|
164
|
+
// ---------------------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
function decodeEntities(text) {
|
|
167
|
+
return text
|
|
168
|
+
.replace(/&#(\d+);/g, (_match, code) => String.fromCodePoint(Number.parseInt(code, 10)))
|
|
169
|
+
.replace(/&(amp|lt|gt|quot|apos|hellip|ldquo|rdquo|ndash|mdash);/g, (match, name) => {
|
|
170
|
+
const map = {
|
|
171
|
+
amp: "&",
|
|
172
|
+
lt: "<",
|
|
173
|
+
gt: ">",
|
|
174
|
+
quot: '"',
|
|
175
|
+
apos: "'",
|
|
176
|
+
hellip: "…",
|
|
177
|
+
ldquo: "“",
|
|
178
|
+
rdquo: "”",
|
|
179
|
+
ndash: "–",
|
|
180
|
+
mdash: "—"
|
|
181
|
+
};
|
|
182
|
+
return map[name] ?? match;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function textOf(html) {
|
|
187
|
+
return decodeEntities(
|
|
188
|
+
html
|
|
189
|
+
.replace(/<[^>]*>/g, " ")
|
|
190
|
+
.replace(/\s+/g, " ")
|
|
191
|
+
.trim()
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function firstMatch(html, re) {
|
|
196
|
+
const match = re.exec(html);
|
|
197
|
+
return match ? match[1].trim() : null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Classifies a single reference as "remote", "local" or "missing-local".
|
|
202
|
+
* Relative references resolve against the page directory, absolute ones
|
|
203
|
+
* against the site root.
|
|
204
|
+
*/
|
|
205
|
+
function classifyRef(rawUrl, pageDir, docsRoot, siteHost) {
|
|
206
|
+
const url = rawUrl.trim();
|
|
207
|
+
if (!url) return null;
|
|
208
|
+
if (url.startsWith("#")) return null;
|
|
209
|
+
if (/^(data|javascript|mailto|tel|about):/i.test(url)) return null;
|
|
210
|
+
|
|
211
|
+
if (url.startsWith("//")) {
|
|
212
|
+
const host = url.slice(2).split("/")[0];
|
|
213
|
+
return host === siteHost ? resolveHostRelative(url, pageDir, docsRoot, siteHost) : { kind: "remote", url };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(url)) {
|
|
217
|
+
let parsed;
|
|
218
|
+
try {
|
|
219
|
+
parsed = new URL(url);
|
|
220
|
+
} catch {
|
|
221
|
+
return { kind: "remote", url };
|
|
222
|
+
}
|
|
223
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null;
|
|
224
|
+
if (parsed.host !== siteHost) return { kind: "remote", url };
|
|
225
|
+
const localPath = join(docsRoot, decodeURIComponent(parsed.pathname));
|
|
226
|
+
return existsSync(localPath) ? { kind: "local", url, localPath } : { kind: "missing-local", url, localPath };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const clean = url.split(/[?#]/)[0];
|
|
230
|
+
if (!clean) return null;
|
|
231
|
+
const localPath = clean.startsWith("/") ? join(docsRoot, clean) : resolve(pageDir, clean);
|
|
232
|
+
return existsSync(localPath) ? { kind: "local", url, localPath } : { kind: "missing-local", url, localPath };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function resolveHostRelative(url, pageDir, docsRoot, siteHost) {
|
|
236
|
+
const path = url.slice(url.indexOf(siteHost) + siteHost.length).split(/[?#]/)[0];
|
|
237
|
+
const clean = path.startsWith("/") ? path : `/${path}`;
|
|
238
|
+
const localPath = join(docsRoot, decodeURIComponent(clean));
|
|
239
|
+
return existsSync(localPath) ? { kind: "local", url, localPath } : { kind: "missing-local", url, localPath };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function inspectPage(file, docsRoot, siteHost) {
|
|
243
|
+
const html = readFileSync(file, "utf8");
|
|
244
|
+
const pageDir = dirname(file);
|
|
245
|
+
|
|
246
|
+
const remoteRefs = new Set();
|
|
247
|
+
const missingRefs = new Set();
|
|
248
|
+
let hasScriptTag = false;
|
|
249
|
+
|
|
250
|
+
ASSET_TAG_RE.lastIndex = 0;
|
|
251
|
+
for (const tagMatch of html.matchAll(ASSET_TAG_RE)) {
|
|
252
|
+
const tag = tagMatch[1].toLowerCase();
|
|
253
|
+
const tagSource = tagMatch[0];
|
|
254
|
+
|
|
255
|
+
if (tag === "script") hasScriptTag = true;
|
|
256
|
+
|
|
257
|
+
URL_ATTR_RE.lastIndex = 0;
|
|
258
|
+
const refs = [...tagSource.matchAll(URL_ATTR_RE)].map((m) => m[1]);
|
|
259
|
+
SRCSET_ATTR_RE.lastIndex = 0;
|
|
260
|
+
for (const m of tagSource.matchAll(SRCSET_ATTR_RE)) refs.push(...m[1].split(",").map((c) => c.trim().split(/\s+/)[0]));
|
|
261
|
+
|
|
262
|
+
for (const ref of refs) {
|
|
263
|
+
const classified = classifyRef(ref, pageDir, docsRoot, siteHost);
|
|
264
|
+
if (!classified) continue;
|
|
265
|
+
if (classified.kind === "remote") remoteRefs.add(classified.url);
|
|
266
|
+
if (classified.kind === "missing-local") missingRefs.add(classified.url);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const headingHtml =
|
|
271
|
+
firstMatch(html, /<h1[^>]*class="[^"]*post-title[^"]*"[^>]*>([\s\S]*?)<\/h1>/i) ??
|
|
272
|
+
firstMatch(html, /<title[^>]*>([\s\S]*?)<\/title>/i) ??
|
|
273
|
+
"";
|
|
274
|
+
|
|
275
|
+
const properties = {
|
|
276
|
+
redirect: /http-equiv\s*=\s*"refresh"/i.test(html),
|
|
277
|
+
"js-content": /class="[^"]*\bscript\b[^"]*"|<canvas[\s>]|<a-scene[\s>]|<model-viewer[\s>]/i.test(html),
|
|
278
|
+
img: /<img[\s>]/i.test(html),
|
|
279
|
+
gallery: /class="[^"]*gallery/i.test(html),
|
|
280
|
+
code: /<pre[\s>]|<code[\s>]/i.test(html),
|
|
281
|
+
footnote: /class="footnote"/i.test(html),
|
|
282
|
+
"pdf-link": /href="[^"]*\.pdf(#[^"]*)?"/i.test(html),
|
|
283
|
+
table: /<table[\s>]/i.test(html),
|
|
284
|
+
details: /<details[\s>]/i.test(html),
|
|
285
|
+
math: /katex|MathJax|class="math"/i.test(html),
|
|
286
|
+
video: /<video[\s>]|<audio[\s>]/i.test(html),
|
|
287
|
+
canvas: /<canvas[\s>]|<model-viewer[\s>]/i.test(html),
|
|
288
|
+
iframe: /<iframe[\s>]/i.test(html),
|
|
289
|
+
script: hasScriptTag,
|
|
290
|
+
"remote-refs": remoteRefs.size > 0,
|
|
291
|
+
"missing-local-assets": missingRefs.size > 0
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
return {
|
|
295
|
+
file,
|
|
296
|
+
slug: basename(dirname(file)),
|
|
297
|
+
title: textOf(headingHtml),
|
|
298
|
+
author: firstMatch(html, /<meta\s+name="author"\s+content="([^"]*)"/i),
|
|
299
|
+
properties,
|
|
300
|
+
flags: PROPERTIES.filter((prop) => properties[prop]),
|
|
301
|
+
remoteRefs: [...remoteRefs],
|
|
302
|
+
missingRefs: [...missingRefs]
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// ---------------------------------------------------------------------------
|
|
307
|
+
// Discovery / selection
|
|
308
|
+
// ---------------------------------------------------------------------------
|
|
309
|
+
|
|
310
|
+
function discoverPages(docsRoot, pageFile) {
|
|
311
|
+
const postRoot = join(docsRoot, "post");
|
|
312
|
+
if (!existsSync(postRoot)) throw new Error(`Not a post directory: ${postRoot}`);
|
|
313
|
+
|
|
314
|
+
return readdirSync(postRoot, { withFileTypes: true })
|
|
315
|
+
.filter((entry) => entry.isDirectory())
|
|
316
|
+
.map((entry) => join(postRoot, entry.name, pageFile))
|
|
317
|
+
.filter((file) => existsSync(file))
|
|
318
|
+
.sort();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function selectPages(pages, options) {
|
|
322
|
+
let selected = pages;
|
|
323
|
+
if (options.pages) selected = selected.filter((page) => options.pages.includes(page.slug));
|
|
324
|
+
if (options.only) selected = selected.filter((page) => options.only.some((prop) => page.properties[prop]));
|
|
325
|
+
if (options.skip) selected = selected.filter((page) => !options.skip.some((prop) => page.properties[prop]));
|
|
326
|
+
if (!options.includeRedirect) selected = selected.filter((page) => !page.properties.redirect);
|
|
327
|
+
if (options.limit !== null) selected = selected.slice(0, options.limit);
|
|
328
|
+
return selected;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function listPages(pages) {
|
|
332
|
+
const header = ["slug", ...PROPERTIES].join("\t");
|
|
333
|
+
console.log(header);
|
|
334
|
+
for (const page of pages) {
|
|
335
|
+
const cells = PROPERTIES.map((prop) => (page.properties[prop] ? "x" : "."));
|
|
336
|
+
const missing = page.missingRefs.length > 0 ? ` (${page.missingRefs.length} missing)` : "";
|
|
337
|
+
const remote = page.remoteRefs.length > 0 ? ` (${page.remoteRefs.length} remote)` : "";
|
|
338
|
+
console.log([page.slug, ...cells].join("\t") + missing + remote);
|
|
339
|
+
}
|
|
340
|
+
const counts = PROPERTIES.map((prop) => `${prop}=${pages.filter((page) => page.properties[prop]).length}`);
|
|
341
|
+
console.log(`\n${pages.length} pages — ${counts.join(" ")}`);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// ---------------------------------------------------------------------------
|
|
345
|
+
// Conversion
|
|
346
|
+
// ---------------------------------------------------------------------------
|
|
347
|
+
|
|
348
|
+
function buildArgv(page, options, docsRoot, outFile) {
|
|
349
|
+
const cliPath = resolve(options.cli);
|
|
350
|
+
const siteOrigin = `https://${options.siteHost}`;
|
|
351
|
+
const argv = [cliPath, "-i", page.file, "-o", outFile, "--asset-base", `${siteOrigin}=${docsRoot}`];
|
|
352
|
+
if (options.language) argv.push("--language", options.language);
|
|
353
|
+
if (page.title) argv.push("--title", page.title);
|
|
354
|
+
if (page.author) argv.push("--author", page.author);
|
|
355
|
+
if (options.fetchMissing) argv.push("--fetch-missing");
|
|
356
|
+
if (options.waitForContent !== null) argv.push("--wait-for-content", String(options.waitForContent));
|
|
357
|
+
if (options.pixelRatio !== null) argv.push("--pixel-ratio", String(options.pixelRatio));
|
|
358
|
+
if (options.timeout !== null) argv.push("--timeout", String(options.timeout));
|
|
359
|
+
if (options.debug) argv.push("-d");
|
|
360
|
+
if (options.extraArgs.length > 0) argv.push("--", ...options.extraArgs);
|
|
361
|
+
return argv;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function quoteArgv(argv) {
|
|
365
|
+
return argv.map((arg) => (/[\s"']/.test(arg) ? JSON.stringify(arg) : arg)).join(" ");
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function spawnCli(argv) {
|
|
369
|
+
return new Promise((resolvePromise) => {
|
|
370
|
+
const child = spawn(process.execPath, argv, { stdio: ["ignore", "pipe", "pipe"] });
|
|
371
|
+
let stdout = "";
|
|
372
|
+
let stderr = "";
|
|
373
|
+
child.stdout.on("data", (chunk) => (stdout += chunk));
|
|
374
|
+
child.stderr.on("data", (chunk) => (stderr += chunk));
|
|
375
|
+
child.on("error", (err) => resolvePromise({ code: 1, stdout, stderr: `${stderr}\n${String(err)}` }));
|
|
376
|
+
child.on("close", (code) => resolvePromise({ code: code ?? 1, stdout, stderr }));
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function collectWarnings(output) {
|
|
381
|
+
const warnings = [];
|
|
382
|
+
for (const line of output.split("\n")) {
|
|
383
|
+
const trimmed = line.trim();
|
|
384
|
+
if (/does not exist|\[offline\]|\[fetch\]|\[wait\]/.test(trimmed)) warnings.push(trimmed);
|
|
385
|
+
}
|
|
386
|
+
return warnings;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
async function runPool(items, limit, worker) {
|
|
390
|
+
const results = new Array(items.length);
|
|
391
|
+
let cursor = 0;
|
|
392
|
+
const runners = Array.from({ length: Math.max(1, Math.min(limit, items.length)) }, async () => {
|
|
393
|
+
while (cursor < items.length) {
|
|
394
|
+
const index = cursor++;
|
|
395
|
+
results[index] = await worker(items[index], index);
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
await Promise.all(runners);
|
|
399
|
+
return results;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
async function main() {
|
|
403
|
+
const options = parseOptions(process.argv);
|
|
404
|
+
|
|
405
|
+
const docsRoot = resolve(options.docs);
|
|
406
|
+
const outDir = resolve(options.out);
|
|
407
|
+
const cliPath = resolve(options.cli);
|
|
408
|
+
|
|
409
|
+
const pages = discoverPages(docsRoot, options.pageFile).map((file) => inspectPage(file, docsRoot, options.siteHost));
|
|
410
|
+
const selected = selectPages(pages, options);
|
|
411
|
+
|
|
412
|
+
if (options.list) {
|
|
413
|
+
listPages(selected);
|
|
414
|
+
return 0;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (!existsSync(cliPath) && !options.dryRun) {
|
|
418
|
+
console.error(`Built CLI not found: ${cliPath}\nRun: npm install && npm run build`);
|
|
419
|
+
return 1;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
mkdirSync(outDir, { recursive: true });
|
|
423
|
+
const logDir = join(outDir, "logs");
|
|
424
|
+
|
|
425
|
+
console.log(`${pages.length} pages found, ${selected.length} selected, ${options.jobs} job(s)`);
|
|
426
|
+
if (options.dryRun) {
|
|
427
|
+
for (const page of selected) {
|
|
428
|
+
console.log(`${page.slug}\n ${quoteArgv(buildArgv(page, options, docsRoot, join(outDir, `${page.slug}.pdf`)))}`);
|
|
429
|
+
}
|
|
430
|
+
return 0;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const pending = [];
|
|
434
|
+
let skipped = 0;
|
|
435
|
+
for (const page of selected) {
|
|
436
|
+
const outFile = join(outDir, `${page.slug}.pdf`);
|
|
437
|
+
if (existsSync(outFile) && !options.force) {
|
|
438
|
+
skipped++;
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
pending.push({ page, outFile });
|
|
442
|
+
}
|
|
443
|
+
if (skipped > 0) console.log(`Skipping ${skipped} page(s) with an existing PDF (use --force to redo)`);
|
|
444
|
+
if (pending.length === 0) return 0;
|
|
445
|
+
|
|
446
|
+
const report = {
|
|
447
|
+
startedAt: new Date().toISOString(),
|
|
448
|
+
docsRoot,
|
|
449
|
+
outDir,
|
|
450
|
+
jobs: options.jobs,
|
|
451
|
+
fetchMissing: options.fetchMissing,
|
|
452
|
+
total: pending.length,
|
|
453
|
+
converted: 0,
|
|
454
|
+
failed: 0,
|
|
455
|
+
pages: []
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
let done = 0;
|
|
459
|
+
const results = await runPool(pending, options.jobs, async ({ page, outFile }) => {
|
|
460
|
+
const started = Date.now();
|
|
461
|
+
const argv = buildArgv(page, options, docsRoot, outFile);
|
|
462
|
+
const { code, stdout, stderr } = await spawnCli(argv);
|
|
463
|
+
const seconds = ((Date.now() - started) / 1000).toFixed(1);
|
|
464
|
+
done++;
|
|
465
|
+
|
|
466
|
+
const warnings = collectWarnings(`${stdout}\n${stderr}`);
|
|
467
|
+
const ok = code === 0 && existsSync(outFile);
|
|
468
|
+
if (ok) report.converted++;
|
|
469
|
+
else report.failed++;
|
|
470
|
+
|
|
471
|
+
report.pages.push({
|
|
472
|
+
slug: page.slug,
|
|
473
|
+
file: page.file,
|
|
474
|
+
output: outFile,
|
|
475
|
+
ok,
|
|
476
|
+
exitCode: code,
|
|
477
|
+
seconds: Number(seconds),
|
|
478
|
+
flags: page.flags,
|
|
479
|
+
title: page.title,
|
|
480
|
+
warnings,
|
|
481
|
+
remoteRefs: page.remoteRefs,
|
|
482
|
+
missingRefs: page.missingRefs
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
if (!ok || options.keepLogs) {
|
|
486
|
+
mkdirSync(logDir, { recursive: true });
|
|
487
|
+
writeFileSync(join(logDir, `${page.slug}.log`), `argv: ${quoteArgv(argv)}\n\n${stdout}\n${stderr}`, "utf-8");
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const status = ok ? "ok" : "FAILED";
|
|
491
|
+
const warnNote = warnings.length > 0 ? ` (${warnings.length} warning(s))` : "";
|
|
492
|
+
console.log(`[${done}/${pending.length}] ${status} ${page.slug} ${seconds}s${warnNote}`);
|
|
493
|
+
return { ok };
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
report.finishedAt = new Date().toISOString();
|
|
497
|
+
const reportPath = resolve(options.report);
|
|
498
|
+
mkdirSync(dirname(reportPath), { recursive: true });
|
|
499
|
+
writeFileSync(reportPath, `${JSON.stringify(report, null, 2)}\n`, "utf-8");
|
|
500
|
+
|
|
501
|
+
const failed = results.filter((result) => !result.ok).length;
|
|
502
|
+
console.log(`\n${report.converted} converted, ${failed} failed, ${skipped} skipped — report: ${reportPath}`);
|
|
503
|
+
|
|
504
|
+
const withMissing = report.pages.filter((entry) => entry.missingRefs.length > 0);
|
|
505
|
+
if (withMissing.length > 0) {
|
|
506
|
+
console.log(`\n${withMissing.length} page(s) reference assets missing from ${docsRoot}:`);
|
|
507
|
+
for (const entry of withMissing) console.log(` ${entry.slug}: ${entry.missingRefs.join(", ")}`);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return failed > 0 ? 1 : 0;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
main()
|
|
514
|
+
.then((code) => {
|
|
515
|
+
if (code !== 0) process.exitCode = code;
|
|
516
|
+
})
|
|
517
|
+
.catch((err) => {
|
|
518
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
519
|
+
process.exitCode = 1;
|
|
520
|
+
});
|