文档部分基本完成,还需要完善生成目录
This commit is contained in:
1 parent
578109c8de
commit
365b1db47e
109 files changed
+6222
-289
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { Browser } from "playwright";
|
||||
import { PFiles } from "../file.ts";
|
||||
import MarkdownIt from "markdown-it";
|
||||
import TurndownService from "turndown";
|
||||
|
||||
function resolveChromiumPath(): string {
|
||||
const chromiumPath = Deno.env.get("CHROMIUM_PATH");
|
||||
@@ -16,13 +17,15 @@ function resolveChromiumPath(): string {
|
||||
].join("\n"),
|
||||
);
|
||||
}
|
||||
|
||||
//原文->中间体(html)->目标格式(pdf/md/...)
|
||||
//生成pdf的流程:原文->中间体(html)->playwright渲染->pdf
|
||||
//生成md的流程:原文->中间体(html)->turndown转换->md
|
||||
export class DocuBuilder {
|
||||
html_pages: Array<string>;
|
||||
pages: Array<string>;
|
||||
browser: Browser | null;
|
||||
pfiles: PFiles | null = null;
|
||||
constructor() {
|
||||
this.html_pages = [];
|
||||
this.pages = [];
|
||||
this.browser = null;
|
||||
}
|
||||
async with_browser() {
|
||||
@@ -39,65 +42,77 @@ export class DocuBuilder {
|
||||
this.pfiles = new PFiles(dir);
|
||||
}
|
||||
|
||||
as_html_page() {
|
||||
this.html_pages = [];
|
||||
as_markdown() {
|
||||
const turndownService = new TurndownService();
|
||||
const md_page = turndownService.turndown(this.as_html_page());
|
||||
|
||||
this.pages = [md_page];
|
||||
}
|
||||
|
||||
as_html_page(): string {
|
||||
this.pages = [];
|
||||
const md = new MarkdownIt({
|
||||
html: true,
|
||||
linkify: true,
|
||||
typographer: true,
|
||||
});
|
||||
|
||||
this.html_pages.push(this.pfiles?.cover_html ?? "");
|
||||
this.html_pages.push(this.pfiles?.style_html ?? "");
|
||||
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.html_pages.push(`<h1 class="global">${colume.name}</h1>`);
|
||||
this.pages.push(
|
||||
`<ch1 class="global c_title">${colume.name}</ch1>`,
|
||||
);
|
||||
colume.pfiles.forEach((pfile) => {
|
||||
const html = md.render(pfile.content);
|
||||
this.html_pages.push(
|
||||
this.pages.push(
|
||||
`<div style='page-break-after: always;' class="global c${c_id++} passages" id="page-${id++}">${html}</div>`,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
this.html_pages.push(this.pfiles?.end_html ?? "");
|
||||
this.pages.push(this.pfiles?.end_html ?? "");
|
||||
|
||||
console.debug(structuredClone(this.html_pages).join("\n"));
|
||||
//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")),
|
||||
);
|
||||
}
|
||||
|
||||
async build_as_pdf(name = "output") {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
async build_as_pdf(pw_config: any) {
|
||||
if (this.browser == null) {
|
||||
throw new Error("Browser is not initialized. Call with_browser() first.");
|
||||
}
|
||||
if (this.pfiles != null && this.html_pages.length === 0) {
|
||||
if (this.pfiles != null && this.pages.length === 0) {
|
||||
this.as_html_page();
|
||||
}
|
||||
const page = await this.browser.newPage();
|
||||
try {
|
||||
await page.setContent(this.html_pages.join("\n"));
|
||||
await page.pdf({
|
||||
path: `outputs/${name}-computer.pdf`,
|
||||
format: "A4",
|
||||
printBackground: true,
|
||||
margin: { top: "20mm", bottom: "20mm", left: "10mm", right: "10mm" },
|
||||
//displayHeaderFooter: true,
|
||||
});
|
||||
await page.pdf({
|
||||
path: `outputs/${name}-smartphone.pdf`,
|
||||
format: "A4",
|
||||
printBackground: true,
|
||||
margin: { top: "20mm", bottom: "20mm", left: "10mm", right: "10mm" },
|
||||
//displayHeaderFooter: true,
|
||||
scale: 1.75,
|
||||
|
||||
});
|
||||
await page.setContent(this.pages.join("\n"));
|
||||
await page.pdf(
|
||||
pw_config,
|
||||
);
|
||||
//
|
||||
// await page.pdf({
|
||||
// path: `outputs/${name}-smartphone.pdf`,
|
||||
// format: "A4",
|
||||
// printBackground: true,
|
||||
// //margin: { top: "20mm", bottom: "20mm", left: "10mm", right: "10mm" },
|
||||
// //displayHeaderFooter: true,
|
||||
// scale: 1.75,
|
||||
// });
|
||||
} finally {
|
||||
await page.close();
|
||||
await this.browser.close();
|
||||
this.browser = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,3 +124,9 @@ export class DocuBuilder {
|
||||
this.browser = null;
|
||||
}
|
||||
}
|
||||
|
||||
export class IndexBuilder {
|
||||
|
||||
constructor() {
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user