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,232 @@
|
||||
//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 { createTemplateVariables } from "../template_engine.ts";
|
||||
import { prepareLocalCache } from "../local_cache.ts";
|
||||
import { EpubBuilder } from "../epub_builder.ts";
|
||||
|
||||
function selectedConfigs() {
|
||||
return cfg.settings.filter((config: Record<string, unknown>) =>
|
||||
!args.name || config.name === args.name
|
||||
);
|
||||
}
|
||||
|
||||
export async function just_local_cache() {
|
||||
for (const config of selectedConfigs()) {
|
||||
const publicationDir = config.journal.from_directory;
|
||||
const templateDir = config.journal.template_directory;
|
||||
const count = await prepareLocalCache(publicationDir, templateDir);
|
||||
console.log(`Cached ${count} assets for ${publicationDir}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function cacheBeforeGeneration() {
|
||||
if (!args["local-cache"]) return;
|
||||
await just_local_cache();
|
||||
}
|
||||
|
||||
export async function generate_pdf(options: Record<string, any> = {}) {
|
||||
await cacheBeforeGeneration();
|
||||
const maxWorkers = Math.max(1, navigator.hardwareConcurrency || 1);
|
||||
const queue = selectedConfigs();
|
||||
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, cfg.vars).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() {
|
||||
await cacheBeforeGeneration();
|
||||
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_txt() {
|
||||
await cacheBeforeGeneration();
|
||||
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_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() {
|
||||
await cacheBeforeGeneration();
|
||||
for (const config of selectedConfigs()) {
|
||||
const builder = new EpubBuilder(
|
||||
config,
|
||||
await createTemplateVariables(args, config, cfg.vars),
|
||||
);
|
||||
builder.with_files_sync(config.journal.from_directory);
|
||||
const output = builder.outputPath();
|
||||
await builder.build(output);
|
||||
console.log(`Generated EPUB: ${output}`);
|
||||
}
|
||||
}
|
||||
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user