@pretextbook/pretext-html 0.8.0 → 0.9.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.
Files changed (36) hide show
  1. package/assets/preview-html.xsl +16 -0
  2. package/assets/upstream.json +1 -1
  3. package/assets/xsl/extract-dynamic.xsl +54 -3
  4. package/assets/xsl/extract-gdscript.xsl +61 -0
  5. package/assets/xsl/extract-sageplot.xsl +3 -0
  6. package/assets/xsl/localizations/it-IT.xml +37 -41
  7. package/assets/xsl/pretext-assembly.xsl +188 -24
  8. package/assets/xsl/pretext-beamer.xsl +57 -12
  9. package/assets/xsl/pretext-common.xsl +156 -28
  10. package/assets/xsl/pretext-epub.xsl +0 -5
  11. package/assets/xsl/pretext-fo.xsl +110 -48
  12. package/assets/xsl/pretext-html.xsl +147 -54
  13. package/assets/xsl/pretext-jupyter.xsl +1 -0
  14. package/assets/xsl/pretext-latex-classic.xsl +2 -2
  15. package/assets/xsl/pretext-latex-common.xsl +268 -134
  16. package/assets/xsl/pretext-latex.xsl +16 -6
  17. package/assets/xsl/pretext-numbers.xsl +10 -0
  18. package/assets/xsl/pretext-revealjs.xsl +68 -1
  19. package/assets/xsl/pretext-runestone-fitb.xsl +179 -84
  20. package/assets/xsl/pretext-runestone-static.xsl +72 -20
  21. package/assets/xsl/pretext-runestone.xsl +27 -4
  22. package/assets/xsl/pretext-text-utilities.xsl +7 -0
  23. package/assets/xsl/pretext-text.xsl +17 -0
  24. package/assets/xsl/publisher-variables.xsl +39 -0
  25. package/assets/xsl-bundle.json +1 -1
  26. package/dist/cli-BUrNZxov.js +1098 -0
  27. package/dist/cli.js +1 -1
  28. package/dist/html-static.d.ts +2 -2
  29. package/dist/index.browser.js +595 -386
  30. package/dist/index.d.ts +4 -0
  31. package/dist/index.js +35 -30
  32. package/dist/renderer.d.ts +35 -0
  33. package/dist/skeleton.d.ts +41 -0
  34. package/dist/xrefs.d.ts +54 -0
  35. package/package.json +1 -1
  36. package/dist/cli-Bcn4t3B1.js +0 -871
