Files
wpep/tools/gp/epub_builder/epub_builder.ts
T
2026-11-12 18:34:33 +08:00

386 lines
13 KiB
TypeScript

import { strToU8, unzipSync, type Zippable, zipSync } from "fflate";
import { dirname, extname, relative, resolve, sep } from "node:path";
import { PFiles } from "../publication/file.ts";
import { md } from "../mdit/md.ts";
import { renderTemplate, type TemplateVariables } from "../template/engine.ts";
type EpubConfig = {
journal: {
build_mode?: "detached" | "flat";
from_directory: string;
template_directory: string;
title?: string;
author?: string;
language?: string;
identifier?: string;
};
vars?: Record<string, unknown>;
epub?: { path?: string; title?: string; author?: string; language?: string };
md?: { path?: string };
markdown_output?: string;
asset_base?: string;
};
type EpubChapter = {
id: string;
href: string;
title: string;
html: string;
sourcePath: string;
column: string;
};
function mediaType(extension: string): string {
return ({
".avif": "image/avif",
".gif": "image/gif",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
".svg": "image/svg+xml",
".webp": "image/webp",
".css": "text/css",
".woff": "font/woff",
".woff2": "font/woff2",
".ttf": "font/ttf",
".otf": "font/otf",
".html": "application/xhtml+xml",
".htm": "application/xhtml+xml",
".js": "text/javascript",
".mp3": "audio/mpeg",
".mp4": "video/mp4",
".pdf": "application/pdf",
} as Record<string, string>)[extension] ?? "application/octet-stream";
}
function escapeXml(value: string): string {
return value.replace(/[&<>"']/g, (char) => ({
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&apos;",
}[char]!));
}
function normalizeHeadingText(value: string): string {
return value
.replace(/<[^>]*>/g, "")
.replace(
/&(#x[\da-f]+|#\d+|amp|lt|gt|quot|apos);/gi,
(entity, code: string) => {
if (code[0] === "#") {
const hex = code[1]?.toLowerCase() === "x";
const number = Number.parseInt(
code.slice(hex ? 2 : 1),
hex ? 16 : 10,
);
return Number.isFinite(number)
? String.fromCodePoint(number)
: entity;
}
return ({ amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" } as Record<
string,
string
>)[code.toLowerCase()] ?? entity;
},
)
.normalize("NFKC")
.replace(/\s+/g, " ")
.trim();
}
function removeRepeatedLeadingTitle(html: string, title: string): string {
const heading = html.match(/^\s*<h1\b[^>]*>([\s\S]*?)<\/h1>\s*/i);
if (
!heading || normalizeHeadingText(heading[1]) !== normalizeHeadingText(title)
) {
return html;
}
return html.slice(heading[0].length);
}
function slug(value: string, index: number): string {
const normalized = value.normalize("NFKD").toLowerCase()
.replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-|-$/g, "");
return `${String(index).padStart(4, "0")}-${normalized || "chapter"}`;
}
function navXhtml(chapters: EpubChapter[], title: string): string {
let previousColumn = "";
let links = "";
for (const chapter of chapters) {
if (chapter.column !== previousColumn) {
if (previousColumn) links += "</ol></li>";
previousColumn = chapter.column;
links += `<li><span>${escapeXml(chapter.column)}</span><ol>`;
}
links += `<li><a href="${chapter.href}">${
escapeXml(chapter.title)
}</a></li>`;
}
if (previousColumn) links += "</ol></li>";
return `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="zh" xml:lang="zh">
<head><title>${escapeXml(title)}</title><meta charset="utf-8" /></head>
<body><nav epub:type="toc" id="toc"><h1>目录</h1><ol>${links}</ol></nav></body>
</html>`;
}
function packageOpf(
chapters: EpubChapter[],
media: Map<string, string>,
title: string,
author: string,
language: string,
identifier: string,
): string {
const manifest = [
`<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>`,
...chapters.map((chapter) =>
`<item id="${chapter.id}" href="${chapter.href}" media-type="application/xhtml+xml"/>`
),
...[...media.entries()].map(([href, mime], index) =>
`<item id="media-${index + 1}" href="${href}" media-type="${mime}"/>`
),
].join("\n ");
const spine = chapters.map((chapter) => `<itemref idref="${chapter.id}"/>`)
.join("\n ");
return `<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="book-id" xml:lang="${
escapeXml(language)
}">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:identifier id="book-id">${escapeXml(identifier)}</dc:identifier>
<dc:title>${escapeXml(title)}</dc:title>
<dc:language>${escapeXml(language)}</dc:language>
<dc:creator>${escapeXml(author)}</dc:creator>
<meta property="dcterms:modified">${
new Date().toISOString().replace(/\.\d{3}Z$/, "Z")
}</meta>
</metadata>
<manifest>
${manifest}
</manifest>
<spine>
${spine}
</spine>
</package>`;
}
export class EpubBuilder {
private readonly config: EpubConfig;
private readonly variables: TemplateVariables;
private pfiles?: PFiles;
constructor(config: EpubConfig, variables: TemplateVariables = {}) {
this.config = config;
this.variables = variables;
}
with_files_sync(directory = this.config.journal.from_directory): this {
this.pfiles = new PFiles(
this.config.journal.build_mode ?? "flat",
directory,
this.config,
);
return this;
}
async build(path = this.outputPath()): Promise<void> {
if (!this.pfiles) this.with_files_sync();
const chapters: EpubChapter[] = [];
const mediaManifest = new Map<string, string>();
const epubMedia = new Map<string, Uint8Array>();
const allFiles = this.pfiles!;
let index = 0;
const sourceToHref = new Map<string, string>();
for (const column of allFiles.pcolumes) {
for (const pfile of column.pfiles) {
index += 1;
sourceToHref.set(
resolve(pfile.full_name),
`text/${slug(pfile.meta.title, index)}.xhtml`,
);
}
}
index = 0;
for (const column of allFiles.pcolumes) {
for (const pfile of column.pfiles) {
index += 1;
const id = `chapter-${index}`;
const href = `text/${slug(pfile.meta.title, index)}.xhtml`;
let html = md.render(renderTemplate(pfile.content, {
...this.variables,
document: {
title: pfile.meta.title,
index: pfile.meta.index,
file: pfile.full_name,
},
column: { name: column.name, index: column.index },
}));
html = removeRepeatedLeadingTitle(html, pfile.meta.title);
const targetDir = dirname(resolve(href));
const sourceDir = dirname(resolve(pfile.full_name));
const media: Array<{ href: string; bytes: Uint8Array; mime: string }> =
[];
const mediaBySource = new Map<string, string>();
const knownTargets = new Set(sourceToHref.keys());
const assetReferences = [
...html.matchAll(/\b(src|href)=(['"])([^'"]+)\2/gi),
];
for (const reference of assetReferences) {
const sourceUrl = reference[3];
if (sourceUrl.split(/[?#]/, 1)[0].toLowerCase().endsWith(".md")) {
continue;
}
if (/^(?:[a-z][a-z0-9+.-]*:|#|\/\/)/i.test(sourceUrl)) continue;
const sourceAsset = resolve(
sourceDir,
decodeURIComponent(sourceUrl.split(/[?#]/, 1)[0]),
);
const assetMarker = sourceUrl.match(/(?:^|\/)assets\/(.+)$/i);
const sharedAsset = assetMarker && this.config.asset_base
? resolve(this.config.asset_base, assetMarker[1])
: undefined;
const resolvedAsset = sharedAsset && await Deno.stat(sharedAsset)
.then((info) => info.isFile ? sharedAsset : undefined)
.catch(() => undefined) || sourceAsset;
if (knownTargets.has(sourceAsset)) continue;
const localAsset = resolvedAsset.startsWith(
`${resolve(this.config.journal.from_directory)}${sep}`,
) || (sharedAsset != null && resolvedAsset === sharedAsset);
if (!localAsset) continue;
try {
const info = await Deno.stat(resolvedAsset);
if (!info.isFile) continue;
} catch {
continue;
}
const destination = mediaBySource.get(resolvedAsset) ??
`media/${index}/${media.length + 1}${
extname(resolvedAsset).toLowerCase()
}`;
if (!mediaBySource.has(resolvedAsset)) {
mediaBySource.set(resolvedAsset, destination);
const ext = extname(resolvedAsset).toLowerCase();
media.push({
href: destination,
bytes: await Deno.readFile(resolvedAsset),
mime: mediaType(ext),
});
}
const escapedUrl = reference[0];
const replacement = `${reference[1]}=${reference[2]}${
relative(targetDir, resolve(destination)).split(sep).join("/")
}${reference[2]}`;
html = html.replace(escapedUrl, replacement);
}
// Resolve authored links that point to another Markdown chapter inside this EPUB.
html = html.replace(
/\b(href)=(['"])([^'"]+\.md(?:#[^'"]*)?)\2/gi,
(match, attr: string, quote: string, target: string) => {
const [filePart, fragment] = target.split("#", 2);
const targetPath = resolve(sourceDir, decodeURIComponent(filePart));
const chapterHref = sourceToHref.get(targetPath);
return chapterHref
? `${attr}=${quote}${
relative(targetDir, resolve(chapterHref)).split(sep).join("/")
}${fragment ? `#${fragment}` : ""}${quote}`
: match;
},
);
chapters.push({
id,
href,
title: pfile.meta.title,
html,
sourcePath: pfile.full_name,
column: column.name,
});
for (const asset of media) {
epubMedia.set(asset.href, asset.bytes);
mediaManifest.set(asset.href, asset.mime);
}
}
}
if (chapters.length === 0) {
throw new Error("No Markdown articles found for EPUB");
}
const epub = this.config.epub ?? {};
const title = epub.title ?? this.config.journal.title ?? "未命名出版物";
const author = epub.author ?? this.config.journal.author ?? "未知作者";
const language = epub.language ?? this.config.journal.language ?? "zh-CN";
const identifier = this.config.journal.identifier ??
`urn:uuid:${await this.contentIdentifier(chapters)}`;
const files: Record<string, Uint8Array> = {
mimetype: strToU8("application/epub+zip"),
"META-INF/container.xml": strToU8(
`<?xml version="1.0" encoding="UTF-8"?>\n<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="EPUB/content.opf" media-type="application/oebps-package+xml"/></rootfiles></container>`,
),
"EPUB/nav.xhtml": strToU8(navXhtml(chapters, title)),
"EPUB/content.opf": strToU8(
packageOpf(
chapters,
mediaManifest,
title,
author,
language,
identifier,
),
),
};
for (const [href, bytes] of epubMedia) files[`EPUB/${href}`] = bytes;
chapters.forEach((chapter) => {
files[`EPUB/${chapter.href}`] = strToU8(
`<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" lang="${
escapeXml(language)
}" xml:lang="${
escapeXml(language)
}"><head><meta charset="utf-8"/><title>${
escapeXml(chapter.title)
}</title></head><body><h1>${
escapeXml(chapter.title)
}</h1>${chapter.html}</body></html>`,
);
});
const orderedEntries: Zippable = {
mimetype: [files.mimetype, { level: 0 }],
};
for (const [name, bytes] of Object.entries(files)) {
if (name !== "mimetype") orderedEntries[name] = bytes;
}
const archive = zipSync(orderedEntries, {
level: 6,
mtime: new Date("2020-01-01T00:00:00Z"),
});
await Deno.mkdir(path.replace(/[/\\][^/\\]*$/, ""), { recursive: true });
await Deno.writeFile(path, archive);
}
outputPath(): string {
if (this.config.epub?.path) return this.config.epub.path;
const sourcePath = this.config.md?.path ?? this.config.markdown_output;
if (!sourcePath) return "./outputs/book.epub";
return sourcePath.replace(/\.md$/i, ".epub");
}
private async contentIdentifier(chapters: EpubChapter[]): Promise<string> {
const digestInput = new TextEncoder().encode(
`${this.config.journal.from_directory}\n${
chapters.map((chapter) => chapter.sourcePath).join("\n")
}`,
);
const digest = await crypto.subtle.digest("SHA-256", digestInput);
return [...new Uint8Array(digest)].map((byte) =>
byte.toString(16).padStart(2, "0")
).join("");
}
}
export function inspectEpub(bytes: Uint8Array): Record<string, Uint8Array> {
return unzipSync(bytes);
}