修改了默认的构建模式为模板与源码分离,加入文件系统区分专栏模式

This commit is contained in:
:NoBody: committed 2026-06-07 11:46:58 +08:00
1 parent dabb04773e
commit ab5ca2485f
230 files changed
+677 -20542

No files matched your search

+80 -11
View File
@@ -1,5 +1,7 @@
// deno-lint-ignore-file
import { readdir, readdirSync, readFileSync } from "node:fs";
import { cfg } from "./main.ts";
import { join, resolve } from "node:path";
export class PFiles {
cover_html: string | undefined;
@@ -7,29 +9,59 @@ export class PFiles {
style_html: string | undefined;
other_htmls: Array<string> = [];
pcolumes: Array<PColume> = [];
constructor(dir: string) {
cfg: any;
constructor(mode: "detached" | "flat", dir: string, cfg: any) {
this.cfg = cfg;
//const fs = require("fs");
const files = readdirSync(dir);
//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
switch (file) {
let file_name = file.split("/").slice(-1)[0];
switch (file_name) {
case "cover_page.html":
this.cover_html = readFileSync(`${dir}/cover_page.html`, "utf-8");
this.cover_html = readFileSync(
file,
"utf-8",
);
break;
case "end_page.html":
this.end_html = readFileSync(`${dir}/end_page.html`, "utf-8");
this.end_html = readFileSync(
file,
"utf-8",
);
break;
case "style.html":
this.style_html = readFileSync(`${dir}/style.html`, "utf-8");
this.style_html = readFileSync(file, "utf-8");
break;
default:
if (file.endsWith(".html")) {
this.other_htmls.push(readFileSync(`${dir}/${file}`, "utf-8"));
this.other_htmls.push(
readFileSync(file, "utf-8"),
);
break;
}
if ((file.endsWith(".md"))) {
const meta = new PFileMetadata(file);
const path = `${dir}/${file}`;
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)) {
@@ -52,6 +84,33 @@ export class PFiles {
}
}
}
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 {
@@ -82,8 +141,18 @@ export class PFileMetadata {
index: number;
colume: string;
c_index: number;
constructor(filename: string) {
let [c_index, colume, index, title] = filename.split("::");
constructor(filename: string, build_mode: "detached" | "flat" = "detached") {
let [c_index, colume, index, title] = ["0", "default", "0", filename];
if (build_mode == "flat") {
[c_index, colume, index, title] = filename.split("::");
} else if (build_mode == "detached") {
const current_folder_name = filename.split("/").slice(-2)[0];
[c_index, colume] = current_folder_name.split("::");
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 = parseInt(c_index);
this.title = title;