212 lines
5.8 KiB
TypeScript
212 lines
5.8 KiB
TypeScript
//import TurndownService from "turndown";
|
|
import { args, cfg } from "../main.ts";
|
|
import { OpenPGPSigner } from "../pdf_builder/utils.ts";
|
|
import { PublicationInput } from "../document/inputs.ts";
|
|
import { convertDocument } from "../document/types.ts";
|
|
import {
|
|
EpubOutput,
|
|
type TextFormat,
|
|
TextOutput,
|
|
} from "../document/outputs.ts";
|
|
import { createTemplateVariables } from "../template/engine.ts";
|
|
import { prepareLocalCache } from "../assets/local_cache.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_builder/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 generateText("md");
|
|
}
|
|
|
|
export async function generate_html() {
|
|
await generateText("html");
|
|
}
|
|
|
|
export async function generate_txt() {
|
|
await generateText("txt");
|
|
}
|
|
|
|
async function generateText(format: TextFormat) {
|
|
await cacheBeforeGeneration();
|
|
for (const config of selectedConfigs()) {
|
|
const input = new PublicationInput(
|
|
config,
|
|
await createTemplateVariables(args, config, cfg.vars),
|
|
);
|
|
const result = await convertDocument(input, new TextOutput(config, format));
|
|
console.log(`Generated ${format}: ${result.path}`);
|
|
}
|
|
}
|
|
|
|
export async function generate_epub() {
|
|
await cacheBeforeGeneration();
|
|
for (const config of selectedConfigs()) {
|
|
const input = new PublicationInput(
|
|
config,
|
|
await createTemplateVariables(args, config, cfg.vars),
|
|
);
|
|
const result = await convertDocument(input, new EpubOutput(config));
|
|
console.log(`Generated EPUB: ${result.path}`);
|
|
}
|
|
}
|
|
|
|
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}`);
|
|
}
|
|
}
|
|
}
|