asset忘了上传了,重新传一下,增加了模板引擎(见README),修复了printable的目录bug
This commit is contained in:
1 parent
0817d33309
commit
8b88de4942
7268 files changed
+1257020
No files matched your search
@@ -0,0 +1,248 @@
|
||||
//import TurndownService from "turndown";
|
||||
import { args, cfg } from "../main.ts";
|
||||
import { OpenPGPSigner } from "../pdf_builder/utils.ts";
|
||||
import { DocuBuilder } from "../pdf_builder/docu_builder.ts";
|
||||
import { IndexBuilder } from "../pdf_builder/index_builder.ts";
|
||||
import { createTemplateVariables } from "../template_engine.ts";
|
||||
export async function generate_pdf(options: Record<string, any> = {}) {
|
||||
const maxWorkers = Math.max(1, navigator.hardwareConcurrency || 1);
|
||||
const queue = [...cfg.settings];
|
||||
const workers = new Set<Promise<void>>();
|
||||
|
||||
while (queue.length > 0 || workers.size > 0) {
|
||||
while (queue.length > 0 && workers.size < maxWorkers) {
|
||||
const config = queue.shift();
|
||||
if (!config) {
|
||||
break;
|
||||
}
|
||||
if (config.name) {
|
||||
if (args.name != null && args.name !== "" && args.name !== undefined) {
|
||||
if (config.name !== args.name) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
const workerPromise = runPdfWorker(config, options)
|
||||
.finally(() => {
|
||||
workers.delete(workerPromise);
|
||||
});
|
||||
workers.add(workerPromise);
|
||||
}
|
||||
|
||||
if (workers.size > 0) {
|
||||
await Promise.race(workers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function runPdfWorker(
|
||||
config: (typeof cfg.settings)[number],
|
||||
options: Record<string, any>,
|
||||
): Promise<void> {
|
||||
return createTemplateVariables(args, config).then((variables) =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
const worker = new Worker(
|
||||
new URL("../pdf_worker.ts", import.meta.url).href,
|
||||
{
|
||||
type: "module",
|
||||
},
|
||||
);
|
||||
|
||||
worker.onmessage = (
|
||||
event: MessageEvent<
|
||||
{ id: number; ok: true } | { id: number; ok: false; error: string }
|
||||
>,
|
||||
) => {
|
||||
if (event.data.ok) {
|
||||
worker.terminate();
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
worker.terminate();
|
||||
reject(new Error(event.data.error));
|
||||
};
|
||||
|
||||
worker.onerror = (event) => {
|
||||
worker.terminate();
|
||||
reject(event.error ?? new Error(event.message));
|
||||
};
|
||||
|
||||
worker.postMessage({
|
||||
id: Date.now(),
|
||||
config,
|
||||
options: {
|
||||
addIndex: options.add_index,
|
||||
useQpdf: options.use_qpdf,
|
||||
signPdf: options.sign_pdf,
|
||||
signPassphrase: options.sign_passphrase ?? options.passphrase,
|
||||
keepIndexPdf: options.keep_index_pdf ?? options.keepIndexPdf ??
|
||||
options["keep-index-pdf"] ?? false,
|
||||
variables,
|
||||
},
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function generate_md() {
|
||||
try {
|
||||
for (const config of cfg.settings) {
|
||||
const builder = new DocuBuilder(
|
||||
config,
|
||||
await createTemplateVariables(args, config),
|
||||
);
|
||||
|
||||
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_txt() {
|
||||
try {
|
||||
for (const config of cfg.settings) {
|
||||
const builder = new DocuBuilder(
|
||||
config,
|
||||
await createTemplateVariables(args, config),
|
||||
);
|
||||
|
||||
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 add_index_to_pdf(insert_index_after_page: number = 1) {
|
||||
const config = cfg;
|
||||
for (const cfg of config.settings) {
|
||||
console.log(`Adding index to PDF ${cfg.playwright.path}...`);
|
||||
|
||||
const indexBuilder = new IndexBuilder(
|
||||
cfg.playwright.path,
|
||||
cfg,
|
||||
);
|
||||
await indexBuilder.build_index();
|
||||
const _html = indexBuilder.as_html_page();
|
||||
//console.log(html);
|
||||
const tempDir = await makePdfTempDir(cfg.playwright.path);
|
||||
const tempIndexPdfPath = `${tempDir}/index.pdf`;
|
||||
await indexBuilder.html2pdf({
|
||||
"path": tempIndexPdfPath,
|
||||
"format": "A4",
|
||||
"headerTemplate":
|
||||
'<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="title"></span></div>',
|
||||
"footerTemplate":
|
||||
'<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="title"></span></div>',
|
||||
"margin": {
|
||||
"top": "20mm",
|
||||
"bottom": "20mm",
|
||||
},
|
||||
"printBackground": true,
|
||||
});
|
||||
//console.log(indexBuilder.indexed_map);
|
||||
const insertedIndexPages = await indexBuilder
|
||||
.combine_index_pdf_to_main_pdf(
|
||||
tempIndexPdfPath,
|
||||
insert_index_after_page,
|
||||
);
|
||||
await indexBuilder.add_pdf_outline({
|
||||
inserted_index_pages: insertedIndexPages,
|
||||
});
|
||||
|
||||
try {
|
||||
await Deno.remove(tempIndexPdfPath);
|
||||
await Deno.remove(tempDir);
|
||||
} catch (_error) {
|
||||
// Ignore temp cleanup failures.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function makePdfTempDir(outputPath: string) {
|
||||
await Deno.mkdir("./tmps", { recursive: true });
|
||||
const safeStem = outputPath.split("/").pop()?.replace(/\.pdf$/i, "") ?? "pdf";
|
||||
return await Deno.makeTempDir({
|
||||
dir: "./tmps",
|
||||
prefix: `${safeStem}-index-`,
|
||||
});
|
||||
}
|
||||
|
||||
export async function use_qpdf() {
|
||||
const config = cfg;
|
||||
for (const cfg of config.settings) {
|
||||
if (cfg.pdf.use_qpdf?.enabled) {
|
||||
console.log(`Using qpdf to linearize PDF ${cfg.playwright.path}...`);
|
||||
const outputPath = cfg.playwright.path.replace(
|
||||
/\.pdf$/,
|
||||
"_linearized.pdf",
|
||||
);
|
||||
|
||||
const qpdfArgs = [
|
||||
cfg.playwright.path,
|
||||
cfg.pdf.use_qpdf.options,
|
||||
outputPath,
|
||||
];
|
||||
|
||||
const cmd = new Deno.Command(
|
||||
"qpdf",
|
||||
{
|
||||
args: qpdfArgs,
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
},
|
||||
);
|
||||
|
||||
const child = cmd.spawn();
|
||||
|
||||
for await (const chunk of child.stdout) {
|
||||
console.log(new TextDecoder().decode(chunk));
|
||||
}
|
||||
|
||||
const status = await child.status;
|
||||
console.log(`Status: ${status.code}`);
|
||||
if (status.code === 0) {
|
||||
await Deno.remove(cfg.playwright.path);
|
||||
await Deno.rename(outputPath, cfg.playwright.path);
|
||||
console.log(`Successfully linearized PDF: ${cfg.playwright.path}`);
|
||||
} else {
|
||||
console.error(`Failed to linearize PDF: ${cfg.playwright.path}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function sign_pdf(passphrase?: string) {
|
||||
const config = cfg;
|
||||
for (const cfg of config.settings) {
|
||||
if (cfg.pdf.signing?.enabled) {
|
||||
console.log(`Signing PDF ${cfg.playwright.path}...`);
|
||||
const signer = new OpenPGPSigner(
|
||||
cfg.playwright.path,
|
||||
cfg.pdf.signing.key_path,
|
||||
cfg.pdf.signing.output_path,
|
||||
passphrase,
|
||||
);
|
||||
await signer.sign();
|
||||
console.log(`Successfully signed PDF: ${cfg.pdf.signing.output_path}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { generate_md, generate_pdf, generate_txt } from "./handlers.ts";
|
||||
|
||||
export async function match_sub_command_to_function(
|
||||
sub_command: Array<string>,
|
||||
options: Record<string, any>,
|
||||
) {
|
||||
switch (sub_command[0]) {
|
||||
case "generate":
|
||||
switch (sub_command[1]) {
|
||||
case "pdf":
|
||||
//deno-lint-ignore no-await-in-loop
|
||||
await generate_pdf(options);
|
||||
break;
|
||||
|
||||
case "md":
|
||||
await generate_md();
|
||||
break;
|
||||
case "txt":
|
||||
await generate_txt();
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown sub-command: ${sub_command[1]}`);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown sub-command: ${sub_command[0]}`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user