Files
wpep/tools/gp/pdf_builder/docu_builder.ts
T
2026-10-26 12:54:09 +08:00

230 lines
7.2 KiB
TypeScript

import { Browser } from "playwright";
import { PFiles } from "../file.ts";
import { md } from "../mdit/md.ts";
import TurndownService from "turndown";
import "openpgp";
import { resolveChromiumPath } from "./utils.ts";
//原文->中间体(html)->目标格式(pdf/md/...)
//生成pdf的流程:原文->中间体(html)->playwright渲染->pdf
//生成md的流程:原文->中间体(html)->turndown转换->md
export class DocuBuilder {
pages: Array<string>;
// deno-lint-ignore no-explicit-any
cfg: any;
browser: Browser | null;
pfiles: PFiles | null = null;
// deno-lint-ignore no-explicit-any
constructor(cfg: any) {
this.cfg = cfg;
this.pages = [];
this.browser = null;
}
async with_browser() {
const { chromium } = await import("playwright");
const browser = await chromium.launch({
executablePath: resolveChromiumPath(),
headless: true,
args: ["--no-sandbox", "--disable-dev-shm-usage"],
});
this.browser = browser;
}
with_files_sync(dir: string) {
this.pfiles = new PFiles(
this.cfg.journal.build_mode ?? "flat",
dir,
this.cfg,
);
}
private strip_markdown_symbols(markdown: string): string {
return markdown
.replace(/^\s{0,3}#{1,6}\s*/gm, "")
.replace(/^(.+)\n={3,}\s*$/gm, "$1")
.replace(/^(.+)\n-{3,}\s*$/gm, "$1")
.replace(/^\s{0,3}>\s?/gm, "")
.replace(/^\s{0,3}(?:[-*+]|\d+\.)\s+/gm, "")
.replace(/^\s{0,3}(?:---|\*\*\*|___)\s*$/gm, "")
.replace(/!\[([^\]]*)\]\((.*?)\)/g, "$1")
.replace(/\[([^\]]+)\]\((.*?)\)/g, "$1")
.replace(/\[([^\]]+)\]\[[^\]]*\]/g, "$1")
.replace(/!\[([^\]]*)\]\[[^\]]*\]/g, "$1")
.replace(/(`{1,3})([^`]+?)\1/g, "$2")
.replace(/(\*\*|__)(.*?)\1/g, "$2")
.replace(/(\*|_)(.*?)\1/g, "$2")
.replace(/\\([\\`*_{}\[\]()#+\-.!>])/g, "$1")
.replace(/<[^>]+>/g, "")
.replace(/\n{3,}/g, "\n\n")
.trim();
}
async as_markdown() {
const turndownService = new TurndownService();
const html_page = await this.as_html_page();
const cleaned_html = html_page.replace(
/<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi,
"# $1",
).replace(
/<script\b[^>]*>[\s\S]*?<\/script>|<style\b[^>]*>[\s\S]*?<\/style>/gi,
"",
);
const md_page = turndownService.turndown(cleaned_html);
this.pages = [md_page];
}
async as_txt(remove_links = true) {
const turndownService = new TurndownService();
const html_page = await this.as_html_page();
const cleaned_html = html_page.replace(
/<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi,
"# $1",
).replace(
/<script\b[^>]*>[\s\S]*?<\/script>|<style\b[^>]*>[\s\S]*?<\/style>/gi,
"",
).replaceAll("#", ""); // Remove markdown symbols for plain text
const md_page = turndownService.turndown(cleaned_html);
this.pages = [this.strip_markdown_symbols(md_page)];
}
async as_html_page(): Promise<string> {
this.pages = [];
this.pages.push(this.pfiles?.cover_html ?? "");
this.pages.push(this.pfiles?.style_html ?? "");
this.pages.push(this.pfiles?.other_htmls.join("\n") ?? "");
if (this.pfiles != null) {
let id = 1;
this.pfiles.pcolumes.forEach((colume) => {
let c_id = 1;
this.pages.push(
`<ch1 class="global c_title">${colume.name}</ch1>`,
);
colume.pfiles.forEach((pfile) => {
const html = md.render(pfile.content);
this.pages.push(
`<div style='page-break-after: always;' class="global c${c_id++} passages" id="page-${id++}">${html}</div>`,
);
});
});
}
this.pages.push(this.pfiles?.end_html ?? "");
if (this.cfg.journal.build_js_script != false) {
const f = Deno.readTextFileSync(`${this.cfg.journal.build_js_script}`);
const html = this.pages.join("\n");
// Ensure a DOMParser is available; if not, try to load deno_dom dynamically.
let domParserCtor: {
new (): {
parseFromString: (
html: string,
mimeType: string,
) => { documentElement?: { outerHTML: string } } | null;
};
} | undefined = (globalThis as { DOMParser?: unknown }).DOMParser as
| {
new (): {
parseFromString: (
html: string,
mimeType: string,
) => { documentElement?: { outerHTML: string } } | null;
};
}
| undefined;
if (domParserCtor == null) {
try {
const denoDom = await import(
// deno-lint-ignore no-import-prefix
"https://deno.land/x/deno_dom/deno-dom-wasm.ts"
);
if (denoDom && denoDom.DOMParser) {
// deno-lint-ignore no-explicit-any
(globalThis as any).DOMParser = denoDom.DOMParser;
// Cast via unknown to satisfy TypeScript when converting between incompatible constructor types
domParserCtor = denoDom
.DOMParser as unknown as typeof domParserCtor;
}
} catch (e) {
// ignore; will throw below with helpful message
console.error(e);
}
}
if (domParserCtor == null) {
throw new Error(
"DOMParser is not available in this runtime. Install deno_dom or run in a runtime that provides DOMParser.\n" +
"Suggested: import 'https://deno.land/x/deno_dom/deno-dom-wasm.ts' or run with a JS DOM-enabled environment.",
);
}
const document = new domParserCtor().parseFromString(html, "text/html");
const runBuildScript = new Function(
"html",
"document",
"pages",
"cfg",
`${f}
return document?.documentElement?.outerHTML ?? html;`,
) as (
html: string,
document: { documentElement?: { outerHTML: string } } | null,
pages: Array<string>,
cfg: unknown,
) => string;
const builtHtml = runBuildScript(
html,
document,
[...this.pages],
this.cfg,
);
this.pages = [builtHtml];
}
//console.debug(structuredClone(this.pages).join("\n"));
return this.pages.join("\n");
}
async build_as_raw(file_name: string) {
await Deno.writeFile(
file_name,
new TextEncoder().encode(this.pages.join("\n")),
);
}
// deno-lint-ignore no-explicit-any
async build_as_pdf(pw_config: any, pages = this.pages) {
if (this.browser == null) {
throw new Error("Browser is not initialized. Call with_browser() first.");
}
if (this.pfiles != null && pages.length === 0) {
await this.as_html_page();
}
const page = await this.browser.newPage();
try {
// const setContentTimeout = pw_config?.setContent_timeout_ms ?? 120000;
// const setContentWaitUntil = pw_config?.setContent_wait_until ??
// "domcontentloaded";
await page.setContent(pages.join("\n"), {
timeout: 120000,
// waitUntil: setContentWaitUntil,
});
await page.pdf(
pw_config,
);
} finally {
await page.close();
}
}
async close_browser() {
if (this.browser == null) {
return;
}
await this.browser.close();
this.browser = null;
}
}