-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_pages.js
More file actions
50 lines (44 loc) · 1.73 KB
/
generate_pages.js
File metadata and controls
50 lines (44 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// this generates the html pages based on BASE.html and content in the pages folder
const fs = require("fs");
const { replace_strings } = require("./utils.js");
const BASE = fs.readFileSync("BASE.html", { encoding: "utf8" });
function recurse_pages(path) {
fs.stat(path, (err, stats) => {
if (err != null) return console.error(err);
if (stats.isDirectory())
fs.opendir(path, async (err, dir) => {
for await (const dirent of dir) {
if (dirent.isDirectory()) {
recurse_pages(path + "/" + dirent.name);
} else {
const out_path = path.replace("public/pages", "") + "/" + dirent.name;
fs.readFile(path + "/" + dirent.name, { encoding: "utf8" }, (err, data) => {
let title = /<!--(.+)-->/.exec(data);
let back_link = "index";
let path_split = out_path.split("/");
if (path_split.length > 2) {
path_split.pop();
back_link = path_split.join("/") + ".html";
}
let replace_data = {
PAGE: data,
ID: dirent.name.replace(".html", ""),
TITLE: title ? title[1] : ":3",
BACK_LINK: back_link,
};
out = replace_strings(replace_data, BASE);
console.log(out_path);
fs.writeFile("./public" + out_path, out, { encoding: "utf8", flag: "w" }, (err) => {
if (err) {
if (err.errno == -2) {
fs.mkdirSync("./public/" + path.replace("public/pages", ""));
} else console.error(err);
}
});
});
}
}
});
});
}
recurse_pages("public/pages");