diff --git a/publications/26.11.0/2::理论战线/7::大字报专栏.md b/publications/26.11.0/2::理论战线/7::大字报专栏.md index cd4bb77..19bb4f1 100644 --- a/publications/26.11.0/2::理论战线/7::大字报专栏.md +++ b/publications/26.11.0/2::理论战线/7::大字报专栏.md @@ -18,8 +18,6 @@ ### 文章要点 文中记录了作者在厂内工作培养一位工人同志的过程。这位工人同志最初幻想当保安改善自身处境,后遇到问题时,作者及时结合具体困难进行分析,纠正了工人同志的错误倾向,通过持续帮助和灌输,工人同志逐渐认清了资本主义本质,开始主动进厂并引导身边受压迫学生。 -这位工人同志还与一个线上联系工人只讲理论的偏政治报路线的读书小组进行了交流,他感觉聊不来因为讨论哲学在厂内实际工作中作用不大,这反映了政治报和融工派两条路线的差距。 - 文中案例展示了发展工人同志的有效方法,应围绕工人当下生活矛盾展开工作,在提供实际帮助的同时,持续进行思想教育和斗争,帮助发展对象在路线上达成一致。 ### 案例对我做学生群众工作的启发 diff --git a/tools/gp/README.md b/tools/gp/README.md index 408c70e..7cb5e28 100644 --- a/tools/gp/README.md +++ b/tools/gp/README.md @@ -6,6 +6,46 @@ OpenPGP 签名。 ## 代码目录 +开发文档: + +- [文档输入输出接口开发指南](docs/document-api.md):HTML 中间表示、接口契约与扩展示例。 +- [build.js 开发指南](docs/build-js.md):执行时机、注入对象、DOM 修改和配置传播边界。 +- [转换流程 draw.io 源图](docs/diagrams/document-workflow.drawio) +- [build.js 流程 draw.io 源图](docs/diagrams/build-js-workflow.drawio) + +文档转换统一使用 HTML 中间表示,公共接口从 `document/mod.ts` 导出: + +```text +DocumentInput.read() → HtmlDocument → DocumentOutput.write() → OutputResult +``` + +`PublicationInput` 读取出版物并渲染 Markdown 和模板变量;`HtmlInput` 接收已有的 +`HtmlDocument`,无需读取 Markdown 文件。中间文档包含出版物元数据、按顺序排列的 +HTML 章节(ID、标题、专栏、来源路径)、资源基址,以及封面、样式和前后附加内容。 +章节 HTML 保留原始资源引用,不含 PDF 定位标记。 + +`TextOutput` 支持 HTML、Markdown、TXT;`PdfOutput` +负责浏览器排版、目录和后处理; `EpubOutput` +负责章节链接和资源打包。图片嵌入和格式专用标记在输出端处理。 现有 Builder +方法保留作兼容入口,新代码建议使用以下接口: + +```ts +import { + convertDocument, + PublicationInput, + TextOutput, +} from "./document/mod.ts"; + +const input = new PublicationInput(config, variables); +const result = await convertDocument(input, new TextOutput(config, "html")); +console.log(result.path, result.mediaType); +``` + +同一份中间文档可交给多个输出端。新增输入实现 +`DocumentInput.read()`,新增格式实现 +`DocumentOutput.write()`,无需修改其他格式。输出接口不修改传入的中间文档。 +HTML、TXT 可分别配置 `html.path`、`txt.path`;未配置时从 `md.path` 派生。 + `main.ts` 保留为命令入口,配置和输出路径仍相对于 `tools/gp` 工作目录解析。 各模块的测试与实现放在同一目录: @@ -126,8 +166,8 @@ Markdown 和 HTML 导出会将本地图片嵌入为 Base64 data URL。EPUB 不 而是将图片作为 EPUB 包内资源保存并从章节引用,兼容 EPUB 阅读器的资源模型。 EPUB 默认使用 `md.path` 将扩展名替换为 `.epub`;也可在每个配置中设置 -`[settings.epub]` 的 `path`、`title`、`author` 和 `language`。每篇文章会拆成独立章节, -EPUB 导航目录按出版物排序链接到各章节。 +`[settings.epub]` 的 `path`、`title`、`author` 和 +`language`。每篇文章会拆成独立章节, EPUB 导航目录按出版物排序链接到各章节。 ### 本地素材缓存 @@ -223,7 +263,13 @@ draft = false 地址,默认值为 `../../assets`。例如 Markdown 中的 `![图片](pictures/rsf.png)` 会从该目录读取,不需要单独启动附件文件服务器。 -目录搜索策略可以在模板的 `build.js` 中通过 `cfg.indexSearch` 调整。默认是 +PDF 目录优先读取文章标题或首段上的唯一链接标记 +`https://wpep.invalid/article-start-编号`,直接取得起始页码;标记齐全时不会提取或搜索正文。 +合并目录时会删除这些临时链接注释及其 URL,保留标题文字和文章中的普通链接, +并将目录链接、PDF 书签指向插入目录后的对应页面。 + +只有缺少标记的文章才回退到标题全文搜索(例如处理旧 +PDF)。回退搜索策略可以在模板的 `build.js` 中通过 `cfg.indexSearch` 调整。默认是 `single_jump`:逐页扫描,每页只读取一次页面文本,并匹配该页上所有尚未匹配的文章;未匹配到标题的页面不会中断后续查找: ```js diff --git a/tools/gp/cli/handlers.ts b/tools/gp/cli/handlers.ts index 5cabdba..a9bce9e 100644 --- a/tools/gp/cli/handlers.ts +++ b/tools/gp/cli/handlers.ts @@ -1,10 +1,15 @@ //import TurndownService from "turndown"; import { args, cfg } from "../main.ts"; import { OpenPGPSigner } from "../pdf_builder/utils.ts"; -import { DocuBuilder } from "../pdf_builder/docu_builder.ts"; +import { PublicationInput } from "../document/inputs.ts"; +import { convertDocument } from "../document/types.ts"; +import { + EpubOutput, + type TextFormat, + TextOutput, +} from "../document/outputs.ts"; import { createTemplateVariables } from "../template/engine.ts"; import { prepareLocalCache } from "../assets/local_cache.ts"; -import { EpubBuilder } from "../epub_builder/epub_builder.ts"; function selectedConfigs() { return cfg.settings.filter((config: Record) => @@ -109,83 +114,38 @@ function runPdfWorker( } export async function generate_md() { - await cacheBeforeGeneration(); - try { - for (const config of selectedConfigs()) { - const builder = new DocuBuilder( - config, - await createTemplateVariables(args, config, cfg.vars), - ); - - console.debug(`Processing config: ${JSON.stringify(config)}`); - builder.with_files_sync(config.journal.from_directory); - await builder.as_markdown(); - const markdownPath = config.md?.path ?? config.markdown_output; - if (!markdownPath) { - throw new Error("Missing markdown output path in config. Use md.path."); - } - await builder.build_as_raw(markdownPath); - } - } finally { - // No browser to close in this function - } + await generateText("md"); } export async function generate_html() { - await cacheBeforeGeneration(); - for (const config of selectedConfigs()) { - const builder = new DocuBuilder( - config, - await createTemplateVariables(args, config, cfg.vars), - ); - builder.with_files_sync(config.journal.from_directory); - const html = await builder.as_html_page(); - const markdownPath = config.md?.path ?? config.markdown_output; - if (!markdownPath) { - throw new Error("Missing output path in config. Use md.path."); - } - const htmlPath = markdownPath.replace(/\.md$/i, ".html"); - await Deno.writeTextFile(htmlPath, html); - console.log(`Generated HTML: ${htmlPath}`); - } + await generateText("html"); } export async function generate_txt() { + await generateText("txt"); +} + +async function generateText(format: TextFormat) { await cacheBeforeGeneration(); - try { - for (const config of selectedConfigs()) { - const builder = new DocuBuilder( - config, - await createTemplateVariables(args, config, cfg.vars), - ); - - console.debug(`Processing config: ${JSON.stringify(config)}`); - builder.with_files_sync(config.journal.from_directory); - await builder.as_txt(); - const markdownPath = config.md?.path ?? config.markdown_output; - if (!markdownPath) { - throw new Error("Missing txt output path in config. Use md.path."); - } - const txtPath = markdownPath.replace("md", "txt"); - - await builder.build_as_raw(txtPath); - } - } finally { - // No browser to close in this function + for (const config of selectedConfigs()) { + const input = new PublicationInput( + config, + await createTemplateVariables(args, config, cfg.vars), + ); + const result = await convertDocument(input, new TextOutput(config, format)); + console.log(`Generated ${format}: ${result.path}`); } } export async function generate_epub() { await cacheBeforeGeneration(); for (const config of selectedConfigs()) { - const builder = new EpubBuilder( + const input = new PublicationInput( config, await createTemplateVariables(args, config, cfg.vars), ); - builder.with_files_sync(config.journal.from_directory); - const output = builder.outputPath(); - await builder.build(output); - console.log(`Generated EPUB: ${output}`); + const result = await convertDocument(input, new EpubOutput(config)); + console.log(`Generated EPUB: ${result.path}`); } } diff --git a/tools/gp/docs/build-js.md b/tools/gp/docs/build-js.md new file mode 100644 index 0000000..d1b6219 --- /dev/null +++ b/tools/gp/docs/build-js.md @@ -0,0 +1,120 @@ +# build.js 开发指南 + +build.js 是完整 HTML 组装后的同步处理钩子,用于调整 DOM、样式或当前构建配置。 +它由 `DocuBuilder.as_html_page()` 执行,具体实现见 +[`pdf_builder/docu_builder.ts`](../pdf_builder/docu_builder.ts)。 + +![build.js 执行流程](diagrams/build-js-workflow.svg) + +[编辑 draw.io 源图](diagrams/build-js-workflow.drawio)。 + +## 配置与执行时机 + +```toml +[settings.journal] +build_js_script = "../../templates/computer_template/build.js" +``` + +路径相对于进程工作目录解析,通常为 `tools/gp`,不相对于模板目录。 +禁用时应显式配置: + +```toml +build_js_script = false +``` + +当前判断是 `build_js_script != false`;省略字段不会自动禁用,而会尝试读取 +`undefined` 路径,因此不要省略。脚本在章节模板和 Markdown 已渲染、HTML +布局已组装、 本地章节图片已处理之后执行;PDF 的临时定位链接也已加入。 +同一中间文档被多次导出时,每次 HTML 组装都会执行一次脚本。 + +| 格式 | 是否执行 | 后续步骤 | +| -------- | -------- | ---------------------------------------- | +| PDF | 是 | 浏览器分页、目录定位及合并、书签和后处理 | +| HTML | 是 | 写入组装后的 HTML | +| Markdown | 是 | 去除样式与脚本,再由 Turndown 转换 | +| TXT | 是 | 转换 Markdown,再去除 Markdown 标记 | +| EPUB | 否 | 独立从章节 HTML 打包,未接入该钩子 | + +## 注入参数 + +脚本作为 `new Function("html", "document", "pages", "cfg", source)` +的函数体执行。 这些变量由运行器注入,不需要也不能使用模块 `import` / `export` +声明来获取: + +| 名称 | 实际值 | 修改效果 | +| ---------- | ---------------------------------------------------------------- | -------------------------------------------------------- | +| `html` | 全部片段以换行拼接的字符串 | 仅重新赋值不会改变已经解析的 DOM;需要显式返回新的字符串 | +| `document` | `DOMParser.parseFromString(html, "text/html")` 结果,可能为 null | 修改 DOM 会反映到默认序列化结果 | +| `pages` | 组装片段数组的浅复制 | 直接改数组不会自动更新 DOM;需要自行合并并返回 | +| `cfg` | 当前 DocuBuilder 的配置对象 | 可修改本次组装所使用的配置,传播边界见下文 | + +优先使用运行时的 DOMParser;不可用时动态加载 deno_dom。它是 HTML 解析器, +不是浏览器页面:这里没有排版坐标、最终页码、浏览器事件或可依赖的 `window`。 +最终页码应由 PDF 链接标记定位。 + +## DOM 修改与返回值 + +通常直接修改 `document`,无需 return: + +```js +if (document) { + for (const image of document.querySelectorAll("img")) { + image.setAttribute("loading", "eager"); + } + const style = document.createElement("style"); + style.textContent = ".passages { overflow-wrap: anywhere; }"; + document.head?.appendChild(style); +} +``` + +脚本末尾由运行器追加: + +```js +return document?.documentElement?.outerHTML ?? html; +``` + +也可显式返回完整 HTML 字符串,覆盖默认序列化,例如: + +```js +return html.replaceAll("旧刊名", "新刊名"); +``` + +自行返回时要保留布局、章节容器和 PDF 定位链接;返回非字符串没有运行时校验, +会使后续输出行为不可靠。修改 `pages` 后需要 `return pages.join("\n")` 才能生效, +这会绕过 `document` 上的修改。默认的 `outerHTML` 不包含原始 doctype。 + +## 配置修改与索引 + +现有 printable 模板设置: + +```js +cfg.indexSearch = { mode: "sequential" }; +``` + +索引默认优先读取临时文章链接,只有缺失标记时才使用全文搜索配置。 搜索支持 +`single_jump`、`sequential`、`window`;窗口模式可指定 +`beforePages`、`afterPages`。 + +新输出适配器会为 DocuBuilder 浅复制配置。顶层赋值(如 `cfg.indexSearch = ...`) +不会自动同步回流水线原始配置;嵌套对象仍可能共享引用。因此不要依赖脚本修改顶层 +配置来控制 PDF 后续索引或后处理。需要可靠配置时,在调用 `PdfOutput` 前设置 +实际传入的配置;现有 printable 的脚本赋值在新适配器路径下存在这一传播限制。 +脚本适合处理 HTML,转换流程配置应由调用方明确传入。 + +PDF 中 `https://wpep.invalid/article-start-编号` 是定位用链接:不要删除、重排为 +与章节数组不一致的顺序,或将文章标题整体替换到丢失这些链接。目录合并阶段会清除 +临时文章链接,保留正常读者链接。未添加目录的 PDF 当前不会经过该清理阶段。 + +## 运行规则与验证 + +脚本同步执行,不支持顶层 `await`;不要返回 Promise。该钩子不是沙箱:它在 Deno +进程内执行,具有进程已获准的能力。异常会中断导出,不会静默跳过。 + +先用小规模出版物验证 DOM 和最终输出,再检查 PDF 分页。对 HTML 的结构变更应确认: +专栏标题与首篇文章关系、后续文章分页、图片显示、文章定位链接,以及普通链接仍正确。 +修改共享行为后在 `tools/gp` 运行: + +```sh +nix develop -c deno test -A +nix develop -c deno check main.ts document/mod.ts pdf_builder/worker.ts +``` diff --git a/tools/gp/docs/diagrams/build-js-workflow.drawio b/tools/gp/docs/diagrams/build-js-workflow.drawio new file mode 100644 index 0000000..56808cc --- /dev/null +++ b/tools/gp/docs/diagrams/build-js-workflow.drawio @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tools/gp/docs/diagrams/build-js-workflow.svg b/tools/gp/docs/diagrams/build-js-workflow.svg new file mode 100644 index 0000000..9421720 --- /dev/null +++ b/tools/gp/docs/diagrams/build-js-workflow.svg @@ -0,0 +1 @@ +build-js-workflow<mxGraphModel adaptiveColors="auto" pageWidth="650" pageHeight="660"><root><mxCell id="0" /><mxCell id="1" parent="0" /><mxCell id="ir" value="HtmlDocument&lt;br&gt;章节 HTML 与布局" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="40" y="30" width="230" height="80" as="geometry" /></mxCell><mxCell id="assemble" value="DocuBuilder.as_html_page()&lt;br&gt;嵌入图片 · 组装 HTML · PDF 标记" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="40" y="160" width="230" height="80" as="geometry" /></mxCell><mxCell id="disabled" value="build_js_script = false&lt;br&gt;直接返回组装 HTML" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="360" y="160" width="230" height="80" as="geometry" /></mxCell><mxCell id="dom" value="启用脚本:读取文件并解析 DOM&lt;br&gt;注入 html / document / pages / cfg" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="40" y="290" width="230" height="80" as="geometry" /></mxCell><mxCell id="run" value="同步执行 build.js&lt;br&gt;修改 DOM 或显式 return 字符串" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="40" y="420" width="230" height="80" as="geometry" /></mxCell><mxCell id="serialize" value="默认序列化 documentElement&lt;br&gt;无 DOM 时回退 html" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="360" y="420" width="230" height="80" as="geometry" /></mxCell><mxCell id="output" value="继续导出 PDF / HTML / MD / TXT&lt;br&gt;EPUB 不经过此钩子" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="360" y="550" width="230" height="80" as="geometry" /></mxCell><mxCell id="e0" source="ir" target="assemble" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e1" source="assemble" target="disabled" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e2" source="assemble" target="dom" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e3" source="dom" target="run" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e4" source="run" target="serialize" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e5" source="serialize" target="output" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e6" source="disabled" target="output" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell></root></mxGraphModel>HtmlDocument章节 HTML 与布局DocuBuilder.as_html_page()嵌入图片 · 组装 HTML · PDF 标记build_js_script = false直接返回组装 HTML启用脚本:读取文件并解析 DOM注入 html / document / pages / cfg同步执行 build.js修改 DOM 或显式 return 字符串默认序列化 documentElement无 DOM 时回退 html继续导出 PDF / HTML / MD / TXTEPUB 不经过此钩子 \ No newline at end of file diff --git a/tools/gp/docs/diagrams/document-workflow.drawio b/tools/gp/docs/diagrams/document-workflow.drawio new file mode 100644 index 0000000..8475eda --- /dev/null +++ b/tools/gp/docs/diagrams/document-workflow.drawio @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tools/gp/docs/diagrams/document-workflow.svg b/tools/gp/docs/diagrams/document-workflow.svg new file mode 100644 index 0000000..d16a641 --- /dev/null +++ b/tools/gp/docs/diagrams/document-workflow.svg @@ -0,0 +1 @@ +document-workflow<mxGraphModel adaptiveColors="auto" pageWidth="900" pageHeight="570"><root><mxCell id="0" /><mxCell id="1" parent="0" /><mxCell id="publication" value="PublicationInput&lt;br&gt;出版物 → 模板与 Markdown 渲染" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="40" y="30" width="230" height="80" as="geometry" /></mxCell><mxCell id="html" value="HtmlInput&lt;br&gt;已有 HTML 与章节元数据" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="330" y="30" width="230" height="80" as="geometry" /></mxCell><mxCell id="ir" value="HtmlDocument&lt;br&gt;章节 HTML · 元数据 · 资源 · 布局" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#bfe3d8;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="185" y="160" width="230" height="80" as="geometry" /></mxCell><mxCell id="text" value="TextOutput&lt;br&gt;组装 HTML · 图片嵌入 · build.js" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="40" y="300" width="230" height="80" as="geometry" /></mxCell><mxCell id="pdf" value="PdfOutput&lt;br&gt;组装 HTML · 标记 · build.js" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="330" y="300" width="230" height="80" as="geometry" /></mxCell><mxCell id="epub" value="EpubOutput&lt;br&gt;章节链接与资源打包" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="620" y="300" width="230" height="80" as="geometry" /></mxCell><mxCell id="textfile" value="HTML / Markdown / TXT&lt;br&gt;转换并写入文件" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="40" y="450" width="230" height="80" as="geometry" /></mxCell><mxCell id="pdffile" value="PDF&lt;br&gt;排版 → 索引 → 清理标记 → 后处理" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="330" y="450" width="230" height="80" as="geometry" /></mxCell><mxCell id="epubfile" value="EPUB&lt;br&gt;导航 · OPF · XHTML · ZIP" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;" vertex="1" parent="1"><mxGeometry x="620" y="450" width="230" height="80" as="geometry" /></mxCell><mxCell id="e0" source="publication" target="ir" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e1" source="html" target="ir" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e2" source="ir" target="text" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e3" source="ir" target="pdf" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e4" source="ir" target="epub" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e5" source="text" target="textfile" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e6" source="pdf" target="pdffile" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell><mxCell id="e7" source="epub" target="epubfile" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;"><mxGeometry relative="1" as="geometry" /></mxCell></root></mxGraphModel>PublicationInput出版物 → 模板与 Markdown 渲染HtmlInput已有 HTML 与章节元数据HtmlDocument章节 HTML · 元数据 · 资源 · 布局TextOutput组装 HTML · 图片嵌入 · build.jsPdfOutput组装 HTML · 标记 · build.jsEpubOutput章节链接与资源打包HTML / Markdown / TXT转换并写入文件PDF排版 → 索引 → 清理标记 → 后处理EPUB导航 · OPF · XHTML · ZIP \ No newline at end of file diff --git a/tools/gp/docs/document-api.md b/tools/gp/docs/document-api.md new file mode 100644 index 0000000..6a2e878 --- /dev/null +++ b/tools/gp/docs/document-api.md @@ -0,0 +1,179 @@ +# 文档输入输出接口开发指南 + +所有格式以 `HtmlDocument` 为交换数据。输入端负责读取源内容、渲染模板和生成章节 +HTML;输出端负责格式转换、资源处理与写入文件。公共入口是 +[`document/mod.ts`](../document/mod.ts)。 + +![文档转换工作流程](diagrams/document-workflow.svg) + +[编辑 draw.io 源图](diagrams/document-workflow.drawio)。SVG +是文档预览,修改图后应同步更新预览。 + +## 接口契约 + +```ts +interface DocumentInput { + read(): Promise; +} + +interface DocumentOutput { + write(document: HtmlDocument): Promise; +} + +interface OutputResult { + readonly path: string; + readonly mediaType: string; +} +``` + +`convertDocument(input, output)` 依次调用 `read()` 和 `write()`,返回写入结果。 +错误直接向调用方传播;输出端应在 `finally` 中释放浏览器等资源。 +接口没有流式读取、进度、取消或多文件返回协议;需要这些能力时应显式扩展契约。 + +## HTML 中间文档 + +完整类型见 [`document/types.ts`](../document/types.ts)。 + +| 字段 | 含义与约定 | +| --------------- | ------------------------------------------------------------- | +| `kind` | 固定为 `"html"` | +| `metadata` | `title`、`author`、`language`,可选 `identifier` | +| `baseDirectory` | 出版物绝对目录,供资源边界和路径解析使用 | +| `assetBase` | 共享资源目录;`PublicationInput` 会将其规范为绝对文件系统路径 | +| `chapters` | 已排序的章节数组;数组顺序就是输出顺序 | +| `layout` | `cover`、`styles`、`before`、`after` 四个 HTML 字符串 | + +每个 `HtmlChapter` 包含唯一 `id`、`title`、`html`、绝对 `sourcePath`、文章 +`index`,以及 `column: { name, index }`。`html` 是章节片段,保留原始资源引用, +不含 PDF 临时定位链接。`sourcePath` 为 EPUB 相对图片、章节链接提供解析基址, +即使输入并非 Markdown,也应提供有意义的来源路径。 + +`readonly` 是 TypeScript +的编译期约束,不会在运行时冻结对象。输出实现不得修改输入, +需要加工时建立局部字符串或复制数据。同一份文档可顺序交给多个输出端。 当前 PDF +标记编号仍按章节数组的顺序生成,尚未直接使用 `chapter.id`;标题索引仍以 标题为 +Map 键,因此不同文章重名仍是现有索引的限制。 + +## 使用现有输入输出 + +以下示例放在 `tools/gp/` 下运行,`config` 和 `variables` 由调用方提供: + +```ts +import { + convertDocument, + EpubOutput, + PublicationInput, + TextOutput, +} from "./document/mod.ts"; + +await convertDocument( + new PublicationInput(config, variables), + new TextOutput(config, "html"), +); + +// 复用同一份 HTML,避免重复读取和渲染。 +const document = await new PublicationInput(config, variables).read(); +await new TextOutput(config, "md").write(document); +await new EpubOutput(config).write(document); +``` + +| 实现 | 处理方式 | +| -------------------------------------------------- | -------------------------------------------------------------------- | +| `PublicationInput(config, variables)` | 使用 PFiles 排序并读取出版物;渲染模板和 Markdown,生成章节 HTML | +| `HtmlInput(document)` | 直接返回调用方传入的中间文档,不读取出版物 | +| `TextOutput(config, "html" / "md" / "txt", path?)` | 组装 HTML、嵌入本地章节图片、执行 build.js,按需转为 Markdown 或 TXT | +| `PdfOutput(config, options?)` | 浏览器排版、可选目录、清理定位标记、书签、可选 qpdf 和签名 | +| `EpubOutput(config, path?)` | 从章节 HTML 生成 XHTML,重写章节链接、打包本地资源并生成 EPUB 导航 | + +PDF 配置类型为 `GPConfig`,扩展共享 `DocumentConfig`,包含 `pdf` 和 +`playwright`;导出选项见 `PDFPipelineOptions`。CLI 的 PDF Worker +使用同一组接口。 旧 `DocuBuilder` / `EpubBuilder` +方法保留作兼容入口,新扩展优先使用公共接口。 + +输出路径可使用 `md.path`、`html.path`、`txt.path`、`epub.path` 或 +`playwright.path`。HTML/TXT 未单独配置时从 `md.path` 或 `markdown_output` +派生,EPUB 也有兼容回退路径。转换器本身不选择路径,由输出适配器负责。 + +## 实现新的输入 + +例如接入数据库、远程接口或已有 HTML 文件时,实现 `read()` 并生成章节结构: + +```ts +import type { DocumentInput, HtmlDocument } from "./document/mod.ts"; + +class ArticleHtmlInput implements DocumentInput { + constructor(private readonly root: string) {} + + async read(): Promise { + return { + kind: "html", + metadata: { title: "示例刊物", author: "编辑部", language: "zh-CN" }, + baseDirectory: this.root, + assetBase: `${this.root}/assets`, + chapters: [{ + id: "article-1", + title: "示例文章", + html: await Deno.readTextFile(`${this.root}/article.html`), + sourcePath: `${this.root}/article.html`, + index: 1, + column: { name: "示例专栏", index: 1 }, + }], + layout: { cover: "", styles: "", before: "", after: "" }, + }; + } +} +``` + +`root` 应为绝对路径。输入负责确认章节顺序、ID +唯一性和元数据完整性;当前公共流水线 不提供运行时 schema +校验。模板变量应在输入阶段解析一次,不要让新输出再次解释源模板。 + +## 实现新的输出 + +新输出只依赖中间文档,不应重新读取出版物 Markdown: + +```ts +import type { + DocumentOutput, + HtmlDocument, + OutputResult, +} from "./document/mod.ts"; + +class ChapterJsonOutput implements DocumentOutput { + constructor(private readonly path: string) {} + + async write(document: HtmlDocument): Promise { + await Deno.writeTextFile( + this.path, + JSON.stringify(document.chapters, null, 2), + ); + return { path: this.path, mediaType: "application/json" }; + } +} +``` + +此示例要求父目录已存在。生产实现应负责创建输出目录、处理资源路径、释放资源, +并将失败抛给调用方。如果需要 CLI 子命令,再接入 `cli/handlers.ts` 和 +`cli/match.ts`; 无需调整其他输出实现。 + +## 资源与脚本边界 + +本地章节图片在文本和 PDF 组装阶段按共享资源基址嵌入;远程 URL 保持原样。 EPUB +根据章节来源路径和共享资源目录打包本地文件,远程资源当前不自动下载。 +封面、字体和附加布局 HTML 不一定经过相同的章节图片嵌入逻辑。 + +EPUB 当前只消费章节和元数据,不复用 PDF 布局,也不执行 build.js。 +PDF、HTML、Markdown、TXT 的脚本阶段详见 [build.js 开发指南](build-js.md)。 + +## 验证 + +在 `tools/gp/` 运行: + +```sh +nix develop -c deno test -A document/document_test.ts +nix develop -c deno check document/mod.ts main.ts pdf_builder/worker.ts +``` + +新增输入应验证排序、变量与资源基址;新增输出应使用内存构造的 `HtmlDocument` +验证内容、路径、媒体类型以及输入未被修改。PDF / EPUB +的资源和索引变更还应运行相应测试。 diff --git a/tools/gp/document/document_test.ts b/tools/gp/document/document_test.ts new file mode 100644 index 0000000..8d75bec --- /dev/null +++ b/tools/gp/document/document_test.ts @@ -0,0 +1,102 @@ +import { assertEquals, assertStringIncludes } from "@std/assert"; +import { + convertDocument, + EpubOutput, + type HtmlDocument, + HtmlInput, + PdfOutput, + PublicationInput, + TextOutput, + textOutputPath, +} from "./mod.ts"; +import { inspectEpub } from "../epub_builder/epub_builder.ts"; + +Deno.test("publication input renders chapter HTML once and preserves metadata", async () => { + const root = await Deno.makeTempDir(); + try { + await Deno.mkdir(`${root}/1::Column`); + await Deno.writeTextFile( + `${root}/1::Column/1::Title.md`, + "# {{ document.title }}\n\n{{ greeting }}", + ); + const document = await new PublicationInput({ + journal: { + from_directory: root, + template_directory: root, + build_mode: "detached", + build_js_script: false, + }, + }, { greeting: "Hello" }).read(); + assertEquals(document.chapters[0].id, "article-1"); + assertEquals(document.chapters[0].column.name, "Column"); + assertStringIncludes(document.chapters[0].html, "Hello"); + assertEquals(document.chapters[0].html.includes("article-start-"), false); + } finally { + await Deno.remove(root, { recursive: true }); + } +}); + +Deno.test("HTML input exports text and EPUB without reading publication sources", async () => { + const root = await Deno.makeTempDir(); + const document: HtmlDocument = { + kind: "html", + metadata: { title: "Book", author: "Author", language: "en" }, + baseDirectory: root, + assetBase: root, + chapters: [{ + id: "article-1", + title: "Title", + index: 1, + sourcePath: `${root}/missing.md`, + column: { name: "Column", index: 1 }, + html: "

