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