gutterpress 0.11.17-alpha.2 → 0.11.17-alpha.3

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.
Files changed (49) hide show
  1. package/dist/api/index.d.ts +4 -0
  2. package/dist/api/index.js +10 -2
  3. package/dist/{build-1p999cmg.js → build-394c1ee1.js} +4 -4
  4. package/dist/checks/pdf/index.d.ts +1 -0
  5. package/dist/checks/pdf/signature.d.ts +3 -0
  6. package/dist/{cli-70xgehq2.js → cli-3p1e95zc.js} +1 -1
  7. package/dist/{cli-6z5zthzt.js → cli-9nba5ve6.js} +562 -211
  8. package/dist/{cli-ppc0vz4b.js → cli-d57bsfpb.js} +1 -1
  9. package/dist/{cli-gymfg04q.js → cli-jyy9pqf3.js} +15 -10
  10. package/dist/cli.js +12 -12
  11. package/dist/{doctor-reqpnn3h.js → doctor-c459qv5j.js} +2 -2
  12. package/dist/engine/compiler/build.d.ts +3 -2
  13. package/dist/engine/compiler/postprocess.d.ts +6 -4
  14. package/dist/engine/shared/synthesis.d.ts +29 -5
  15. package/dist/{engine-5jzhjjvv.js → engine-3xqgm82d.js} +2 -2
  16. package/dist/{engine-078ggczp.js → engine-zdb41ay1.js} +1 -1
  17. package/dist/{ext-dsbfejt3.js → ext-ha7gg8h9.js} +4 -4
  18. package/dist/{gutterpress-viewer-zkv2stfy.js → gutterpress-viewer-1n793gwd.js} +5 -3
  19. package/dist/{index-c275p061.js → index-bscfmg5r.js} +1714 -1054
  20. package/dist/{index-5dcj1vts.js → index-hrx0gfkj.js} +23 -684
  21. package/dist/index-q1xqxrh9.js +949 -0
  22. package/dist/index.js +11 -3
  23. package/dist/lib/black-text-gray.d.ts +38 -8
  24. package/dist/lib/build-fingerprint.d.ts +2 -0
  25. package/dist/lib/engine.d.ts +2 -1
  26. package/dist/lib/lint-rules.d.ts +9 -0
  27. package/dist/lib/markdown/assemble.d.ts +3 -5
  28. package/dist/lib/markdown/index.d.ts +3 -8
  29. package/dist/lib/markdown/renderer.d.ts +9 -1
  30. package/dist/lib/page-size.d.ts +66 -0
  31. package/dist/lib/pdf-inspect.d.ts +6 -0
  32. package/dist/lib/printsafe.d.ts +8 -1
  33. package/dist/lib/project-scaffold.d.ts +3 -9
  34. package/dist/lib/remote-auth/github-repos.d.ts +7 -0
  35. package/dist/lib/remote-auth/online-backup.d.ts +51 -0
  36. package/dist/lib/snippets.d.ts +3 -0
  37. package/dist/{manifest.schema-40sfq49z.json → manifest.schema-gh0jjve9.json} +14 -2
  38. package/dist/{new-m5y2wepr.js → new-kky3z7kv.js} +4 -4
  39. package/dist/{plugin.js-m4a7m3t4.tpl → plugin.js-cswq7wvd.tpl} +4 -1
  40. package/dist/{plugin.test.js-6n8ct5e3.tpl → plugin.test.js-xgxrd8hf.tpl} +4 -1
  41. package/dist/{preflight-bwpqwwny.js → preflight-t8v6szq6.js} +4 -4
  42. package/dist/preview/file-watcher.d.ts +2 -4
  43. package/dist/{preview-62y7qhkp.js → preview-cxab4y5x.js} +4 -4
  44. package/dist/{publish-k2zyt3x6.js → publish-s2mtenb5.js} +4 -4
  45. package/dist/render.js +30 -5
  46. package/dist/schema/manifest.types.d.ts +17 -0
  47. package/dist/{validate-fgwwxtbn.js → validate-xaajdw4x.js} +4 -4
  48. package/package.json +1 -1
  49. package/dist/index-eyaj6nkc.js +0 -267
