This commit is contained in:
Nobody committed 2026-11-12 18:34:33 +08:00
1 parent 5977b1b272
commit 09af3bd8c0
18 files changed
+57 -30

No files matched your search

+204
View File
@@ -0,0 +1,204 @@
// deno-lint-ignore-file
import { readdir, readdirSync, readFileSync } from "node:fs";
import { join, resolve } from "node:path";
const columnNameCollator = new Intl.Collator("zh-CN");
function parseNumericPrefix(value: string | undefined): number | undefined {
if (value === undefined || value.trim() === "") return undefined;
const number = Number(value);
return Number.isNaN(number) ? undefined : number;
}
export function comparePColumes(a: PColume, b: PColume): number {
const aHasExplicitOrder = !Number.isNaN(a.index);
const bHasExplicitOrder = !Number.isNaN(b.index);
if (aHasExplicitOrder && bHasExplicitOrder && a.index !== b.index) {
return a.index < b.index ? -1 : 1;
}
if (aHasExplicitOrder !== bHasExplicitOrder) {
return aHasExplicitOrder ? -1 : 1;
}
const aFirstCharacter = [...a.name][0] ?? "";
const bFirstCharacter = [...b.name][0] ?? "";
return columnNameCollator.compare(aFirstCharacter, bFirstCharacter) ||
columnNameCollator.compare(a.name, b.name);
}
export class PFiles {
cover_html: string | undefined;
end_html: string | undefined;
style_html: string | undefined;
other_htmls: Array<string> = [];
pcolumes: Array<PColume> = [];
cfg: any;
constructor(mode: "detached" | "flat", dir: string, cfg: any) {
this.cfg = cfg;
//const fs = require("fs");
//const files = readdirSync(dir);
let files = null;
switch (mode) {
case "detached":
files = this.load_file_detached(dir, cfg.journal.template_directory);
this.read_files(files, dir);
break;
case "flat":
files = this.load_file_flat(dir);
this.read_files(files, dir);
break;
default:
throw new Error(`Unknown build mode: ${cfg.journal.build_mode}`);
}
}
private read_files(files: string[], dir: string) {
// console.debug(`Reading files from directory: ${dir}`);
// console.debug(`Files found: ${files.join(", ")}`);
for (const file of files) {
// Process each file
let file_name = file.split("/").slice(-1)[0];
switch (file_name) {
case "cover_page.html":
this.cover_html = readFileSync(
file,
"utf-8",
);
break;
case "end_page.html":
this.end_html = readFileSync(
file,
"utf-8",
);
break;
case "style.html":
this.style_html = readFileSync(file, "utf-8");
break;
default:
if (file.endsWith(".html")) {
this.other_htmls.push(
readFileSync(file, "utf-8"),
);
break;
}
if ((file.endsWith(".md"))) {
const meta = new PFileMetadata(file, this.cfg.journal.build_mode);
const path = file;
const content = readFileSync(path, "utf-8");
const pfile = new PFile(file, meta, content);
if (this.pcolumes.some((v) => v.name === meta.colume)) {
const volume = this.pcolumes.find((v) => v.name === meta.colume);
volume?.pfiles.push(pfile);
//保证每个专栏内的文件按照index排序,每个专栏之间也按照index排序
volume?.pfiles.sort((a, b) => a.meta.index - b.meta.index);
this.pcolumes.sort(comparePColumes);
} else {
this.pcolumes.push(new PColume(meta.colume, [pfile]));
//保证每个专栏内的文件按照index排序,每个专栏之间也按照index排序
this.pcolumes[this.pcolumes.length - 1].pfiles.sort((a, b) =>
a.meta.index - b.meta.index
);
this.pcolumes.sort(comparePColumes);
}
}
}
}
}
load_file_detached(passage_dir: string, template_dir: string): Array<string> {
const passage_files: string[] = [];
const columns = readdirSync(passage_dir);
for (const column of columns) {
const columnPath = join(passage_dir, column);
try {
if (Deno.statSync(columnPath).isDirectory) {
const files = readdirSync(columnPath);
for (const file of files) {
passage_files.push(join(columnPath, file));
}
}
} catch (_e) {
// Skip files at the top level of passage_dir if they are not directories
// or if they are but can't be read.
}
}
const template_files = readdirSync(template_dir).map((f) =>
join(template_dir, f)
);
return [...passage_files, ...template_files];
}
load_file_flat(dir: string): Array<string> {
return readdirSync(dir).map((f) => join(dir, f));
}
}
export class PColume {
name: string;
index: number;
pfiles: Array<PFile> = [];
constructor(name: string, pfiles: Array<PFile>) {
this.name = name;
this.pfiles = pfiles;
this.index = pfiles[0].meta.c_index;
}
}
export class PFile {
full_name: string;
meta: PFileMetadata;
content: string;
constructor(full_name: string, meta: PFileMetadata, content: string) {
this.full_name = full_name;
this.meta = meta;
this.content = content;
}
}
export class PFileMetadata {
title: string;
index: number;
colume: string;
c_index: number;
constructor(filename: string, build_mode: "detached" | "flat" = "detached") {
let [c_index, colume, index, title] = ["0", "default", "0", filename];
if (build_mode == "flat") {
const parts = filename.split("::");
const numericPrefix = parseNumericPrefix(parts[0]);
if (numericPrefix !== undefined && parts.length >= 2) {
[c_index, colume, index, title] = parts;
c_index = String(numericPrefix);
} else {
[colume, index, title] = [
parts[0] ?? filename,
parts[1] ?? "0",
parts[2] ?? filename,
];
c_index = "";
}
} else if (build_mode == "detached") {
const current_folder_name = filename.split("/").slice(-2)[0];
const folderParts = current_folder_name.split("::");
const numericPrefix = parseNumericPrefix(folderParts[0]);
if (numericPrefix !== undefined && folderParts.length >= 2) {
c_index = String(numericPrefix);
colume = folderParts.slice(1).join("::");
} else {
c_index = "";
colume = current_folder_name;
}
index = filename.split("/").slice(-1)[0].split("::")[0] ?? "0";
title = filename.split("/").slice(-1)[0].split("::")[1] ?? filename;
} else {
throw new Error(`Unknown build mode: ${build_mode}`);
}
title = title.replace(".md", "");
this.c_index = c_index === "" ? Number.NaN : Number(c_index);
this.title = title;
this.index = parseInt(index);
this.colume = colume;
}
}
+86
View File
@@ -0,0 +1,86 @@
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",
]);
});