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 @@
|
||||
/nix/store/ljcx5g5zlms59kb3bd7jm0ilc22y4ab7-source
|
||||
@@ -0,0 +1,129 @@
|
||||
asset_base = "../../assets"
|
||||
|
||||
[[settings]]
|
||||
name = "mobile"
|
||||
|
||||
[settings.journal]
|
||||
from_directory = "../../publications/26.11.0"
|
||||
build_js_script = false
|
||||
build_mode = "detached"
|
||||
template_directory = "../../templates/mobile_template"
|
||||
folder_mode = "flat"
|
||||
|
||||
[settings.pdf]
|
||||
generate_index = true
|
||||
|
||||
[settings.pdf.use_qpdf]
|
||||
enabled = true
|
||||
options = ["--linearize"]
|
||||
|
||||
[settings.pdf.signing]
|
||||
enabled = true
|
||||
key_path = "~/keys/中国马列毛主义统一战线_0x874AD0D3_SECRET.asc"
|
||||
output_path = "./outputs/工农解放报RSF续刊第二期-适合手机阅读.pdf.sig"
|
||||
|
||||
[settings.playwright]
|
||||
path = "./outputs/工农解放报RSF续刊第二期-适合手机阅读.pdf"
|
||||
format = "A4"
|
||||
printBackground = true
|
||||
displayHeaderFooter = true
|
||||
footerTemplate = '<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="pageNumber"></span> / <span class="totalPages"></span></div>'
|
||||
headerTemplate = '<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="title"></span></div>'
|
||||
|
||||
[settings.playwright.margin]
|
||||
bottom = "20mm"
|
||||
top = "20mm"
|
||||
|
||||
[settings.md]
|
||||
path = "./outputs/工农解放报RSF续刊第二期-适合手机阅读.md"
|
||||
|
||||
[settings.epub]
|
||||
path = "./outputs/工农解放报RSF续刊第二期-适合手机阅读.epub"
|
||||
title = "工农解放报RSF续刊第二期"
|
||||
language = "zh-CN"
|
||||
|
||||
[[settings]]
|
||||
name = "computer"
|
||||
|
||||
[settings.journal]
|
||||
from_directory = "../../publications/26.11.0"
|
||||
build_js_script = false
|
||||
build_mode = "detached"
|
||||
template_directory = "../../templates/computer_template"
|
||||
folder_mode = "flat"
|
||||
|
||||
[settings.pdf]
|
||||
generate_index = true
|
||||
|
||||
[settings.pdf.use_qpdf]
|
||||
enabled = true
|
||||
options = ["--linearize"]
|
||||
|
||||
[settings.pdf.signing]
|
||||
enabled = true
|
||||
key_path = "~/keys/中国马列毛主义统一战线_0x874AD0D3_SECRET.asc"
|
||||
output_path = "./outputs/工农解放报RSF续刊第二期-适合电脑阅读.pdf.sig"
|
||||
|
||||
[settings.playwright]
|
||||
path = "./outputs/工农解放报RSF续刊第二期-适合电脑阅读.pdf"
|
||||
format = "A4"
|
||||
printBackground = true
|
||||
displayHeaderFooter = true
|
||||
footerTemplate = '<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="pageNumber"></span> / <span class="totalPages"></span></div>'
|
||||
headerTemplate = '<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="title"></span></div>'
|
||||
|
||||
[settings.playwright.margin]
|
||||
bottom = "20mm"
|
||||
top = "20mm"
|
||||
|
||||
[settings.md]
|
||||
path = "./outputs/工农解放报RSF续刊第二期-适合电脑阅读.md"
|
||||
|
||||
[settings.epub]
|
||||
path = "./outputs/工农解放报RSF续刊第二期-适合电脑阅读.epub"
|
||||
title = "工农解放报RSF续刊第二期"
|
||||
language = "zh-CN"
|
||||
|
||||
[[settings]]
|
||||
name = "printable"
|
||||
|
||||
[settings.journal]
|
||||
from_directory = "../../publications/26.11.0"
|
||||
build_mode = "detached"
|
||||
template_directory = "../../templates/printable_template"
|
||||
folder_mode = "flat"
|
||||
build_js_script = "../../templates/printable_template/build.js"
|
||||
|
||||
[settings.pdf]
|
||||
generate_index = true
|
||||
|
||||
[settings.pdf.use_qpdf]
|
||||
enabled = true
|
||||
options = [""]
|
||||
|
||||
[settings.pdf.signing]
|
||||
enabled = true
|
||||
key_path = "~/keys/中国马列毛主义统一战线_0x874AD0D3_SECRET.asc"
|
||||
output_path = "./outputs/工农解放报RSF续刊第二期-适合纸质打印.pdf.sig"
|
||||
|
||||
[settings.playwright]
|
||||
path = "./outputs/工农解放报RSF续刊第二期-适合纸质打印.pdf"
|
||||
format = "A4"
|
||||
printBackground = true
|
||||
displayHeaderFooter = true
|
||||
footerTemplate = '<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="pageNumber"></span> / <span class="totalPages"></span></div>'
|
||||
headerTemplate = '<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="title"></span></div>'
|
||||
|
||||
[settings.playwright.margin]
|
||||
bottom = "20mm"
|
||||
top = "20mm"
|
||||
|
||||
[settings.md]
|
||||
path = "./outputs/工农解放报RSF续刊第二期-适合纸质打印.md"
|
||||
|
||||
[settings.epub]
|
||||
path = "./outputs/工农解放报RSF续刊第二期-适合纸质打印.epub"
|
||||
title = "工农解放报RSF续刊第二期"
|
||||
language = "zh-CN"
|
||||
|
||||
[vars]
|
||||
@@ -0,0 +1,379 @@
|
||||
import { strToU8, unzipSync, type Zippable, zipSync } from "fflate";
|
||||
import { dirname, extname, relative, resolve, sep } from "node:path";
|
||||
import { PFiles } from "./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) => ({
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
'"': """,
|
||||
"'": "'",
|
||||
}[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]),
|
||||
);
|
||||
if (knownTargets.has(sourceAsset)) continue;
|
||||
if (
|
||||
!sourceAsset.startsWith(
|
||||
`${resolve(this.config.journal.from_directory)}${sep}`,
|
||||
)
|
||||
) continue;
|
||||
try {
|
||||
const info = await Deno.stat(sourceAsset);
|
||||
if (!info.isFile) continue;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
const destination = mediaBySource.get(sourceAsset) ??
|
||||
`media/${index}/${media.length + 1}${
|
||||
extname(sourceAsset).toLowerCase()
|
||||
}`;
|
||||
if (!mediaBySource.has(sourceAsset)) {
|
||||
mediaBySource.set(sourceAsset, destination);
|
||||
const ext = extname(sourceAsset).toLowerCase();
|
||||
media.push({
|
||||
href: destination,
|
||||
bytes: await Deno.readFile(sourceAsset),
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { assertEquals, assertStringIncludes } from "@std/assert";
|
||||
import { EpubBuilder, inspectEpub } from "./epub_builder.ts";
|
||||
|
||||
Deno.test("generates EPUB 3 with a linked table of contents and spine", async () => {
|
||||
const root = await Deno.makeTempDir();
|
||||
const publication = `${root}/publication`;
|
||||
const templates = `${root}/templates`;
|
||||
const output = `${root}/book.epub`;
|
||||
await Deno.mkdir(`${publication}/1::专栏`, { recursive: true });
|
||||
await Deno.mkdir(templates, { recursive: true });
|
||||
await Deno.writeTextFile(
|
||||
`${publication}/1::专栏/1::第一篇.md`,
|
||||
"正文甲",
|
||||
);
|
||||
await Deno.writeTextFile(
|
||||
`${publication}/1::专栏/2::第二篇.md`,
|
||||
"# 第二篇\n\n第二段\n\n\n\n[转到第一篇](1::第一篇.md)",
|
||||
);
|
||||
await Deno.mkdir(`${publication}/1::专栏/assets`, { recursive: true });
|
||||
await Deno.writeFile(
|
||||
`${publication}/1::专栏/assets/pixel.png`,
|
||||
new Uint8Array([1, 2, 3]),
|
||||
);
|
||||
const builder = new EpubBuilder({
|
||||
journal: {
|
||||
build_mode: "detached",
|
||||
from_directory: publication,
|
||||
template_directory: templates,
|
||||
identifier: "urn:test:book",
|
||||
},
|
||||
epub: { path: output, title: "测试书", author: "作者甲" },
|
||||
}).with_files_sync();
|
||||
|
||||
try {
|
||||
await builder.build();
|
||||
const archiveBytes = await Deno.readFile(output);
|
||||
const zip = inspectEpub(archiveBytes);
|
||||
const mimetype = new TextDecoder().decode(zip.mimetype);
|
||||
assertEquals(mimetype, "application/epub+zip");
|
||||
const archiveView = new DataView(
|
||||
archiveBytes.buffer,
|
||||
archiveBytes.byteOffset,
|
||||
archiveBytes.byteLength,
|
||||
);
|
||||
// Local-file-header fields: compression method is the uint16 at offset 8.
|
||||
assertEquals(archiveView.getUint16(8, true), 0);
|
||||
assertEquals(
|
||||
new TextDecoder().decode(archiveBytes.slice(30, 38)),
|
||||
"mimetype",
|
||||
);
|
||||
|
||||
const nav = new TextDecoder().decode(zip["EPUB/nav.xhtml"]);
|
||||
assertStringIncludes(nav, 'epub:type="toc"');
|
||||
assertStringIncludes(nav, "text/0001-第一篇.xhtml");
|
||||
assertStringIncludes(nav, "text/0002-第二篇.xhtml");
|
||||
const opf = new TextDecoder().decode(zip["EPUB/content.opf"]);
|
||||
assertStringIncludes(opf, "<spine>");
|
||||
assertStringIncludes(opf, '<itemref idref="chapter-1"/>');
|
||||
assertStringIncludes(
|
||||
new TextDecoder().decode(zip["EPUB/text/0001-第一篇.xhtml"]),
|
||||
"正文甲",
|
||||
);
|
||||
const secondChapter = new TextDecoder().decode(
|
||||
zip["EPUB/text/0002-第二篇.xhtml"],
|
||||
);
|
||||
assertEquals((secondChapter.match(/<h1\b/g) ?? []).length, 1);
|
||||
assertStringIncludes(secondChapter, "第二篇</h1>");
|
||||
assertStringIncludes(secondChapter, "../media/2/1.png");
|
||||
assertStringIncludes(secondChapter, "0001-第一篇.xhtml");
|
||||
assertEquals(zip["EPUB/media/2/1.png"].length, 3);
|
||||
} finally {
|
||||
await Deno.remove(root, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("uses EPUB output path or a stable default", () => {
|
||||
const builder = new EpubBuilder({
|
||||
journal: { from_directory: "publication", template_directory: "templates" },
|
||||
});
|
||||
assertEquals(builder.outputPath(), "./outputs/book.epub");
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { comparePColumes, PColume, PFile, PFileMetadata } from "./file.ts";
|
||||
|
||||
function column(name: string, order: number): PColume {
|
||||
const meta = new PFileMetadata(`${order}::${name}/1::sample.md`);
|
||||
return new PColume(name, [new PFile(`${name}/1::sample.md`, meta, "")]);
|
||||
}
|
||||
|
||||
Deno.test("orders explicitly numbered columns before unnumbered columns", () => {
|
||||
const columns = [column("乙", Number.NaN), column("丙", 2), column("甲", 1)];
|
||||
columns.sort(comparePColumes);
|
||||
assertEquals(columns.map((item) => item.name), ["甲", "丙", "乙"]);
|
||||
});
|
||||
|
||||
Deno.test("orders unnumbered columns by their first Chinese character", () => {
|
||||
const columns = [
|
||||
column("丙栏目", Number.NaN),
|
||||
column("阿栏目", Number.NaN),
|
||||
column("乙栏目", Number.NaN),
|
||||
];
|
||||
columns.sort(comparePColumes);
|
||||
assertEquals(columns.map((item) => item.name), [
|
||||
"阿栏目",
|
||||
"丙栏目",
|
||||
"乙栏目",
|
||||
]);
|
||||
});
|
||||
|
||||
Deno.test("parses detached column folders without an order prefix", () => {
|
||||
const meta = new PFileMetadata("/publication/专栏名称/1::文章标题.md");
|
||||
assertEquals(meta.colume, "专栏名称");
|
||||
assertEquals(Number.isNaN(meta.c_index), true);
|
||||
});
|
||||
|
||||
Deno.test("parses finite JavaScript numbers as detached column order", () => {
|
||||
for (const [prefix, expected] of [
|
||||
["-2.5", -2.5],
|
||||
["+3.25", 3.25],
|
||||
["1e3", 1000],
|
||||
["0x10", 16],
|
||||
]) {
|
||||
const meta = new PFileMetadata(`/publication/${prefix}::专栏/1::文章.md`);
|
||||
assertEquals(meta.colume, "专栏");
|
||||
assertEquals(meta.c_index, expected);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("sorts negative, fractional, and large numeric order values", () => {
|
||||
const columns = [
|
||||
column("大数", Number.MAX_VALUE),
|
||||
column("小数", 0.25),
|
||||
column("负数", -10),
|
||||
column("非数字", Number.NaN),
|
||||
column("负小数", -0.5),
|
||||
];
|
||||
columns.sort(comparePColumes);
|
||||
assertEquals(columns.map((item) => item.name), [
|
||||
"负数",
|
||||
"负小数",
|
||||
"小数",
|
||||
"大数",
|
||||
"非数字",
|
||||
]);
|
||||
});
|
||||
|
||||
Deno.test("does not treat non-finite numeric strings as explicit order", () => {
|
||||
const nanMeta = new PFileMetadata("/publication/NaN::专栏/1::文章.md");
|
||||
assertEquals(nanMeta.colume, "NaN::专栏");
|
||||
assertEquals(Number.isNaN(nanMeta.c_index), true);
|
||||
|
||||
const columns = [
|
||||
column("正无穷", Number.POSITIVE_INFINITY),
|
||||
column("负无穷", Number.NEGATIVE_INFINITY),
|
||||
column("有限数", 1),
|
||||
column("NaN", Number.NaN),
|
||||
];
|
||||
columns.sort(comparePColumes);
|
||||
assertEquals(columns.map((item) => item.name), [
|
||||
"负无穷",
|
||||
"有限数",
|
||||
"正无穷",
|
||||
"NaN",
|
||||
]);
|
||||
});
|
||||
@@ -0,0 +1,267 @@
|
||||
import { dirname, extname, join, resolve } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { parse as parseToml, stringify as stringifyToml } from "@std/toml";
|
||||
|
||||
export type CacheMap = Record<string, string>;
|
||||
|
||||
const RESOURCE_REFERENCE_RE =
|
||||
/(?<prefix><link\b[^>]*?\bhref\s*=\s*["']|\bsrc\s*=\s*["']|\bhref\s*=\s*["']|url\(\s*["']?|@import\s+["']|!\[[^\]]*\]\(\s*["']?|\bcontent\s*=\s*["'][^"']*\burl\s*=\s*["']?)(?<url>https?:\/\/[^\s"'<>()[\]{}]+)(?<suffix>["']?\s*\)?)/gi;
|
||||
const ASSET_EXTENSIONS = new Set([
|
||||
".avif",
|
||||
".css",
|
||||
".gif",
|
||||
".htm",
|
||||
".html",
|
||||
".jpeg",
|
||||
".jpg",
|
||||
".js",
|
||||
".mjs",
|
||||
".otf",
|
||||
".png",
|
||||
".svg",
|
||||
".ttf",
|
||||
".webp",
|
||||
".woff",
|
||||
".woff2",
|
||||
]);
|
||||
|
||||
function isLocalUrl(url: string): boolean {
|
||||
return /^(?:data:|blob:|file:|\.\.?\/|\/|#)/i.test(url);
|
||||
}
|
||||
|
||||
function isHttpUrl(url: string): boolean {
|
||||
return /^https?:\/\//i.test(url);
|
||||
}
|
||||
|
||||
function isAssetUrl(
|
||||
url: string,
|
||||
prefix: string,
|
||||
sourceExtension: string,
|
||||
): boolean {
|
||||
if (isLocalUrl(url)) return false;
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(url);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
if (!/^https?:$/.test(parsed.protocol)) return false;
|
||||
const extension = extname(parsed.pathname).toLowerCase();
|
||||
if (ASSET_EXTENSIONS.has(extension)) return true;
|
||||
if (/\.(?:css|html?)$/i.test(sourceExtension)) {
|
||||
return /^(?:src|url\(|@import|!\[)/i.test(prefix.trim());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function isResourceReference(prefix: string, url: string): boolean {
|
||||
const extension = extname(new URL(url).pathname).toLowerCase();
|
||||
if (prefix.toLowerCase().startsWith("<link")) {
|
||||
return ASSET_EXTENSIONS.has(extension);
|
||||
}
|
||||
if (prefix.toLowerCase().startsWith("href")) return extension === ".css";
|
||||
if (prefix.toLowerCase().startsWith("content")) {
|
||||
return /\.(?:png|jpe?g|gif|webp|avif|svg)$/i.test(extension);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function sourceFiles(dir: string): string[] {
|
||||
const result: string[] = [];
|
||||
if (!existsSync(dir)) return result;
|
||||
for (const entry of Deno.readDirSync(dir)) {
|
||||
const path = join(dir, entry.name);
|
||||
if (entry.isDirectory) {
|
||||
if (entry.name !== ".local-cache") result.push(...sourceFiles(path));
|
||||
} else if (entry.isFile && /\.(?:css|html?|js|mjs|md)$/i.test(entry.name)) {
|
||||
result.push(path);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function existsSync(path: string): boolean {
|
||||
try {
|
||||
return Deno.statSync(path).isDirectory;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function cacheFileName(url: string): Promise<string> {
|
||||
const parsed = new URL(url);
|
||||
const name = parsed.pathname.split("/").filter(Boolean).pop() || "asset";
|
||||
const digest = await crypto.subtle.digest(
|
||||
"SHA-256",
|
||||
new TextEncoder().encode(url),
|
||||
);
|
||||
const hash = Array.from(new Uint8Array(digest)).slice(0, 12).map((byte) =>
|
||||
byte.toString(16).padStart(2, "0")
|
||||
).join("");
|
||||
return `${hash}-${name.replace(/[^\p{L}\p{N}._-]/gu, "_")}`;
|
||||
}
|
||||
|
||||
async function readCache(path: string): Promise<CacheMap> {
|
||||
try {
|
||||
return parseToml(await Deno.readTextFile(path)) as CacheMap;
|
||||
} catch (error) {
|
||||
if (error instanceof Deno.errors.NotFound) return {};
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function prepareLocalCache(
|
||||
publicationDir: string,
|
||||
templateDir?: string,
|
||||
): Promise<number> {
|
||||
const pubDir = resolve(publicationDir);
|
||||
if (!existsSync(pubDir)) {
|
||||
throw new Error(`Publication directory does not exist: ${pubDir}`);
|
||||
}
|
||||
const cacheToml = join(pubDir, ".cache.toml");
|
||||
const cacheDir = join(pubDir, ".local-cache");
|
||||
const map = await readCache(cacheToml);
|
||||
const initialCachedCount = Object.keys(map).length;
|
||||
const files = new Set(sourceFiles(pubDir));
|
||||
if (templateDir) {
|
||||
for (const file of sourceFiles(resolve(templateDir))) files.add(file);
|
||||
}
|
||||
|
||||
const urls = new Set<string>(Object.keys(map));
|
||||
for (const file of files) {
|
||||
const content = await Deno.readTextFile(file);
|
||||
for (const match of content.matchAll(RESOURCE_REFERENCE_RE)) {
|
||||
const url = match.groups?.url;
|
||||
const prefix = match.groups?.prefix ?? "";
|
||||
if (
|
||||
url && isAssetUrl(url, prefix, extname(file).toLowerCase()) &&
|
||||
isResourceReference(prefix, url)
|
||||
) {
|
||||
urls.add(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Deno.mkdir(cacheDir, { recursive: true });
|
||||
const pending = [...urls];
|
||||
const processed = new Set<string>();
|
||||
while (pending.length > 0) {
|
||||
const url = pending.shift()!;
|
||||
if (processed.has(url)) continue;
|
||||
processed.add(url);
|
||||
const fileName = map[url]?.replace(/^\.local-cache\//, "") ??
|
||||
await cacheFileName(url);
|
||||
const target = join(cacheDir, fileName);
|
||||
if (!(await exists(target))) {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to cache ${url}: HTTP ${response.status}`);
|
||||
}
|
||||
const bytes = new Uint8Array(await response.arrayBuffer());
|
||||
await Deno.writeFile(target, bytes);
|
||||
}
|
||||
map[url] = `.local-cache/${fileName}`;
|
||||
}
|
||||
|
||||
await Deno.writeTextFile(cacheToml, stringifyToml(map));
|
||||
for (const file of files) await rewriteFile(file, pubDir, map);
|
||||
const inspected = new Set<string>();
|
||||
for (const url of [...urls]) {
|
||||
if (/\.(?:css|html?)$/i.test(new URL(url).pathname)) {
|
||||
const localFile = resolve(pubDir, map[url]);
|
||||
if (await exists(localFile)) {
|
||||
await scanDownloadedReferences(url, pubDir, map, inspected);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Object.keys(map).length - initialCachedCount;
|
||||
}
|
||||
|
||||
async function scanDownloadedReferences(
|
||||
sourceUrl: string,
|
||||
pubDir: string,
|
||||
map: CacheMap,
|
||||
processed: Set<string>,
|
||||
): Promise<void> {
|
||||
const file = resolve(pubDir, map[sourceUrl]);
|
||||
if (processed.has(sourceUrl)) return;
|
||||
processed.add(sourceUrl);
|
||||
const content = await Deno.readTextFile(file);
|
||||
const sourceExtension = extname(new URL(sourceUrl).pathname).toLowerCase();
|
||||
for (const match of content.matchAll(RESOURCE_REFERENCE_RE)) {
|
||||
const prefix = match.groups?.prefix ?? "";
|
||||
const reference = match.groups?.url;
|
||||
if (
|
||||
!reference ||
|
||||
!isAssetUrl(reference, prefix, sourceExtension) ||
|
||||
!isResourceReference(prefix, reference)
|
||||
) continue;
|
||||
let dependency: string;
|
||||
try {
|
||||
dependency = new URL(reference, sourceUrl).href;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (!isHttpUrl(dependency) || !isResourceReference(prefix, dependency)) {
|
||||
continue;
|
||||
}
|
||||
if (!map[dependency] || !(await exists(resolve(pubDir, map[dependency])))) {
|
||||
const response = await fetch(dependency);
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to cache ${dependency}: HTTP ${response.status}`,
|
||||
);
|
||||
}
|
||||
const bytes = new Uint8Array(await response.arrayBuffer());
|
||||
const fileName = await cacheFileName(dependency);
|
||||
await Deno.writeFile(join(pubDir, ".local-cache", fileName), bytes);
|
||||
map[dependency] = `.local-cache/${fileName}`;
|
||||
}
|
||||
processed.add(dependency);
|
||||
if (/\.(?:css|html?)$/i.test(new URL(dependency).pathname)) {
|
||||
await scanDownloadedReferences(dependency, pubDir, map, processed);
|
||||
}
|
||||
}
|
||||
await rewriteFile(file, pubDir, map, sourceUrl);
|
||||
await Deno.writeTextFile(join(pubDir, ".cache.toml"), stringifyToml(map));
|
||||
}
|
||||
|
||||
async function rewriteFile(
|
||||
file: string,
|
||||
pubDir: string,
|
||||
map: CacheMap,
|
||||
sourceUrl?: string,
|
||||
): Promise<void> {
|
||||
const original = await Deno.readTextFile(file);
|
||||
const rewritten = original.replace(
|
||||
RESOURCE_REFERENCE_RE,
|
||||
(whole, prefix: string, url: string) => {
|
||||
let resolvedUrl = url;
|
||||
try {
|
||||
if (sourceUrl) resolvedUrl = new URL(url, sourceUrl).href;
|
||||
} catch {
|
||||
return whole;
|
||||
}
|
||||
const localPath = map[resolvedUrl] ?? map[url];
|
||||
if (
|
||||
!localPath || !isHttpUrl(resolvedUrl) ||
|
||||
!isResourceReference(prefix, resolvedUrl)
|
||||
) return whole;
|
||||
return whole.replace(
|
||||
url,
|
||||
pathToFileURL(resolve(pubDir, localPath)).href,
|
||||
);
|
||||
},
|
||||
);
|
||||
if (rewritten !== original) await Deno.writeTextFile(file, rewritten);
|
||||
}
|
||||
|
||||
async function exists(path: string): Promise<boolean> {
|
||||
try {
|
||||
await Deno.stat(path);
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (error instanceof Deno.errors.NotFound) return false;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import { assert, assertEquals, assertStringIncludes } from "@std/assert";
|
||||
import { prepareLocalCache } from "./local_cache.ts";
|
||||
|
||||
Deno.test("local cache downloads resource references and preserves reader links", async () => {
|
||||
const root = await Deno.makeTempDir();
|
||||
const publication = `${root}/publication`;
|
||||
const templates = `${root}/templates`;
|
||||
await Deno.mkdir(publication, { recursive: true });
|
||||
await Deno.mkdir(templates, { recursive: true });
|
||||
const assetUrl = "https://cdn.example.test/asset.png";
|
||||
const cssUrl = "https://cdn.example.test/theme.css";
|
||||
const fontUrl = "https://cdn.example.test/font.woff2";
|
||||
const htmlUrl = "https://cdn.example.test/widget.html";
|
||||
const nestedImageUrl = "https://cdn.example.test/widget.png";
|
||||
await Deno.writeTextFile(
|
||||
`${publication}/article.md`,
|
||||
`\n\n[reader link](${assetUrl})`,
|
||||
);
|
||||
await Deno.writeTextFile(
|
||||
`${templates}/cover.html`,
|
||||
`<img src="${assetUrl}"><iframe src="${htmlUrl}"></iframe><link href="${cssUrl}"><a href="https://example.com/page.png">reader</a>`,
|
||||
);
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
const fetched: string[] = [];
|
||||
globalThis.fetch = async (input) => {
|
||||
const url = String(input);
|
||||
fetched.push(url);
|
||||
assert(
|
||||
url === assetUrl || url === cssUrl || url === fontUrl ||
|
||||
url === htmlUrl || url === nestedImageUrl,
|
||||
);
|
||||
if (url === cssUrl) {
|
||||
return new Response(`@font-face { src: url("${fontUrl}"); }`, {
|
||||
headers: { "content-type": "text/css" },
|
||||
});
|
||||
}
|
||||
if (url === htmlUrl) {
|
||||
return new Response(
|
||||
`<img src="https://cdn.example.test/widget.png"><a href="https://reader.example.test/story.png">story</a>`,
|
||||
{ headers: { "content-type": "text/html" } },
|
||||
);
|
||||
}
|
||||
if (url === fontUrl) {
|
||||
return new Response(new Uint8Array([1, 2, 3]), {
|
||||
headers: { "content-type": "font/woff2" },
|
||||
});
|
||||
}
|
||||
return new Response(new Uint8Array([1, 2, 3]), {
|
||||
headers: { "content-type": "image/png" },
|
||||
});
|
||||
};
|
||||
try {
|
||||
assertEquals(await prepareLocalCache(publication, templates), 5);
|
||||
const rewrittenMd = await Deno.readTextFile(`${publication}/article.md`);
|
||||
const rewrittenHtml = await Deno.readTextFile(`${templates}/cover.html`);
|
||||
assertStringIncludes(rewrittenMd, "file://");
|
||||
assertStringIncludes(rewrittenMd, `[reader link](${assetUrl})`);
|
||||
assertStringIncludes(rewrittenHtml, "file://");
|
||||
assertStringIncludes(rewrittenHtml, 'iframe src="file://');
|
||||
assertStringIncludes(rewrittenHtml, "https://example.com/page.png");
|
||||
assertEquals(
|
||||
fetched.sort(),
|
||||
[assetUrl, cssUrl, fontUrl, htmlUrl, nestedImageUrl].sort(),
|
||||
);
|
||||
|
||||
const cache = await Deno.readTextFile(`${publication}/.cache.toml`);
|
||||
assertStringIncludes(cache, assetUrl);
|
||||
assertEquals(await prepareLocalCache(publication, templates), 0);
|
||||
assertEquals(fetched.length, 5);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
await Deno.remove(root, { recursive: true });
|
||||
}
|
||||
assert(true);
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { DocuBuilder } from "./docu_builder.ts";
|
||||
|
||||
Deno.test("PDF rendering loads relative assets from configured file base", async () => {
|
||||
const chromiumPath = Deno.env.get("CHROMIUM_PATH");
|
||||
if (!chromiumPath) {
|
||||
console.warn("Skipping Chromium asset test: CHROMIUM_PATH is unset");
|
||||
return;
|
||||
}
|
||||
|
||||
const builder = new DocuBuilder({
|
||||
asset_base: "../../assets",
|
||||
journal: { build_js_script: false },
|
||||
});
|
||||
const pdfPath = await Deno.makeTempFile({ suffix: ".pdf" });
|
||||
await builder.with_browser();
|
||||
try {
|
||||
await builder.build_as_pdf({ path: pdfPath, format: "A4" }, [
|
||||
'<img src="pictures/rsf.png" alt="Asset">',
|
||||
]);
|
||||
} finally {
|
||||
await builder.close_browser();
|
||||
}
|
||||
assertEquals((await Deno.stat(pdfPath)).size > 0, true);
|
||||
await Deno.remove(pdfPath);
|
||||
});
|
||||
@@ -0,0 +1,307 @@
|
||||
import { basename, dirname, join, relative, resolve } from "node:path";
|
||||
import { cfg } from "./main.ts";
|
||||
|
||||
type WebDavOptions = Record<string, unknown>;
|
||||
type WebDavConfig = {
|
||||
baseUrl: string;
|
||||
username?: string;
|
||||
password?: string;
|
||||
remoteDir: string;
|
||||
};
|
||||
|
||||
async function readDotEnv(): Promise<Record<string, string>> {
|
||||
const result: Record<string, string> = {};
|
||||
try {
|
||||
for (const line of (await Deno.readTextFile(".env")).split(/\r?\n/)) {
|
||||
const match = line.match(
|
||||
/^\s*(?:export\s+)?([A-Za-z_][\w]*)\s*=\s*(.*)\s*$/,
|
||||
);
|
||||
if (!match) continue;
|
||||
result[match[1]] = match[2].replace(/^(["'])(.*)\1$/, "$2");
|
||||
}
|
||||
} catch (error) {
|
||||
if (!(error instanceof Deno.errors.NotFound)) throw error;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async function webDavConfig(options: WebDavOptions): Promise<WebDavConfig> {
|
||||
const env = await readDotEnv();
|
||||
const envValue = (...keys: string[]) => {
|
||||
for (const key of keys) {
|
||||
const value = env[key] ?? Deno.env.get(key);
|
||||
if (value) return value;
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
const baseUrl = String(
|
||||
options.url ?? envValue("WEBDAV_URL", "WEBDAV_BASE_URL") ?? "",
|
||||
);
|
||||
if (!baseUrl) {
|
||||
throw new Error("WebDAV URL required: --url or WEBDAV_URL in .env");
|
||||
}
|
||||
const parsedBaseUrl = new URL(baseUrl);
|
||||
if (!/^https?:$/.test(parsedBaseUrl.protocol)) {
|
||||
throw new Error("WebDAV URL must use HTTP or HTTPS");
|
||||
}
|
||||
const remoteDir = String(
|
||||
options["remote-dir"] ?? envValue("WEBDAV_REMOTE_DIR") ?? "gp_upload",
|
||||
).replace(/^\/+|\/+$/g, "");
|
||||
if (remoteDir.split("/").some((part) => part === "." || part === "..")) {
|
||||
throw new Error("WebDAV remote directory cannot contain dot segments");
|
||||
}
|
||||
return {
|
||||
baseUrl: baseUrl.replace(/\/+$/, ""),
|
||||
username: String(
|
||||
options.user ?? envValue("WEBDAV_USERNAME", "WEBDAV_USER") ?? "",
|
||||
) || undefined,
|
||||
password: String(options.password ?? envValue("WEBDAV_PASSWORD") ?? "") ||
|
||||
undefined,
|
||||
remoteDir,
|
||||
};
|
||||
}
|
||||
|
||||
function authHeaders(config: WebDavConfig): HeadersInit {
|
||||
if (!config.username && !config.password) return {};
|
||||
const token = btoa(`${config.username ?? ""}:${config.password ?? ""}`);
|
||||
return { authorization: `Basic ${token}` };
|
||||
}
|
||||
|
||||
function remoteUrl(config: WebDavConfig, path: string): string {
|
||||
const base = new URL(`${config.baseUrl}/`);
|
||||
const suffix = [config.remoteDir, path].filter(Boolean).join("/");
|
||||
return new URL(suffix.split("/").map(encodeURIComponent).join("/"), base)
|
||||
.href;
|
||||
}
|
||||
|
||||
async function ensureCollection(
|
||||
config: WebDavConfig,
|
||||
path: string,
|
||||
): Promise<void> {
|
||||
const url = remoteUrl(config, path);
|
||||
const response = await fetch(url, {
|
||||
method: "MKCOL",
|
||||
headers: authHeaders(config),
|
||||
});
|
||||
if (
|
||||
response.ok || response.status === 405 || response.status === 301 ||
|
||||
response.status === 302
|
||||
) return;
|
||||
if (response.status === 409) {
|
||||
const parent = dirname(path);
|
||||
if (parent !== "." && parent !== path) {
|
||||
await ensureCollection(config, parent);
|
||||
}
|
||||
const retry = await fetch(url, {
|
||||
method: "MKCOL",
|
||||
headers: authHeaders(config),
|
||||
});
|
||||
if (retry.ok || retry.status === 405) return;
|
||||
throw new Error(`WebDAV MKCOL ${path} failed: HTTP ${retry.status}`);
|
||||
}
|
||||
throw new Error(`WebDAV MKCOL ${path} failed: HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
async function walkFiles(root: string): Promise<string[]> {
|
||||
const files: string[] = [];
|
||||
for (const entry of Deno.readDirSync(root)) {
|
||||
const path = join(root, entry.name);
|
||||
if (entry.isDirectory) files.push(...await walkFiles(path));
|
||||
else if (entry.isFile) files.push(path);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
function roots(
|
||||
options: WebDavOptions,
|
||||
): Array<{ local: string; remote: string }> {
|
||||
const type = String(options.type ?? "all");
|
||||
if (!new Set(["all", "assets", "cache"]).has(type)) {
|
||||
throw new Error("--type must be all, cache, or assets");
|
||||
}
|
||||
const injectedRoots = options.localRoots;
|
||||
if (Array.isArray(injectedRoots)) {
|
||||
return injectedRoots.filter((
|
||||
root,
|
||||
): root is { local: string; remote: string } =>
|
||||
root && typeof root.local === "string" &&
|
||||
typeof root.remote === "string" &&
|
||||
((type === "all" &&
|
||||
(root.remote === "assets" || root.remote.startsWith("cache/"))) ||
|
||||
(type === "assets" && root.remote === "assets") ||
|
||||
(type === "cache" && root.remote.startsWith("cache/")))
|
||||
);
|
||||
}
|
||||
const roots: Array<{ local: string; remote: string }> = [];
|
||||
if (type === "all" || type === "assets") {
|
||||
roots.push({ local: resolve("../../assets"), remote: "assets" });
|
||||
}
|
||||
if (type === "all" || type === "cache") {
|
||||
for (const setting of cfg.settings) {
|
||||
const publication = resolve(setting.journal.from_directory);
|
||||
roots.push({
|
||||
local: publication,
|
||||
remote: `cache/${basename(publication)}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
return roots;
|
||||
}
|
||||
|
||||
export async function upload_webdav(options: WebDavOptions): Promise<void> {
|
||||
const config = await webDavConfig(options);
|
||||
await ensureCollection(config, "");
|
||||
for (const root of roots(options)) {
|
||||
try {
|
||||
await Deno.stat(root.local);
|
||||
} catch (error) {
|
||||
if (error instanceof Deno.errors.NotFound) continue;
|
||||
throw error;
|
||||
}
|
||||
await ensureCollection(config, root.remote);
|
||||
for (const file of await walkFiles(root.local)) {
|
||||
const path = relative(root.local, file).split("\\").join("/");
|
||||
if (
|
||||
root.remote.startsWith("cache/") && path !== ".cache.toml" &&
|
||||
!path.startsWith(".local-cache/")
|
||||
) continue;
|
||||
const parent = dirname(path);
|
||||
if (parent !== ".") {
|
||||
await ensureCollection(config, `${root.remote}/${parent}`);
|
||||
}
|
||||
const response = await fetch(
|
||||
remoteUrl(config, `${root.remote}/${path}`),
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
...authHeaders(config),
|
||||
"content-type": "application/octet-stream",
|
||||
},
|
||||
body: await Deno.readFile(file),
|
||||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(`WebDAV PUT ${path} failed: HTTP ${response.status}`);
|
||||
}
|
||||
console.log(`Uploaded ${file}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function sync_webdav(options: WebDavOptions): Promise<void> {
|
||||
const config = await webDavConfig(options);
|
||||
for (const root of roots(options)) {
|
||||
await Deno.mkdir(root.local, { recursive: true });
|
||||
await downloadCollection(config, root.remote, root.local);
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadCollection(
|
||||
config: WebDavConfig,
|
||||
remotePath: string,
|
||||
localPath: string,
|
||||
): Promise<void> {
|
||||
const response = await fetch(remoteUrl(config, remotePath), {
|
||||
method: "PROPFIND",
|
||||
headers: { ...authHeaders(config), Depth: "1" },
|
||||
});
|
||||
if (response.status === 404) return;
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`WebDAV PROPFIND ${remotePath} failed: HTTP ${response.status}`,
|
||||
);
|
||||
}
|
||||
const xml = await response.text();
|
||||
const requestUrl = new URL(remoteUrl(config, remotePath));
|
||||
const basePath = requestUrl.pathname.replace(/\/+$/, "");
|
||||
const responses = [
|
||||
...xml.matchAll(/<[^>]*response\b[^>]*>([\s\S]*?)<\/[^>]*response>/gi),
|
||||
];
|
||||
for (const [, responseXml] of responses) {
|
||||
const hrefMatch = responseXml.match(/<[^>]*href[^>]*>(.*?)<\/[^>]*href>/i);
|
||||
if (!hrefMatch) continue;
|
||||
const href = decodeXmlText(hrefMatch[1].trim());
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(href, config.baseUrl);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (parsed.origin !== requestUrl.origin) continue;
|
||||
const entry = parsed.pathname.replace(/\/+$/, "");
|
||||
if (entry === basePath || !entry.startsWith(`${basePath}/`)) continue;
|
||||
const relativePath = entry.slice(basePath.length + 1);
|
||||
let name: string;
|
||||
try {
|
||||
name = decodeURIComponent(relativePath);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (name.includes("/")) continue;
|
||||
if (!name || name === "." || name === ".." || name.includes("\\")) {
|
||||
continue;
|
||||
}
|
||||
const isDirectory = /<[^>]*collection\b/i.test(responseXml);
|
||||
if (isDirectory) {
|
||||
await Deno.mkdir(join(localPath, name), { recursive: true });
|
||||
await downloadCollection(
|
||||
config,
|
||||
`${remotePath}/${name}`,
|
||||
join(localPath, name),
|
||||
);
|
||||
} else {
|
||||
const fileResponse = await fetch(new URL(href, config.baseUrl), {
|
||||
headers: authHeaders(config),
|
||||
});
|
||||
if (!fileResponse.ok) {
|
||||
throw new Error(
|
||||
`WebDAV GET ${name} failed: HTTP ${fileResponse.status}`,
|
||||
);
|
||||
}
|
||||
await Deno.writeFile(
|
||||
join(localPath, name),
|
||||
new Uint8Array(await fileResponse.arrayBuffer()),
|
||||
);
|
||||
console.log(`Downloaded ${join(localPath, name)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function decodeXmlText(text: string): string {
|
||||
return text.replace(/&(?:amp|lt|gt|quot|apos);/g, (entity) => ({
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
""": '"',
|
||||
"'": "'",
|
||||
}[entity] ?? entity));
|
||||
}
|
||||
|
||||
export async function clean_local_files(options: WebDavOptions): Promise<void> {
|
||||
const type = String(options.type ?? "all");
|
||||
if (!new Set(["all", "cache", "assets"]).has(type)) {
|
||||
throw new Error("clean --type must be all, cache, or assets");
|
||||
}
|
||||
const targets: string[] = [];
|
||||
if (type === "all" || type === "assets") {
|
||||
targets.push(resolve("../../assets"));
|
||||
}
|
||||
if (type === "all" || type === "cache") {
|
||||
for (const setting of cfg.settings) {
|
||||
targets.push(
|
||||
join(resolve(setting.journal.from_directory), ".local-cache"),
|
||||
);
|
||||
targets.push(
|
||||
join(resolve(setting.journal.from_directory), ".cache.toml"),
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const target of new Set(targets)) {
|
||||
try {
|
||||
await Deno.remove(target, { recursive: true });
|
||||
console.log(`Removed ${target}`);
|
||||
} catch (error) {
|
||||
if (!(error instanceof Deno.errors.NotFound)) throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
import { assert, assertEquals, assertRejects } from "@std/assert";
|
||||
import { clean_local_files, sync_webdav, upload_webdav } from "./webdav.ts";
|
||||
|
||||
function propfindResponse(
|
||||
entries: Array<{
|
||||
href: string;
|
||||
collection?: boolean;
|
||||
}>,
|
||||
): Response {
|
||||
const responses = entries.map(({ href, collection }) =>
|
||||
`<d:response><d:href>${href}</d:href><d:propstat><d:prop>$
|
||||
${collection ? "<d:resourcetype><d:collection/></d:resourcetype>" : ""}
|
||||
</d:prop></d:propstat></d:response>`
|
||||
).join("");
|
||||
return new Response(
|
||||
`<?xml version="1.0"?><d:multistatus xmlns:d="DAV:">${responses}</d:multistatus>`,
|
||||
{ status: 207 },
|
||||
);
|
||||
}
|
||||
|
||||
Deno.test("WebDAV sync maps assets and cache into their local roots", async () => {
|
||||
const base = "https://dav.example.test/remote.php/dav/files/user";
|
||||
const root = await Deno.makeTempDir();
|
||||
const assetsRoot = `${root}/assets`;
|
||||
const cacheRoot = `${root}/publication`;
|
||||
const seen: string[] = [];
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (input, init) => {
|
||||
const url = String(input);
|
||||
const method = (init as RequestInit | undefined)?.method ?? "GET";
|
||||
seen.push(`${method} ${url}`);
|
||||
if (method === "PROPFIND" && url.endsWith("/gp_upload/assets")) {
|
||||
return propfindResponse([
|
||||
{ href: `${base}/gp_upload/assets/` },
|
||||
{ href: `${base}/gp_upload/assets/pictures/`, collection: true },
|
||||
{
|
||||
href:
|
||||
"https://evil.example.test/remote.php/dav/files/user/gp_upload/assets/outside.png",
|
||||
},
|
||||
]);
|
||||
}
|
||||
if (method === "PROPFIND" && url.endsWith("/gp_upload/cache/26.11.0")) {
|
||||
return propfindResponse([
|
||||
{ href: `${base}/gp_upload/cache/26.11.0/` },
|
||||
{ href: `${base}/gp_upload/cache/26.11.0/.cache.toml` },
|
||||
{
|
||||
href: `${base}/gp_upload/cache/26.11.0/.local-cache/`,
|
||||
collection: true,
|
||||
},
|
||||
]);
|
||||
}
|
||||
if (method === "PROPFIND" && url.endsWith("/gp_upload/cache")) {
|
||||
return propfindResponse([
|
||||
{ href: `${base}/gp_upload/cache/` },
|
||||
{ href: `${base}/gp_upload/cache/26.11.0/`, collection: true },
|
||||
]);
|
||||
}
|
||||
if (method === "PROPFIND" && url.endsWith("/assets/pictures")) {
|
||||
return propfindResponse([
|
||||
{ href: `${base}/gp_upload/assets/pictures/` },
|
||||
{ href: `${base}/gp_upload/assets/pictures/rsf.png` },
|
||||
]);
|
||||
}
|
||||
if (method === "PROPFIND" && url.endsWith("/.local-cache")) {
|
||||
return propfindResponse([
|
||||
{ href: `${base}/gp_upload/cache/26.11.0/.local-cache/` },
|
||||
{
|
||||
href: `${base}/gp_upload/cache/26.11.0/.local-cache/a.png`,
|
||||
},
|
||||
]);
|
||||
}
|
||||
if (method === "GET") return new Response("ok");
|
||||
throw new Error(`Unexpected WebDAV request: ${method} ${url}`);
|
||||
};
|
||||
try {
|
||||
await sync_webdav({
|
||||
url: base,
|
||||
"remote-dir": "gp_upload",
|
||||
type: "all",
|
||||
localRoots: [
|
||||
{ local: assetsRoot, remote: "assets" },
|
||||
{ local: cacheRoot, remote: "cache/26.11.0" },
|
||||
],
|
||||
});
|
||||
assertEquals(
|
||||
await Deno.readTextFile(`${assetsRoot}/pictures/rsf.png`),
|
||||
"ok",
|
||||
);
|
||||
assertEquals(await Deno.readTextFile(`${cacheRoot}/.cache.toml`), "ok");
|
||||
assertEquals(
|
||||
await Deno.readTextFile(`${cacheRoot}/.local-cache/a.png`),
|
||||
"ok",
|
||||
);
|
||||
assertEquals(
|
||||
seen.some((request) => request.includes("outside.png")),
|
||||
false,
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
await Deno.remove(root, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("WebDAV upload creates remote collections and uploads only cache files", async () => {
|
||||
const root = await Deno.makeTempDir();
|
||||
const publication = `${root}/publication`;
|
||||
await Deno.mkdir(`${publication}/.local-cache`, { recursive: true });
|
||||
await Deno.writeTextFile(`${publication}/.cache.toml`, '"a" = "b"');
|
||||
await Deno.writeTextFile(`${publication}/.local-cache/a.png`, "data");
|
||||
await Deno.writeTextFile(`${publication}/article.md`, "must not upload");
|
||||
const methods: string[] = [];
|
||||
const uploads: string[] = [];
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (input, init) => {
|
||||
const method = (init as RequestInit | undefined)?.method ?? "GET";
|
||||
methods.push(method);
|
||||
if (method === "PUT") uploads.push(String(input));
|
||||
return new Response(null, { status: 201 });
|
||||
};
|
||||
try {
|
||||
await upload_webdav({
|
||||
url: "https://dav.example.test/root",
|
||||
type: "cache",
|
||||
localRoots: [
|
||||
{ local: publication, remote: "cache/test-publication" },
|
||||
],
|
||||
});
|
||||
assert(methods.includes("MKCOL"));
|
||||
assert(methods.includes("PUT"));
|
||||
assertEquals(uploads.length, 2);
|
||||
assertEquals(uploads.some((url) => url.endsWith("article.md")), false);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
await Deno.remove(root, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("WebDAV commands reject invalid types and unsafe remote roots", async () => {
|
||||
await assertRejects(
|
||||
() => sync_webdav({ url: "https://dav.example.test", type: "other" }),
|
||||
Error,
|
||||
"--type must be all, cache, or assets",
|
||||
);
|
||||
await assertRejects(
|
||||
() => clean_local_files({ type: "invalid" }),
|
||||
Error,
|
||||
"clean --type must be all, cache, or assets",
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user