@pretextbook/pretext-html 0.1.0 → 0.2.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 (106) hide show
  1. package/README.md +104 -4
  2. package/assets/preview-html.xsl +230 -230
  3. package/assets/upstream.json +7 -7
  4. package/assets/xsl/entities.ent +154 -154
  5. package/assets/xsl/extract-asymptote.xsl +68 -68
  6. package/assets/xsl/extract-biblio-csl.xsl +327 -327
  7. package/assets/xsl/extract-datafile.xsl +71 -71
  8. package/assets/xsl/extract-dynamic.xsl +139 -139
  9. package/assets/xsl/extract-identity.xsl +136 -136
  10. package/assets/xsl/extract-interactive.xsl +81 -81
  11. package/assets/xsl/extract-latex-image.xsl +118 -118
  12. package/assets/xsl/extract-mermaid.xsl +58 -58
  13. package/assets/xsl/extract-mom.xsl +63 -63
  14. package/assets/xsl/extract-pg.xsl +2897 -2897
  15. package/assets/xsl/extract-prefigure.xsl +157 -157
  16. package/assets/xsl/extract-qrcode.xsl +100 -100
  17. package/assets/xsl/extract-runestone-page-template.xsl +139 -139
  18. package/assets/xsl/extract-sageplot.xsl +166 -166
  19. package/assets/xsl/extract-stack.xsl +56 -56
  20. package/assets/xsl/extract-trace.xsl +134 -134
  21. package/assets/xsl/extract-youtube.xsl +63 -63
  22. package/assets/xsl/html-symbols.xsl +3344 -3344
  23. package/assets/xsl/latex/pretext-latex-AIM.xsl +56 -56
  24. package/assets/xsl/latex/pretext-latex-CLP.xsl +148 -148
  25. package/assets/xsl/latex/pretext-latex-chaos.xsl +317 -317
  26. package/assets/xsl/latex/pretext-latex-dyslexic-font.xsl +76 -76
  27. package/assets/xsl/latex/pretext-latex-guide.xsl +104 -104
  28. package/assets/xsl/latex/pretext-latex-texstyle.xsl +862 -862
  29. package/assets/xsl/localizations/af-ZA.xml +276 -276
  30. package/assets/xsl/localizations/bg-BG.xml +276 -276
  31. package/assets/xsl/localizations/ca-ES.xml +285 -285
  32. package/assets/xsl/localizations/cs-CZ.xml +280 -280
  33. package/assets/xsl/localizations/de-DE.xml +281 -281
  34. package/assets/xsl/localizations/en-US.xml +427 -427
  35. package/assets/xsl/localizations/es-ES.xml +287 -287
  36. package/assets/xsl/localizations/fi-FI.xml +275 -275
  37. package/assets/xsl/localizations/fr-CA.xml +276 -276
  38. package/assets/xsl/localizations/fr-FR.xml +280 -280
  39. package/assets/xsl/localizations/hu-HU.xml +280 -280
  40. package/assets/xsl/localizations/it-IT.xml +276 -276
  41. package/assets/xsl/localizations/ku-CKB.xml +275 -275
  42. package/assets/xsl/localizations/localizations.xml +48 -48
  43. package/assets/xsl/localizations/nl-NL.xml +275 -275
  44. package/assets/xsl/localizations/pt-BR.xml +288 -288
  45. package/assets/xsl/localizations/pt-PT.xml +292 -292
  46. package/assets/xsl/localizations/zh-HANS.xml +269 -269
  47. package/assets/xsl/pretext-assembly.xsl +4795 -4795
  48. package/assets/xsl/pretext-basic-html.xsl +86 -86
  49. package/assets/xsl/pretext-beamer.xsl +662 -662
  50. package/assets/xsl/pretext-braille-preprint.xsl +2249 -2249
  51. package/assets/xsl/pretext-common.xsl +12018 -12018
  52. package/assets/xsl/pretext-dynamic.xsl +65 -65
  53. package/assets/xsl/pretext-epub.xsl +1483 -1483
  54. package/assets/xsl/pretext-fo.xsl +4593 -4593
  55. package/assets/xsl/pretext-html.xsl +14791 -14791
  56. package/assets/xsl/pretext-json-manifest.xsl +150 -150
  57. package/assets/xsl/pretext-jupyter.xsl +868 -868
  58. package/assets/xsl/pretext-latex-classic.xsl +952 -952
  59. package/assets/xsl/pretext-latex-common.xsl +11563 -11563
  60. package/assets/xsl/pretext-latex.xsl +47 -47
  61. package/assets/xsl/pretext-litprog.xsl +105 -105
  62. package/assets/xsl/pretext-merge.xsl +41 -41
  63. package/assets/xsl/pretext-numbers.xsl +875 -875
  64. package/assets/xsl/pretext-revealjs.xsl +522 -522
  65. package/assets/xsl/pretext-runestone-fitb.xsl +1233 -1233
  66. package/assets/xsl/pretext-runestone-static.xsl +1438 -1438
  67. package/assets/xsl/pretext-runestone.xsl +3083 -3083
  68. package/assets/xsl/pretext-sage-doctest.xsl +289 -289
  69. package/assets/xsl/pretext-smc.xsl +492 -492
  70. package/assets/xsl/pretext-solution-manual-latex.xsl +323 -323
  71. package/assets/xsl/pretext-text-utilities.xsl +1217 -1217
  72. package/assets/xsl/pretext-text.xsl +392 -392
  73. package/assets/xsl/pretext-units.xsl +195 -195
  74. package/assets/xsl/pretext-view-source.xsl +157 -157
  75. package/assets/xsl/pretext-ww-problem-sets.xsl +391 -391
  76. package/assets/xsl/publisher-variables.xsl +4756 -4756
  77. package/assets/xsl/support/extract-math.xsl +256 -256
  78. package/assets/xsl/support/package-math.xsl +141 -141
  79. package/assets/xsl/support/play-button/play-button.svg +3 -3
  80. package/assets/xsl/support/pretext-pg-macros.xsl +110 -110
  81. package/assets/xsl/support/webpack_static_imports.xml +14 -14
  82. package/assets/xsl/tests/null.xml +3 -3
  83. package/assets/xsl/tests/pretext-text-utilities-test.xsl +333 -333
  84. package/assets/xsl/utilities/author-report.xsl +107 -107
  85. package/assets/xsl/utilities/deprecate-autoname.sed +18 -18
  86. package/assets/xsl/utilities/deprecate-index.sed +33 -33
  87. package/assets/xsl/utilities/fix-deprecations.xsl +663 -663
  88. package/assets/xsl/utilities/pretext-enhanced-source.xsl +112 -112
  89. package/assets/xsl/utilities/report-publisher-variables.xsl +162 -162
  90. package/assets/xsl/xml-to-json.xsl +326 -326
  91. package/assets/xsl/xml-to-string.xsl +328 -328
  92. package/assets/xsl-bundle.json +1 -0
  93. package/dist/{cli-C6tfAVvi.js → cli-B2LiKunq.js} +268 -233
  94. package/dist/cli.js +1 -1
  95. package/dist/host.browser.d.ts +29 -0
  96. package/dist/host.d.ts +41 -0
  97. package/dist/index.browser.d.ts +29 -0
  98. package/dist/index.browser.js +650 -0
  99. package/dist/index.d.ts +5 -0
  100. package/dist/index.js +16 -8
  101. package/dist/internal/posix-path.d.ts +59 -0
  102. package/dist/mounts.d.ts +18 -0
  103. package/dist/renderer.d.ts +14 -1
  104. package/dist/theme.d.ts +68 -0
  105. package/dist/theme.js +93 -0
  106. package/package.json +22 -4