@@ -1,871 +0,0 @@
1
- import { readFile as M, writeFile as W } from "node:fs/promises";
2
- import * as v from "node:process";
3
- import * as l from "node:path";
4
- import { fileURLToPath as we } from "node:url";
5
- import { fromXml as y } from "xast-util-from-xml";
6
- import { toXml as S } from "xast-util-to-xml";
7
- import { injectRevealBridge as be } from "./reveal.js";
8
- import { injectThemeBridge as ve } from "./theme.js";
9
- const ge = "ptxPreviewBannerDismissed", xe = "About live preview";
10
- function H(e) {
11
- return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
12
- }
13
- function ye(e) {
14
- return [
15
- '<div id="ptx-preview-banner" class="ptx-preview-banner" role="note">',
16
- ` <span class="ptx-preview-banner-text">${H(e)}</span>`,
17
- ' <button type="button" class="ptx-preview-banner-dismiss" aria-label="Dismiss">&times;</button>',
18
- "</div>",
19
- `<button type="button" id="ptx-preview-banner-restore" class="ptx-preview-banner-restore" hidden>${H(xe)}</button>`
20
- ].join(`
21
- `);
22
- }
23
- function Ee() {
24
- return [
25
- "<style>",
26
- ".ptx-preview-banner {",
27
- " display: flex;",
28
- " align-items: center;",
29
- " gap: 0.75em;",
30
- " padding: 0.5em 1em;",
31
- " font-size: 0.9em;",
32
- " background: #fff8e1;",
33
- " color: #6b5300;",
34
- " border-bottom: 1px solid #f0c36d;",
35
- "}",
36
- // The [hidden] content attribute only hides an element via the
37
- // user-agent stylesheet's `[hidden] { display: none }` rule, and a
38
- // normal-priority *author* rule always wins over a normal-priority UA
39
- // rule regardless of selector specificity — so the unconditional
40
- // `display: flex` above would otherwise keep the banner visible even
41
- // after previewBannerScript sets `banner.hidden = true`.
42
- ".ptx-preview-banner[hidden] {",
43
- " display: none;",
44
- "}",
45
- ".ptx-preview-banner-text {",
46
- " flex: 1 1 auto;",
47
- "}",
48
- ".ptx-preview-banner-dismiss {",
49
- " flex: 0 0 auto;",
50
- " background: transparent;",
51
- " border: none;",
52
- " font-size: 1.2em;",
53
- " line-height: 1;",
54
- " cursor: pointer;",
55
- " color: inherit;",
56
- " padding: 0 0.25em;",
57
- "}",
58
- "html.dark-mode .ptx-preview-banner {",
59
- " background: #3a2f00;",
60
- " color: #f2d27a;",
61
- " border-bottom-color: #6b5300;",
62
- "}",
63
- ".ptx-preview-banner-restore {",
64
- " position: fixed;",
65
- " right: 0.5em;",
66
- " bottom: 0.5em;",
67
- " z-index: 1000;",
68
- " background: transparent;",
69
- " border: none;",
70
- " padding: 0.15em 0.4em;",
71
- " font-size: 0.75em;",
72
- " color: inherit;",
73
- " opacity: 0.45;",
74
- " text-decoration: underline;",
75
- " cursor: pointer;",
76
- "}",
77
- ".ptx-preview-banner-restore:hover {",
78
- " opacity: 0.85;",
79
- "}",
80
- "</style>"
81
- ].join(`
82
- `);
83
- }
84
- function Te() {
85
- return [
86
- "<script>",
87
- "(function () {",
88
- " var KEY = " + JSON.stringify(ge) + ";",
89
- ' var banner = document.getElementById("ptx-preview-banner");',
90
- ' var restore = document.getElementById("ptx-preview-banner-restore");',
91
- " if (!banner || !restore) { return; }",
92
- // localStorage can throw (disabled cookies/storage, sandboxed iframe);
93
- // fall back to always showing the banner rather than crashing the page.
94
- " function isDismissed() {",
95
- " try { return window.localStorage.getItem(KEY) === '1'; }",
96
- " catch (e) { return false; }",
97
- " }",
98
- " function setDismissed(value) {",
99
- " try {",
100
- " if (value) { window.localStorage.setItem(KEY, '1'); }",
101
- " else { window.localStorage.removeItem(KEY); }",
102
- " } catch (e) {}",
103
- " }",
104
- " function apply(dismissed) {",
105
- " banner.hidden = dismissed;",
106
- " restore.hidden = !dismissed;",
107
- " }",
108
- " apply(isDismissed());",
109
- ' var dismissBtn = banner.querySelector(".ptx-preview-banner-dismiss");',
110
- " if (dismissBtn) {",
111
- " dismissBtn.addEventListener('click', function () {",
112
- " setDismissed(true);",
113
- " apply(true);",
114
- " });",
115
- " }",
116
- " restore.addEventListener('click', function () {",
117
- " setDismissed(false);",
118
- " apply(false);",
119
- " });",
120
- "})();",
121
- "<\/script>"
122
- ].join(`
123
- `);
124
- }
125
- const Se = "<!--ptx-preview-banner-->", $e = "<!--/ptx-preview-banner-->", ke = /\n?<!--ptx-preview-banner-->[\s\S]*?<!--\/ptx-preview-banner-->/;
126
- function Pe(e, r) {
127
- const t = ke.exec(e), n = t ? e.slice(0, t.index) + e.slice(t.index + t[0].length) : e;
128
- if (!/<body[^>]*>/i.test(n))
129
- return n;
130
- const o = `
131
- ${Se}
132
- ${Ee()}
133
- ${ye(r.message)}
134
- ${Te()}
135
- ${$e}`;
136
- return n.replace(/<body([^>]*)>/i, `<body$1>${o}`);
137
- }
138
- async function ee(e, r) {
139
- const t = l.resolve(e), n = l.resolve(t, `.${l.posix.normalize(r)}`);
140
- if (n !== t && !n.startsWith(t + l.sep)) {
141
- console.error(
142
- `pretext-html: refusing to serve ${r} from outside ${t}`
143
- );
144
- return;
145
- }
146
- try {
147
- return new Uint8Array(await M(n));
148
- } catch {
149
- return;
150
- }
151
- }
152
- async function B(e) {
153
- try {
154
- return await M(e, "utf8");
155
- } catch {
156
- return;
157
- }
158
- }
159
- let te;
160
- function kt(e) {
161
- te = e;
162
- }
163
- function j() {
164
- return te ?? process.env.PRETEXT_HTML_ASSETS ?? l.resolve(l.dirname(we(import.meta.url)), "..", "assets");
165
- }
166
- function Ie(e, r) {
167
- return l.join(e, r);
168
- }
169
- const re = "2.46.0", De = "8.2.1", Re = [
170
- "prefix-runtime.ed2cce180827a7ad.bundle.js",
171
- "prefix-926.5c7038c8ef173faf.bundle.js",
172
- "prefix-runestone.1f660864c47fc762.bundle.js"
173
- ], je = [
174
- "prefix-926.229729d09512acc9.css",
175
- "prefix-runestone.8086b62682b271d1.css"
176
- ], Le = "born-hidden-knowl", Ae = /<details\b([^>]*)>(\s*)(?:<summary\b([^>]*)>)?/g;
177
- function Me(e) {
178
- const r = /\bclass="([^"]*)"/.exec(e);
179
- return r ? r[1].trim().split(/\s+/) : [];
180
- }
181
- function Be(e) {
182
- return /(^|\s)open(\s|=|$)/.test(e);
183
- }
184
- function J(e) {
185
- return Be(e) ? e : ` open${e}`;
186
- }
187
- function _e(e) {
188
- return e.replace(
189
- Ae,
190
- (r, t, n, o) => {
191
- if (!Me(t).includes(Le))
192
- return r;
193
- const s = `<details${J(t)}>`;
194
- return o === void 0 ? `${s}${n}` : `${s}${n}<summary${J(o)}>`;
195
- }
196
- );
197
- }
198
- const _ = /* @__PURE__ */ new Map(), ne = /* @__PURE__ */ new Map();
199
- let L, Ce = 0, oe = ee;
200
- function Pt(e) {
201
- oe = e;
202
- }
203
- function Oe(e, r) {
204
- const t = typeof e == "string" ? e : e instanceof URL ? e.href : e.url, n = ne.get(t);
205
- if (n !== void 0)
206
- return Promise.resolve(new Response(n, { status: 200 }));
207
- let o;
208
- try {
209
- o = new URL(t).host;
210
- } catch {
211
- o = "";
212
- }
213
- const s = _.get(o);
214
- return s !== void 0 ? Ne(s, t) : L(e, r);
215
- }
216
- const Xe = '<?xml version="1.0"?><pretext-preview-missing-file/>';
217
- async function Ne(e, r) {
218
- const t = decodeURIComponent(new URL(r).pathname), n = await oe(e, t);
219
- if (n === void 0)
220
- return new Response(Xe, { status: 200 });
221
- const o = typeof n == "string" ? n : new Uint8Array(n);
222
- return new Response(o, { status: 200 });
223
- }
224
- function se() {
225
- L || (L = globalThis.fetch, globalThis.fetch = Oe);
226
- }
227
- function ie(e) {
228
- se();
229
- const r = `mnt${++Ce}.ptx.invalid`;
230
- return _.set(r, e), `http://${r}`;
231
- }
232
- function Fe(e) {
233
- _.delete(new URL(e).host);
234
- }
235
- function Ue(e, r) {
236
- se(), ne.set(e, r);
237
- }
238
- const A = `<?xml version="1.0" encoding="UTF-8"?>
239
- <publication>
240
- <source>
241
- <directories external="../assets" generated="../generated-assets" />
242
- </source>
243
- <html>
244
- <platform portable="yes"/>
245
- </html>
246
- </publication>
247
- `, We = ["theme", "style", "shell"];
248
- function b(e, r) {
249
- for (const t of e.children)
250
- if (t.type === "element" && t.name === r)
251
- return t;
252
- }
253
- function g(e, r) {
254
- let t = b(e, r);
255
- return t || (t = { type: "element", name: r, attributes: {}, children: [] }, e.children.push(t)), t;
256
- }
257
- function He(e, r) {
258
- const t = b(e, "css");
259
- if (t && We.some((o) => t.attributes?.[o]))
260
- return;
261
- const n = t ?? g(e, "css");
262
- n.attributes = { ...n.attributes, theme: r };
263
- }
264
- function Je(e, r) {
265
- const t = b(e, "appearance");
266
- if (t?.attributes?.theme)
267
- return;
268
- const n = t ?? g(e, "appearance");
269
- n.attributes = { ...n.attributes, theme: r };
270
- }
271
- function Ke(e) {
272
- const r = g(e, "resources");
273
- r.attributes = {
274
- ...r.attributes,
275
- host: "cdn",
276
- math: "online"
277
- };
278
- }
279
- function ze(e, r = {}) {
280
- const t = r.cssTheme?.trim(), n = r.revealTheme?.trim(), o = r.target === "slides";
281
- if (!e && !t && !o)
282
- return A;
283
- const s = y(e ?? A), a = b(s, "publication");
284
- if (!a)
285
- throw new Error(
286
- "Invalid publication file: no root <publication> element found"
287
- );
288
- const i = g(a, "html"), u = g(i, "platform");
289
- if (u.attributes = { ...u.attributes, portable: "yes" }, t && He(i, t), o) {
290
- const c = g(a, "revealjs");
291
- Ke(c), n && Je(c, n);
292
- }
293
- return S(s);
294
- }
295
- function K(e) {
296
- if (!(e === void 0 || e === "" || e.startsWith("/")))
297
- return e.endsWith("/") ? e : `${e}/`;
298
- }
299
- function Ye(e) {
300
- const r = y(e ?? A), t = b(r, "publication"), n = t && b(t, "source"), o = n && b(n, "directories"), s = K(
301
- o?.attributes?.external ?? void 0
302
- ), a = K(
303
- o?.attributes?.generated ?? void 0
304
- );
305
- return {
306
- external: s,
307
- generated: a,
308
- managed: s !== void 0 && a !== void 0
309
- };
310
- }
311
- const ae = "http://www.w3.org/2001/XInclude", z = 64;
312
- async function Ve(e, r, t) {
313
- return e.includes("include") ? (await ce(e, r, t)).content : e;
314
- }
315
- async function ce(e, r, t) {
316
- const n = y(e), o = {
317
- projectDir: l.resolve(t),
318
- stack: [l.resolve(r)]
319
- }, s = await x(
320
- n,
321
- l.dirname(l.resolve(r)),
322
- { "": "" },
323
- o
324
- );
325
- return { tree: n, content: s ? S(n) : e };
326
- }
327
- function Ge(e) {
328
- return e.data?.ptxIncludedFile;
329
- }
330
- function qe(e, r) {
331
- const t = e.data ?? (e.data = {});
332
- t.ptxIncludedFile = r;
333
- }
334
- function $(e, r) {
335
- let t = e;
336
- for (const [n, o] of Object.entries(r.attributes))
337
- (n === "xmlns" || n.startsWith("xmlns:")) && (t === e && (t = { ...e }), t[n === "xmlns" ? "" : n.slice(6)] = o ?? "");
338
- return t;
339
- }
340
- function le(e, r) {
341
- const t = e.name.indexOf(":"), n = t === -1 ? "" : e.name.slice(0, t);
342
- return (t === -1 ? e.name : e.name.slice(t + 1)) === "include" && r[n] === ae;
343
- }
344
- async function x(e, r, t, n) {
345
- let o = !1;
346
- for (let s = 0; s < e.children.length; s++) {
347
- const a = e.children[s];
348
- if (a.type !== "element")
349
- continue;
350
- const i = $(t, a);
351
- if (le(a, i)) {
352
- const u = await ue(
353
- a,
354
- r,
355
- i,
356
- n
357
- );
358
- e.children.splice(s, 1, ...u), s += u.length - 1, o = !0;
359
- } else await x(a, r, i, n) && (o = !0);
360
- }
361
- return o;
362
- }
363
- async function ue(e, r, t, n) {
364
- const o = n.stack[n.stack.length - 1];
365
- if (e.attributes.xpointer)
366
- throw new Error(
367
- `xi:include with xpointer is not supported by the JS preview build (in ${o})`
368
- );
369
- const s = e.attributes.href;
370
- if (!s)
371
- throw new Error(`xi:include without href (in ${o})`);
372
- let a = s;
373
- try {
374
- a = decodeURIComponent(s);
375
- } catch {
376
- }
377
- const i = l.resolve(r, a);
378
- if (i !== n.projectDir && !i.startsWith(n.projectDir + l.sep))
379
- throw new Error(
380
- `xi:include escapes the project directory: ${s} (in ${o})`
381
- );
382
- if (n.stack.includes(i))
383
- throw new Error(
384
- `Circular xi:include: ${[...n.stack, i].join(" -> ")}`
385
- );
386
- if (n.stack.length >= z)
387
- throw new Error(`xi:include nesting exceeds ${z} levels`);
388
- const u = await B(i);
389
- if (u === void 0) {
390
- const p = Qe(e, t);
391
- if (p)
392
- return await x(p, r, t, n), p.children;
393
- throw new Error(`xi:include target not found: ${s} (in ${o})`);
394
- }
395
- if (e.attributes.parse === "text")
396
- return [{ type: "text", value: u }];
397
- let c;
398
- try {
399
- c = y(u);
400
- } catch (p) {
401
- throw new Error(
402
- `Could not parse xi:include target ${i} as XML: ${p instanceof Error ? p.message : p}. (Included files must be well-formed, with a single root element.)`
403
- );
404
- }
405
- n.stack.push(i);
406
- try {
407
- await x(c, l.dirname(i), { "": "" }, n);
408
- } finally {
409
- n.stack.pop();
410
- }
411
- const f = c.children.filter(
412
- (p) => p.type === "element"
413
- );
414
- for (const p of f)
415
- qe(p, i);
416
- return f;
417
- }
418
- function Y(e) {
419
- return e.name === "docinfo";
420
- }
421
- async function Ze(e, r, t) {
422
- try {
423
- const o = y(e).children.find(
424
- (c) => c.type === "element"
425
- );
426
- if (!o)
427
- return;
428
- const s = l.resolve(r), a = l.dirname(s), i = {
429
- projectDir: l.resolve(t),
430
- stack: [s]
431
- }, u = $({ "": "" }, o);
432
- for (const c of o.children) {
433
- if (c.type !== "element")
434
- continue;
435
- const f = $(u, c);
436
- if (Y(c))
437
- return await x(c, a, f, i), S(c);
438
- if (le(c, f)) {
439
- const m = (await ue(
440
- c,
441
- a,
442
- f,
443
- i
444
- )).find(
445
- (h) => h.type === "element" && Y(h)
446
- );
447
- if (m)
448
- return S(m);
449
- continue;
450
- }
451
- break;
452
- }
453
- return;
454
- } catch {
455
- return;
456
- }
457
- }
458
- function Qe(e, r) {
459
- for (const t of e.children) {
460
- if (t.type !== "element")
461
- continue;
462
- const n = $(r, t), o = t.name.indexOf(":"), s = o === -1 ? "" : t.name.slice(0, o);
463
- if ((o === -1 ? t.name : t.name.slice(o + 1)) === "fallback" && n[s] === ae)
464
- return t;
465
- }
466
- }
467
- function de(e) {
468
- return typeof e == "object" && e !== null && e.type === "element";
469
- }
470
- function et(e) {
471
- return /^(pf|xhtml):/.test(e.name);
472
- }
473
- function tt(e, r, t) {
474
- const n = [], o = e.children.find(de);
475
- if (o) {
476
- const s = t?.parentId ?? "root";
477
- fe(o, s, t?.position ?? 1, r, s, n);
478
- }
479
- return n;
480
- }
481
- function fe(e, r, t, n, o, s) {
482
- const a = Ge(e) ?? n;
483
- let i, u;
484
- if (et(e))
485
- u = "root";
486
- else {
487
- const f = e.attributes.label ?? e.attributes["xml:id"];
488
- i = f ?? `${r}-${t}`, s.push({
489
- id: i,
490
- file: a,
491
- line: e.position?.start.line ?? 1,
492
- column: e.position?.start.column ?? 1,
493
- endLine: e.position?.end.line ?? e.position?.start.line ?? 1,
494
- parent: o
495
- }), u = i;
496
- }
497
- let c = 0;
498
- for (const f of e.children)
499
- de(f) && (c += 1, fe(
500
- f,
501
- u,
502
- c,
503
- a,
504
- i ?? o,
505
- s
506
- ));
507
- }
508
- function It(e, r) {
509
- let t;
510
- for (const n of e)
511
- n.line <= r && (t = n);
512
- return t ?? e[0];
513
- }
514
- function rt(e) {
515
- const r = e.replace(/<!--[\s\S]*?-->/g, " ").replace(/<!\[CDATA\[[\s\S]*?\]\]>/g, " ");
516
- return /<\s*slide(?:show)?[\s/>]/.test(r) ? "slides" : "html";
517
- }
518
- function nt() {
519
- return Ie(j(), "xsl");
520
- }
521
- function ot() {
522
- return "Suspending" in WebAssembly;
523
- }
524
- function st() {
525
- if (!ot())
526
- throw new Error(
527
- "WebAssembly JSPI is not enabled. Run Node with --experimental-wasm-jspi (or use the pretext-html CLI, which re-launches itself with the flag)."
528
- );
529
- }
530
- function I(e) {
531
- return e.includes("'") ? e.includes('"') ? `concat(${e.split("'").map((t) => `'${t}'`).join(`, "'", `)})` : `"${e}"` : `'${e}'`;
532
- }
533
- function it(e) {
534
- return e.replace(
535
- /from '\.\/(https:\/\/[^']+)'/g,
536
- (r, t) => `from '${t}'`
537
- );
538
- }
539
- const at = `https://cdn.jsdelivr.net/gh/PreTeXtBook/html-static@${re}/dist/`, ct = {
540
- "cli.version": re,
541
- // Colon-delimited: pretext-runestone.xsl splits them with str:tokenize.
542
- "rs-js": Re.join(":"),
543
- "rs-css": je.join(":"),
544
- "rs-version": De
545
- };
546
- function lt(e) {
547
- return e.replace(
548
- /(<link\b[^>]*\bhref=["'])_static\//gi,
549
- `$1${at}_static/`
550
- );
551
- }
552
- function ut(e) {
553
- return e.slice(0, 65536).replace(/<\?[\s\S]*?\?>/g, " ").replace(/<!--[\s\S]*?-->/g, " ").replace(/<!DOCTYPE[^>]*>/gi, " ").match(/<\s*([A-Za-z][\w:-]*)/)?.[1];
554
- }
555
- async function V(e) {
556
- const r = await B(e);
557
- if (r === void 0)
558
- throw new Error(
559
- `Could not read ${e}. (In the browser build only absolute URLs are readable — pass sourceContent instead of relying on sourcePath.)`
560
- );
561
- return r;
562
- }
563
- async function dt(e, r) {
564
- const t = l.resolve(e), n = await B(t);
565
- if (n !== void 0)
566
- return Ze(n, t, r);
567
- }
568
- function ft(e, r, t, n) {
569
- const o = e.replace(/^\uFEFF/, "").replace(/<\?xml[\s\S]*?\?>/, "").replace(/<!DOCTYPE[^>]*>/i, ""), s = t === "slides" ? "slideshow" : r === "chapter" || r === "part" ? "book" : "article";
570
- return `<pretext>
571
- ${n?.trim() ? `${n.trim()}
572
- ` : ""}<${s}>
573
- <title/>
574
- ${o}
575
- </${s}>
576
- </pretext>
577
- `;
578
- }
579
- let pe = !1;
580
- function pt(e) {
581
- return e instanceof Error && /\bAborted\(/.test(e.message);
582
- }
583
- function mt(e) {
584
- return e instanceof Error && /memory access|out of bounds/i.test(e.message);
585
- }
586
- function ht() {
587
- if (pe)
588
- throw new Error(
589
- "The WebAssembly renderer aborted earlier in this process and cannot be reused. Restart the process (or reload the page) to render again."
590
- );
591
- }
592
- function G(e) {
593
- if (pt(e))
594
- throw pe = !0, new Error(
595
- "The WebAssembly renderer aborted and cannot be reused. Restart the process (or reload the page) to render again.",
596
- { cause: e }
597
- );
598
- if (mt(e))
599
- throw new Error(
600
- "The WebAssembly renderer hit a memory fault. Most often the document is too large for this build's stack — whole-book builds are a known limitation, so try a smaller document, or see the README about rebuilding libxslt-wasm with a larger stack. The same fault also results from running two renders concurrently against the shared libxslt state.",
601
- { cause: e }
602
- );
603
- }
604
- let D;
605
- async function me() {
606
- return D || (D = (async () => {
607
- const [e, r] = await Promise.all([
608
- import("@pretextbook/libxslt-wasm"),
609
- import("@pretextbook/libxslt-wasm/exslt")
610
- ]);
611
- return r.registerAll(), e;
612
- })()), D;
613
- }
614
- const R = /* @__PURE__ */ new Map(), wt = {
615
- html: "preview-html.xsl",
616
- slides: "preview-revealjs.xsl"
617
- };
618
- async function bt(e, r) {
619
- const t = wt[r], n = `${r}\0${e}`;
620
- let o = R.get(n);
621
- return o || (o = (async () => {
622
- const { XmlDocument: s, XsltStylesheet: a } = await me(), i = ie(e), u = await ee(j(), `/${t}`);
623
- if (u === void 0)
624
- throw new Error(
625
- `Could not read ${t} from ${j()}. Check the assets location (PRETEXT_HTML_ASSETS under Node, setAssetsBase() in the browser).`
626
- );
627
- const c = new TextDecoder().decode(u), f = await s.fromString(c, {
628
- url: `${i}/${t}`,
629
- options: { noEnt: !0, dtdLoad: !0, huge: !0 }
630
- });
631
- return a.fromXmlDocument(f);
632
- })(), R.set(n, o), o.catch(() => R.delete(n))), o;
633
- }
634
- let q = Promise.resolve();
635
- function vt(e) {
636
- const r = q.then(
637
- () => Z(e),
638
- () => Z(e)
639
- );
640
- return q = r.catch(() => {
641
- }), r;
642
- }
643
- async function Z(e) {
644
- st(), ht();
645
- const { XmlDocument: r } = await me(), t = l.resolve(e.sourcePath), n = l.resolve(
646
- e.projectDir ?? l.dirname(t)
647
- ), o = e.sourceContent ?? await V(t), s = l.relative(n, t);
648
- if (s.startsWith(".."))
649
- throw new Error(
650
- `sourcePath (${t}) must live inside projectDir (${n})`
651
- );
652
- const a = l.relative(
653
- n,
654
- l.resolve(e.mainSourcePath ?? t)
655
- ), i = a.startsWith("..") ? s : a, u = ie(n);
656
- try {
657
- let c, f = `${u}/__pretext-preview-publication.xml`;
658
- if (e.publicationPath) {
659
- const d = l.resolve(e.publicationPath);
660
- c = await V(d);
661
- const w = l.relative(n, d);
662
- w.startsWith("..") || (f = `${u}/${w.split(l.sep).join("/")}`);
663
- }
664
- let p, m;
665
- if (e.sourceMap) {
666
- const d = await ce(
667
- o,
668
- t,
669
- n
670
- );
671
- p = d.tree, m = d.content;
672
- } else
673
- m = await Ve(
674
- o,
675
- t,
676
- n
677
- );
678
- const h = e.target ?? rt(m), he = await bt(e.xslDir ?? nt(), h), C = ze(c, {
679
- cssTheme: e.cssTheme,
680
- revealTheme: e.revealTheme,
681
- target: h
682
- });
683
- Ue(f, C);
684
- const E = Ye(C), O = l.dirname(l.resolve(n, i)), X = E.external !== void 0 && E.generated !== void 0 ? {
685
- external: l.resolve(O, E.external),
686
- generated: l.resolve(O, E.generated)
687
- } : void 0, T = ut(m);
688
- let N = { parentId: "root", position: 1 };
689
- if (T !== "pretext" && T !== "mathbook")
690
- if (e.fragment) {
691
- const d = e.docinfo ?? (e.docinfoSourcePath ? await dt(e.docinfoSourcePath, n) : void 0);
692
- m = ft(
693
- m,
694
- T,
695
- h,
696
- d
697
- ), N = { parentId: d?.trim() ? "root-1-2" : "root-1-1", position: 2 };
698
- } else
699
- throw new Error(
700
- `The document root is <${T ?? "?"}>, not <pretext> — this looks like an xi:included fragment. Render the project's main source file instead, or pass the fragment option.`
701
- );
702
- const F = p ? tt(p, t, N) : void 0, U = await r.fromString(m, {
703
- url: `${u}/${i.split(l.sep).join("/")}`,
704
- options: { noEnt: !0, dtdLoad: !0, huge: !0 }
705
- }), k = {
706
- publisher: I(f)
707
- };
708
- for (const [d, w] of Object.entries(ct))
709
- k[d] = I(w);
710
- for (const [d, w] of Object.entries(e.stringParams ?? {}))
711
- k[d] = I(w);
712
- let P;
713
- try {
714
- P = await he.apply(U, k);
715
- } catch (d) {
716
- throw G(d), new Error(
717
- "PreTeXt XSLT transform failed; see the PTX:ERROR/PTX:FATAL messages on stderr for details.",
718
- { cause: d }
719
- );
720
- } finally {
721
- U.delete();
722
- }
723
- try {
724
- let d = it(P.toHtmlString());
725
- return (e.openKnowls ?? !0) && (d = _e(d)), h === "slides" ? (d = lt(d), d = be(d, e.revealView ?? "scroll", {
726
- zoom: e.revealZoom
727
- })) : e.theme && (d = ve(d, e.theme)), e.previewBanner && (d = Pe(d, e.previewBanner)), {
728
- html: d,
729
- target: h,
730
- ...F ? { sourceMap: F } : {},
731
- ...X ? { assetDirs: X } : {}
732
- };
733
- } catch (d) {
734
- throw G(d), d;
735
- } finally {
736
- P.delete();
737
- }
738
- } finally {
739
- Fe(u);
740
- }
741
- }
742
- const Q = `Usage: pretext-html <source.ptx> [options]
743
-
744
- Render a PreTeXt document to a single standalone HTML page (portable build:
745
- one page, css/js/MathJax from CDN). No PreTeXt installation required.
746
-
747
- Options:
748
- -o, --output <file> Write HTML to a file instead of stdout
749
- --publication <file> Publication file (portable html is forced on)
750
- --css-theme <name> HTML theme to use when the publication file names
751
- none of its own: default-modern (PreTeXt's default),
752
- denver, tacoma, salem, greeley or boulder
753
- --project-dir <dir> Directory served to the transform for xi:includes
754
- (default: the source file's directory)
755
- --param <name=value> Extra XSLT string parameter (repeatable)
756
- --xsl-dir <dir> Use PreTeXt stylesheets from this directory
757
- instead of the vendored copy
758
- --fragment Allow a non-root source file: wrap the fragment
759
- in a minimal <pretext> document (an <article>,
760
- or a <book> for <chapter>/<part> fragments)
761
- --docinfo <file> File whose contents (a <docinfo> element) are
762
- injected into the wrapper in --fragment mode, so
763
- the fragment keeps the project's LaTeX macros
764
- --docinfo-source <file> Complete source file (e.g. main.ptx) to lift the
765
- <docinfo> from for --fragment mode, resolving
766
- xi:includes (used when --docinfo is not given)
767
- --source-map <file> Also write a JSON source map: HTML id → source
768
- file/line for every element (for editor sync)
769
- --no-open-knowls Leave solutions, hints and other born-hidden
770
- knowls collapsed, as a real PreTeXt build does.
771
- They are expanded by default, since these pages
772
- are previews of work in progress
773
- -h, --help Show this help
774
- `;
775
- function gt(e) {
776
- let r, t, n, o;
777
- const s = {}, a = {};
778
- for (let i = 0; i < e.length; i++) {
779
- const u = e[i], c = () => {
780
- if (i += 1, i >= e.length)
781
- throw new Error(`Missing value for ${u}`);
782
- return e[i];
783
- };
784
- switch (u) {
785
- case "-h":
786
- case "--help":
787
- v.stdout.write(Q), v.exit(0);
788
- break;
789
- case "-o":
790
- case "--output":
791
- t = c();
792
- break;
793
- case "--publication":
794
- s.publicationPath = c();
795
- break;
796
- case "--css-theme":
797
- s.cssTheme = c();
798
- break;
799
- case "--project-dir":
800
- s.projectDir = c();
801
- break;
802
- case "--xsl-dir":
803
- s.xslDir = c();
804
- break;
805
- case "--fragment":
806
- s.fragment = !0;
807
- break;
808
- case "--no-open-knowls":
809
- s.openKnowls = !1;
810
- break;
811
- case "--docinfo":
812
- n = c();
813
- break;
814
- case "--docinfo-source":
815
- s.docinfoSourcePath = c();
816
- break;
817
- case "--source-map":
818
- o = c(), s.sourceMap = !0;
819
- break;
820
- case "--param": {
821
- const f = c(), p = f.indexOf("=");
822
- if (p === -1)
823
- throw new Error(`--param expects name=value, got: ${f}`);
824
- a[f.slice(0, p)] = f.slice(p + 1);
825
- break;
826
- }
827
- default:
828
- if (u.startsWith("-"))
829
- throw new Error(`Unknown option: ${u}`);
830
- if (r)
831
- throw new Error(`Unexpected extra argument: ${u}`);
832
- r = u;
833
- }
834
- }
835
- if (!r)
836
- throw new Error(`No source file given.
837
-
838
- ` + Q);
839
- return Object.keys(a).length > 0 && (s.stringParams = a), {
840
- options: { ...s, sourcePath: r },
841
- output: t,
842
- docinfoPath: n,
843
- sourceMapPath: o
844
- };
845
- }
846
- async function Dt(e) {
847
- const { options: r, output: t, docinfoPath: n, sourceMapPath: o } = gt(e);
848
- n && (r.docinfo = await M(n, "utf8"));
849
- const s = Date.now(), { html: a, sourceMap: i } = await vt(r);
850
- v.stderr.write(`pretext-html: rendered in ${Date.now() - s}ms
851
- `), o && i && (await W(o, JSON.stringify(i, null, 2)), v.stderr.write(`pretext-html: wrote ${o}
852
- `)), t ? (await W(t, a), v.stderr.write(`pretext-html: wrote ${t}
853
- `)) : v.stdout.write(a);
854
- }
855
- export {
856
- ge as P,
857
- rt as a,
858
- ze as b,
859
- ot as c,
860
- nt as d,
861
- Pt as e,
862
- It as f,
863
- gt as g,
864
- Pe as i,
865
- Dt as m,
866
- _e as o,
867
- Te as p,
868
- vt as r,
869
- kt as s,
870
- I as x
871
- };