83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
// deno-lint-ignore-file
|
|
import { readdir, readdirSync, readFileSync } from "node:fs";
|
|
|
|
export class PFiles {
|
|
cover_html: string | undefined;
|
|
end_html: string | undefined;
|
|
pcolumes: Array<PColume> = [];
|
|
constructor(dir: string) {
|
|
//const fs = require("fs");
|
|
const files = readdirSync(dir);
|
|
for (const file of files) {
|
|
// Process each file
|
|
switch (file) {
|
|
case "cover_page.html":
|
|
this.cover_html = readFileSync(`${dir}/cover_page.html`, "utf-8");
|
|
break;
|
|
case "end_page.html":
|
|
this.end_html = readFileSync(`${dir}/end_page.html`, "utf-8");
|
|
break;
|
|
default:
|
|
const meta = new PFileMetadata(file);
|
|
const path = `${dir}/${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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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.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: string;
|
|
constructor(filename: string) {
|
|
let [c_index, colume, index, title] = filename.split("::");
|
|
title = title.replace(".md", "");
|
|
this.c_index = c_index;
|
|
this.title = title;
|
|
this.index = parseInt(index);
|
|
this.colume = colume;
|
|
}
|
|
}
|