@@ -1,13 +1,48 @@
1
- import { readFile as m, writeFile as j } from "node:fs/promises";
2
- import * as w from "node:process";
3
- import * as c from "node:path";
4
- import { fileURLToPath as K } from "node:url";
5
- import { fromXml as $ } from "xast-util-from-xml";
1
+ import { readFile as S, writeFile as L } from "node:fs/promises";
2
+ import * as m from "node:process";
3
+ import * as l from "node:path";
4
+ import { fileURLToPath as re } from "node:url";
5
+ import { fromXml as v } from "xast-util-from-xml";
6
6
  import { toXml as y } from "xast-util-to-xml";
7
- const I = /* @__PURE__ */ new Map(), W = /* @__PURE__ */ new Map();
8
- let S, Q = 0;
9
- function ee(e, r) {
10
- const t = typeof e == "string" ? e : e instanceof URL ? e.href : e.url, n = W.get(t);
7
+ import { injectThemeBridge as ne } from "./theme.js";
8
+ async function N(e, r) {
9
+ const t = l.resolve(e), n = l.resolve(t, `.${l.posix.normalize(r)}`);
10
+ if (n !== t && !n.startsWith(t + l.sep)) {
11
+ console.error(
12
+ `pretext-html: refusing to serve ${r} from outside ${t}`
13
+ );
14
+ return;
15
+ }
16
+ try {
17
+ return new Uint8Array(await S(n));
18
+ } catch {
19
+ return;
20
+ }
21
+ }
22
+ async function I(e) {
23
+ try {
24
+ return await S(e, "utf8");
25
+ } catch {
26
+ return;
27
+ }
28
+ }
29
+ let _;
30
+ function Fe(e) {
31
+ _ = e;
32
+ }
33
+ function k() {
34
+ return _ ?? process.env.PRETEXT_HTML_ASSETS ?? l.resolve(l.dirname(re(import.meta.url)), "..", "assets");
35
+ }
36
+ function oe(e, r) {
37
+ return l.join(e, r);
38
+ }
39
+ const D = /* @__PURE__ */ new Map(), H = /* @__PURE__ */ new Map();
40
+ let T, ie = 0, J = N;
41
+ function Re(e) {
42
+ J = e;
43
+ }
44
+ function se(e, r) {
45
+ const t = typeof e == "string" ? e : e instanceof URL ? e.href : e.url, n = H.get(t);
11
46
  if (n !== void 0)
12
47
  return Promise.resolve(new Response(n, { status: 200 }));
13
48
  let o;
@@ -16,38 +51,32 @@ function ee(e, r) {
16
51
  } catch {
17
52
  o = "";
18
53
  }
19
- const i = I.get(o);
20
- return i !== void 0 ? te(i, t) : S(e, r);
21
- }
22
- const F = '<?xml version="1.0"?><pretext-preview-missing-file/>';
23
- async function te(e, r) {
24
- const t = decodeURIComponent(new URL(r).pathname), n = c.resolve(e, "." + c.posix.normalize(t));
25
- if (n !== e && !n.startsWith(e + c.sep))
26
- return console.error(
27
- `pretext-html: refusing to serve ${t} from outside ${e}`
28
- ), new Response(F, { status: 200 });
29
- try {
30
- const o = await m(n);
31
- return new Response(new Uint8Array(o), { status: 200 });
32
- } catch {
33
- return new Response(F, { status: 200 });
34
- }
54
+ const i = D.get(o);
55
+ return i !== void 0 ? ce(i, t) : T(e, r);
35
56
  }
36
- function N() {
37
- S || (S = globalThis.fetch, globalThis.fetch = ee);
57
+ const ae = '<?xml version="1.0"?><pretext-preview-missing-file/>';
58
+ async function ce(e, r) {
59
+ const t = decodeURIComponent(new URL(r).pathname), n = await J(e, t);
60
+ if (n === void 0)
61
+ return new Response(ae, { status: 200 });
62
+ const o = typeof n == "string" ? n : new Uint8Array(n);
63
+ return new Response(o, { status: 200 });
38
64
  }
39
- function B(e) {
40
- N();
41
- const r = `mnt${++Q}.ptx.invalid`;
42
- return I.set(r, c.resolve(e)), `http://${r}`;
65
+ function q() {
66
+ T || (T = globalThis.fetch, globalThis.fetch = se);
43
67
  }
44
- function re(e) {
45
- I.delete(new URL(e).host);
68
+ function z(e) {
69
+ q();
70
+ const r = `mnt${++ie}.ptx.invalid`;
71
+ return D.set(r, e), `http://${r}`;
46
72
  }
47
- function ne(e, r) {
48
- N(), W.set(e, r);
73
+ function le(e) {
74
+ D.delete(new URL(e).host);
49
75
  }
50
- const oe = `<?xml version="1.0" encoding="UTF-8"?>
76
+ function ue(e, r) {
77
+ q(), H.set(e, r);
78
+ }
79
+ const fe = `<?xml version="1.0" encoding="UTF-8"?>
51
80
  <publication>
52
81
  <source>
53
82
  <directories external="../assets" generated="../generated-assets" />
@@ -57,79 +86,79 @@ const oe = `<?xml version="1.0" encoding="UTF-8"?>
57
86
  </html>
58
87
  </publication>
59
88
  `;
60
- function _(e, r) {
89
+ function G(e, r) {
61
90
  for (const t of e.children)
62
91
  if (t.type === "element" && t.name === r)
63
92
  return t;
64
93
  }
65
- function M(e, r) {
66
- let t = _(e, r);
94
+ function F(e, r) {
95
+ let t = G(e, r);
67
96
  return t || (t = { type: "element", name: r, attributes: {}, children: [] }, e.children.push(t)), t;
68
97
  }
69
- function L(e) {
98
+ function R(e) {
70
99
  if (!e)
71
- return oe;
72
- const r = $(e), t = _(r, "publication");
100
+ return fe;
101
+ const r = v(e), t = G(r, "publication");
73
102
  if (!t)
74
103
  throw new Error(
75
104
  "Invalid publication file: no root <publication> element found"
76
105
  );
77
- const n = M(t, "html"), o = M(n, "platform");
106
+ const n = F(t, "html"), o = F(n, "platform");
78
107
  return o.attributes = { ...o.attributes, portable: "yes" }, y(r);
79
108
  }
80
- const H = "http://www.w3.org/2001/XInclude", R = 64;
81
- async function ie(e, r, t) {
82
- return e.includes("include") ? (await J(e, r, t)).content : e;
109
+ const Y = "http://www.w3.org/2001/XInclude", A = 64;
110
+ async function de(e, r, t) {
111
+ return e.includes("include") ? (await V(e, r, t)).content : e;
83
112
  }
84
- async function J(e, r, t) {
85
- const n = $(e), o = {
86
- projectDir: c.resolve(t),
87
- stack: [c.resolve(r)]
88
- }, i = await x(
113
+ async function V(e, r, t) {
114
+ const n = v(e), o = {
115
+ projectDir: l.resolve(t),
116
+ stack: [l.resolve(r)]
117
+ }, i = await g(
89
118
  n,
90
- c.dirname(c.resolve(r)),
119
+ l.dirname(l.resolve(r)),
91
120
  { "": "" },
92
121
  o
93
122
  );
94
123
  return { tree: n, content: i ? y(n) : e };
95
124
  }
96
- function se(e) {
125
+ function he(e) {
97
126
  return e.data?.ptxIncludedFile;
98
127
  }
99
- function ae(e, r) {
128
+ function pe(e, r) {
100
129
  const t = e.data ?? (e.data = {});
101
130
  t.ptxIncludedFile = r;
102
131
  }
103
- function v(e, r) {
132
+ function x(e, r) {
104
133
  let t = e;
105
134
  for (const [n, o] of Object.entries(r.attributes))
106
135
  (n === "xmlns" || n.startsWith("xmlns:")) && (t === e && (t = { ...e }), t[n === "xmlns" ? "" : n.slice(6)] = o ?? "");
107
136
  return t;
108
137
  }
109
- function q(e, r) {
138
+ function Z(e, r) {
110
139
  const t = e.name.indexOf(":"), n = t === -1 ? "" : e.name.slice(0, t);
111
- return (t === -1 ? e.name : e.name.slice(t + 1)) === "include" && r[n] === H;
140
+ return (t === -1 ? e.name : e.name.slice(t + 1)) === "include" && r[n] === Y;
112
141
  }
113
- async function x(e, r, t, n) {
142
+ async function g(e, r, t, n) {
114
143
  let o = !1;
115
144
  for (let i = 0; i < e.children.length; i++) {
116
- const l = e.children[i];
117
- if (l.type !== "element")
145
+ const c = e.children[i];
146
+ if (c.type !== "element")
118
147
  continue;
119
- const s = v(t, l);
120
- if (q(l, s)) {
121
- const f = await z(
122
- l,
148
+ const s = x(t, c);
149
+ if (Z(c, s)) {
150
+ const u = await K(
151
+ c,
123
152
  r,
124
153
  s,
125
154
  n
126
155
  );
127
- e.children.splice(i, 1, ...f), i += f.length - 1, o = !0;
128
- } else await x(l, r, s, n) && (o = !0);
156
+ e.children.splice(i, 1, ...u), i += u.length - 1, o = !0;
157
+ } else await g(c, r, s, n) && (o = !0);
129
158
  }
130
159
  return o;
131
160
  }
132
- async function z(e, r, t, n) {
161
+ async function K(e, r, t, n) {
133
162
  const o = n.stack[n.stack.length - 1];
134
163
  if (e.attributes.xpointer)
135
164
  throw new Error(
@@ -138,13 +167,13 @@ async function z(e, r, t, n) {
138
167
  const i = e.attributes.href;
139
168
  if (!i)
140
169
  throw new Error(`xi:include without href (in ${o})`);
141
- let l = i;
170
+ let c = i;
142
171
  try {
143
- l = decodeURIComponent(i);
172
+ c = decodeURIComponent(i);
144
173
  } catch {
145
174
  }
146
- const s = c.resolve(r, l);
147
- if (s !== n.projectDir && !s.startsWith(n.projectDir + c.sep))
175
+ const s = l.resolve(r, c);
176
+ if (s !== n.projectDir && !s.startsWith(n.projectDir + l.sep))
148
177
  throw new Error(
149
178
  `xi:include escapes the project directory: ${i} (in ${o})`
150
179
  );
@@ -152,71 +181,69 @@ async function z(e, r, t, n) {
152
181
  throw new Error(
153
182
  `Circular xi:include: ${[...n.stack, s].join(" -> ")}`
154
183
  );
155
- if (n.stack.length >= R)
156
- throw new Error(`xi:include nesting exceeds ${R} levels`);
157
- let f;
158
- try {
159
- f = await m(s, "utf8");
160
- } catch {
161
- const u = le(e, t);
162
- if (u)
163
- return await x(u, r, t, n), u.children;
184
+ if (n.stack.length >= A)
185
+ throw new Error(`xi:include nesting exceeds ${A} levels`);
186
+ const u = await I(s);
187
+ if (u === void 0) {
188
+ const f = we(e, t);
189
+ if (f)
190
+ return await g(f, r, t, n), f.children;
164
191
  throw new Error(`xi:include target not found: ${i} (in ${o})`);
165
192
  }
166
193
  if (e.attributes.parse === "text")
167
- return [{ type: "text", value: f }];
194
+ return [{ type: "text", value: u }];
168
195
  let a;
169
196
  try {
170
- a = $(f);
171
- } catch (u) {
197
+ a = v(u);
198
+ } catch (f) {
172
199
  throw new Error(
173
- `Could not parse xi:include target ${s} as XML: ${u instanceof Error ? u.message : u}. (Included files must be well-formed, with a single root element.)`
200
+ `Could not parse xi:include target ${s} as XML: ${f instanceof Error ? f.message : f}. (Included files must be well-formed, with a single root element.)`
174
201
  );
175
202
  }
176
203
  n.stack.push(s);
177
204
  try {
178
- await x(a, c.dirname(s), { "": "" }, n);
205
+ await g(a, l.dirname(s), { "": "" }, n);
179
206
  } finally {
180
207
  n.stack.pop();
181
208
  }
182
209
  const d = a.children.filter(
183
- (u) => u.type === "element"
210
+ (f) => f.type === "element"
184
211
  );
185
- for (const u of d)
186
- ae(u, s);
212
+ for (const f of d)
213
+ pe(f, s);
187
214
  return d;
188
215
  }
189
- function A(e) {
216
+ function U(e) {
190
217
  return e.name === "docinfo";
191
218
  }
192
- async function ce(e, r, t) {
219
+ async function me(e, r, t) {
193
220
  try {
194
- const o = $(e).children.find(
221
+ const o = v(e).children.find(
195
222
  (a) => a.type === "element"
196
223
  );
197
224
  if (!o)
198
225
  return;
199
- const i = c.resolve(r), l = c.dirname(i), s = {
200
- projectDir: c.resolve(t),
226
+ const i = l.resolve(r), c = l.dirname(i), s = {
227
+ projectDir: l.resolve(t),
201
228
  stack: [i]
202
- }, f = v({ "": "" }, o);
229
+ }, u = x({ "": "" }, o);
203
230
  for (const a of o.children) {
204
231
  if (a.type !== "element")
205
232
  continue;
206
- const d = v(f, a);
207
- if (A(a))
208
- return await x(a, l, d, s), y(a);
209
- if (q(a, d)) {
210
- const h = (await z(
233
+ const d = x(u, a);
234
+ if (U(a))
235
+ return await g(a, c, d, s), y(a);
236
+ if (Z(a, d)) {
237
+ const p = (await K(
211
238
  a,
212
- l,
239
+ c,
213
240
  d,
214
241
  s
215
242
  )).find(
216
- (g) => g.type === "element" && A(g)
243
+ (w) => w.type === "element" && U(w)
217
244
  );
218
- if (h)
219
- return y(h);
245
+ if (p)
246
+ return y(p);
220
247
  continue;
221
248
  }
222
249
  break;
@@ -226,103 +253,100 @@ async function ce(e, r, t) {
226
253
  return;
227
254
  }
228
255
  }
229
- function le(e, r) {
256
+ function we(e, r) {
230
257
  for (const t of e.children) {
231
258
  if (t.type !== "element")
232
259
  continue;
233
- const n = v(r, t), o = t.name.indexOf(":"), i = o === -1 ? "" : t.name.slice(0, o);
234
- if ((o === -1 ? t.name : t.name.slice(o + 1)) === "fallback" && n[i] === H)
260
+ const n = x(r, t), o = t.name.indexOf(":"), i = o === -1 ? "" : t.name.slice(0, o);
261
+ if ((o === -1 ? t.name : t.name.slice(o + 1)) === "fallback" && n[i] === Y)
235
262
  return t;
236
263
  }
237
264
  }
238
- function G(e) {
265
+ function Q(e) {
239
266
  return typeof e == "object" && e !== null && e.type === "element";
240
267
  }
241
- function ue(e) {
268
+ function be(e) {
242
269
  return /^(pf|xhtml):/.test(e.name);
243
270
  }
244
- function fe(e, r, t) {
245
- const n = [], o = e.children.find(G);
271
+ function ge(e, r, t) {
272
+ const n = [], o = e.children.find(Q);
246
273
  if (o) {
247
274
  const i = t?.parentId ?? "root";
248
- Y(o, i, t?.position ?? 1, r, i, n);
275
+ ee(o, i, t?.position ?? 1, r, i, n);
249
276
  }
250
277
  return n;
251
278
  }
252
- function Y(e, r, t, n, o, i) {
253
- const l = se(e) ?? n;
254
- let s, f;
255
- if (ue(e))
256
- f = "root";
279
+ function ee(e, r, t, n, o, i) {
280
+ const c = he(e) ?? n;
281
+ let s, u;
282
+ if (be(e))
283
+ u = "root";
257
284
  else {
258
285
  const d = e.attributes.label ?? e.attributes["xml:id"];
259
286
  s = d ?? `${r}-${t}`, i.push({
260
287
  id: s,
261
- file: l,
288
+ file: c,
262
289
  line: e.position?.start.line ?? 1,
263
290
  column: e.position?.start.column ?? 1,
264
291
  endLine: e.position?.end.line ?? e.position?.start.line ?? 1,
265
292
  parent: o
266
- }), f = s;
293
+ }), u = s;
267
294
  }
268
295
  let a = 0;
269
296
  for (const d of e.children)
270
- G(d) && (a += 1, Y(
297
+ Q(d) && (a += 1, ee(
271
298
  d,
272
- f,
299
+ u,
273
300
  a,
274
- l,
301
+ c,
275
302
  s ?? o,
276
303
  i
277
304
  ));
278
305
  }
279
- function Ie(e, r) {
306
+ function Ae(e, r) {
280
307
  let t;
281
308
  for (const n of e)
282
309
  n.line <= r && (t = n);
283
310
  return t ?? e[0];
284
311
  }
285
- function V() {
286
- return process.env.PRETEXT_HTML_ASSETS ?? c.resolve(c.dirname(K(import.meta.url)), "..", "assets");
287
- }
288
- function de() {
289
- return c.join(V(), "xsl");
312
+ function ye() {
313
+ return oe(k(), "xsl");
290
314
  }
291
- function pe() {
292
- return c.join(V(), "preview-html.xsl");
293
- }
294
- function he() {
315
+ function xe() {
295
316
  return "Suspending" in WebAssembly;
296
317
  }
297
- function me() {
298
- if (!he())
318
+ function ve() {
319
+ if (!xe())
299
320
  throw new Error(
300
321
  "WebAssembly JSPI is not enabled. Run Node with --experimental-wasm-jspi (or use the pretext-html CLI, which re-launches itself with the flag)."
301
322
  );
302
323
  }
303
- function U(e) {
324
+ function C(e) {
304
325
  return e.includes("'") ? e.includes('"') ? `concat(${e.split("'").map((t) => `'${t}'`).join(`, "'", `)})` : `"${e}"` : `'${e}'`;
305
326
  }
306
- function we(e) {
327
+ function Ee(e) {
307
328
  return e.replace(
308
329
  /from '\.\/(https:\/\/[^']+)'/g,
309
330
  (r, t) => `from '${t}'`
310
331
  );
311
332
  }
312
- function ge(e) {
333
+ function $e(e) {
313
334
  return e.slice(0, 65536).replace(/<\?[\s\S]*?\?>/g, " ").replace(/<!--[\s\S]*?-->/g, " ").replace(/<!DOCTYPE[^>]*>/gi, " ").match(/<\s*([A-Za-z][\w:-]*)/)?.[1];
314
335
  }
315
- async function be(e, r) {
316
- const t = c.resolve(e);
317
- let n;
318
- try {
319
- n = await m(t, "utf8");
320
- } catch {
321
- return;
322
- }
323
- return ce(n, t, r);
336
+ async function O(e) {
337
+ const r = await I(e);
338
+ if (r === void 0)
339
+ throw new Error(
340
+ `Could not read ${e}. (In the browser build only absolute URLs are readable — pass sourceContent instead of relying on sourcePath.)`
341
+ );
342
+ return r;
343
+ }
344
+ async function Pe(e, r) {
345
+ const t = l.resolve(e), n = await I(t);
346
+ if (n !== void 0)
347
+ return me(n, t, r);
324
348
  }
325
- function xe(e, r, t) {
349
+ function ke(e, r, t) {
326
350
  const n = e.replace(/^\uFEFF/, "").replace(/<\?xml[\s\S]*?\?>/, "").replace(/<!DOCTYPE[^>]*>/i, ""), o = r === "chapter" || r === "part" ? "book" : "article";
327
351
  return `<pretext>
328
352
  ${t?.trim() ? `${t.trim()}
@@ -333,115 +357,124 @@ ${n}
333
357
  </pretext>
334
358
  `;
335
359
  }
336
- function C(e) {
360
+ function B(e) {
337
361
  if (e instanceof Error && /memory access|out of bounds/i.test(e.message))
338
362
  throw new Error(
339
363
  "The document is too large for the current WebAssembly build (stack overflow). Whole-book builds are a known limitation; try a smaller document, or see the README about rebuilding libxslt-wasm with a larger stack.",
340
364
  { cause: e }
341
365
  );
342
366
  }
343
- let P;
344
- async function Z() {
345
- return P || (P = (async () => {
367
+ let $;
368
+ async function te() {
369
+ return $ || ($ = (async () => {
346
370
  const [e, r] = await Promise.all([
347
371
  import("@pretextbook/libxslt-wasm"),
348
372
  import("@pretextbook/libxslt-wasm/exslt")
349
373
  ]);
350
374
  return r.registerAll(), e;
351
- })()), P;
375
+ })()), $;
352
376
  }
353
- const k = /* @__PURE__ */ new Map();
354
- async function ye(e) {
355
- const r = c.resolve(e);
356
- let t = k.get(r);
377
+ const P = /* @__PURE__ */ new Map();
378
+ async function Te(e) {
379
+ const r = e;
380
+ let t = P.get(r);
357
381
  return t || (t = (async () => {
358
- const { XmlDocument: n, XsltStylesheet: o } = await Z(), i = B(r), l = await m(pe(), "utf8"), s = await n.fromString(l, {
382
+ const { XmlDocument: n, XsltStylesheet: o } = await te(), i = z(r), c = await N(
383
+ k(),
384
+ "/preview-html.xsl"
385
+ );
386
+ if (c === void 0)
387
+ throw new Error(
388
+ `Could not read preview-html.xsl from ${k()}. Check the assets location (PRETEXT_HTML_ASSETS under Node, setAssetsBase() in the browser).`
389
+ );
390
+ const s = new TextDecoder().decode(c), u = await n.fromString(s, {
359
391
  url: `${i}/preview-html.xsl`,
360
392
  options: { noEnt: !0, dtdLoad: !0, huge: !0 }
361
393
  });
362
- return o.fromXmlDocument(s);
363
- })(), k.set(r, t), t.catch(() => k.delete(r))), t;
364
- }
365
- async function ve(e) {
366
- me();
367
- const { XmlDocument: r } = await Z(), t = await ye(e.xslDir ?? de()), n = c.resolve(e.sourcePath), o = c.resolve(
368
- e.projectDir ?? c.dirname(n)
369
- ), i = e.sourceContent ?? await m(n, "utf8"), l = c.relative(o, n);
370
- if (l.startsWith(".."))
394
+ return o.fromXmlDocument(u);
395
+ })(), P.set(r, t), t.catch(() => P.delete(r))), t;
396
+ }
397
+ async function Se(e) {
398
+ ve();
399
+ const { XmlDocument: r } = await te(), t = await Te(e.xslDir ?? ye()), n = l.resolve(e.sourcePath), o = l.resolve(
400
+ e.projectDir ?? l.dirname(n)
401
+ ), i = e.sourceContent ?? await O(n), c = l.relative(o, n);
402
+ if (c.startsWith(".."))
371
403
  throw new Error(
372
404
  `sourcePath (${n}) must live inside projectDir (${o})`
373
405
  );
374
- const s = B(o);
406
+ const s = z(o);
375
407
  try {
376
- let f, a = `${s}/__pretext-preview-publication.xml`;
408
+ let u, a = `${s}/__pretext-preview-publication.xml`;
377
409
  if (e.publicationPath) {
378
- const p = c.resolve(e.publicationPath);
379
- f = L(
380
- await m(p, "utf8")
410
+ const h = l.resolve(e.publicationPath);
411
+ u = R(
412
+ await O(h)
381
413
  );
382
- const b = c.relative(o, p);
383
- b.startsWith("..") || (a = `${s}/${b.split(c.sep).join("/")}`);
414
+ const b = l.relative(o, h);
415
+ b.startsWith("..") || (a = `${s}/${b.split(l.sep).join("/")}`);
384
416
  } else
385
- f = L();
386
- ne(a, f);
387
- let d, u;
417
+ u = R();
418
+ ue(a, u);
419
+ let d, f;
388
420
  if (e.sourceMap) {
389
- const p = await J(
421
+ const h = await V(
390
422
  i,
391
423
  n,
392
424
  o
393
425
  );
394
- d = p.tree, u = p.content;
426
+ d = h.tree, f = h.content;
395
427
  } else
396
- u = await ie(
428
+ f = await de(
397
429
  i,
398
430
  n,
399
431
  o
400
432
  );
401
- const h = ge(u);
402
- let g = { parentId: "root", position: 1 };
403
- if (h !== "pretext" && h !== "mathbook")
433
+ const p = $e(f);
434
+ let w = { parentId: "root", position: 1 };
435
+ if (p !== "pretext" && p !== "mathbook")
404
436
  if (e.fragment) {
405
- const p = e.docinfo ?? (e.docinfoSourcePath ? await be(e.docinfoSourcePath, o) : void 0);
406
- u = xe(u, h, p), g = { parentId: p?.trim() ? "root-1-2" : "root-1-1", position: 2 };
437
+ const h = e.docinfo ?? (e.docinfoSourcePath ? await Pe(e.docinfoSourcePath, o) : void 0);
438
+ f = ke(f, p, h), w = { parentId: h?.trim() ? "root-1-2" : "root-1-1", position: 2 };
407
439
  } else
408
440
  throw new Error(
409
- `The document root is <${h ?? "?"}>, not <pretext> — this looks like an xi:included fragment. Render the project's main source file instead, or pass the fragment option.`
441
+ `The document root is <${p ?? "?"}>, not <pretext> — this looks like an xi:included fragment. Render the project's main source file instead, or pass the fragment option.`
410
442
  );
411
- const T = d ? fe(d, n, g) : void 0, D = await r.fromString(u, {
412
- url: `${s}/${l.split(c.sep).join("/")}`,
443
+ const M = d ? ge(d, n, w) : void 0, X = await r.fromString(f, {
444
+ url: `${s}/${c.split(l.sep).join("/")}`,
413
445
  options: { noEnt: !0, dtdLoad: !0, huge: !0 }
414
- }), X = {
415
- publisher: U(a)
446
+ }), j = {
447
+ publisher: C(a)
416
448
  };
417
- for (const [p, b] of Object.entries(e.stringParams ?? {}))
418
- X[p] = U(b);
449
+ for (const [h, b] of Object.entries(e.stringParams ?? {}))
450
+ j[h] = C(b);
419
451
  let E;
420
452
  try {
421
- E = await t.apply(D, X);
422
- } catch (p) {
423
- throw C(p), new Error(
453
+ E = await t.apply(X, j);
454
+ } catch (h) {
455
+ throw B(h), new Error(
424
456
  "PreTeXt XSLT transform failed; see the PTX:ERROR/PTX:FATAL messages on stderr for details.",
425
- { cause: p }
457
+ { cause: h }
426
458
  );
427
459
  } finally {
428
- D.delete();
460
+ X.delete();
429
461
  }
430
462
  try {
431
- return {
432
- html: we(E.toHtmlString()),
433
- ...T ? { sourceMap: T } : {}
463
+ let h = Ee(E.toHtmlString());
464
+ return e.theme && (h = ne(h, e.theme)), {
465
+ html: h,
466
+ ...M ? { sourceMap: M } : {}
434
467
  };
435
- } catch (p) {
436
- throw C(p), p;
468
+ } catch (h) {
469
+ throw B(h), h;
437
470
  } finally {
438
471
  E.delete();
439
472
  }
440
473
  } finally {
441
- re(s);
474
+ le(s);
442
475
  }
443
476
  }
444
- const O = `Usage: pretext-html <source.ptx> [options]
477
+ const W = `Usage: pretext-html <source.ptx> [options]
445
478
 
446
479
  Render a PreTeXt document to a single standalone HTML page (portable build:
447
480
  one page, css/js/MathJax from CDN). No PreTeXt installation required.
@@ -467,19 +500,19 @@ Options:
467
500
  file/line for every element (for editor sync)
468
501
  -h, --help Show this help
469
502
  `;
470
- function $e(e) {
503
+ function Ie(e) {
471
504
  let r, t, n, o;
472
- const i = {}, l = {};
505
+ const i = {}, c = {};
473
506
  for (let s = 0; s < e.length; s++) {
474
- const f = e[s], a = () => {
507
+ const u = e[s], a = () => {
475
508
  if (s += 1, s >= e.length)
476
- throw new Error(`Missing value for ${f}`);
509
+ throw new Error(`Missing value for ${u}`);
477
510
  return e[s];
478
511
  };
479
- switch (f) {
512
+ switch (u) {
480
513
  case "-h":
481
514
  case "--help":
482
- w.stdout.write(O), w.exit(0);
515
+ m.stdout.write(W), m.exit(0);
483
516
  break;
484
517
  case "-o":
485
518
  case "--output":
@@ -507,47 +540,49 @@ function $e(e) {
507
540
  o = a(), i.sourceMap = !0;
508
541
  break;
509
542
  case "--param": {
510
- const d = a(), u = d.indexOf("=");
511
- if (u === -1)
543
+ const d = a(), f = d.indexOf("=");
544
+ if (f === -1)
512
545
  throw new Error(`--param expects name=value, got: ${d}`);
513
- l[d.slice(0, u)] = d.slice(u + 1);
546
+ c[d.slice(0, f)] = d.slice(f + 1);
514
547
  break;
515
548
  }
516
549
  default:
517
- if (f.startsWith("-"))
518
- throw new Error(`Unknown option: ${f}`);
550
+ if (u.startsWith("-"))
551
+ throw new Error(`Unknown option: ${u}`);
519
552
  if (r)
520
- throw new Error(`Unexpected extra argument: ${f}`);
521
- r = f;
553
+ throw new Error(`Unexpected extra argument: ${u}`);
554
+ r = u;
522
555
  }
523
556
  }
524
557
  if (!r)
525
558
  throw new Error(`No source file given.
526
559
 
527
- ` + O);
528
- return Object.keys(l).length > 0 && (i.stringParams = l), {
560
+ ` + W);
561
+ return Object.keys(c).length > 0 && (i.stringParams = c), {
529
562
  options: { ...i, sourcePath: r },
530
563
  output: t,
531
564
  docinfoPath: n,
532
565
  sourceMapPath: o
533
566
  };
534
567
  }
535
- async function Te(e) {
536
- const { options: r, output: t, docinfoPath: n, sourceMapPath: o } = $e(e);
537
- n && (r.docinfo = await m(n, "utf8"));
538
- const i = Date.now(), { html: l, sourceMap: s } = await ve(r);
539
- w.stderr.write(`pretext-html: rendered in ${Date.now() - i}ms
540
- `), o && s && (await j(o, JSON.stringify(s, null, 2)), w.stderr.write(`pretext-html: wrote ${o}
541
- `)), t ? (await j(t, l), w.stderr.write(`pretext-html: wrote ${t}
542
- `)) : w.stdout.write(l);
568
+ async function Ue(e) {
569
+ const { options: r, output: t, docinfoPath: n, sourceMapPath: o } = Ie(e);
570
+ n && (r.docinfo = await S(n, "utf8"));
571
+ const i = Date.now(), { html: c, sourceMap: s } = await Se(r);
572
+ m.stderr.write(`pretext-html: rendered in ${Date.now() - i}ms
573
+ `), o && s && (await L(o, JSON.stringify(s, null, 2)), m.stderr.write(`pretext-html: wrote ${o}
574
+ `)), t ? (await L(t, c), m.stderr.write(`pretext-html: wrote ${t}
575
+ `)) : m.stdout.write(c);
543
576
  }
544
577
  export {
545
- L as a,
546
- de as d,
547
- Ie as f,
548
- he as i,
549
- Te as m,
550
- $e as p,
551
- ve as r,
552
- U as x
578
+ R as a,
579
+ Re as b,
580
+ ye as d,
581
+ Ae as f,
582
+ xe as i,
583
+ Ue as m,
584
+ Ie as p,
585
+ Se as r,
586
+ Fe as s,
587
+ C as x
553
588
  };