3 Commits
Author SHA1 Message Date
Nobody d2d6ae5e58 第二期修订版 2026-11-17 16:26:24 +08:00
Nobody 33f15d9ce0 Use article link markers for PDF indexing 2026-11-17 16:26:01 +08:00
Nobody 09af3bd8c0 2026-11-12 18:34:33 +08:00
31 changed files with 1190 additions and 319 deletions

No files matched your search

@@ -18,8 +18,6 @@
### 文章要点 ### 文章要点
文中记录了作者在厂内工作培养一位工人同志的过程。这位工人同志最初幻想当保安改善自身处境,后遇到问题时,作者及时结合具体困难进行分析,纠正了工人同志的错误倾向,通过持续帮助和灌输,工人同志逐渐认清了资本主义本质,开始主动进厂并引导身边受压迫学生。 文中记录了作者在厂内工作培养一位工人同志的过程。这位工人同志最初幻想当保安改善自身处境,后遇到问题时,作者及时结合具体困难进行分析,纠正了工人同志的错误倾向,通过持续帮助和灌输,工人同志逐渐认清了资本主义本质,开始主动进厂并引导身边受压迫学生。
这位工人同志还与一个线上联系工人只讲理论的偏政治报路线的读书小组进行了交流,他感觉聊不来因为讨论哲学在厂内实际工作中作用不大,这反映了政治报和融工派两条路线的差距。
文中案例展示了发展工人同志的有效方法,应围绕工人当下生活矛盾展开工作,在提供实际帮助的同时,持续进行思想教育和斗争,帮助发展对象在路线上达成一致。 文中案例展示了发展工人同志的有效方法,应围绕工人当下生活矛盾展开工作,在提供实际帮助的同时,持续进行思想教育和斗争,帮助发展对象在路线上达成一致。
### 案例对我做学生群众工作的启发 ### 案例对我做学生群众工作的启发
+68 -4
View File
@@ -4,6 +4,64 @@ GP 是一个基于 Deno、Markdown 和 HTML 模板的刊物构建工具,可以
PDF,也可以导出合并后的 Markdown 或纯文本。PDF 支持自动生成目录、PDF 线性化和 PDF,也可以导出合并后的 Markdown 或纯文本。PDF 支持自动生成目录、PDF 线性化和
OpenPGP 签名。 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` 工作目录解析。
各模块的测试与实现放在同一目录:
```text
tools/gp/
├── main.ts # 命令入口和配置加载
├── cli/ # 子命令分发与导出处理
├── publication/ # 出版物、专栏、文章读取及排序
├── template/ # 模板变量和模板渲染
├── mdit/ # Markdown 解析及插件
├── assets/ # 外部素材缓存
├── pdf_builder/ # 文档渲染、PDF 目录、流水线及 Worker
├── epub_builder/ # EPUB 章节和资源打包
└── sync/ # WebDAV 上传、同步及清理
```
## 工作流程 ## 工作流程
```text ```text
@@ -108,8 +166,8 @@ Markdown 和 HTML 导出会将本地图片嵌入为 Base64 data URL。EPUB 不
而是将图片作为 EPUB 包内资源保存并从章节引用,兼容 EPUB 阅读器的资源模型。 而是将图片作为 EPUB 包内资源保存并从章节引用,兼容 EPUB 阅读器的资源模型。
EPUB 默认使用 `md.path` 将扩展名替换为 `.epub`;也可在每个配置中设置 EPUB 默认使用 `md.path` 将扩展名替换为 `.epub`;也可在每个配置中设置
`[settings.epub]` 的 `path`、`title`、`author` 和 `language`。每篇文章会拆成独立章节, `[settings.epub]` 的 `path`、`title`、`author` 和
EPUB 导航目录按出版物排序链接到各章节。 `language`。每篇文章会拆成独立章节, EPUB 导航目录按出版物排序链接到各章节。
### 本地素材缓存 ### 本地素材缓存
@@ -205,7 +263,13 @@ draft = false
地址,默认值为 `../../assets`。例如 Markdown 中的 `![图片](pictures/rsf.png)` 地址,默认值为 `../../assets`。例如 Markdown 中的 `![图片](pictures/rsf.png)`
会从该目录读取,不需要单独启动附件文件服务器。 会从该目录读取,不需要单独启动附件文件服务器。
目录搜索策略可以在模板的 `build.js` 中通过 `cfg.indexSearch` 调整。默认是 PDF 目录优先读取文章标题或首段上的唯一链接标记
`https://wpep.invalid/article-start-编号`,直接取得起始页码;标记齐全时不会提取或搜索正文。
合并目录时会删除这些临时链接注释及其 URL,保留标题文字和文章中的普通链接,
并将目录链接、PDF 书签指向插入目录后的对应页面。
只有缺少标记的文章才回退到标题全文搜索(例如处理旧
PDF)。回退搜索策略可以在模板的 `build.js` 中通过 `cfg.indexSearch` 调整。默认是
`single_jump`:逐页扫描,每页只读取一次页面文本,并匹配该页上所有尚未匹配的文章;未匹配到标题的页面不会中断后续查找: `single_jump`:逐页扫描,每页只读取一次页面文本,并匹配该页上所有尚未匹配的文章;未匹配到标题的页面不会中断后续查找:
```js ```js
@@ -382,7 +446,7 @@ deno test -A
deno task dev deno task dev
# 检查类型和格式 # 检查类型和格式
deno check main.ts pdf_pipeline.ts deno check main.ts pdf_builder/pipeline.ts
deno fmt --check deno fmt --check
``` ```
File renamed without changes.
File renamed without changes.
+26 -66
View File
@@ -1,10 +1,15 @@
//import TurndownService from "turndown"; //import TurndownService from "turndown";
import { args, cfg } from "../main.ts"; import { args, cfg } from "../main.ts";
import { OpenPGPSigner } from "../pdf_builder/utils.ts"; import { OpenPGPSigner } from "../pdf_builder/utils.ts";
import { DocuBuilder } from "../pdf_builder/docu_builder.ts"; import { PublicationInput } from "../document/inputs.ts";
import { createTemplateVariables } from "../template_engine.ts"; import { convertDocument } from "../document/types.ts";
import { prepareLocalCache } from "../local_cache.ts"; import {
import { EpubBuilder } from "../epub_builder.ts"; EpubOutput,
type TextFormat,
TextOutput,
} from "../document/outputs.ts";
import { createTemplateVariables } from "../template/engine.ts";
import { prepareLocalCache } from "../assets/local_cache.ts";
function selectedConfigs() { function selectedConfigs() {
return cfg.settings.filter((config: Record<string, unknown>) => return cfg.settings.filter((config: Record<string, unknown>) =>
@@ -65,7 +70,7 @@ function runPdfWorker(
return createTemplateVariables(args, config, cfg.vars).then((variables) => return createTemplateVariables(args, config, cfg.vars).then((variables) =>
new Promise<void>((resolve, reject) => { new Promise<void>((resolve, reject) => {
const worker = new Worker( const worker = new Worker(
new URL("../pdf_worker.ts", import.meta.url).href, new URL("../pdf_builder/worker.ts", import.meta.url).href,
{ {
type: "module", type: "module",
}, },
@@ -109,83 +114,38 @@ function runPdfWorker(
} }
export async function generate_md() { export async function generate_md() {
await cacheBeforeGeneration(); await generateText("md");
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
}
} }
export async function generate_html() { export async function generate_html() {
await cacheBeforeGeneration(); await generateText("html");
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}`);
}
} }
export async function generate_txt() { export async function generate_txt() {
await generateText("txt");
}
async function generateText(format: TextFormat) {
await cacheBeforeGeneration(); await cacheBeforeGeneration();
try { for (const config of selectedConfigs()) {
for (const config of selectedConfigs()) { const input = new PublicationInput(
const builder = new DocuBuilder( config,
config, await createTemplateVariables(args, config, cfg.vars),
await createTemplateVariables(args, config, cfg.vars), );
); const result = await convertDocument(input, new TextOutput(config, format));
console.log(`Generated ${format}: ${result.path}`);
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
} }
} }
export async function generate_epub() { export async function generate_epub() {
await cacheBeforeGeneration(); await cacheBeforeGeneration();
for (const config of selectedConfigs()) { for (const config of selectedConfigs()) {
const builder = new EpubBuilder( const input = new PublicationInput(
config, config,
await createTemplateVariables(args, config, cfg.vars), await createTemplateVariables(args, config, cfg.vars),
); );
builder.with_files_sync(config.journal.from_directory); const result = await convertDocument(input, new EpubOutput(config));
const output = builder.outputPath(); console.log(`Generated EPUB: ${result.path}`);
await builder.build(output);
console.log(`Generated EPUB: ${output}`);
} }
} }
+5 -1
View File
@@ -6,7 +6,11 @@ import {
generate_txt, generate_txt,
just_local_cache, just_local_cache,
} from "./handlers.ts"; } from "./handlers.ts";
import { clean_local_files, sync_webdav, upload_webdav } from "../webdav.ts"; import {
clean_local_files,
sync_webdav,
upload_webdav,
} from "../sync/webdav.ts";
export async function match_sub_command_to_function( export async function match_sub_command_to_function(
sub_command: Array<string>, sub_command: Array<string>,
+120
View File
@@ -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
```
@@ -0,0 +1 @@
<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>
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="650" height="660" viewBox="0 0 650 660" role="img"><title>build-js-workflow</title><metadata>&lt;mxGraphModel adaptiveColors=&quot;auto&quot; pageWidth=&quot;650&quot; pageHeight=&quot;660&quot;&gt;&lt;root&gt;&lt;mxCell id=&quot;0&quot; /&gt;&lt;mxCell id=&quot;1&quot; parent=&quot;0&quot; /&gt;&lt;mxCell id=&quot;ir&quot; value=&quot;HtmlDocument&amp;lt;br&amp;gt;章节 HTML 与布局&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;40&quot; y=&quot;30&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;assemble&quot; value=&quot;DocuBuilder.as_html_page()&amp;lt;br&amp;gt;嵌入图片 · 组装 HTML · PDF 标记&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;40&quot; y=&quot;160&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;disabled&quot; value=&quot;build_js_script = false&amp;lt;br&amp;gt;直接返回组装 HTML&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;360&quot; y=&quot;160&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;dom&quot; value=&quot;启用脚本:读取文件并解析 DOM&amp;lt;br&amp;gt;注入 html / document / pages / cfg&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;40&quot; y=&quot;290&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;run&quot; value=&quot;同步执行 build.js&amp;lt;br&amp;gt;修改 DOM 或显式 return 字符串&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;40&quot; y=&quot;420&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;serialize&quot; value=&quot;默认序列化 documentElement&amp;lt;br&amp;gt;无 DOM 时回退 html&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;360&quot; y=&quot;420&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;output&quot; value=&quot;继续导出 PDF / HTML / MD / TXT&amp;lt;br&amp;gt;EPUB 不经过此钩子&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;360&quot; y=&quot;550&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e0&quot; source=&quot;ir&quot; target=&quot;assemble&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGeometry relative=&quot;1&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e1&quot; source=&quot;assemble&quot; target=&quot;disabled&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGeometry relative=&quot;1&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e2&quot; source=&quot;assemble&quot; target=&quot;dom&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGeometry relative=&quot;1&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e3&quot; source=&quot;dom&quot; target=&quot;run&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGeometry relative=&quot;1&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e4&quot; source=&quot;run&quot; target=&quot;serialize&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGeometry relative=&quot;1&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e5&quot; source=&quot;serialize&quot; target=&quot;output&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGeometry relative=&quot;1&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e6&quot; source=&quot;disabled&quot; targLine truncated

After

Width:  |  Height:  |  Size: 8.9 KiB

@@ -0,0 +1 @@
<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>
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="900" height="570" viewBox="0 0 900 570" role="img"><title>document-workflow</title><metadata>&lt;mxGraphModel adaptiveColors=&quot;auto&quot; pageWidth=&quot;900&quot; pageHeight=&quot;570&quot;&gt;&lt;root&gt;&lt;mxCell id=&quot;0&quot; /&gt;&lt;mxCell id=&quot;1&quot; parent=&quot;0&quot; /&gt;&lt;mxCell id=&quot;publication&quot; value=&quot;PublicationInput&amp;lt;br&amp;gt;出版物 → 模板与 Markdown 渲染&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;40&quot; y=&quot;30&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;html&quot; value=&quot;HtmlInput&amp;lt;br&amp;gt;已有 HTML 与章节元数据&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#d9eee8;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;330&quot; y=&quot;30&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;ir&quot; value=&quot;HtmlDocument&amp;lt;br&amp;gt;章节 HTML · 元数据 · 资源 · 布局&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#bfe3d8;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;185&quot; y=&quot;160&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;text&quot; value=&quot;TextOutput&amp;lt;br&amp;gt;组装 HTML · 图片嵌入 · build.js&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;40&quot; y=&quot;300&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;pdf&quot; value=&quot;PdfOutput&amp;lt;br&amp;gt;组装 HTML · 标记 · build.js&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;330&quot; y=&quot;300&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;epub&quot; value=&quot;EpubOutput&amp;lt;br&amp;gt;章节链接与资源打包&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#e5e4f5;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;620&quot; y=&quot;300&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;textfile&quot; value=&quot;HTML / Markdown / TXT&amp;lt;br&amp;gt;转换并写入文件&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;40&quot; y=&quot;450&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;pdffile&quot; value=&quot;PDF&amp;lt;br&amp;gt;排版 → 索引 → 清理标记 → 后处理&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;330&quot; y=&quot;450&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;epubfile&quot; value=&quot;EPUB&amp;lt;br&amp;gt;导航 · OPF · XHTML · ZIP&quot; style=&quot;rounded=1;whiteSpace=wrap;html=1;fillColor=#fff0cf;strokeColor=#52716b;fontSize=14;&quot; vertex=&quot;1&quot; parent=&quot;1&quot;&gt;&lt;mxGeometry x=&quot;620&quot; y=&quot;450&quot; width=&quot;230&quot; height=&quot;80&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e0&quot; source=&quot;publication&quot; target=&quot;ir&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGeometry relative=&quot;1&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e1&quot; source=&quot;html&quot; target=&quot;ir&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGeometry relative=&quot;1&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e2&quot; source=&quot;ir&quot; target=&quot;text&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGeometry relative=&quot;1&quot; as=&quot;geometry&quot; /&gt;&lt;/mxCell&gt;&lt;mxCell id=&quot;e3&quot; source=&quot;ir&quot; target=&quot;pdf&quot; edge=&quot;1&quot; parent=&quot;1&quot; style=&quot;edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=block;&quot;&gt;&lt;mxGLine truncated

After

Width:  |  Height:  |  Size: 11 KiB

+179
View File
@@ -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<HtmlDocument>;
}
interface DocumentOutput {
write(document: HtmlDocument): Promise<OutputResult>;
}
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<HtmlDocument> {
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<OutputResult> {
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
的资源和索引变更还应运行相应测试。
+102
View File
@@ -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: "<h1>Title</h1><p>Hello <strong>world</strong></p>",
}],
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 });
}
});
+77
View File
@@ -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<HtmlDocument> {
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<HtmlDocument> {
return this.document;
}
}
+8
View File
@@ -0,0 +1,8 @@
export * from "./types.ts";
export { HtmlInput, PublicationInput } from "./inputs.ts";
export {
EpubOutput,
PdfOutput,
TextOutput,
textOutputPath,
} from "./outputs.ts";
+96
View File
@@ -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<OutputResult> {
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<OutputResult> {
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<OutputResult> {
await processPdfConfig(this.config, this.options, document);
return { path: this.config.playwright.path, mediaType: "application/pdf" };
}
}
+73
View File
@@ -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<HtmlDocument>;
}
export interface OutputResult {
readonly path: string;
readonly mediaType: string;
}
export interface DocumentOutput {
write(document: HtmlDocument): Promise<OutputResult>;
}
/** The pipeline has no knowledge of publication files or output formats. */
export async function convertDocument(
input: DocumentInput,
output: DocumentOutput,
): Promise<OutputResult> {
return await output.write(await input.read());
}
@@ -1,25 +1,11 @@
import { strToU8, unzipSync, type Zippable, zipSync } from "fflate"; import { strToU8, unzipSync, type Zippable, zipSync } from "fflate";
import { dirname, extname, relative, resolve, sep } from "node:path"; import { dirname, extname, relative, resolve, sep } from "node:path";
import { PFiles } from "./file.ts"; import { PFiles } from "../publication/file.ts";
import { md } from "./mdit/md.ts"; import type { TemplateVariables } from "../template/engine.ts";
import { renderTemplate, type TemplateVariables } from "./template_engine.ts"; import { publicationToHtml } from "../document/inputs.ts";
import type { DocumentConfig, HtmlDocument } from "../document/types.ts";
type EpubConfig = { type EpubConfig = DocumentConfig;
journal: {
build_mode?: "detached" | "flat";
from_directory: string;
template_directory: string;
title?: string;
author?: string;
language?: string;
identifier?: string;
};
vars?: Record<string, unknown>;
epub?: { path?: string; title?: string; author?: string; language?: string };
md?: { path?: string };
markdown_output?: string;
asset_base?: string;
};
type EpubChapter = { type EpubChapter = {
id: string; id: string;
@@ -188,121 +174,112 @@ export class EpubBuilder {
return this; return this;
} }
async build(path = this.outputPath()): Promise<void> { async build(
if (!this.pfiles) this.with_files_sync(); path = this.outputPath(),
document?: HtmlDocument,
): Promise<void> {
if (!document && !this.pfiles) this.with_files_sync();
const input = document ??
publicationToHtml(this.pfiles!, this.config, this.variables);
const chapters: EpubChapter[] = []; const chapters: EpubChapter[] = [];
const mediaManifest = new Map<string, string>(); const mediaManifest = new Map<string, string>();
const epubMedia = new Map<string, Uint8Array>(); const epubMedia = new Map<string, Uint8Array>();
const allFiles = this.pfiles!;
let index = 0; let index = 0;
const sourceToHref = new Map<string, string>(); const sourceToHref = new Map<string, string>();
for (const column of allFiles.pcolumes) { for (const chapter of input.chapters) {
for (const pfile of column.pfiles) { index += 1;
index += 1; sourceToHref.set(
sourceToHref.set( resolve(chapter.sourcePath),
resolve(pfile.full_name), `text/${slug(chapter.title, index)}.xhtml`,
`text/${slug(pfile.meta.title, index)}.xhtml`, );
);
}
} }
index = 0; index = 0;
for (const column of allFiles.pcolumes) { for (const chapter of input.chapters) {
for (const pfile of column.pfiles) { index += 1;
index += 1; const id = `chapter-${index}`;
const id = `chapter-${index}`; const href = `text/${slug(chapter.title, index)}.xhtml`;
const href = `text/${slug(pfile.meta.title, index)}.xhtml`; let html = removeRepeatedLeadingTitle(chapter.html, chapter.title);
let html = md.render(renderTemplate(pfile.content, { const targetDir = dirname(resolve(href));
...this.variables, const sourceDir = dirname(resolve(chapter.sourcePath));
document: { const media: Array<{ href: string; bytes: Uint8Array; mime: string }> =
title: pfile.meta.title, [];
index: pfile.meta.index, const mediaBySource = new Map<string, string>();
file: pfile.full_name, const knownTargets = new Set(sourceToHref.keys());
}, const assetReferences = [
column: { name: column.name, index: column.index }, ...html.matchAll(/\b(src|href)=(['"])([^'"]+)\2/gi),
})); ];
html = removeRepeatedLeadingTitle(html, pfile.meta.title); for (const reference of assetReferences) {
const targetDir = dirname(resolve(href)); const sourceUrl = reference[3];
const sourceDir = dirname(resolve(pfile.full_name)); if (sourceUrl.split(/[?#]/, 1)[0].toLowerCase().endsWith(".md")) {
const media: Array<{ href: string; bytes: Uint8Array; mime: string }> = continue;
[];
const mediaBySource = new Map<string, string>();
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);
} }
// Resolve authored links that point to another Markdown chapter inside this EPUB. if (/^(?:[a-z][a-z0-9+.-]*:|#|\/\/)/i.test(sourceUrl)) continue;
html = html.replace( const sourceAsset = resolve(
/\b(href)=(['"])([^'"]+\.md(?:#[^'"]*)?)\2/gi, sourceDir,
(match, attr: string, quote: string, target: string) => { decodeURIComponent(sourceUrl.split(/[?#]/, 1)[0]),
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({ const assetMarker = sourceUrl.match(/(?:^|\/)assets\/(.+)$/i);
id, const sharedAsset = assetMarker && input.assetBase
href, ? resolve(input.assetBase, assetMarker[1])
title: pfile.meta.title, : undefined;
html, const resolvedAsset = sharedAsset && await Deno.stat(sharedAsset)
sourcePath: pfile.full_name, .then((info) => info.isFile ? sharedAsset : undefined)
column: column.name, .catch(() => undefined) || sourceAsset;
}); if (knownTargets.has(sourceAsset)) continue;
for (const asset of media) { const localAsset = resolvedAsset.startsWith(
epubMedia.set(asset.href, asset.bytes); `${input.baseDirectory}${sep}`,
mediaManifest.set(asset.href, asset.mime); ) || (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) { if (chapters.length === 0) {
@@ -310,10 +287,10 @@ export class EpubBuilder {
} }
const epub = this.config.epub ?? {}; const epub = this.config.epub ?? {};
const title = epub.title ?? this.config.journal.title ?? "未命名出版物"; const title = epub.title ?? input.metadata.title;
const author = epub.author ?? this.config.journal.author ?? "未知作者"; const author = epub.author ?? input.metadata.author;
const language = epub.language ?? this.config.journal.language ?? "zh-CN"; const language = epub.language ?? input.metadata.language;
const identifier = this.config.journal.identifier ?? const identifier = input.metadata.identifier ??
`urn:uuid:${await this.contentIdentifier(chapters)}`; `urn:uuid:${await this.contentIdentifier(chapters)}`;
const files: Record<string, Uint8Array> = { const files: Record<string, Uint8Array> = {
mimetype: strToU8("application/epub+zip"), mimetype: strToU8("application/epub+zip"),
File renamed without changes.
+1 -2
View File
@@ -30,7 +30,6 @@ import { assertEquals } from "@std/assert";
// async fn() { // async fn() {
// }, // },
// }); // });
import { ripgrep } from "ripgrep";
Deno.test("default config is TOML with an asset base and vars table", async () => { Deno.test("default config is TOML with an asset base and vars table", async () => {
const config = parseToml(await Deno.readTextFile("./config.toml")) as { const config = parseToml(await Deno.readTextFile("./config.toml")) as {
asset_base: string; asset_base: string;
@@ -56,7 +55,7 @@ Deno.test("ripgrep test", async () => {
await writer.write( await writer.write(
new TextEncoder().encode("This is a test string with NULL in it."), new TextEncoder().encode("This is a test string with NULL in it."),
); );
writer.releaseLock(); await writer.close();
const { code, stdout, stderr } = await child.output(); const { code, stdout, stderr } = await child.output();
console.log(`Exit code: ${code}`); console.log(`Exit code: ${code}`);
console.log(`stdout: ${new TextDecoder().decode(stdout)}`); console.log(`stdout: ${new TextDecoder().decode(stdout)}`);
+56 -52
View File
@@ -1,14 +1,15 @@
import { Browser } from "playwright"; import { Browser } from "playwright";
import { resolve } from "node:path"; import { resolve } from "node:path";
import { pathToFileURL } from "node:url"; import { pathToFileURL } from "node:url";
import { PFiles } from "../file.ts"; 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 TurndownService from "turndown";
import "openpgp"; import "openpgp";
import { resolveChromiumPath } from "./utils.ts"; import { resolveChromiumPath } from "./utils.ts";
import { renderTemplate, type TemplateVariables } from "../template_engine.ts"; import { renderTemplate, type TemplateVariables } from "../template/engine.ts";
//原文->中间体(html)->目标格式(pdf/md/...) //原文->中间体(html)->目标格式(pdf/md/...)
//生成pdf的流程:原文->中间体(html)->playwright渲染->pdf //生成pdf的流程:原文->中间体(html)->playwright渲染->pdf
@@ -124,9 +125,11 @@ export class DocuBuilder {
); );
} }
async as_markdown() { async as_markdown(document?: HtmlDocument) {
const turndownService = new TurndownService(); 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( const cleaned_html = html_page.replace(
/<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi, /<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi,
"# $1", "# $1",
@@ -138,9 +141,11 @@ export class DocuBuilder {
this.pages = [md_page]; this.pages = [md_page];
} }
async as_txt(remove_links = true) { async as_txt(remove_links = true, document?: HtmlDocument) {
const turndownService = new TurndownService(); 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( const cleaned_html = html_page.replace(
/<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi, /<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi,
"# $1", "# $1",
@@ -152,57 +157,56 @@ export class DocuBuilder {
this.pages = [this.strip_markdown_symbols(md_page)]; this.pages = [this.strip_markdown_symbols(md_page)];
} }
async as_html_page(): Promise<string> { async as_html_page(
document?: HtmlDocument,
options: { articleMarkers?: boolean } = {},
): Promise<string> {
this.pages = []; 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 ?? "")); if (htmlDocument) {
this.pages.push(this.render(this.pfiles?.style_html ?? "")); let previousColumn: string | undefined;
this.pages.push(this.render(this.pfiles?.other_htmls.join("\n") ?? "")); let c_id = 0;
htmlDocument.chapters.forEach((chapter, index) => {
if (this.pfiles != null) { const first = previousColumn !== chapter.column.name;
let id = 1; if (first) {
this.pfiles.pcolumes.forEach((colume) => { c_id = 0;
let c_id = 1;
this.pages.push(
`<ch1 class="global c_title">${colume.name}</ch1>`,
);
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(
/<h1(\b[^>]*)>([\s\S]*?)<\/h1>/i,
(_match, attributes: string, title: string) => {
foundHeading = true;
return `<h1${attributes}><a href="https://wpep.invalid/article-start-${passageId}" style="color:inherit;text-decoration:none">${title}</a></h1>`;
},
);
const markedHtml = foundHeading ? indexedHtml : indexedHtml.replace(
/<p(\b[^>]*)>([\s\S]*?)<\/p>/i,
(_match, attributes: string, content: string) =>
`<p${attributes}><a href="https://wpep.invalid/article-start-${passageId}" style="color:inherit;text-decoration:none">${content}</a></p>`,
);
const printableHtml = markedHtml;
this.pages.push( this.pages.push(
`<div class="global c${c_id++} passages${firstPassageClass}" id="page-${passageId}">${printableHtml}</div>`, `<ch1 class="global c_title">${chapter.column.name}</ch1>`,
); );
}); }
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(
/<h1(\b[^>]*)>([\s\S]*?)<\/h1>/i,
(_match, attributes: string, title: string) => {
foundHeading = true;
return `<h1${attributes}><a href="https://wpep.invalid/article-start-${passageId}" style="color:inherit;text-decoration:none">${title}</a></h1>`;
},
);
const markedHtml = foundHeading ? indexedHtml : indexedHtml.replace(
/<p(\b[^>]*)>([\s\S]*?)<\/p>/i,
(_match, attributes: string, content: string) =>
`<p${attributes}><a href="https://wpep.invalid/article-start-${passageId}" style="color:inherit;text-decoration:none">${content}</a></p>`,
);
const printableHtml = options.articleMarkers === false
? html
: markedHtml;
this.pages.push(
`<div class="global c${++c_id} passages${firstPassageClass}" id="page-${passageId}">${printableHtml}</div>`,
);
}); });
} }
this.pages.push(this.render(this.pfiles?.end_html ?? "")); this.pages.push(htmlDocument?.layout.after ?? "");
if (this.cfg.journal.build_js_script != false) { if (this.cfg.journal.build_js_script != false) {
const f = Deno.readTextFileSync(`${this.cfg.journal.build_js_script}`); const f = Deno.readTextFileSync(`${this.cfg.journal.build_js_script}`);
const html = this.pages.join("\n"); const html = this.pages.join("\n");
+83 -37
View File
@@ -1,4 +1,4 @@
import { PFiles } from "../file.ts"; import { PFiles } from "../publication/file.ts";
import { PDFParse } from "pdf-parse"; import { PDFParse } from "pdf-parse";
import { import {
PDFArray, PDFArray,
@@ -7,9 +7,11 @@ import {
PDFHexString, PDFHexString,
PDFName, PDFName,
PDFNumber, PDFNumber,
PDFString,
} from "pdf-lib"; } from "pdf-lib";
import "openpgp"; import "openpgp";
import { DocuBuilder } from "./docu_builder.ts"; import { DocuBuilder } from "./docu_builder.ts";
import type { HtmlDocument } from "../document/types.ts";
export function normalizePdfText(text: string): string { export function normalizePdfText(text: string): string {
return text.normalize("NFKC").replace( return text.normalize("NFKC").replace(
@@ -95,7 +97,11 @@ export class IndexBuilder {
passage_dir: string = ""; passage_dir: string = "";
file_path: string = ""; file_path: string = "";
cfg: any; cfg: any;
constructor(file_path: string, cfg: any) { constructor(
file_path: string,
cfg: any,
private readonly document?: HtmlDocument,
) {
if (cfg.journal.build_mode == "flat") { if (cfg.journal.build_mode == "flat") {
this.template_dir = cfg.journal.from_directory; this.template_dir = cfg.journal.from_directory;
this.passage_dir = cfg.journal.from_directory; this.passage_dir = cfg.journal.from_directory;
@@ -112,6 +118,21 @@ export class IndexBuilder {
passages_index: Map<string, number>; passages_index: Map<string, number>;
passages_column_name: Map<string, string>; passages_column_name: Map<string, string>;
} { } {
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 pfiles = new PFiles(mode, this.passage_dir, this.cfg);
const titles: Array<string> = []; const titles: Array<string> = [];
const passages_index: Map<string, number> = new Map(); const passages_index: Map<string, number> = new Map();
@@ -127,7 +148,7 @@ export class IndexBuilder {
} }
async read_pdf() { async read_pdf() {
const parser = new PDFParse({ url: this.file_path }); const parser = new PDFParse({ data: await Deno.readFile(this.file_path) });
try { try {
const pdfText = await parser.getText(); const pdfText = await parser.getText();
this.total_pages = pdfText.total; this.total_pages = pdfText.total;
@@ -136,30 +157,38 @@ export class IndexBuilder {
await parser.destroy(); await parser.destroy();
} }
} }
//修改实现为在每页文本中查找目录标题,支持目录标题跨行的情况。通过维护一个标题游标,逐页扫描PDF文本,尝试匹配当前标题。每页文本被拆分成行,并进行规范化(去除空格和换行)。对于每个标题,尝试将连续的行合并起来进行匹配,如果找到匹配则记录该标题对应的页码和章节索引,并移动到下一个标题。这样可以更准确地处理目录标题在PDF中被拆分成多行的情况。
async build_index() { async build_index() {
const pdf_obj = await this.read_pdf(); this.indexed_map.clear();
const { titles, passages_index, passages_column_name } = this const { titles, passages_index, passages_column_name } = this
.get_index_titles_from_file_name( .get_index_titles_from_file_name(
this.cfg.journal.build_mode ?? "flat", 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 markerPages = await this.getArticleStartPages();
const missingIndices = titles.map((_, index) => index).filter((index) =>
!markerPages.has(index + 1)
);
const fallbackPages = new Map<number, number | undefined>();
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()) { 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)) { if (page !== undefined && !this.indexed_map.has(title)) {
this.indexed_map.set(title, { this.indexed_map.set(title, {
page, page,
@@ -168,10 +197,14 @@ export class IndexBuilder {
}); });
} }
} }
const missingTitles = titles.filter((title) => !this.indexed_map.has(title)); const missingTitles = titles.filter((title) =>
!this.indexed_map.has(title)
);
if (missingTitles.length > 0) { if (missingTitles.length > 0) {
throw new Error( throw new Error(
`Index coverage incomplete (${this.indexed_map.size}/${titles.length}); missing: ${missingTitles.join("、")}`, `Index coverage incomplete (${this.indexed_map.size}/${titles.length}); missing: ${
missingTitles.join("、")
}`,
); );
} }
} }
@@ -179,6 +212,7 @@ export class IndexBuilder {
private async getArticleStartPages(): Promise<Map<number, number>> { private async getArticleStartPages(): Promise<Map<number, number>> {
const parser = await PDFDocument.load(await Deno.readFile(this.file_path)); const parser = await PDFDocument.load(await Deno.readFile(this.file_path));
const pages = parser.getPages(); const pages = parser.getPages();
this.total_pages = pages.length;
const markerPages = new Map<number, number>(); const markerPages = new Map<number, number>();
const markerPattern = /^https:\/\/wpep\.invalid\/article-start-(\d+)$/; const markerPattern = /^https:\/\/wpep\.invalid\/article-start-(\d+)$/;
@@ -194,8 +228,8 @@ export class IndexBuilder {
const actionValue = annotation.lookup(PDFName.of("A")); const actionValue = annotation.lookup(PDFName.of("A"));
const action = actionValue instanceof PDFDict ? actionValue : undefined; const action = actionValue instanceof PDFDict ? actionValue : undefined;
const uri = action?.lookup(PDFName.of("URI")); const uri = action?.lookup(PDFName.of("URI"));
const url = uri && "decodeText" in uri const url = uri instanceof PDFString || uri instanceof PDFHexString
? (uri as PDFHexString).decodeText() ? uri.decodeText()
: ""; : "";
const match = markerPattern.exec(url); const match = markerPattern.exec(url);
if (match) { if (match) {
@@ -358,10 +392,14 @@ export class IndexBuilder {
const actionValue = annotation.lookup(PDFName.of("A")); const actionValue = annotation.lookup(PDFName.of("A"));
const action = actionValue instanceof PDFDict ? actionValue : undefined; const action = actionValue instanceof PDFDict ? actionValue : undefined;
const uri = action?.lookup(PDFName.of("URI")); const uri = action?.lookup(PDFName.of("URI"));
const url = uri && "decodeText" in uri const url = uri instanceof PDFString || uri instanceof PDFHexString
? (uri as PDFHexString).decodeText() ? 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)); page.node.set(PDFName.of("Annots"), pdfDoc.context.obj(retainedRefs));
} }
@@ -381,7 +419,7 @@ export class IndexBuilder {
} }
const sourcePages = sourcePdf.getPages(); const sourcePages = sourcePdf.getPages();
let annotationsAdded = 0; let annotationsAdded = 0;
let entryIndex = 0; const linkedPages = new Set<number>();
sourcePages.forEach((sourcePage, pageIndex) => { sourcePages.forEach((sourcePage, pageIndex) => {
const sourceAnnots = sourcePage.node.Annots(); const sourceAnnots = sourcePage.node.Annots();
if (!sourceAnnots) return; if (!sourceAnnots) return;
@@ -395,12 +433,13 @@ export class IndexBuilder {
const uriText = uri && "decodeText" in uri const uriText = uri && "decodeText" in uri
? (uri as any).decodeText() ? (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; continue;
} }
const entry = sortedEntries[entryIndex++]; const pageNumber = Number(match[1]);
if (!entry) continue;
const pageNumber = entry[1].page;
const sourceRect = sourceAnnot.lookup(PDFName.of("Rect"), PDFArray); const sourceRect = sourceAnnot.lookup(PDFName.of("Rect"), PDFArray);
if (!sourceRect || sourceRect.size() !== 4) continue; if (!sourceRect || sourceRect.size() !== 4) continue;
const mappedPage = this.remap_page_after_index_insert( const mappedPage = this.remap_page_after_index_insert(
@@ -417,7 +456,15 @@ export class IndexBuilder {
]; ];
const targetIndexPage = indexPages[pageIndex]; const targetIndexPage = indexPages[pageIndex];
if (!targetIndexPage) continue; 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"), PDFName.of("Dest"),
ctx.obj([ ctx.obj([
(destinationPage as any).ref, (destinationPage as any).ref,
@@ -425,15 +472,15 @@ export class IndexBuilder {
PDFNumber.of(destinationPage.getSize().height), PDFNumber.of(destinationPage.getSize().height),
]), ]),
); );
sourceAnnot.delete(PDFName.of("A")); const ref = ctx.register(targetAnnot);
const ref = ctx.register(sourceAnnot);
const annots = targetIndexPage.node.Annots(); const annots = targetIndexPage.node.Annots();
if (annots) annots.push(ref); if (annots) annots.push(ref);
else targetIndexPage.node.set(PDFName.of("Annots"), ctx.obj([ref])); else targetIndexPage.node.set(PDFName.of("Annots"), ctx.obj([ref]));
annotationsAdded++; annotationsAdded++;
linkedPages.add(pageNumber);
} }
}); });
if (annotationsAdded !== sortedEntries.length) { if (sortedEntries.some(([, entry]) => !linkedPages.has(entry.page))) {
throw new Error( throw new Error(
`Chromium index PDF link annotation mismatch: expected ${sortedEntries.length}, found ${annotationsAdded}`, `Chromium index PDF link annotation mismatch: expected ${sortedEntries.length}, found ${annotationsAdded}`,
); );
@@ -506,8 +553,7 @@ export class IndexBuilder {
} }
}); });
// deno-lint-ignore no-explicit-any const catalog = pdfDoc.catalog;
const catalog = (pdfDoc.catalog as any).dict;
catalog.set(PDFName.of("Outlines"), outlinesRef); catalog.set(PDFName.of("Outlines"), outlinesRef);
if (open_outline_by_default) { if (open_outline_by_default) {
catalog.set(PDFName.of("PageMode"), PDFName.of("UseOutlines")); catalog.set(PDFName.of("PageMode"), PDFName.of("UseOutlines"));
+145
View File
@@ -2,8 +2,153 @@ import { assertEquals } from "@std/assert";
import { import {
findTitlePage, findTitlePage,
findTitlePages, findTitlePages,
IndexBuilder,
normalizePdfText, normalizePdfText,
} from "./index_builder.ts"; } 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", () => { Deno.test("normalizes title whitespace and line breaks", () => {
assertEquals(normalizePdfText("工人\n 解放 报"), "工人解放报"); assertEquals(normalizePdfText("工人\n 解放 报"), "工人解放报");
@@ -1,16 +1,11 @@
import { OpenPGPSigner } from "./pdf_builder/utils.ts"; import { OpenPGPSigner } from "./utils.ts";
import { DocuBuilder } from "./pdf_builder/docu_builder.ts"; import { DocuBuilder } from "./docu_builder.ts";
import { IndexBuilder } from "./pdf_builder/index_builder.ts"; import { IndexBuilder } from "./index_builder.ts";
import type { TemplateVariables } from "./template_engine.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 = { export type GPConfig = DocumentConfig & {
journal: {
from_directory: string;
template_directory?: string;
build_mode?: "detached" | "flat";
build_js_script?: string | false;
};
asset_base?: string;
pdf: { pdf: {
generate_index?: boolean; generate_index?: boolean;
use_qpdf?: { use_qpdf?: {
@@ -42,14 +37,19 @@ export type PDFPipelineOptions = {
export async function processPdfConfig( export async function processPdfConfig(
config: GPConfig, config: GPConfig,
options: PDFPipelineOptions = {}, 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 { try {
await builder.with_browser(); await builder.with_browser();
console.debug(`Processing config: ${JSON.stringify(config)}`); console.debug(`Processing config: ${JSON.stringify(config)}`);
builder.with_files_sync(config.journal.from_directory); await builder.as_html_page(input);
await builder.as_html_page();
await builder.build_as_pdf(config.playwright); await builder.build_as_pdf(config.playwright);
} finally { } finally {
await builder.close_browser(); await builder.close_browser();
@@ -64,6 +64,7 @@ export async function processPdfConfig(
config, config,
insertIndexAfterPage, insertIndexAfterPage,
options.keepIndexPdf ?? false, options.keepIndexPdf ?? false,
input,
); );
} }
@@ -80,10 +81,15 @@ async function addIndexToPdf(
config: GPConfig, config: GPConfig,
insertIndexAfterPage = 1, insertIndexAfterPage = 1,
keepIndexPdf = false, keepIndexPdf = false,
document?: HtmlDocument,
) { ) {
console.log(`Adding index to PDF ${config.playwright.path}...`); 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(); await indexBuilder.build_index();
console.log( console.log(
`Index entries for ${config.playwright.path}: ${indexBuilder.indexed_map.size}`, `Index entries for ${config.playwright.path}: ${indexBuilder.indexed_map.size}`,
@@ -121,6 +127,7 @@ async function addIndexToPdf(
} }
await indexBuilder.add_pdf_outline({ await indexBuilder.add_pdf_outline({
inserted_index_pages: insertedIndexPages, inserted_index_pages: insertedIndexPages,
insert_index_after_page: insertIndexAfterPage,
}); });
if (keepIndexPdf) { if (keepIndexPdf) {
@@ -1,5 +1,8 @@
import { type GPConfig, processPdfConfig } from "./pdf_pipeline.ts"; import { type GPConfig } from "./pipeline.ts";
import type { TemplateVariables } from "./template_engine.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 = { type WorkerRequest = {
id: number; id: number;
@@ -26,7 +29,10 @@ const workerGlobal = self as unknown as {
workerGlobal.onmessage = async (event: MessageEvent<WorkerRequest>) => { workerGlobal.onmessage = async (event: MessageEvent<WorkerRequest>) => {
const { id, config, options } = event.data; const { id, config, options } = event.data;
try { try {
await processPdfConfig(config, options); await convertDocument(
new PublicationInput(config, options.variables),
new PdfOutput(config, options),
);
workerGlobal.postMessage({ id, ok: true } satisfies WorkerResponse); workerGlobal.postMessage({ id, ok: true } satisfies WorkerResponse);
} catch (error) { } catch (error) {
workerGlobal.postMessage( workerGlobal.postMessage(
File renamed without changes.
@@ -33,12 +33,14 @@ Deno.test("parses detached column folders without an order prefix", () => {
}); });
Deno.test("parses finite JavaScript numbers as detached column order", () => { Deno.test("parses finite JavaScript numbers as detached column order", () => {
for (const [prefix, expected] of [ for (
["-2.5", -2.5], const [prefix, expected] of [
["+3.25", 3.25], ["-2.5", -2.5],
["1e3", 1000], ["+3.25", 3.25],
["0x10", 16], ["1e3", 1000],
]) { ["0x10", 16],
]
) {
const meta = new PFileMetadata(`/publication/${prefix}::专栏/1::文章.md`); const meta = new PFileMetadata(`/publication/${prefix}::专栏/1::文章.md`);
assertEquals(meta.colume, "专栏"); assertEquals(meta.colume, "专栏");
assertEquals(meta.c_index, expected); assertEquals(meta.c_index, expected);
@@ -1,5 +1,5 @@
import { basename, dirname, join, relative, resolve } from "node:path"; import { basename, dirname, join, relative, resolve } from "node:path";
import { cfg } from "./main.ts"; import { cfg } from "../main.ts";
type WebDavOptions = Record<string, unknown>; type WebDavOptions = Record<string, unknown>;
type WebDavConfig = { type WebDavConfig = {
File renamed without changes.
File renamed without changes.
@@ -1,6 +1,6 @@
import { assertEquals } from "@std/assert"; import { assertEquals } from "@std/assert";
import { parse as parseToml } from "@std/toml"; import { parse as parseToml } from "@std/toml";
import { createTemplateVariables, renderTemplate } from "./template_engine.ts"; import { createTemplateVariables, renderTemplate } from "./engine.ts";
Deno.test("renders Nunjucks variables, conditionals, loops, and constants", () => { Deno.test("renders Nunjucks variables, conditionals, loops, and constants", () => {
const result = renderTemplate( const result = renderTemplate(