diff --git a/tools/gp/README.md b/tools/gp/README.md index 7834737..408c70e 100644 --- a/tools/gp/README.md +++ b/tools/gp/README.md @@ -4,6 +4,24 @@ GP 是一个基于 Deno、Markdown 和 HTML 模板的刊物构建工具,可以 PDF,也可以导出合并后的 Markdown 或纯文本。PDF 支持自动生成目录、PDF 线性化和 OpenPGP 签名。 +## 代码目录 + +`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 @@ -382,7 +400,7 @@ deno test -A deno task dev # 检查类型和格式 -deno check main.ts pdf_pipeline.ts +deno check main.ts pdf_builder/pipeline.ts deno fmt --check ``` diff --git a/tools/gp/local_cache.ts b/tools/gp/assets/local_cache.ts similarity index 100% rename from tools/gp/local_cache.ts rename to tools/gp/assets/local_cache.ts diff --git a/tools/gp/local_cache_test.ts b/tools/gp/assets/local_cache_test.ts similarity index 100% rename from tools/gp/local_cache_test.ts rename to tools/gp/assets/local_cache_test.ts diff --git a/tools/gp/cli/handlers.ts b/tools/gp/cli/handlers.ts index f673c06..5cabdba 100644 --- a/tools/gp/cli/handlers.ts +++ b/tools/gp/cli/handlers.ts @@ -2,9 +2,9 @@ import { args, cfg } from "../main.ts"; import { OpenPGPSigner } from "../pdf_builder/utils.ts"; import { DocuBuilder } from "../pdf_builder/docu_builder.ts"; -import { createTemplateVariables } from "../template_engine.ts"; -import { prepareLocalCache } from "../local_cache.ts"; -import { EpubBuilder } from "../epub_builder.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) => @@ -65,7 +65,7 @@ function runPdfWorker( return createTemplateVariables(args, config, cfg.vars).then((variables) => new Promise((resolve, reject) => { 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", }, diff --git a/tools/gp/cli/match.ts b/tools/gp/cli/match.ts index 9b52f5f..bbec9a2 100644 --- a/tools/gp/cli/match.ts +++ b/tools/gp/cli/match.ts @@ -6,7 +6,11 @@ import { generate_txt, just_local_cache, } 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( sub_command: Array, diff --git a/tools/gp/epub_builder.ts b/tools/gp/epub_builder/epub_builder.ts similarity index 98% rename from tools/gp/epub_builder.ts rename to tools/gp/epub_builder/epub_builder.ts index a198211..516cb9f 100644 --- a/tools/gp/epub_builder.ts +++ b/tools/gp/epub_builder/epub_builder.ts @@ -1,8 +1,8 @@ import { strToU8, unzipSync, type Zippable, zipSync } from "fflate"; import { dirname, extname, relative, resolve, sep } from "node:path"; -import { PFiles } from "./file.ts"; -import { md } from "./mdit/md.ts"; -import { renderTemplate, type TemplateVariables } from "./template_engine.ts"; +import { PFiles } from "../publication/file.ts"; +import { md } from "../mdit/md.ts"; +import { renderTemplate, type TemplateVariables } from "../template/engine.ts"; type EpubConfig = { journal: { diff --git a/tools/gp/epub_builder_test.ts b/tools/gp/epub_builder/epub_builder_test.ts similarity index 100% rename from tools/gp/epub_builder_test.ts rename to tools/gp/epub_builder/epub_builder_test.ts diff --git a/tools/gp/main_test.ts b/tools/gp/main_test.ts index 3922de3..0c52c21 100644 --- a/tools/gp/main_test.ts +++ b/tools/gp/main_test.ts @@ -30,7 +30,6 @@ import { assertEquals } from "@std/assert"; // async fn() { // }, // }); -import { ripgrep } from "ripgrep"; Deno.test("default config is TOML with an asset base and vars table", async () => { const config = parseToml(await Deno.readTextFile("./config.toml")) as { asset_base: string; @@ -56,7 +55,7 @@ Deno.test("ripgrep test", async () => { await writer.write( new TextEncoder().encode("This is a test string with NULL in it."), ); - writer.releaseLock(); + await writer.close(); const { code, stdout, stderr } = await child.output(); console.log(`Exit code: ${code}`); console.log(`stdout: ${new TextDecoder().decode(stdout)}`); diff --git a/tools/gp/pdf_builder/docu_builder.ts b/tools/gp/pdf_builder/docu_builder.ts index 98c55a4..6fc9d33 100644 --- a/tools/gp/pdf_builder/docu_builder.ts +++ b/tools/gp/pdf_builder/docu_builder.ts @@ -1,14 +1,14 @@ import { Browser } from "playwright"; import { resolve } from "node:path"; import { pathToFileURL } from "node:url"; -import { PFiles } from "../file.ts"; +import { PFiles } from "../publication/file.ts"; import { md } from "../mdit/md.ts"; import TurndownService from "turndown"; import "openpgp"; import { resolveChromiumPath } from "./utils.ts"; -import { renderTemplate, type TemplateVariables } from "../template_engine.ts"; +import { renderTemplate, type TemplateVariables } from "../template/engine.ts"; //原文->中间体(html)->目标格式(pdf/md/...) //生成pdf的流程:原文->中间体(html)->playwright渲染->pdf diff --git a/tools/gp/pdf_builder/index_builder.ts b/tools/gp/pdf_builder/index_builder.ts index a72b40c..6691ab3 100644 --- a/tools/gp/pdf_builder/index_builder.ts +++ b/tools/gp/pdf_builder/index_builder.ts @@ -1,4 +1,4 @@ -import { PFiles } from "../file.ts"; +import { PFiles } from "../publication/file.ts"; import { PDFParse } from "pdf-parse"; import { PDFArray, @@ -168,10 +168,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) { 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("、") + }`, ); } } diff --git a/tools/gp/pdf_pipeline.ts b/tools/gp/pdf_builder/pipeline.ts similarity index 95% rename from tools/gp/pdf_pipeline.ts rename to tools/gp/pdf_builder/pipeline.ts index b0c81aa..e6d26e4 100644 --- a/tools/gp/pdf_pipeline.ts +++ b/tools/gp/pdf_builder/pipeline.ts @@ -1,7 +1,7 @@ -import { OpenPGPSigner } from "./pdf_builder/utils.ts"; -import { DocuBuilder } from "./pdf_builder/docu_builder.ts"; -import { IndexBuilder } from "./pdf_builder/index_builder.ts"; -import type { TemplateVariables } from "./template_engine.ts"; +import { OpenPGPSigner } from "./utils.ts"; +import { DocuBuilder } from "./docu_builder.ts"; +import { IndexBuilder } from "./index_builder.ts"; +import type { TemplateVariables } from "../template/engine.ts"; export type GPConfig = { journal: { diff --git a/tools/gp/pdf_worker.ts b/tools/gp/pdf_builder/worker.ts similarity index 88% rename from tools/gp/pdf_worker.ts rename to tools/gp/pdf_builder/worker.ts index 9aaaba3..d68d3e7 100644 --- a/tools/gp/pdf_worker.ts +++ b/tools/gp/pdf_builder/worker.ts @@ -1,5 +1,5 @@ -import { type GPConfig, processPdfConfig } from "./pdf_pipeline.ts"; -import type { TemplateVariables } from "./template_engine.ts"; +import { type GPConfig, processPdfConfig } from "./pipeline.ts"; +import type { TemplateVariables } from "../template/engine.ts"; type WorkerRequest = { id: number; diff --git a/tools/gp/file.ts b/tools/gp/publication/file.ts similarity index 100% rename from tools/gp/file.ts rename to tools/gp/publication/file.ts diff --git a/tools/gp/file_test.ts b/tools/gp/publication/file_test.ts similarity index 94% rename from tools/gp/file_test.ts rename to tools/gp/publication/file_test.ts index 42aaba4..e46fedb 100644 --- a/tools/gp/file_test.ts +++ b/tools/gp/publication/file_test.ts @@ -33,12 +33,14 @@ Deno.test("parses detached column folders without an order prefix", () => { }); Deno.test("parses finite JavaScript numbers as detached column order", () => { - for (const [prefix, expected] of [ - ["-2.5", -2.5], - ["+3.25", 3.25], - ["1e3", 1000], - ["0x10", 16], - ]) { + for ( + const [prefix, expected] of [ + ["-2.5", -2.5], + ["+3.25", 3.25], + ["1e3", 1000], + ["0x10", 16], + ] + ) { const meta = new PFileMetadata(`/publication/${prefix}::专栏/1::文章.md`); assertEquals(meta.colume, "专栏"); assertEquals(meta.c_index, expected); diff --git a/tools/gp/webdav.ts b/tools/gp/sync/webdav.ts similarity index 99% rename from tools/gp/webdav.ts rename to tools/gp/sync/webdav.ts index 16e06e8..a411a3d 100644 --- a/tools/gp/webdav.ts +++ b/tools/gp/sync/webdav.ts @@ -1,5 +1,5 @@ import { basename, dirname, join, relative, resolve } from "node:path"; -import { cfg } from "./main.ts"; +import { cfg } from "../main.ts"; type WebDavOptions = Record; type WebDavConfig = { diff --git a/tools/gp/webdav_test.ts b/tools/gp/sync/webdav_test.ts similarity index 100% rename from tools/gp/webdav_test.ts rename to tools/gp/sync/webdav_test.ts diff --git a/tools/gp/template_engine.ts b/tools/gp/template/engine.ts similarity index 100% rename from tools/gp/template_engine.ts rename to tools/gp/template/engine.ts diff --git a/tools/gp/template_engine_test.ts b/tools/gp/template/engine_test.ts similarity index 96% rename from tools/gp/template_engine_test.ts rename to tools/gp/template/engine_test.ts index 80ea0be..223f8a7 100644 --- a/tools/gp/template_engine_test.ts +++ b/tools/gp/template/engine_test.ts @@ -1,6 +1,6 @@ import { assertEquals } from "@std/assert"; 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", () => { const result = renderTemplate(