@@ -0,0 +1,949 @@
1
+ // src/lib/build-error.ts
2
+ var EXIT_CODES = {
3
+ OK: 0,
4
+ FINDINGS: 1,
5
+ USAGE: 2,
6
+ PIPELINE: 3
7
+ };
8
+
9
+ class BuildError extends Error {
10
+ exitCode;
11
+ constructor(message, exitCode = EXIT_CODES.PIPELINE, options) {
12
+ super(message, options);
13
+ this.name = "BuildError";
14
+ this.exitCode = exitCode;
15
+ }
16
+ }
17
+
18
+ // src/engine/shared/synthesis.ts
19
+ var RECTO_VERSO_VALUES = /^(right|recto|left|verso)$/;
20
+ function isRectoVersoBreak(decl) {
21
+ return decl.prop === "break-before" && RECTO_VERSO_VALUES.test(decl.value.trim());
22
+ }
23
+ function wantsRecto(value) {
24
+ return /^(right|recto)$/.test(value.trim());
25
+ }
26
+ function planRectoBlanks(sites) {
27
+ let shift = 0;
28
+ return sites.map((site) => {
29
+ if (site.page <= 0)
30
+ return false;
31
+ const effective = site.page + shift;
32
+ const onRecto = effective % 2 === 1;
33
+ const wrong = site.wantsRecto ? !onRecto : onRecto;
34
+ if (wrong)
35
+ shift++;
36
+ return wrong;
37
+ });
38
+ }
39
+ function lastPageMode(print) {
40
+ return print.lastPage ?? (print.reserveLastPage ? "blank" : undefined);
41
+ }
42
+ function signaturePadding(pageCount, signature = 1, lastPage) {
43
+ const size = signature > 1 ? signature : 1;
44
+ if (lastPage === "short-by-one")
45
+ return (size - (pageCount + 1) % size) % size;
46
+ const padded = (size - pageCount % size) % size;
47
+ return padded === 0 && lastPage === "blank" ? size : padded;
48
+ }
49
+ var WHICH_VALUES = new Set(["first", "start", "last", "first-except"]);
50
+ function parseWhich(raw) {
51
+ const w = (raw ?? "").trim();
52
+ return WHICH_VALUES.has(w) ? w : "first";
53
+ }
54
+ function stringValueAt(entries, page, which = "first") {
55
+ let entry = "";
56
+ const onPage = [];
57
+ for (const e of entries) {
58
+ if (e.page < page)
59
+ entry = e.value;
60
+ else if (e.page === page)
61
+ onPage.push(e.value);
62
+ else
63
+ break;
64
+ }
65
+ switch (which) {
66
+ case "start":
67
+ return entry;
68
+ case "last":
69
+ return onPage.length ? onPage[onPage.length - 1] : entry;
70
+ case "first-except":
71
+ return onPage.length ? "" : entry;
72
+ case "first":
73
+ return onPage.length ? onPage[0] : entry;
74
+ }
75
+ }
76
+ function stringSymbols(entries, pageCount, which = "first") {
77
+ const symbols = [];
78
+ for (let p = 1;p <= pageCount; p++)
79
+ symbols.push(stringValueAt(entries, p, which));
80
+ return symbols;
81
+ }
82
+ function cssQuote(s) {
83
+ return `"${s.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
84
+ }
85
+ function counterStyleName(name, which) {
86
+ return which === "first" ? `gp-${name}` : `gp-${name}--${which}`;
87
+ }
88
+ function pageCounterValues(resets, pageCount) {
89
+ const byPage = new Map;
90
+ for (const r of resets)
91
+ if (r.page >= 1)
92
+ byPage.set(r.page, r.start);
93
+ const values = [];
94
+ let value = 0;
95
+ for (let p = 1;p <= pageCount; p++) {
96
+ if (byPage.has(p))
97
+ value = byPage.get(p) - 1;
98
+ value++;
99
+ values.push(value);
100
+ }
101
+ return values;
102
+ }
103
+ function restartedPageValues(resetSites, pageMap, pageCount) {
104
+ const resets = resetSites.map((s) => ({ page: pageMap[s.id] ?? 0, start: s.start })).filter((r) => r.page > 0);
105
+ return resets.length ? pageCounterValues(resets, pageCount) : null;
106
+ }
107
+ function toFolioPage(physicalPage, pageValues) {
108
+ if (!pageValues)
109
+ return physicalPage;
110
+ return pageValues[physicalPage - 1] ?? physicalPage;
111
+ }
112
+ var LEADER_START = "";
113
+ var LEADER_END = "";
114
+ function leaderMarker(glue) {
115
+ return `${LEADER_START}${glue}${LEADER_END}`;
116
+ }
117
+ function generatedContentCss(selectors) {
118
+ const rules = new Set;
119
+ for (const raw of selectors) {
120
+ for (const one of raw.split(",")) {
121
+ const selector = one.trim();
122
+ if (!selector)
123
+ continue;
124
+ const m = /^(.*?)(::?)(after|before)\s*$/i.exec(selector);
125
+ if (!m)
126
+ continue;
127
+ const [, base, colons, pseudo] = m;
128
+ if (base === undefined || colons === undefined || pseudo === undefined)
129
+ continue;
130
+ const where = pseudo.toLowerCase();
131
+ rules.add(`${base.trim()}[data-gp-${where}]${colons}${where} { content: attr(data-gp-${where}); }`);
132
+ }
133
+ }
134
+ rules.add(`[data-gp-after]::after { content: attr(data-gp-after); }`);
135
+ rules.add(`[data-gp-before]::before { content: attr(data-gp-before); }`);
136
+ return [...rules].join(`
137
+ `);
138
+ }
139
+
140
+ // src/lib/embedded-assets.ts
141
+ import { existsSync } from "node:fs";
142
+ import { mkdtemp, mkdir, writeFile, readFile } from "node:fs/promises";
143
+ import { tmpdir } from "node:os";
144
+ import { dirname, join, resolve } from "node:path";
145
+ import { fileURLToPath } from "node:url";
146
+
147
+ // src/assets/favicon.ico
148
+ var favicon_default = "./favicon-wkbm9cjn.ico";
149
+
150
+ // src/assets/manifest.schema.json
151
+ var manifest_schema_default = "./manifest.schema-gh0jjve9.json";
152
+
153
+ // src/assets/preview/scripts/preview-interface.js
154
+ var preview_interface_default = "./preview-interface-c44qk17f.js";
155
+
156
+ // src/assets/preview/scripts/preview-bridge.js
157
+ var preview_bridge_default = "./preview-bridge-8jhd1wpb.js";
158
+
159
+ // src/assets/preview/scripts/preview-shell.js
160
+ var preview_shell_default = "./preview-shell-xwn84kqr.js";
161
+
162
+ // profiles/CGATS21_CRPC1.icc
163
+ var CGATS21_CRPC1_default = "./CGATS21_CRPC1-g0e3k7kr.icc";
164
+
165
+ // src/assets/engine/gutterpress-viewer.js
166
+ var gutterpress_viewer_default = "./gutterpress-viewer-1n793gwd.js";
167
+
168
+ // src/assets/engine/gutterpress-agent.js
169
+ var gutterpress_agent_default = "./gutterpress-agent-cazqstr1.js";
170
+
171
+ // src/assets/templates/book/manifest.yaml
172
+ var manifest_default = "./manifest-n1gh3qw5.yaml";
173
+
174
+ // src/assets/templates/book/chapter-01.md
175
+ var chapter_01_default = "./chapter-01-rkpyvjna.md";
176
+
177
+ // src/assets/templates/zine/manifest.yaml
178
+ var manifest_default2 = "./manifest-n1gh3qw5.yaml";
179
+
180
+ // src/assets/templates/zine/chapter-01.md
181
+ var chapter_01_default2 = "./chapter-01-7jwqg1ct.md";
182
+
183
+ // src/assets/templates/technical/manifest.yaml
184
+ var manifest_default3 = "./manifest-n1gh3qw5.yaml";
185
+
186
+ // src/assets/templates/technical/chapter-01.md
187
+ var chapter_01_default3 = "./chapter-01-6fn9pvd0.md";
188
+
189
+ // src/assets/extension-templates/plugin/package.json
190
+ var package_default = "./package-9rv69pxm.json";
191
+
192
+ // src/assets/extension-templates/plugin/README.md
193
+ var README_default = "./README-vsnsc1b3.md";
194
+
195
+ // src/assets/extension-templates/plugin/plugin.js.tpl
196
+ var plugin_js_default = "./plugin.js-cswq7wvd.tpl";
197
+
198
+ // src/assets/extension-templates/plugin/styles/plugin.css
199
+ var plugin_default = "./plugin-spbcem4j.css";
200
+
201
+ // src/assets/extension-templates/plugin/snippets/term-box.md
202
+ var term_box_default = "./term-box-4y0w7y32.md";
203
+
204
+ // src/assets/extension-templates/plugin/test/fixture.md
205
+ var fixture_default = "./fixture-2qnyqgb5.md";
206
+
207
+ // src/assets/extension-templates/plugin/test/expected.html
208
+ var expected_default = "./expected-ycmacem8.html";
209
+
210
+ // src/assets/extension-templates/plugin/test/plugin.test.js.tpl
211
+ var plugin_test_js_default = "./plugin.test.js-xgxrd8hf.tpl";
212
+
213
+ // src/assets/extension-templates/theme/package.json
214
+ var package_default2 = "./package-aenwjew3.json";
215
+
216
+ // src/assets/extension-templates/theme/README.md
217
+ var README_default2 = "./README-3nj919rm.md";
218
+
219
+ // src/assets/extension-templates/theme/components.yaml
220
+ var components_default = "./components-dhgv2sm7.yaml";
221
+
222
+ // src/assets/extension-templates/theme/snippets/callout.md
223
+ var callout_default = "./callout-hv56m16h.md";
224
+
225
+ // src/assets/extension-templates/theme/styles/tokens.css
226
+ var tokens_default = "./tokens-7984461t.css";
227
+
228
+ // src/assets/extension-templates/theme/styles/base.css
229
+ var base_default = "./base-ypqh9n7f.css";
230
+
231
+ // src/assets/extension-templates/theme/styles/components.css
232
+ var components_default2 = "./components-aam0fe2z.css";
233
+
234
+ // src/assets/extension-templates/theme/styles/page-templates.css
235
+ var page_templates_default = "./page-templates-7md4ev7d.css";
236
+
237
+ // src/assets/extension-templates/theme/styles/page-rules.css
238
+ var page_rules_default = "./page-rules-nsj3aw42.css";
239
+
240
+ // src/assets/extension-templates/theme/styles/book.css
241
+ var book_default = "./book-ted3tzrp.css";
242
+
243
+ // src/assets/themes/clean-book/theme.css
244
+ var theme_default = "./theme-j2bagrfx.css";
245
+
246
+ // src/assets/themes/clean-book/package.json
247
+ var package_default3 = "./package-97gm31sg.json";
248
+
249
+ // src/assets/themes/zine/theme.css
250
+ var theme_default2 = "./theme-h5recz6c.css";
251
+
252
+ // src/assets/themes/zine/package.json
253
+ var package_default4 = "./package-gag6ykwn.json";
254
+
255
+ // src/assets/themes/technical-doc/theme.css
256
+ var theme_default3 = "./theme-nn6d53zy.css";
257
+
258
+ // src/assets/themes/technical-doc/package.json
259
+ var package_default5 = "./package-nrhkwf2m.json";
260
+
261
+ // src/lib/embedded-assets.ts
262
+ var __libdir = dirname(fileURLToPath(import.meta.url));
263
+ var abs = (p) => resolve(__libdir, p);
264
+ var filePath = (v) => v;
265
+ var EMBEDDED_ASSETS = {
266
+ "favicon.ico": abs(favicon_default),
267
+ "manifest.schema.json": abs(filePath(manifest_schema_default)),
268
+ "preview/scripts/preview-interface.js": abs(filePath(preview_interface_default)),
269
+ "preview/scripts/preview-bridge.js": abs(filePath(preview_bridge_default)),
270
+ "preview/scripts/preview-shell.js": abs(filePath(preview_shell_default)),
271
+ "profiles/CGATS21_CRPC1.icc": abs(filePath(CGATS21_CRPC1_default)),
272
+ "engine/gutterpress-viewer.js": abs(filePath(gutterpress_viewer_default)),
273
+ "engine/gutterpress-agent.js": abs(filePath(gutterpress_agent_default)),
274
+ "templates/book/manifest.yaml": abs(filePath(manifest_default)),
275
+ "templates/book/chapter-01.md": abs(filePath(chapter_01_default)),
276
+ "templates/zine/manifest.yaml": abs(filePath(manifest_default2)),
277
+ "templates/zine/chapter-01.md": abs(filePath(chapter_01_default2)),
278
+ "templates/technical/manifest.yaml": abs(filePath(manifest_default3)),
279
+ "templates/technical/chapter-01.md": abs(filePath(chapter_01_default3)),
280
+ "extension-templates/plugin/package.json": abs(filePath(package_default)),
281
+ "extension-templates/plugin/README.md": abs(filePath(README_default)),
282
+ "extension-templates/plugin/plugin.js.tpl": abs(filePath(plugin_js_default)),
283
+ "extension-templates/plugin/styles/plugin.css": abs(filePath(plugin_default)),
284
+ "extension-templates/plugin/snippets/term-box.md": abs(filePath(term_box_default)),
285
+ "extension-templates/plugin/test/fixture.md": abs(filePath(fixture_default)),
286
+ "extension-templates/plugin/test/expected.html": abs(filePath(expected_default)),
287
+ "extension-templates/plugin/test/plugin.test.js.tpl": abs(filePath(plugin_test_js_default)),
288
+ "extension-templates/theme/package.json": abs(filePath(package_default2)),
289
+ "extension-templates/theme/README.md": abs(filePath(README_default2)),
290
+ "extension-templates/theme/components.yaml": abs(filePath(components_default)),
291
+ "extension-templates/theme/snippets/callout.md": abs(filePath(callout_default)),
292
+ "extension-templates/theme/styles/tokens.css": abs(filePath(tokens_default)),
293
+ "extension-templates/theme/styles/base.css": abs(filePath(base_default)),
294
+ "extension-templates/theme/styles/components.css": abs(filePath(components_default2)),
295
+ "extension-templates/theme/styles/page-templates.css": abs(filePath(page_templates_default)),
296
+ "extension-templates/theme/styles/page-rules.css": abs(filePath(page_rules_default)),
297
+ "extension-templates/theme/styles/book.css": abs(filePath(book_default)),
298
+ "themes/clean-book/theme.css": abs(filePath(theme_default)),
299
+ "themes/clean-book/package.json": abs(filePath(package_default3)),
300
+ "themes/zine/theme.css": abs(filePath(theme_default2)),
301
+ "themes/zine/package.json": abs(filePath(package_default4)),
302
+ "themes/technical-doc/theme.css": abs(filePath(theme_default3)),
303
+ "themes/technical-doc/package.json": abs(filePath(package_default5))
304
+ };
305
+ var extractPromise = null;
306
+ async function extractAssets() {
307
+ const root = await mkdtemp(join(tmpdir(), "gutterpress-assets-"));
308
+ for (const [relPath, srcPath] of Object.entries(EMBEDDED_ASSETS)) {
309
+ const dest = join(root, relPath);
310
+ await mkdir(dirname(dest), { recursive: true });
311
+ const bytes = await readFile(srcPath);
312
+ await writeFile(dest, bytes);
313
+ }
314
+ return root;
315
+ }
316
+ var SENTINEL_ASSET = "engine/gutterpress-viewer.js";
317
+ async function getAssetsDir() {
318
+ if (extractPromise) {
319
+ try {
320
+ const root = await extractPromise;
321
+ if (existsSync(join(root, SENTINEL_ASSET))) {
322
+ return root;
323
+ }
324
+ } catch {}
325
+ extractPromise = null;
326
+ }
327
+ extractPromise = extractAssets();
328
+ return extractPromise;
329
+ }
330
+ async function getAssetPath(relPath) {
331
+ const root = await getAssetsDir();
332
+ return join(root, relPath);
333
+ }
334
+
335
+ // src/engine/shared/margin-box-support.ts
336
+ var MARGIN_BOX_IGNORED_PROPERTIES = new Set([
337
+ "transform",
338
+ "rotate",
339
+ "translate",
340
+ "scale",
341
+ "perspective",
342
+ "box-shadow",
343
+ "opacity",
344
+ "outline",
345
+ "outline-color",
346
+ "outline-style",
347
+ "outline-width",
348
+ "outline-offset",
349
+ "filter",
350
+ "backdrop-filter",
351
+ "mix-blend-mode",
352
+ "clip-path"
353
+ ]);
354
+ function isIgnoredMarginBoxProperty(property) {
355
+ return MARGIN_BOX_IGNORED_PROPERTIES.has(property.toLowerCase());
356
+ }
357
+ function marginBoxRectPt(name, g) {
358
+ const { top, right, bottom, left } = g.margin;
359
+ const cw = g.width - left - right;
360
+ const ch = g.height - top - bottom;
361
+ const third = (n) => n / 3;
362
+ const T = {
363
+ "top-left-corner": [0, 0, left, top],
364
+ "top-left": [left, 0, third(cw), top],
365
+ "top-center": [left + third(cw), 0, third(cw), top],
366
+ "top-right": [left + 2 * third(cw), 0, third(cw), top],
367
+ "top-right-corner": [g.width - right, 0, right, top],
368
+ "bottom-left-corner": [0, g.height - bottom, left, bottom],
369
+ "bottom-left": [left, g.height - bottom, third(cw), bottom],
370
+ "bottom-center": [left + third(cw), g.height - bottom, third(cw), bottom],
371
+ "bottom-right": [left + 2 * third(cw), g.height - bottom, third(cw), bottom],
372
+ "bottom-right-corner": [g.width - right, g.height - bottom, right, bottom],
373
+ "left-top": [0, top, left, third(ch)],
374
+ "left-middle": [0, top + third(ch), left, third(ch)],
375
+ "left-bottom": [0, top + 2 * third(ch), left, third(ch)],
376
+ "right-top": [g.width - right, top, right, third(ch)],
377
+ "right-middle": [g.width - right, top + third(ch), right, third(ch)],
378
+ "right-bottom": [g.width - right, top + 2 * third(ch), right, third(ch)]
379
+ };
380
+ const [x, y, w, h] = T[name] ?? [0, 0, 0, 0];
381
+ return { x, y, w, h };
382
+ }
383
+ function marginBoxAlign(name) {
384
+ if (name.includes("center") || name.includes("middle"))
385
+ return "center";
386
+ return /right/.test(name) ? "end" : "start";
387
+ }
388
+
389
+ // src/engine/shared/gcpm-extract.ts
390
+ var NESTED_AT_RULES = /^@(media|supports|layer|scope|container|document)\b/i;
391
+ function scanRules(css) {
392
+ const out = [];
393
+ let i = 0;
394
+ let start = 0;
395
+ let depth = 0;
396
+ let bodyStart = -1;
397
+ while (i < css.length) {
398
+ const c = css[i];
399
+ if (c === "/" && css[i + 1] === "*") {
400
+ const end = css.indexOf("*/", i + 2);
401
+ i = end === -1 ? css.length : end + 2;
402
+ continue;
403
+ }
404
+ if (c === '"' || c === "'") {
405
+ i = skipString(css, i);
406
+ continue;
407
+ }
408
+ if (c === "{") {
409
+ if (depth === 0)
410
+ bodyStart = i;
411
+ depth++;
412
+ i++;
413
+ continue;
414
+ }
415
+ if (c === "}") {
416
+ depth--;
417
+ if (depth === 0) {
418
+ out.push({
419
+ prelude: stripComments(css.slice(start, bodyStart)).trim(),
420
+ body: css.slice(bodyStart + 1, i)
421
+ });
422
+ start = i + 1;
423
+ }
424
+ i++;
425
+ continue;
426
+ }
427
+ if (c === ";" && depth === 0) {
428
+ const stmt = stripComments(css.slice(start, i)).trim();
429
+ if (stmt)
430
+ out.push({ statement: stmt });
431
+ start = i + 1;
432
+ i++;
433
+ continue;
434
+ }
435
+ i++;
436
+ }
437
+ return out;
438
+ }
439
+ function stripComments(s) {
440
+ let out = "";
441
+ let i = 0;
442
+ while (i < s.length) {
443
+ if (s[i] === "/" && s[i + 1] === "*") {
444
+ const end = s.indexOf("*/", i + 2);
445
+ i = end === -1 ? s.length : end + 2;
446
+ continue;
447
+ }
448
+ if (s[i] === '"' || s[i] === "'") {
449
+ const end = skipString(s, i);
450
+ out += s.slice(i, end);
451
+ i = end;
452
+ continue;
453
+ }
454
+ out += s[i++];
455
+ }
456
+ return out;
457
+ }
458
+ function skipString(css, i) {
459
+ const quote = css[i];
460
+ i++;
461
+ while (i < css.length) {
462
+ if (css[i] === "\\")
463
+ i += 2;
464
+ else if (css[i] === quote)
465
+ return i + 1;
466
+ else
467
+ i++;
468
+ }
469
+ return i;
470
+ }
471
+ function parseDeclarations(body) {
472
+ const decls = {};
473
+ let i = 0;
474
+ let start = 0;
475
+ let depth = 0;
476
+ const push = (chunk) => {
477
+ const s = stripComments(chunk).trim();
478
+ if (!s)
479
+ return;
480
+ const colon = indexOfTopLevel(s, ":");
481
+ if (colon <= 0)
482
+ return;
483
+ const rawProp = s.slice(0, colon).trim();
484
+ const prop = rawProp.startsWith("--") ? rawProp : rawProp.toLowerCase();
485
+ const value = s.slice(colon + 1).trim().replace(/\s*!important$/i, "");
486
+ if (prop)
487
+ decls[prop] = value;
488
+ };
489
+ while (i < body.length) {
490
+ const c = body[i];
491
+ if (c === "/" && body[i + 1] === "*") {
492
+ const end = body.indexOf("*/", i + 2);
493
+ i = end === -1 ? body.length : end + 2;
494
+ continue;
495
+ }
496
+ if (c === '"' || c === "'") {
497
+ i = skipString(body, i);
498
+ continue;
499
+ }
500
+ if (c === "(" || c === "{" || c === "[")
501
+ depth++;
502
+ else if (c === ")" || c === "}" || c === "]")
503
+ depth--;
504
+ else if (c === ";" && depth === 0) {
505
+ push(body.slice(start, i));
506
+ start = i + 1;
507
+ }
508
+ i++;
509
+ }
510
+ push(body.slice(start));
511
+ return decls;
512
+ }
513
+ function indexOfTopLevel(s, ch) {
514
+ let depth = 0;
515
+ for (let i = 0;i < s.length; i++) {
516
+ const c = s[i];
517
+ if (c === '"' || c === "'") {
518
+ i = skipString(s, i) - 1;
519
+ continue;
520
+ }
521
+ if (c === "(")
522
+ depth++;
523
+ else if (c === ")")
524
+ depth--;
525
+ else if (c === ch && depth === 0)
526
+ return i;
527
+ }
528
+ return -1;
529
+ }
530
+ function extract(css) {
531
+ const model = {
532
+ pageRules: [],
533
+ stringSets: [],
534
+ pageAssignments: [],
535
+ breaks: [],
536
+ xrefs: [],
537
+ counterResets: [],
538
+ pageNames: [],
539
+ warnings: []
540
+ };
541
+ walk(css, model);
542
+ resolveGeometryVars(model, collectRootCustomProperties(css));
543
+ const names = new Set;
544
+ for (const r of model.pageRules)
545
+ if (r.name)
546
+ names.add(r.name);
547
+ for (const a of model.pageAssignments)
548
+ names.add(a.page);
549
+ model.pageNames = [...names];
550
+ return model;
551
+ }
552
+ var GEOMETRY_PROPS = [
553
+ "size",
554
+ "margin",
555
+ "margin-top",
556
+ "margin-right",
557
+ "margin-bottom",
558
+ "margin-left",
559
+ "bleed",
560
+ "marks"
561
+ ];
562
+ function collectRootCustomProperties(css) {
563
+ const props = new Map;
564
+ const walkForRoot = (body) => {
565
+ for (const rule of scanRules(body)) {
566
+ if ("statement" in rule)
567
+ continue;
568
+ const { prelude, body: ruleBody } = rule;
569
+ if (NESTED_AT_RULES.test(prelude)) {
570
+ walkForRoot(ruleBody);
571
+ continue;
572
+ }
573
+ if (!prelude.startsWith("@")) {
574
+ const selectors = splitTopLevel(prelude, ",");
575
+ if (selectors.some((s) => s.trim() === ":root")) {
576
+ const decls = parseDeclarations(ruleBody);
577
+ for (const [k, v] of Object.entries(decls)) {
578
+ if (k.startsWith("--"))
579
+ props.set(k, v);
580
+ }
581
+ }
582
+ }
583
+ }
584
+ };
585
+ walkForRoot(css);
586
+ return props;
587
+ }
588
+ function matchParen(s, open) {
589
+ let depth = 0;
590
+ let i = open;
591
+ while (i < s.length) {
592
+ const c = s[i];
593
+ if (c === '"' || c === "'") {
594
+ i = skipString(s, i);
595
+ continue;
596
+ }
597
+ if (c === "(")
598
+ depth++;
599
+ else if (c === ")") {
600
+ depth--;
601
+ if (depth === 0)
602
+ return i;
603
+ }
604
+ i++;
605
+ }
606
+ return -1;
607
+ }
608
+ function resolveVarsInValue(value, customProps, stack = new Set) {
609
+ let out = "";
610
+ let i = 0;
611
+ while (i < value.length) {
612
+ const isVarStart = /^var\(/i.test(value.slice(i, i + 4)) && (i === 0 || !/[\w-]/.test(value[i - 1]));
613
+ if (!isVarStart) {
614
+ out += value[i];
615
+ i++;
616
+ continue;
617
+ }
618
+ const open = i + 3;
619
+ const close = matchParen(value, open);
620
+ if (close === -1) {
621
+ out += value.slice(i);
622
+ break;
623
+ }
624
+ const inner = value.slice(open + 1, close);
625
+ const commaIdx = indexOfTopLevel(inner, ",");
626
+ const name = (commaIdx === -1 ? inner : inner.slice(0, commaIdx)).trim();
627
+ const fallback = commaIdx === -1 ? undefined : inner.slice(commaIdx + 1).trim();
628
+ if (!/^--[\w-]+$/.test(name)) {
629
+ return { text: out, unresolved: `var(${inner}) is not a valid custom property reference` };
630
+ }
631
+ if (stack.has(name)) {
632
+ return { text: out, unresolved: `var(${name}) is circular` };
633
+ }
634
+ const rootValue = customProps.get(name);
635
+ if (rootValue !== undefined) {
636
+ const nested = resolveVarsInValue(rootValue, customProps, new Set(stack).add(name));
637
+ if (nested.unresolved)
638
+ return { text: out, unresolved: nested.unresolved };
639
+ out += nested.text;
640
+ } else if (fallback !== undefined) {
641
+ if (/\bvar\(/i.test(fallback)) {
642
+ return {
643
+ text: out,
644
+ unresolved: `var(${name}, ${fallback}) — a fallback containing another var() is not resolved`
645
+ };
646
+ }
647
+ out += fallback;
648
+ } else {
649
+ return {
650
+ text: out,
651
+ unresolved: `${name} is not defined at :root and var() has no fallback`
652
+ };
653
+ }
654
+ i = close + 1;
655
+ }
656
+ return { text: out };
657
+ }
658
+ function resolveGeometryVars(model, customProps) {
659
+ for (const rule of model.pageRules) {
660
+ for (const prop of GEOMETRY_PROPS) {
661
+ const value = rule.decls[prop];
662
+ if (!value || !/\bvar\(/i.test(value))
663
+ continue;
664
+ const { text, unresolved } = resolveVarsInValue(value, customProps);
665
+ if (unresolved) {
666
+ throw new Error(`${rule.raw} { ${prop}: ${value} } — cannot resolve ${unresolved}. ` + `Define the custom property at :root, add a literal var(--x, fallback) fallback, or use a literal value.`);
667
+ }
668
+ const unparsable = unparsableGeometry(prop, text);
669
+ if (unparsable) {
670
+ throw new Error(`${rule.raw} { ${prop}: ${value} } — resolves to \`${text.trim()}\`, ${unparsable}. ` + `Use a value this engine can read (a length like 0.75in/12pt/10mm, or a named page size for \`size\`).`);
671
+ }
672
+ rule.decls[prop] = text;
673
+ }
674
+ }
675
+ }
676
+ function unparsableGeometry(prop, text) {
677
+ const t = text.trim();
678
+ if (prop === "size") {
679
+ if (!t)
680
+ return "which is empty";
681
+ return parseSize(t) ? undefined : "which is not a page size";
682
+ }
683
+ if (prop === "margin" || prop.startsWith("margin-")) {
684
+ if (!t)
685
+ return "which is empty";
686
+ const parts = t.split(/\s+/);
687
+ if (prop !== "margin" && parts.length !== 1)
688
+ return "which is not a single length";
689
+ if (parts.length > 4)
690
+ return "which is not a valid margin";
691
+ const bad = parts.filter((p) => toPt(p) === undefined);
692
+ if (bad.length)
693
+ return `which this engine cannot read as a length (\`${bad[0]}\`)`;
694
+ return;
695
+ }
696
+ if (prop === "bleed") {
697
+ if (!t)
698
+ return "which is empty";
699
+ if (/^(auto|none)$/i.test(t))
700
+ return;
701
+ return toPt(t) === undefined ? "which this engine cannot read as a length" : undefined;
702
+ }
703
+ return;
704
+ }
705
+ function walk(css, model) {
706
+ for (const rule of scanRules(css)) {
707
+ if ("statement" in rule)
708
+ continue;
709
+ const { prelude, body } = rule;
710
+ if (NESTED_AT_RULES.test(prelude)) {
711
+ walk(body, model);
712
+ continue;
713
+ }
714
+ if (/^@page\b/i.test(prelude)) {
715
+ model.pageRules.push(parsePageRule(prelude, body));
716
+ continue;
717
+ }
718
+ if (prelude.startsWith("@"))
719
+ continue;
720
+ parseQualifiedRule(prelude, body, model);
721
+ }
722
+ }
723
+ function parsePageRule(prelude, body) {
724
+ const sel = prelude.replace(/^@page\s*/i, "").trim();
725
+ const nameMatch = /^([A-Za-z_][\w-]*)/.exec(sel);
726
+ const pseudos = [...sel.matchAll(/:([A-Za-z-]+)(\([^)]*\))?/g)].map((m) => m[1] + (m[2] ?? ""));
727
+ const rule = {
728
+ name: nameMatch?.[1],
729
+ pseudos,
730
+ decls: {},
731
+ marginBoxes: {},
732
+ raw: `@page ${sel}`.trim()
733
+ };
734
+ let rest = "";
735
+ let i = 0;
736
+ while (i < body.length) {
737
+ const at = findNextAtRule(body, i);
738
+ if (at === -1) {
739
+ rest += body.slice(i);
740
+ break;
741
+ }
742
+ rest += body.slice(i, at);
743
+ const open = body.indexOf("{", at);
744
+ if (open === -1) {
745
+ rest += body.slice(at);
746
+ break;
747
+ }
748
+ const name = body.slice(at + 1, open).trim().toLowerCase();
749
+ let depth = 1;
750
+ let j = open + 1;
751
+ while (j < body.length && depth > 0) {
752
+ const c = body[j];
753
+ if (c === "/" && body[j + 1] === "*") {
754
+ const end = body.indexOf("*/", j + 2);
755
+ j = end === -1 ? body.length : end + 2;
756
+ continue;
757
+ }
758
+ if (c === '"' || c === "'") {
759
+ j = skipString(body, j);
760
+ continue;
761
+ }
762
+ if (c === "{")
763
+ depth++;
764
+ else if (c === "}")
765
+ depth--;
766
+ j++;
767
+ }
768
+ rule.marginBoxes[`@${name}`] = parseDeclarations(body.slice(open + 1, j - 1));
769
+ i = j;
770
+ }
771
+ rule.decls = parseDeclarations(rest);
772
+ return rule;
773
+ }
774
+ function findNextAtRule(body, start) {
775
+ let i = start;
776
+ while (i < body.length) {
777
+ const c = body[i];
778
+ if (c === "/" && body[i + 1] === "*") {
779
+ const end = body.indexOf("*/", i + 2);
780
+ i = end === -1 ? body.length : end + 2;
781
+ continue;
782
+ }
783
+ if (c === '"' || c === "'") {
784
+ i = skipString(body, i);
785
+ continue;
786
+ }
787
+ if (c === "@")
788
+ return i;
789
+ i++;
790
+ }
791
+ return -1;
792
+ }
793
+ function parseQualifiedRule(selector, body, model) {
794
+ const decls = parseDeclarations(body);
795
+ for (const [prop, value] of Object.entries(decls)) {
796
+ if (prop === "string-set") {
797
+ for (const entry of splitTopLevel(value, ",")) {
798
+ const m = /^\s*([A-Za-z_][\w-]*)\s+(.+)$/.exec(entry);
799
+ if (m && m[1] !== undefined && m[2] !== undefined)
800
+ model.stringSets.push({ selector, name: m[1], value: m[2].trim() });
801
+ }
802
+ } else if (prop === "page") {
803
+ if (value && value !== "auto")
804
+ model.pageAssignments.push({ selector, page: value.trim() });
805
+ } else if (prop === "break-before" || prop === "break-after" || prop === "break-inside") {
806
+ model.breaks.push({ selector, prop, value });
807
+ } else if (prop === "counter-reset") {
808
+ const m = /\bpage\s+(-?\d+)/.exec(value);
809
+ if (m)
810
+ model.counterResets.push({ selector, start: Number(m[1]) });
811
+ } else if (prop === "content") {
812
+ for (const fn of ["target-counter", "target-text", "leader", "string"]) {
813
+ if (new RegExp(`\\b${fn}\\s*\\(`).test(value)) {
814
+ model.xrefs.push({ selector, content: value, fn });
815
+ }
816
+ }
817
+ }
818
+ }
819
+ }
820
+ function splitTopLevel(s, sep) {
821
+ const out = [];
822
+ let depth = 0;
823
+ let start = 0;
824
+ for (let i = 0;i < s.length; i++) {
825
+ const c = s[i];
826
+ if (c === '"' || c === "'") {
827
+ i = skipString(s, i) - 1;
828
+ continue;
829
+ }
830
+ if (c === "(")
831
+ depth++;
832
+ else if (c === ")")
833
+ depth--;
834
+ else if (c === sep && depth === 0) {
835
+ out.push(s.slice(start, i));
836
+ start = i + 1;
837
+ }
838
+ }
839
+ out.push(s.slice(start));
840
+ return out.map((x) => x.trim()).filter(Boolean);
841
+ }
842
+ var UNITS_PER_PT = {
843
+ pt: 1,
844
+ px: 0.75,
845
+ in: 72,
846
+ pc: 12,
847
+ cm: 72 / 2.54,
848
+ mm: 72 / 25.4,
849
+ q: 72 / 101.6
850
+ };
851
+ function toPt(value) {
852
+ const m = /^(-?[\d.]+)([a-z%]*)$/i.exec(value.trim());
853
+ if (!m)
854
+ return;
855
+ const n = Number(m[1]);
856
+ const unit = (m[2] || "px").toLowerCase();
857
+ const factor = UNITS_PER_PT[unit];
858
+ return factor === undefined ? undefined : n * factor;
859
+ }
860
+ var PAGE_SIZES = {
861
+ a5: [419.53, 595.28],
862
+ a4: [595.28, 841.89],
863
+ a3: [841.89, 1190.55],
864
+ b5: [498.9, 708.66],
865
+ b4: [708.66, 1000.63],
866
+ "jis-b5": [515.91, 728.5],
867
+ "jis-b4": [728.5, 1031.81],
868
+ letter: [612, 792],
869
+ legal: [612, 1008],
870
+ ledger: [1224, 792]
871
+ };
872
+ function parseSize(value) {
873
+ const parts = value.trim().split(/\s+/);
874
+ let landscape = false;
875
+ const lens = [];
876
+ let named;
877
+ for (const p of parts) {
878
+ const key = p.toLowerCase();
879
+ if (key === "landscape")
880
+ landscape = true;
881
+ else if (key === "portrait")
882
+ continue;
883
+ else if (PAGE_SIZES[key])
884
+ named = PAGE_SIZES[key];
885
+ else {
886
+ const pt = toPt(p);
887
+ if (pt !== undefined)
888
+ lens.push(pt);
889
+ }
890
+ }
891
+ let w, h;
892
+ if (named)
893
+ [w, h] = named;
894
+ else if (lens.length === 1)
895
+ [w, h] = [lens[0], lens[0]];
896
+ else if (lens.length >= 2)
897
+ [w, h] = [lens[0], lens[1]];
898
+ else
899
+ return;
900
+ if (landscape && h > w)
901
+ [w, h] = [h, w];
902
+ return { width: w, height: h };
903
+ }
904
+ function parseMargin(value) {
905
+ const parts = value.trim().split(/\s+/).map((p) => toPt(p) ?? 0);
906
+ const a = parts[0] ?? 0;
907
+ const b = parts[1] ?? a;
908
+ const c = parts[2] ?? a;
909
+ const d = parts[3] ?? b;
910
+ return { top: a, right: b, bottom: c, left: d };
911
+ }
912
+ function resolvePage(model, ctx = {}) {
913
+ const wanted = new Set(ctx.pseudos ?? []);
914
+ const applicable = model.pageRules.filter((r) => r.name ? r.name === ctx.name : true).filter((r) => r.pseudos.every((p) => wanted.has(p))).sort((a, b) => specificity(a) - specificity(b));
915
+ const decls = {};
916
+ const marginBoxes = {};
917
+ const margin = { top: 72, right: 72, bottom: 72, left: 72 };
918
+ for (const rule of applicable) {
919
+ for (const [prop, value] of Object.entries(rule.decls)) {
920
+ if (prop === "margin")
921
+ Object.assign(margin, parseMargin(value));
922
+ else if (prop === "margin-top" || prop === "margin-right" || prop === "margin-bottom" || prop === "margin-left") {
923
+ const side = prop.slice(7);
924
+ margin[side] = toPt(value) ?? margin[side];
925
+ }
926
+ }
927
+ Object.assign(decls, rule.decls);
928
+ for (const [box, d] of Object.entries(rule.marginBoxes)) {
929
+ marginBoxes[box] = { ...marginBoxes[box] ?? {}, ...d };
930
+ }
931
+ }
932
+ const size = parseSize(decls.size ?? "letter") ?? { width: 612, height: 792 };
933
+ return {
934
+ decls,
935
+ marginBoxes,
936
+ geometry: {
937
+ width: size.width,
938
+ height: size.height,
939
+ margin,
940
+ bleed: decls.bleed ? toPt(decls.bleed) ?? 0 : 0,
941
+ marks: (decls.marks ?? "none").split(/\s+/).filter((m) => m && m !== "none")
942
+ }
943
+ };
944
+ }
945
+ function specificity(r) {
946
+ return (r.name ? 2 : 0) + r.pseudos.length;
947
+ }
948
+
949
+ export { EXIT_CODES, BuildError, isRectoVersoBreak, wantsRecto, planRectoBlanks, lastPageMode, signaturePadding, parseWhich, stringValueAt, stringSymbols, cssQuote, counterStyleName, restartedPageValues, toFolioPage, leaderMarker, generatedContentCss, getAssetsDir, getAssetPath, MARGIN_BOX_IGNORED_PROPERTIES, isIgnoredMarginBoxProperty, marginBoxRectPt, marginBoxAlign, extract, splitTopLevel, parseSize, resolvePage };