新版本
This commit is contained in:
commit
8945301baf
508 files changed
+54357
No files matched your search
@@ -0,0 +1,161 @@
|
||||
// deno-lint-ignore-file
|
||||
import { readdir, readdirSync, readFileSync } from "node:fs";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
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((a, b) => a.index - b.index);
|
||||
} 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((a, b) => a.index - b.index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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") {
|
||||
[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;
|
||||
this.index = parseInt(index);
|
||||
this.colume = colume;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user