@drghaliasri/butex 8.1.2 → 8.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/book.d.mts +19 -1
- package/dist/book.d.ts +19 -1
- package/dist/book.js +2501 -2
- package/dist/book.js.map +1 -1
- package/dist/book.mjs +2500 -2
- package/dist/book.mjs.map +1 -1
- package/dist/document-cli.js +222 -1
- package/dist/document-cli.js.map +1 -1
- package/dist/document.d.mts +1 -0
- package/dist/document.d.ts +1 -0
- package/dist/document.js +10 -2
- package/dist/document.js.map +1 -1
- package/dist/document.mjs +10 -2
- package/dist/document.mjs.map +1 -1
- package/dist/react-book.d.mts +1 -0
- package/dist/react-book.d.ts +1 -0
- package/dist/react-book.js +10 -2
- package/dist/react-book.js.map +1 -1
- package/dist/react-book.mjs +10 -2
- package/dist/react-book.mjs.map +1 -1
- package/dist/react-document-preview.d.mts +1 -0
- package/dist/react-document-preview.d.ts +1 -0
- package/dist/react-document-preview.js +10 -2
- package/dist/react-document-preview.js.map +1 -1
- package/dist/react-document-preview.mjs +10 -2
- package/dist/react-document-preview.mjs.map +1 -1
- package/dist/react-document.d.mts +1 -0
- package/dist/react-document.d.ts +1 -0
- package/dist/react-document.js +10 -2
- package/dist/react-document.js.map +1 -1
- package/dist/react-document.mjs +10 -2
- package/dist/react-document.mjs.map +1 -1
- package/package.json +1 -1
package/dist/document-cli.js
CHANGED
|
@@ -8207,6 +8207,217 @@ function validateBookSnapshot(snapshot) {
|
|
|
8207
8207
|
return { valid: resolution.issues.every((x) => x.severity !== "error"), issues: resolution.issues };
|
|
8208
8208
|
}
|
|
8209
8209
|
|
|
8210
|
+
// src/book/exportLatex.ts
|
|
8211
|
+
var SAFE_PATH = /^[A-Za-z0-9][A-Za-z0-9._-]*(\/[A-Za-z0-9][A-Za-z0-9._-]*){0,3}$/;
|
|
8212
|
+
var IMAGE_EXT = /\.(?:png|jpe?g|pdf)$/i;
|
|
8213
|
+
function escapeLatex(value) {
|
|
8214
|
+
return value.replace(/[\\{}%#$&_~^]/g, (char) => ({
|
|
8215
|
+
"\\": "\\textbackslash{}",
|
|
8216
|
+
"{": "\\{",
|
|
8217
|
+
"}": "\\}",
|
|
8218
|
+
"%": "\\%",
|
|
8219
|
+
"#": "\\#",
|
|
8220
|
+
"$": "\\$",
|
|
8221
|
+
"&": "\\&",
|
|
8222
|
+
"_": "\\_",
|
|
8223
|
+
"~": "\\textasciitilde{}",
|
|
8224
|
+
"^": "\\textasciicircum{}"
|
|
8225
|
+
})[char] ?? char);
|
|
8226
|
+
}
|
|
8227
|
+
function visitAssets(blocks, collect) {
|
|
8228
|
+
for (const block of blocks) {
|
|
8229
|
+
if (block.command === "\\includegraphics") {
|
|
8230
|
+
if (!block.asset_id || typeof block.value !== "string") {
|
|
8231
|
+
throw new BookContractError("Book images require an asset_id and relative image path");
|
|
8232
|
+
}
|
|
8233
|
+
collect(block.asset_id, block.value);
|
|
8234
|
+
}
|
|
8235
|
+
for (const item of block.items ?? []) {
|
|
8236
|
+
visitAssets(item.blocks ?? [], collect);
|
|
8237
|
+
}
|
|
8238
|
+
}
|
|
8239
|
+
}
|
|
8240
|
+
function fingerprint(files, assets) {
|
|
8241
|
+
let value = 2166136261;
|
|
8242
|
+
const payload = JSON.stringify({ files, assets });
|
|
8243
|
+
for (const byte of new TextEncoder().encode(payload)) {
|
|
8244
|
+
value = Math.imul(value ^ byte, 16777619) >>> 0;
|
|
8245
|
+
}
|
|
8246
|
+
return "fnv1a32:" + value.toString(16).padStart(8, "0");
|
|
8247
|
+
}
|
|
8248
|
+
function heading(entry, resolution) {
|
|
8249
|
+
return resolution.outline.find((item) => item.id === entry.id)?.display_label ?? entry.title;
|
|
8250
|
+
}
|
|
8251
|
+
function unitBoundary(entry, projection, unnumbered) {
|
|
8252
|
+
const numbered = (entry.type === "chapter" || entry.type === "appendix") && entry.numbered;
|
|
8253
|
+
const lines = [];
|
|
8254
|
+
if (numbered) {
|
|
8255
|
+
const target = projection.targets.find((t) => t.unitId === entry.id && t.ownerId === entry.id);
|
|
8256
|
+
if (!target?.ordinal) throw new BookContractError("Missing Book numbering projection for " + entry.id);
|
|
8257
|
+
lines.push(
|
|
8258
|
+
"\\renewcommand{\\thechapter}{" + target.ordinal + "}",
|
|
8259
|
+
"\\refstepcounter{chapter}",
|
|
8260
|
+
"\\renewcommand{\\thesection}{\\thechapter.\\arabic{section}}"
|
|
8261
|
+
);
|
|
8262
|
+
} else {
|
|
8263
|
+
lines.push(
|
|
8264
|
+
"\\setcounter{section}{0}",
|
|
8265
|
+
"\\renewcommand{\\thesection}{\\arabic{section}}"
|
|
8266
|
+
);
|
|
8267
|
+
}
|
|
8268
|
+
for (const [kind, counter] of [["fig", "figure"], ["tab", "table"], ["eq", "equation"]]) {
|
|
8269
|
+
lines.push("\\setcounter{" + counter + "}{" + (numbered ? "0" : String(unnumbered[kind])) + "}");
|
|
8270
|
+
lines.push("\\renewcommand{\\the" + counter + "}{" + (numbered ? "\\thechapter." : "") + "\\arabic{" + counter + "}}");
|
|
8271
|
+
}
|
|
8272
|
+
const title = escapeLatex(heading(entry, projection));
|
|
8273
|
+
lines.push("\\chapter*{" + title + "}");
|
|
8274
|
+
if (entry.include_in_toc) {
|
|
8275
|
+
lines.push("\\addcontentsline{toc}{chapter}{" + title + "}");
|
|
8276
|
+
}
|
|
8277
|
+
if (numbered) {
|
|
8278
|
+
const target = projection.targets.find((t) => t.unitId === entry.id && t.ownerId === entry.id);
|
|
8279
|
+
if (target) lines.push("\\label{" + target.key + "}");
|
|
8280
|
+
if (entry.label && entry.label !== target?.key) lines.push("\\label{" + entry.label + "}");
|
|
8281
|
+
} else if (entry.label) {
|
|
8282
|
+
lines.push("\\label{" + entry.label + "}");
|
|
8283
|
+
}
|
|
8284
|
+
return lines.join("\n");
|
|
8285
|
+
}
|
|
8286
|
+
function bookPreamble(snapshot) {
|
|
8287
|
+
const arabicDigits = snapshot.manifest.meta.numbering?.digit_form !== "western";
|
|
8288
|
+
const articlePreamble = getArabicXeLatexPreamble({
|
|
8289
|
+
digitsMapping: arabicDigits ? "arabicdigits" : "digits"
|
|
8290
|
+
});
|
|
8291
|
+
const withoutClass = articlePreamble.replace(/\\documentclass\[[^\]]*\]\{article\}/, "");
|
|
8292
|
+
if (withoutClass === articlePreamble) throw new BookContractError("Arabic preamble class seam has changed");
|
|
8293
|
+
return [
|
|
8294
|
+
"\\NeedsTeXFormat{LaTeX2e}",
|
|
8295
|
+
"\\ProvidesPackage{book}[2026/11/14 BuTeX Arabic Book]",
|
|
8296
|
+
withoutClass.replace(/\\usepackage/g, "\\RequirePackage"),
|
|
8297
|
+
"\\renewcommand{\\figurename}{\u0627\u0644\u0634\u0643\u0644}",
|
|
8298
|
+
"\\renewcommand{\\tablename}{\u0627\u0644\u062C\u062F\u0648\u0644}",
|
|
8299
|
+
"\\renewcommand{\\contentsname}{\u0627\u0644\u0645\u062D\u062A\u0648\u064A\u0627\u062A}"
|
|
8300
|
+
].join("\n");
|
|
8301
|
+
}
|
|
8302
|
+
function normalizeBookEquationLabels(content, unitId, projection) {
|
|
8303
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
8304
|
+
let latex = content;
|
|
8305
|
+
for (const target of projection.targets) {
|
|
8306
|
+
if (target.kind !== "eq" || !target.manualKey || target.manualKey === target.key) continue;
|
|
8307
|
+
aliases.set(target.manualKey, target.key);
|
|
8308
|
+
if (target.unitId === unitId) {
|
|
8309
|
+
const automatic = "\\label{" + target.key + "}";
|
|
8310
|
+
const manual = "\\label{" + target.manualKey + "}";
|
|
8311
|
+
latex = latex.replace(automatic + "\n" + manual, automatic);
|
|
8312
|
+
}
|
|
8313
|
+
}
|
|
8314
|
+
if (!aliases.size) return latex;
|
|
8315
|
+
return latex.replace(/\\(eqref|ref)\{([^}]*)\}/g, (_whole, command, keys) => "\\" + command + "{" + keys.split(",").map((key) => aliases.get(key.trim()) ?? key).join(",") + "}");
|
|
8316
|
+
}
|
|
8317
|
+
function book2Latex(raw, _options = {}) {
|
|
8318
|
+
const snapshot = normalizeBookSnapshot(raw);
|
|
8319
|
+
const projection = resolveBookSnapshot(snapshot);
|
|
8320
|
+
const fatal = projection.issues.filter((issue2) => issue2.severity === "error");
|
|
8321
|
+
if (fatal.length) {
|
|
8322
|
+
throw new BookContractError("Book export blocked: " + fatal[0].code + " at " + fatal[0].path);
|
|
8323
|
+
}
|
|
8324
|
+
const files = {};
|
|
8325
|
+
const assets = [];
|
|
8326
|
+
const paths = /* @__PURE__ */ new Map();
|
|
8327
|
+
const addAsset = (assetId, path) => {
|
|
8328
|
+
const tail = path.split("/").at(-1) ?? "";
|
|
8329
|
+
const extensionless = !tail.includes(".");
|
|
8330
|
+
if (!SAFE_PATH.test(path) || path.split("/").some((part) => part.startsWith(".")) || !(IMAGE_EXT.test(path) || extensionless)) {
|
|
8331
|
+
throw new BookContractError("Unsafe or unsupported Book image path: " + path);
|
|
8332
|
+
}
|
|
8333
|
+
const alias = path.toLowerCase();
|
|
8334
|
+
const prior = paths.get(alias);
|
|
8335
|
+
if (prior && prior !== assetId) {
|
|
8336
|
+
throw new BookContractError("Multiple Book assets claim the same image path: " + path);
|
|
8337
|
+
}
|
|
8338
|
+
paths.set(alias, assetId);
|
|
8339
|
+
if (!assets.some((a) => a.asset_id === assetId && a.output_path === path)) {
|
|
8340
|
+
assets.push({ asset_id: assetId, output_path: path });
|
|
8341
|
+
}
|
|
8342
|
+
};
|
|
8343
|
+
const meta = snapshot.manifest.meta;
|
|
8344
|
+
const main2 = [
|
|
8345
|
+
"\\documentclass[12pt,a4paper]{book}",
|
|
8346
|
+
"\\usepackage{book}",
|
|
8347
|
+
"\\begin{document}"
|
|
8348
|
+
];
|
|
8349
|
+
const unnumbered = { fig: 0, tab: 0, eq: 0 };
|
|
8350
|
+
let hasBibliography = false;
|
|
8351
|
+
let hasToc = false;
|
|
8352
|
+
for (const entry of snapshot.manifest.entries) {
|
|
8353
|
+
if (entry.kind === "generated") {
|
|
8354
|
+
if (entry.generated === "title-page") {
|
|
8355
|
+
main2.push(
|
|
8356
|
+
"\\begin{titlepage}\\centering",
|
|
8357
|
+
"{\\Huge " + escapeLatex(meta.title) + "\\par}",
|
|
8358
|
+
"\\vspace{2cm}",
|
|
8359
|
+
...meta.authors.map((author) => escapeLatex(author) + "\\par"),
|
|
8360
|
+
...meta.date ? [escapeLatex(meta.date) + "\\par"] : [],
|
|
8361
|
+
"\\end{titlepage}"
|
|
8362
|
+
);
|
|
8363
|
+
} else if (entry.generated === "table-of-contents") {
|
|
8364
|
+
hasToc = true;
|
|
8365
|
+
main2.push("\\tableofcontents");
|
|
8366
|
+
} else if (entry.generated === "list-of-figures") {
|
|
8367
|
+
main2.push("\\listoffigures");
|
|
8368
|
+
} else if (entry.generated === "list-of-tables") {
|
|
8369
|
+
main2.push("\\listoftables");
|
|
8370
|
+
} else if (entry.generated === "bibliography") {
|
|
8371
|
+
hasBibliography = true;
|
|
8372
|
+
main2.push("\\input{references.tex}");
|
|
8373
|
+
}
|
|
8374
|
+
continue;
|
|
8375
|
+
}
|
|
8376
|
+
const unitPath = "units/" + entry.id + ".tex";
|
|
8377
|
+
const doc = snapshot.documents[entry.id];
|
|
8378
|
+
visitAssets(doc.blocks, addAsset);
|
|
8379
|
+
const context = {
|
|
8380
|
+
unitId: entry.id,
|
|
8381
|
+
references: snapshot.references.references,
|
|
8382
|
+
targets: projection.targets
|
|
8383
|
+
};
|
|
8384
|
+
const content = normalizeBookEquationLabels(documentContentLatex(fromDocumentJson(doc), context), entry.id, projection);
|
|
8385
|
+
files[unitPath] = [unitBoundary(entry, projection, unnumbered), content, ""].join("\n\n");
|
|
8386
|
+
main2.push("\\input{" + unitPath + "}");
|
|
8387
|
+
if (!((entry.type === "chapter" || entry.type === "appendix") && entry.numbered)) {
|
|
8388
|
+
for (const kind of ["fig", "tab", "eq"]) {
|
|
8389
|
+
unnumbered[kind] += projection.targets.filter((t) => t.unitId === entry.id && t.kind === kind && t.numbered).length;
|
|
8390
|
+
}
|
|
8391
|
+
}
|
|
8392
|
+
}
|
|
8393
|
+
if (!hasToc && _options.include_toc_if_missing) {
|
|
8394
|
+
main2.splice(3, 0, "\\tableofcontents");
|
|
8395
|
+
}
|
|
8396
|
+
if (snapshot.references.references.length) {
|
|
8397
|
+
const entries = snapshot.references.references.map((ref) => " " + bibliographyEntryLatex(referenceFromJson(ref)));
|
|
8398
|
+
files["references.tex"] = [
|
|
8399
|
+
"\\begin{thebibliography}{" + String(Math.max(9, entries.length)) + "}",
|
|
8400
|
+
...entries,
|
|
8401
|
+
"\\end{thebibliography}",
|
|
8402
|
+
""
|
|
8403
|
+
].join("\n");
|
|
8404
|
+
if (!hasBibliography) main2.push("\\input{references.tex}");
|
|
8405
|
+
} else if (hasBibliography) {
|
|
8406
|
+
files["references.tex"] = "% No shared references\n";
|
|
8407
|
+
}
|
|
8408
|
+
main2.push("\\end{document}", "");
|
|
8409
|
+
files["main.tex"] = main2.join("\n\n");
|
|
8410
|
+
files["book.sty"] = bookPreamble(snapshot);
|
|
8411
|
+
const ordered = Object.fromEntries(Object.entries(files).sort(([a], [b]) => a.localeCompare(b)));
|
|
8412
|
+
return {
|
|
8413
|
+
main_file: "main.tex",
|
|
8414
|
+
files: ordered,
|
|
8415
|
+
assets,
|
|
8416
|
+
diagnostics: projection.issues,
|
|
8417
|
+
fingerprint: fingerprint(ordered, assets)
|
|
8418
|
+
};
|
|
8419
|
+
}
|
|
8420
|
+
|
|
8210
8421
|
// src/book-cli/execute.ts
|
|
8211
8422
|
function executeBookWorkerRequest(request) {
|
|
8212
8423
|
switch (request.action) {
|
|
@@ -8214,6 +8425,8 @@ function executeBookWorkerRequest(request) {
|
|
|
8214
8425
|
return { ok: true, book: resolveBookSnapshot(request.snapshot) };
|
|
8215
8426
|
case "book_validate":
|
|
8216
8427
|
return { ok: true, validation: validateBookSnapshot(request.snapshot) };
|
|
8428
|
+
case "book_export":
|
|
8429
|
+
return { ok: true, bundle: book2Latex(request.snapshot, request.options) };
|
|
8217
8430
|
case "book_commands":
|
|
8218
8431
|
return { ok: true, snapshot: applyBookCommand(request.snapshot, request.command) };
|
|
8219
8432
|
}
|
|
@@ -8223,7 +8436,8 @@ function executeBookWorkerRequest(request) {
|
|
|
8223
8436
|
var BOOK_WORKER_ROUTES = {
|
|
8224
8437
|
book_resolve: "/v1/book/resolve",
|
|
8225
8438
|
book_validate: "/v1/book/validate",
|
|
8226
|
-
book_commands: "/v1/book/commands"
|
|
8439
|
+
book_commands: "/v1/book/commands",
|
|
8440
|
+
book_export: "/v1/book/export"
|
|
8227
8441
|
};
|
|
8228
8442
|
function bookActionForPath(pathname) {
|
|
8229
8443
|
for (const [action, path] of Object.entries(BOOK_WORKER_ROUTES)) {
|
|
@@ -8249,6 +8463,13 @@ function parseBookWorkerRequest(value) {
|
|
|
8249
8463
|
}
|
|
8250
8464
|
return { action, snapshot: value.snapshot, command };
|
|
8251
8465
|
}
|
|
8466
|
+
if (action === "book_export") {
|
|
8467
|
+
const options = value.options;
|
|
8468
|
+
if (options !== void 0 && (typeof options !== "object" || options === null || Array.isArray(options) || Object.keys(options).some((key) => key !== "include_toc_if_missing") || options.include_toc_if_missing !== void 0 && typeof options.include_toc_if_missing !== "boolean")) {
|
|
8469
|
+
throw new BookContractError("Invalid Book export options");
|
|
8470
|
+
}
|
|
8471
|
+
return { action, snapshot: value.snapshot, ...options === void 0 ? {} : { options } };
|
|
8472
|
+
}
|
|
8252
8473
|
return { action, snapshot: value.snapshot };
|
|
8253
8474
|
}
|
|
8254
8475
|
|