// 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 = []; pcolumes: Array = []; 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 { 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 { return readdirSync(dir).map((f) => join(dir, f)); } } export class PColume { name: string; index: number; pfiles: Array = []; constructor(name: string, pfiles: Array) { 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; } }