Title

Hello world

", + }], + layout: { cover: "", styles: "", before: "", after: "" }, + }; + const original = JSON.stringify(document); + const config = { + journal: { + from_directory: `${root}/does-not-exist`, + build_js_script: false as const, + }, + md: { path: `${root}/book.md` }, + }; + try { + for (const format of ["html", "md", "txt"] as const) { + const result = await convertDocument( + new HtmlInput(document), + new TextOutput(config, format), + ); + const content = await Deno.readTextFile(result.path); + assertStringIncludes(content, "Hello"); + assertEquals(content.includes("article-start-"), false); + } + const result = await convertDocument( + new HtmlInput(document), + new EpubOutput(config), + ); + const archive = inspectEpub(await Deno.readFile(result.path)); + assertStringIncludes( + new TextDecoder().decode(archive["EPUB/text/0001-title.xhtml"]), + "Hello", + ); + assertEquals(JSON.stringify(document), original); + assertEquals( + textOutputPath({ ...config, html: { path: "custom.html" } }, "html"), + "custom.html", + ); + if (Deno.env.get("CHROMIUM_PATH")) { + const pdf = await convertDocument( + new HtmlInput(document), + new PdfOutput({ + ...config, + pdf: { generate_index: false }, + playwright: { path: `${root}/book.pdf`, format: "A4" }, + }), + ); + assertEquals((await Deno.stat(pdf.path)).size > 0, true); + } + } finally { + await Deno.remove(root, { recursive: true }); + } +}); diff --git a/tools/gp/document/inputs.ts b/tools/gp/document/inputs.ts new file mode 100644 index 0000000..8793adf --- /dev/null +++ b/tools/gp/document/inputs.ts @@ -0,0 +1,77 @@ +import { resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import { PFiles } from "../publication/file.ts"; +import { md } from "../mdit/md.ts"; +import { renderTemplate, type TemplateVariables } from "../template/engine.ts"; +import type { DocumentConfig, DocumentInput, HtmlDocument } from "./types.ts"; + +export function publicationToHtml( + files: PFiles, + config: DocumentConfig, + variables: TemplateVariables = {}, +): HtmlDocument { + const render = (source: string) => renderTemplate(source, variables); + let id = 0; + return { + kind: "html", + metadata: { + title: config.journal.title ?? "未命名出版物", + author: config.journal.author ?? "未知作者", + language: config.journal.language ?? "zh-CN", + identifier: config.journal.identifier, + }, + baseDirectory: resolve(config.journal.from_directory), + assetBase: /^file:\/\//i.test(config.asset_base ?? "") + ? fileURLToPath(config.asset_base!) + : resolve(config.asset_base ?? "../../assets"), + chapters: files.pcolumes.flatMap((column) => + column.pfiles.map((file) => ({ + id: `article-${++id}`, + title: file.meta.title, + sourcePath: resolve(file.full_name), + index: file.meta.index, + column: { name: column.name, index: column.index }, + html: md.render(renderTemplate(file.content, { + ...variables, + document: { + title: file.meta.title, + index: file.meta.index, + file: file.full_name, + }, + column: { name: column.name, index: column.index }, + })), + })) + ), + layout: { + cover: render(files.cover_html ?? ""), + styles: render(files.style_html ?? ""), + before: render(files.other_htmls.join("\n")), + after: render(files.end_html ?? ""), + }, + }; +} + +export class PublicationInput implements DocumentInput { + constructor( + private readonly config: DocumentConfig, + private readonly variables: TemplateVariables = {}, + ) {} + + async read(): Promise { + const files = new PFiles( + this.config.journal.build_mode ?? "flat", + this.config.journal.from_directory, + this.config, + ); + return publicationToHtml(files, this.config, this.variables); + } +} + +/** Allows callers and future parsers to supply HTML without publication files. */ +export class HtmlInput implements DocumentInput { + constructor(private readonly document: HtmlDocument) {} + + async read(): Promise { + return this.document; + } +} diff --git a/tools/gp/document/mod.ts b/tools/gp/document/mod.ts new file mode 100644 index 0000000..e2beb76 --- /dev/null +++ b/tools/gp/document/mod.ts @@ -0,0 +1,8 @@ +export * from "./types.ts"; +export { HtmlInput, PublicationInput } from "./inputs.ts"; +export { + EpubOutput, + PdfOutput, + TextOutput, + textOutputPath, +} from "./outputs.ts"; diff --git a/tools/gp/document/outputs.ts b/tools/gp/document/outputs.ts new file mode 100644 index 0000000..bbf7dce --- /dev/null +++ b/tools/gp/document/outputs.ts @@ -0,0 +1,96 @@ +import { dirname } from "node:path"; +import { DocuBuilder } from "../pdf_builder/docu_builder.ts"; +import { EpubBuilder } from "../epub_builder/epub_builder.ts"; +import { + type GPConfig, + type PDFPipelineOptions, + processPdfConfig, +} from "../pdf_builder/pipeline.ts"; +import type { + DocumentConfig, + DocumentOutput, + HtmlDocument, + OutputResult, +} from "./types.ts"; + +export type TextFormat = "html" | "md" | "txt"; + +export function textOutputPath( + config: DocumentConfig, + format: TextFormat, +): string { + const explicit = format === "html" + ? config.html?.path + : format === "txt" + ? config.txt?.path + : config.md?.path; + if (explicit) return explicit; + const source = config.md?.path ?? config.markdown_output; + if (!source) { + throw new Error( + `Missing ${format} output path. Use ${format}.path or md.path.`, + ); + } + return format === "md" + ? source + : /\.md$/i.test(source) + ? source.replace(/\.md$/i, `.${format}`) + : `${source}.${format}`; +} + +export class TextOutput implements DocumentOutput { + constructor( + private readonly config: DocumentConfig, + private readonly format: TextFormat, + private readonly path = textOutputPath(config, format), + ) {} + + async write(document: HtmlDocument): Promise { + const builder = new DocuBuilder({ + ...this.config, + asset_base: document.assetBase, + }); + let content: string; + if (this.format === "html") { + content = await builder.as_html_page(document, { articleMarkers: false }); + } else { + if (this.format === "md") await builder.as_markdown(document); + else await builder.as_txt(true, document); + content = builder.pages.join("\n"); + } + await Deno.mkdir(dirname(this.path), { recursive: true }); + await Deno.writeTextFile(this.path, content); + return { + path: this.path, + mediaType: this.format === "html" + ? "text/html" + : this.format === "md" + ? "text/markdown" + : "text/plain", + }; + } +} + +export class EpubOutput implements DocumentOutput { + constructor( + private readonly config: DocumentConfig, + private readonly path?: string, + ) {} + async write(document: HtmlDocument): Promise { + const builder = new EpubBuilder(this.config); + const path = this.path ?? builder.outputPath(); + await builder.build(path, document); + return { path, mediaType: "application/epub+zip" }; + } +} + +export class PdfOutput implements DocumentOutput { + constructor( + private readonly config: GPConfig, + private readonly options: PDFPipelineOptions = {}, + ) {} + async write(document: HtmlDocument): Promise { + await processPdfConfig(this.config, this.options, document); + return { path: this.config.playwright.path, mediaType: "application/pdf" }; + } +} diff --git a/tools/gp/document/types.ts b/tools/gp/document/types.ts new file mode 100644 index 0000000..6b27613 --- /dev/null +++ b/tools/gp/document/types.ts @@ -0,0 +1,73 @@ +import type { TemplateVariables } from "../template/engine.ts"; + +/** Configuration shared by publication inputs and output adapters. */ +export interface DocumentConfig { + journal: { + from_directory: string; + template_directory?: string; + build_mode?: "detached" | "flat"; + build_js_script?: string | false; + title?: string; + author?: string; + language?: string; + identifier?: string; + }; + asset_base?: string; + vars?: TemplateVariables; + md?: { path?: string }; + html?: { path?: string }; + txt?: { path?: string }; + epub?: { path?: string; title?: string; author?: string; language?: string }; + markdown_output?: string; +} + +/** An HTML fragment with stable identity and its original resource base. */ +export interface HtmlChapter { + readonly id: string; + readonly title: string; + readonly html: string; + readonly sourcePath: string; + readonly index: number; + readonly column: { readonly name: string; readonly index: number }; +} + +/** Portable intermediate representation. HTML fragments contain no PDF markers. */ +export interface HtmlDocument { + readonly kind: "html"; + readonly metadata: { + readonly title: string; + readonly author: string; + readonly language: string; + readonly identifier?: string; + }; + readonly baseDirectory: string; + readonly assetBase: string; + readonly chapters: readonly HtmlChapter[]; + readonly layout: { + readonly cover: string; + readonly styles: string; + readonly before: string; + readonly after: string; + }; +} + +export interface DocumentInput { + read(): Promise; +} + +export interface OutputResult { + readonly path: string; + readonly mediaType: string; +} + +export interface DocumentOutput { + write(document: HtmlDocument): Promise; +} + +/** The pipeline has no knowledge of publication files or output formats. */ +export async function convertDocument( + input: DocumentInput, + output: DocumentOutput, +): Promise { + return await output.write(await input.read()); +} diff --git a/tools/gp/epub_builder/epub_builder.ts b/tools/gp/epub_builder/epub_builder.ts index 516cb9f..f57e2fa 100644 --- a/tools/gp/epub_builder/epub_builder.ts +++ b/tools/gp/epub_builder/epub_builder.ts @@ -1,25 +1,11 @@ import { strToU8, unzipSync, type Zippable, zipSync } from "fflate"; import { dirname, extname, relative, resolve, sep } from "node:path"; import { PFiles } from "../publication/file.ts"; -import { md } from "../mdit/md.ts"; -import { renderTemplate, type TemplateVariables } from "../template/engine.ts"; +import type { TemplateVariables } from "../template/engine.ts"; +import { publicationToHtml } from "../document/inputs.ts"; +import type { DocumentConfig, HtmlDocument } from "../document/types.ts"; -type EpubConfig = { - journal: { - build_mode?: "detached" | "flat"; - from_directory: string; - template_directory: string; - title?: string; - author?: string; - language?: string; - identifier?: string; - }; - vars?: Record; - epub?: { path?: string; title?: string; author?: string; language?: string }; - md?: { path?: string }; - markdown_output?: string; - asset_base?: string; -}; +type EpubConfig = DocumentConfig; type EpubChapter = { id: string; @@ -188,121 +174,112 @@ export class EpubBuilder { return this; } - async build(path = this.outputPath()): Promise { - if (!this.pfiles) this.with_files_sync(); + async build( + path = this.outputPath(), + document?: HtmlDocument, + ): Promise { + if (!document && !this.pfiles) this.with_files_sync(); + const input = document ?? + publicationToHtml(this.pfiles!, this.config, this.variables); const chapters: EpubChapter[] = []; const mediaManifest = new Map(); const epubMedia = new Map(); - const allFiles = this.pfiles!; let index = 0; const sourceToHref = new Map(); - for (const column of allFiles.pcolumes) { - for (const pfile of column.pfiles) { - index += 1; - sourceToHref.set( - resolve(pfile.full_name), - `text/${slug(pfile.meta.title, index)}.xhtml`, - ); - } + for (const chapter of input.chapters) { + index += 1; + sourceToHref.set( + resolve(chapter.sourcePath), + `text/${slug(chapter.title, index)}.xhtml`, + ); } index = 0; - for (const column of allFiles.pcolumes) { - for (const pfile of column.pfiles) { - index += 1; - const id = `chapter-${index}`; - const href = `text/${slug(pfile.meta.title, index)}.xhtml`; - let html = md.render(renderTemplate(pfile.content, { - ...this.variables, - document: { - title: pfile.meta.title, - index: pfile.meta.index, - file: pfile.full_name, - }, - column: { name: column.name, index: column.index }, - })); - html = removeRepeatedLeadingTitle(html, pfile.meta.title); - const targetDir = dirname(resolve(href)); - const sourceDir = dirname(resolve(pfile.full_name)); - const media: Array<{ href: string; bytes: Uint8Array; mime: string }> = - []; - const mediaBySource = new Map(); - const knownTargets = new Set(sourceToHref.keys()); - const assetReferences = [ - ...html.matchAll(/\b(src|href)=(['"])([^'"]+)\2/gi), - ]; - for (const reference of assetReferences) { - const sourceUrl = reference[3]; - if (sourceUrl.split(/[?#]/, 1)[0].toLowerCase().endsWith(".md")) { - continue; - } - if (/^(?:[a-z][a-z0-9+.-]*:|#|\/\/)/i.test(sourceUrl)) continue; - const sourceAsset = resolve( - sourceDir, - decodeURIComponent(sourceUrl.split(/[?#]/, 1)[0]), - ); - const assetMarker = sourceUrl.match(/(?:^|\/)assets\/(.+)$/i); - const sharedAsset = assetMarker && this.config.asset_base - ? resolve(this.config.asset_base, assetMarker[1]) - : undefined; - const resolvedAsset = sharedAsset && await Deno.stat(sharedAsset) - .then((info) => info.isFile ? sharedAsset : undefined) - .catch(() => undefined) || sourceAsset; - if (knownTargets.has(sourceAsset)) continue; - const localAsset = resolvedAsset.startsWith( - `${resolve(this.config.journal.from_directory)}${sep}`, - ) || (sharedAsset != null && resolvedAsset === sharedAsset); - if (!localAsset) continue; - try { - const info = await Deno.stat(resolvedAsset); - if (!info.isFile) continue; - } catch { - continue; - } - const destination = mediaBySource.get(resolvedAsset) ?? - `media/${index}/${media.length + 1}${ - extname(resolvedAsset).toLowerCase() - }`; - if (!mediaBySource.has(resolvedAsset)) { - mediaBySource.set(resolvedAsset, destination); - const ext = extname(resolvedAsset).toLowerCase(); - media.push({ - href: destination, - bytes: await Deno.readFile(resolvedAsset), - mime: mediaType(ext), - }); - } - const escapedUrl = reference[0]; - const replacement = `${reference[1]}=${reference[2]}${ - relative(targetDir, resolve(destination)).split(sep).join("/") - }${reference[2]}`; - html = html.replace(escapedUrl, replacement); + for (const chapter of input.chapters) { + index += 1; + const id = `chapter-${index}`; + const href = `text/${slug(chapter.title, index)}.xhtml`; + let html = removeRepeatedLeadingTitle(chapter.html, chapter.title); + const targetDir = dirname(resolve(href)); + const sourceDir = dirname(resolve(chapter.sourcePath)); + const media: Array<{ href: string; bytes: Uint8Array; mime: string }> = + []; + const mediaBySource = new Map(); + const knownTargets = new Set(sourceToHref.keys()); + const assetReferences = [ + ...html.matchAll(/\b(src|href)=(['"])([^'"]+)\2/gi), + ]; + for (const reference of assetReferences) { + const sourceUrl = reference[3]; + if (sourceUrl.split(/[?#]/, 1)[0].toLowerCase().endsWith(".md")) { + continue; } - // Resolve authored links that point to another Markdown chapter inside this EPUB. - html = html.replace( - /\b(href)=(['"])([^'"]+\.md(?:#[^'"]*)?)\2/gi, - (match, attr: string, quote: string, target: string) => { - const [filePart, fragment] = target.split("#", 2); - const targetPath = resolve(sourceDir, decodeURIComponent(filePart)); - const chapterHref = sourceToHref.get(targetPath); - return chapterHref - ? `${attr}=${quote}${ - relative(targetDir, resolve(chapterHref)).split(sep).join("/") - }${fragment ? `#${fragment}` : ""}${quote}` - : match; - }, + if (/^(?:[a-z][a-z0-9+.-]*:|#|\/\/)/i.test(sourceUrl)) continue; + const sourceAsset = resolve( + sourceDir, + decodeURIComponent(sourceUrl.split(/[?#]/, 1)[0]), ); - chapters.push({ - id, - href, - title: pfile.meta.title, - html, - sourcePath: pfile.full_name, - column: column.name, - }); - for (const asset of media) { - epubMedia.set(asset.href, asset.bytes); - mediaManifest.set(asset.href, asset.mime); + const assetMarker = sourceUrl.match(/(?:^|\/)assets\/(.+)$/i); + const sharedAsset = assetMarker && input.assetBase + ? resolve(input.assetBase, assetMarker[1]) + : undefined; + const resolvedAsset = sharedAsset && await Deno.stat(sharedAsset) + .then((info) => info.isFile ? sharedAsset : undefined) + .catch(() => undefined) || sourceAsset; + if (knownTargets.has(sourceAsset)) continue; + const localAsset = resolvedAsset.startsWith( + `${input.baseDirectory}${sep}`, + ) || (sharedAsset != null && resolvedAsset === sharedAsset); + if (!localAsset) continue; + try { + const info = await Deno.stat(resolvedAsset); + if (!info.isFile) continue; + } catch { + continue; } + const destination = mediaBySource.get(resolvedAsset) ?? + `media/${index}/${media.length + 1}${ + extname(resolvedAsset).toLowerCase() + }`; + if (!mediaBySource.has(resolvedAsset)) { + mediaBySource.set(resolvedAsset, destination); + const ext = extname(resolvedAsset).toLowerCase(); + media.push({ + href: destination, + bytes: await Deno.readFile(resolvedAsset), + mime: mediaType(ext), + }); + } + const escapedUrl = reference[0]; + const replacement = `${reference[1]}=${reference[2]}${ + relative(targetDir, resolve(destination)).split(sep).join("/") + }${reference[2]}`; + html = html.replace(escapedUrl, replacement); + } + // Resolve authored links that point to another Markdown chapter inside this EPUB. + html = html.replace( + /\b(href)=(['"])([^'"]+\.md(?:#[^'"]*)?)\2/gi, + (match, attr: string, quote: string, target: string) => { + const [filePart, fragment] = target.split("#", 2); + const targetPath = resolve(sourceDir, decodeURIComponent(filePart)); + const chapterHref = sourceToHref.get(targetPath); + return chapterHref + ? `${attr}=${quote}${ + relative(targetDir, resolve(chapterHref)).split(sep).join("/") + }${fragment ? `#${fragment}` : ""}${quote}` + : match; + }, + ); + chapters.push({ + id, + href, + title: chapter.title, + html, + sourcePath: chapter.sourcePath, + column: chapter.column.name, + }); + for (const asset of media) { + epubMedia.set(asset.href, asset.bytes); + mediaManifest.set(asset.href, asset.mime); } } if (chapters.length === 0) { @@ -310,10 +287,10 @@ export class EpubBuilder { } const epub = this.config.epub ?? {}; - const title = epub.title ?? this.config.journal.title ?? "未命名出版物"; - const author = epub.author ?? this.config.journal.author ?? "未知作者"; - const language = epub.language ?? this.config.journal.language ?? "zh-CN"; - const identifier = this.config.journal.identifier ?? + const title = epub.title ?? input.metadata.title; + const author = epub.author ?? input.metadata.author; + const language = epub.language ?? input.metadata.language; + const identifier = input.metadata.identifier ?? `urn:uuid:${await this.contentIdentifier(chapters)}`; const files: Record = { mimetype: strToU8("application/epub+zip"), diff --git a/tools/gp/pdf_builder/docu_builder.ts b/tools/gp/pdf_builder/docu_builder.ts index 6fc9d33..c0cf522 100644 --- a/tools/gp/pdf_builder/docu_builder.ts +++ b/tools/gp/pdf_builder/docu_builder.ts @@ -2,7 +2,8 @@ import { Browser } from "playwright"; import { resolve } from "node:path"; import { pathToFileURL } from "node:url"; import { PFiles } from "../publication/file.ts"; -import { md } from "../mdit/md.ts"; +import { publicationToHtml } from "../document/inputs.ts"; +import type { HtmlDocument } from "../document/types.ts"; import TurndownService from "turndown"; import "openpgp"; @@ -124,9 +125,11 @@ export class DocuBuilder { ); } - async as_markdown() { + async as_markdown(document?: HtmlDocument) { const turndownService = new TurndownService(); - const html_page = await this.as_html_page(); + const html_page = await this.as_html_page(document, { + articleMarkers: false, + }); const cleaned_html = html_page.replace( /<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi, "# $1", @@ -138,9 +141,11 @@ export class DocuBuilder { this.pages = [md_page]; } - async as_txt(remove_links = true) { + async as_txt(remove_links = true, document?: HtmlDocument) { const turndownService = new TurndownService(); - const html_page = await this.as_html_page(); + const html_page = await this.as_html_page(document, { + articleMarkers: false, + }); const cleaned_html = html_page.replace( /<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi, "# $1", @@ -152,57 +157,56 @@ export class DocuBuilder { this.pages = [this.strip_markdown_symbols(md_page)]; } - async as_html_page(): Promise { + async as_html_page( + document?: HtmlDocument, + options: { articleMarkers?: boolean } = {}, + ): Promise { this.pages = []; + const htmlDocument = document ?? + (this.pfiles + ? publicationToHtml(this.pfiles, this.cfg, this.variables) + : undefined); + this.pages.push(htmlDocument?.layout.cover ?? ""); + this.pages.push(htmlDocument?.layout.styles ?? ""); + this.pages.push(htmlDocument?.layout.before ?? ""); - this.pages.push(this.render(this.pfiles?.cover_html ?? "")); - this.pages.push(this.render(this.pfiles?.style_html ?? "")); - this.pages.push(this.render(this.pfiles?.other_htmls.join("\n") ?? "")); - - if (this.pfiles != null) { - let id = 1; - this.pfiles.pcolumes.forEach((colume) => { - let c_id = 1; - this.pages.push( - `${colume.name}`, - ); - colume.pfiles.forEach((pfile) => { - const passageId = id++; - const firstPassageClass = c_id === 1 ? " first-passage" : ""; - const html = this.resolveLocalAssetImages( - md.render(this.render(pfile.content, { - document: { - title: pfile.meta.title, - index: pfile.meta.index, - file: pfile.full_name, - }, - column: { - name: colume.name, - index: colume.index, - }, - })), - ); - let foundHeading = false; - const indexedHtml = html.replace( - /]*)>([\s\S]*?)<\/h1>/i, - (_match, attributes: string, title: string) => { - foundHeading = true; - return `${title}`; - }, - ); - const markedHtml = foundHeading ? indexedHtml : indexedHtml.replace( - /]*)>([\s\S]*?)<\/p>/i, - (_match, attributes: string, content: string) => - `${content}

`, - ); - const printableHtml = markedHtml; + if (htmlDocument) { + let previousColumn: string | undefined; + let c_id = 0; + htmlDocument.chapters.forEach((chapter, index) => { + const first = previousColumn !== chapter.column.name; + if (first) { + c_id = 0; this.pages.push( - `
${printableHtml}
`, + `${chapter.column.name}`, ); - }); + } + previousColumn = chapter.column.name; + const passageId = index + 1; + const firstPassageClass = first ? " first-passage" : ""; + const html = this.resolveLocalAssetImages(chapter.html); + let foundHeading = false; + const indexedHtml = html.replace( + /]*)>([\s\S]*?)<\/h1>/i, + (_match, attributes: string, title: string) => { + foundHeading = true; + return `${title}`; + }, + ); + const markedHtml = foundHeading ? indexedHtml : indexedHtml.replace( + /]*)>([\s\S]*?)<\/p>/i, + (_match, attributes: string, content: string) => + `${content}

`, + ); + const printableHtml = options.articleMarkers === false + ? html + : markedHtml; + this.pages.push( + `
${printableHtml}
`, + ); }); } - this.pages.push(this.render(this.pfiles?.end_html ?? "")); + this.pages.push(htmlDocument?.layout.after ?? ""); if (this.cfg.journal.build_js_script != false) { const f = Deno.readTextFileSync(`${this.cfg.journal.build_js_script}`); const html = this.pages.join("\n"); diff --git a/tools/gp/pdf_builder/index_builder.ts b/tools/gp/pdf_builder/index_builder.ts index 6691ab3..a19caf3 100644 --- a/tools/gp/pdf_builder/index_builder.ts +++ b/tools/gp/pdf_builder/index_builder.ts @@ -7,9 +7,11 @@ import { PDFHexString, PDFName, PDFNumber, + PDFString, } from "pdf-lib"; import "openpgp"; import { DocuBuilder } from "./docu_builder.ts"; +import type { HtmlDocument } from "../document/types.ts"; export function normalizePdfText(text: string): string { return text.normalize("NFKC").replace( @@ -95,7 +97,11 @@ export class IndexBuilder { passage_dir: string = ""; file_path: string = ""; cfg: any; - constructor(file_path: string, cfg: any) { + constructor( + file_path: string, + cfg: any, + private readonly document?: HtmlDocument, + ) { if (cfg.journal.build_mode == "flat") { this.template_dir = cfg.journal.from_directory; this.passage_dir = cfg.journal.from_directory; @@ -112,6 +118,21 @@ export class IndexBuilder { passages_index: Map; passages_column_name: Map; } { + if (this.document) { + return { + titles: this.document.chapters.map((chapter) => chapter.title), + passages_index: new Map( + this.document.chapters.map(( + chapter, + ) => [chapter.title, chapter.index]), + ), + passages_column_name: new Map( + this.document.chapters.map(( + chapter, + ) => [chapter.title, chapter.column.name]), + ), + }; + } const pfiles = new PFiles(mode, this.passage_dir, this.cfg); const titles: Array = []; const passages_index: Map = new Map(); @@ -127,7 +148,7 @@ export class IndexBuilder { } async read_pdf() { - const parser = new PDFParse({ url: this.file_path }); + const parser = new PDFParse({ data: await Deno.readFile(this.file_path) }); try { const pdfText = await parser.getText(); this.total_pages = pdfText.total; @@ -136,30 +157,38 @@ export class IndexBuilder { await parser.destroy(); } } - //修改实现为在每页文本中查找目录标题,支持目录标题跨行的情况。通过维护一个标题游标,逐页扫描PDF文本,尝试匹配当前标题。每页文本被拆分成行,并进行规范化(去除空格和换行)。对于每个标题,尝试将连续的行合并起来进行匹配,如果找到匹配则记录该标题对应的页码和章节索引,并移动到下一个标题。这样可以更准确地处理目录标题在PDF中被拆分成多行的情况。 async build_index() { - const pdf_obj = await this.read_pdf(); + this.indexed_map.clear(); const { titles, passages_index, passages_column_name } = this .get_index_titles_from_file_name( this.cfg.journal.build_mode ?? "flat", ); - // Normalize titles (remove spaces/newlines) and require exact equality. - // Matching supports title text split across multiple PDF lines. - const pageTexts = Array.from( - { length: this.total_pages }, - (_, index) => pdf_obj.getPageText(index + 1), - ); - const searchOptions = (this.cfg.indexSearch ?? {}) as IndexSearchOptions; - const matchedPages = findTitlePages( - titles, - pageTexts, - searchOptions, - ); - const markerPages = await this.getArticleStartPages(); + const missingIndices = titles.map((_, index) => index).filter((index) => + !markerPages.has(index + 1) + ); + const fallbackPages = new Map(); + if (missingIndices.length > 0) { + console.warn( + `Missing article markers (${missingIndices.length}/${titles.length}); falling back to title search`, + ); + const pdf = await this.read_pdf(); + const pageTexts = Array.from( + { length: this.total_pages }, + (_, index) => pdf.getPageText(index + 1), + ); + const matchedPages = findTitlePages( + missingIndices.map((index) => titles[index]), + pageTexts, + (this.cfg.indexSearch ?? {}) as IndexSearchOptions, + ); + missingIndices.forEach((index, offset) => + fallbackPages.set(index, matchedPages[offset]) + ); + } for (const [index, title] of titles.entries()) { - const page = markerPages.get(index + 1) ?? matchedPages[index]; + const page = markerPages.get(index + 1) ?? fallbackPages.get(index); if (page !== undefined && !this.indexed_map.has(title)) { this.indexed_map.set(title, { page, @@ -183,6 +212,7 @@ export class IndexBuilder { private async getArticleStartPages(): Promise> { const parser = await PDFDocument.load(await Deno.readFile(this.file_path)); const pages = parser.getPages(); + this.total_pages = pages.length; const markerPages = new Map(); const markerPattern = /^https:\/\/wpep\.invalid\/article-start-(\d+)$/; @@ -198,8 +228,8 @@ export class IndexBuilder { const actionValue = annotation.lookup(PDFName.of("A")); const action = actionValue instanceof PDFDict ? actionValue : undefined; const uri = action?.lookup(PDFName.of("URI")); - const url = uri && "decodeText" in uri - ? (uri as PDFHexString).decodeText() + const url = uri instanceof PDFString || uri instanceof PDFHexString + ? uri.decodeText() : ""; const match = markerPattern.exec(url); if (match) { @@ -362,10 +392,14 @@ export class IndexBuilder { const actionValue = annotation.lookup(PDFName.of("A")); const action = actionValue instanceof PDFDict ? actionValue : undefined; const uri = action?.lookup(PDFName.of("URI")); - const url = uri && "decodeText" in uri - ? (uri as PDFHexString).decodeText() + const url = uri instanceof PDFString || uri instanceof PDFHexString + ? uri.decodeText() : ""; - return !markerPattern.test(url); + if (!markerPattern.test(url)) return true; + // Scrub the action too: detached annotation objects may still be saved. + action?.delete(PDFName.of("URI")); + annotation.delete(PDFName.of("A")); + return false; }); page.node.set(PDFName.of("Annots"), pdfDoc.context.obj(retainedRefs)); } @@ -385,7 +419,7 @@ export class IndexBuilder { } const sourcePages = sourcePdf.getPages(); let annotationsAdded = 0; - let entryIndex = 0; + const linkedPages = new Set(); sourcePages.forEach((sourcePage, pageIndex) => { const sourceAnnots = sourcePage.node.Annots(); if (!sourceAnnots) return; @@ -399,12 +433,13 @@ export class IndexBuilder { const uriText = uri && "decodeText" in uri ? (uri as any).decodeText() : ""; - if (!/^https:\/\/wpep\.invalid\/index-page-\d+$/.test(uriText)) { + const match = /^https:\/\/wpep\.invalid\/index-page-(\d+)$/.exec( + uriText, + ); + if (!match) { continue; } - const entry = sortedEntries[entryIndex++]; - if (!entry) continue; - const pageNumber = entry[1].page; + const pageNumber = Number(match[1]); const sourceRect = sourceAnnot.lookup(PDFName.of("Rect"), PDFArray); if (!sourceRect || sourceRect.size() !== 4) continue; const mappedPage = this.remap_page_after_index_insert( @@ -421,7 +456,15 @@ export class IndexBuilder { ]; const targetIndexPage = indexPages[pageIndex]; if (!targetIndexPage) continue; - sourceAnnot.set( + const targetAnnot = ctx.obj({ + Type: "Annot", + Subtype: "Link", + Rect: sourceRect.asArray().map((value) => + sourcePdf.context.lookup(value, PDFNumber).asNumber() + ), + Border: [0, 0, 0], + }); + targetAnnot.set( PDFName.of("Dest"), ctx.obj([ (destinationPage as any).ref, @@ -429,15 +472,15 @@ export class IndexBuilder { PDFNumber.of(destinationPage.getSize().height), ]), ); - sourceAnnot.delete(PDFName.of("A")); - const ref = ctx.register(sourceAnnot); + const ref = ctx.register(targetAnnot); const annots = targetIndexPage.node.Annots(); if (annots) annots.push(ref); else targetIndexPage.node.set(PDFName.of("Annots"), ctx.obj([ref])); annotationsAdded++; + linkedPages.add(pageNumber); } }); - if (annotationsAdded !== sortedEntries.length) { + if (sortedEntries.some(([, entry]) => !linkedPages.has(entry.page))) { throw new Error( `Chromium index PDF link annotation mismatch: expected ${sortedEntries.length}, found ${annotationsAdded}`, ); @@ -510,8 +553,7 @@ export class IndexBuilder { } }); - // deno-lint-ignore no-explicit-any - const catalog = (pdfDoc.catalog as any).dict; + const catalog = pdfDoc.catalog; catalog.set(PDFName.of("Outlines"), outlinesRef); if (open_outline_by_default) { catalog.set(PDFName.of("PageMode"), PDFName.of("UseOutlines")); diff --git a/tools/gp/pdf_builder/index_builder_test.ts b/tools/gp/pdf_builder/index_builder_test.ts index 24e05a0..15af01b 100644 --- a/tools/gp/pdf_builder/index_builder_test.ts +++ b/tools/gp/pdf_builder/index_builder_test.ts @@ -2,8 +2,153 @@ import { assertEquals } from "@std/assert"; import { findTitlePage, findTitlePages, + IndexBuilder, normalizePdfText, } from "./index_builder.ts"; +import { PDFArray, PDFDict, PDFDocument, PDFName, PDFString } from "pdf-lib"; + +function addLink(pdf: PDFDocument, pageIndex: number, url: string) { + const annotation = pdf.context.obj({ + Type: "Annot", + Subtype: "Link", + Rect: [10, 10, 100, 30], + A: { S: "URI", URI: PDFString.of(url) }, + }); + pdf.getPage(pageIndex).node.addAnnot(pdf.context.register(annotation)); +} + +function markerBuilder(path: string) { + const builder = new IndexBuilder(path, { + journal: { build_mode: "flat", from_directory: "." }, + }); + builder.get_index_titles_from_file_name = () => ({ + titles: ["Article A", "Article B"], + passages_index: new Map([["Article A", 1], ["Article B", 2]]), + passages_column_name: new Map([["Article A", "Column"], [ + "Article B", + "Column", + ]]), + }); + return builder; +} + +Deno.test("marker index skips text extraction and uses earliest wrapped marker", async () => { + const path = await Deno.makeTempFile({ suffix: ".pdf" }); + try { + const pdf = await PDFDocument.create(); + for (let i = 0; i < 4; i++) pdf.addPage(); + addLink(pdf, 1, "https://wpep.invalid/article-start-1"); + addLink(pdf, 2, "https://wpep.invalid/article-start-1"); + addLink(pdf, 2, "https://wpep.invalid/article-start-2"); + await Deno.writeFile(path, await pdf.save()); + const builder = markerBuilder(path); + builder.read_pdf = () => { + throw new Error("Text extraction must not run"); + }; + await builder.build_index(); + assertEquals(builder.total_pages, 4); + assertEquals([...builder.indexed_map.values()].map((entry) => entry.page), [ + 2, + 3, + ]); + } finally { + await Deno.remove(path); + } +}); + +Deno.test("missing markers fall back to text without replacing marker matches", async () => { + const path = await Deno.makeTempFile({ suffix: ".pdf" }); + try { + const pdf = await PDFDocument.create(); + pdf.addPage(); + pdf.addPage(); + pdf.getPage(0).drawText("Article A Article B"); + addLink(pdf, 1, "https://wpep.invalid/article-start-1"); + await Deno.writeFile(path, await pdf.save()); + const builder = markerBuilder(path); + await builder.build_index(); + assertEquals([...builder.indexed_map.values()].map((entry) => entry.page), [ + 2, + 1, + ]); + } finally { + await Deno.remove(path); + } +}); + +Deno.test("merge removes marker URLs, preserves reader links and retargets wrapped TOC and outlines", async () => { + const dir = await Deno.makeTempDir(); + const path = `${dir}/main.pdf`; + const indexPath = `${dir}/index.pdf`; + try { + const pdf = await PDFDocument.create(); + for (let i = 0; i < 4; i++) pdf.addPage(); + addLink(pdf, 1, "https://wpep.invalid/article-start-1"); + addLink(pdf, 2, "https://wpep.invalid/article-start-2"); + addLink(pdf, 1, "https://example.com/reader"); + pdf.getPage(1).drawText("Article A"); + await Deno.writeFile(path, await pdf.save()); + const index = await PDFDocument.create(); + index.addPage(); + addLink(index, 0, "https://wpep.invalid/index-page-2"); + addLink(index, 0, "https://wpep.invalid/index-page-2"); + addLink(index, 0, "https://wpep.invalid/index-page-3"); + await Deno.writeFile(indexPath, await index.save()); + const builder = markerBuilder(path); + await builder.build_index(); + const inserted = await builder.combine_index_pdf_to_main_pdf(indexPath, 2); + await builder.add_pdf_outline({ + inserted_index_pages: inserted, + insert_index_after_page: 2, + }); + const result = await PDFDocument.load(await Deno.readFile(path)); + assertEquals(result.getPageCount(), 5); + const links = result.getPage(2).node.Annots()!; + assertEquals(links.size(), 3); + const destinations = links.asArray().map((ref) => { + const annotation = result.context.lookup(ref, PDFDict); + return annotation.lookup(PDFName.of("Dest"), PDFArray).get(0).toString(); + }); + assertEquals(destinations, [ + result.getPage(1).ref.toString(), + result.getPage(1).ref.toString(), + result.getPage(3).ref.toString(), + ]); + const readerLinks = result.getPage(1).node.Annots()!; + assertEquals(readerLinks.size(), 1); + const reader = result.context.lookup(readerLinks.get(0), PDFDict); + assertEquals( + reader.lookup(PDFName.of("A"), PDFDict).lookup( + PDFName.of("URI"), + PDFString, + ).decodeText(), + "https://example.com/reader", + ); + const outlines = result.catalog.lookup(PDFName.of("Outlines"), PDFDict); + const first = outlines.lookup(PDFName.of("First"), PDFDict); + const second = first.lookup(PDFName.of("Next"), PDFDict); + assertEquals( + first.lookup(PDFName.of("Dest"), PDFArray).get(0).toString(), + result.getPage(1).ref.toString(), + ); + assertEquals( + second.lookup(PDFName.of("Dest"), PDFArray).get(0).toString(), + result.getPage(3).ref.toString(), + ); + for (const [, object] of result.context.enumerateIndirectObjects()) { + if (object instanceof PDFDict) { + const uri = object.lookup(PDFName.of("URI")); + if (uri instanceof PDFString) { + assertEquals(uri.decodeText().includes("article-start-"), false); + } + } + } + const cleanedText = await builder.read_pdf(); + assertEquals(cleanedText.getPageText(2).includes("Article A"), true); + } finally { + await Deno.remove(dir, { recursive: true }); + } +}); Deno.test("normalizes title whitespace and line breaks", () => { assertEquals(normalizePdfText("工人\n 解放 报"), "工人解放报"); diff --git a/tools/gp/pdf_builder/pipeline.ts b/tools/gp/pdf_builder/pipeline.ts index e6d26e4..712e2b3 100644 --- a/tools/gp/pdf_builder/pipeline.ts +++ b/tools/gp/pdf_builder/pipeline.ts @@ -2,15 +2,10 @@ import { OpenPGPSigner } from "./utils.ts"; import { DocuBuilder } from "./docu_builder.ts"; import { IndexBuilder } from "./index_builder.ts"; import type { TemplateVariables } from "../template/engine.ts"; +import { PublicationInput } from "../document/inputs.ts"; +import type { DocumentConfig, HtmlDocument } from "../document/types.ts"; -export type GPConfig = { - journal: { - from_directory: string; - template_directory?: string; - build_mode?: "detached" | "flat"; - build_js_script?: string | false; - }; - asset_base?: string; +export type GPConfig = DocumentConfig & { pdf: { generate_index?: boolean; use_qpdf?: { @@ -42,14 +37,19 @@ export type PDFPipelineOptions = { export async function processPdfConfig( config: GPConfig, options: PDFPipelineOptions = {}, + document?: HtmlDocument, ) { - const builder = new DocuBuilder(config, options.variables); + const input = document ?? + await new PublicationInput(config, options.variables).read(); + const builder = new DocuBuilder( + { ...config, asset_base: input.assetBase }, + options.variables, + ); try { await builder.with_browser(); console.debug(`Processing config: ${JSON.stringify(config)}`); - builder.with_files_sync(config.journal.from_directory); - await builder.as_html_page(); + await builder.as_html_page(input); await builder.build_as_pdf(config.playwright); } finally { await builder.close_browser(); @@ -64,6 +64,7 @@ export async function processPdfConfig( config, insertIndexAfterPage, options.keepIndexPdf ?? false, + input, ); } @@ -80,10 +81,15 @@ async function addIndexToPdf( config: GPConfig, insertIndexAfterPage = 1, keepIndexPdf = false, + document?: HtmlDocument, ) { console.log(`Adding index to PDF ${config.playwright.path}...`); - const indexBuilder = new IndexBuilder(config.playwright.path, config); + const indexBuilder = new IndexBuilder( + config.playwright.path, + config, + document, + ); await indexBuilder.build_index(); console.log( `Index entries for ${config.playwright.path}: ${indexBuilder.indexed_map.size}`, @@ -121,6 +127,7 @@ async function addIndexToPdf( } await indexBuilder.add_pdf_outline({ inserted_index_pages: insertedIndexPages, + insert_index_after_page: insertIndexAfterPage, }); if (keepIndexPdf) { diff --git a/tools/gp/pdf_builder/worker.ts b/tools/gp/pdf_builder/worker.ts index d68d3e7..f0f4104 100644 --- a/tools/gp/pdf_builder/worker.ts +++ b/tools/gp/pdf_builder/worker.ts @@ -1,4 +1,7 @@ -import { type GPConfig, processPdfConfig } from "./pipeline.ts"; +import { type GPConfig } from "./pipeline.ts"; +import { PublicationInput } from "../document/inputs.ts"; +import { PdfOutput } from "../document/outputs.ts"; +import { convertDocument } from "../document/types.ts"; import type { TemplateVariables } from "../template/engine.ts"; type WorkerRequest = { @@ -26,7 +29,10 @@ const workerGlobal = self as unknown as { workerGlobal.onmessage = async (event: MessageEvent) => { const { id, config, options } = event.data; try { - await processPdfConfig(config, options); + await convertDocument( + new PublicationInput(config, options.variables), + new PdfOutput(config, options), + ); workerGlobal.postMessage({ id, ok: true } satisfies WorkerResponse); } catch (error) { workerGlobal.postMessage(