2023-07-13 06:11:06 +00:00
|
|
|
import nunjucks from "nunjucks"
|
|
|
|
import toml from "toml";
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import { program } from "commander";
|
|
|
|
import { v4 as uuidv4 } from "uuid";
|
|
|
|
import express from "express";
|
|
|
|
|
|
|
|
const url_path = "/";
|
|
|
|
const config_path = "config.toml";
|
2023-07-14 07:56:29 +00:00
|
|
|
const template_dir = "template/";
|
2023-07-13 06:11:06 +00:00
|
|
|
const template_path = "index.html.njk";
|
|
|
|
const output_path = "dist/index.html";
|
|
|
|
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
program
|
|
|
|
.option("--serve", "serve the gerneated files")
|
|
|
|
.option("--generate", "generate prod ready files")
|
|
|
|
program.parse();
|
|
|
|
const options = program.opts();
|
|
|
|
|
|
|
|
if (options.serve && options.generate
|
|
|
|
|| ! (options.serve || options.generate)) {
|
|
|
|
program.help();
|
|
|
|
}
|
|
|
|
|
2023-07-14 07:56:29 +00:00
|
|
|
let config = fs.readFileSync(config_path, {encoding: "utf-8"});
|
2023-07-13 06:11:06 +00:00
|
|
|
config = toml.parse(config)
|
|
|
|
|
2023-07-14 07:56:29 +00:00
|
|
|
for (let i = 0; i < config["section"].length; i++) {
|
|
|
|
for (let j = 0; j < config["section"][i]["items"].length; j++) {
|
|
|
|
let item = config["section"][i]["items"][j];
|
|
|
|
if (item.hide_permalink) {
|
|
|
|
let seed = Math.floor(Math.random() * 1000000007)
|
|
|
|
item.permalink = hide(seed, item.permalink);
|
|
|
|
item.seed = seed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let nunjucks_env = nunjucks.configure(template_dir, {
|
2023-07-13 06:11:06 +00:00
|
|
|
autoescape: true,
|
|
|
|
throwOnUndefined: true,
|
|
|
|
trimBlocks: true,
|
|
|
|
lstripBlocks: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.serve) {
|
2023-07-14 07:56:29 +00:00
|
|
|
serve(config, nunjucks_env);
|
2023-07-13 06:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.generate) {
|
2023-07-14 07:56:29 +00:00
|
|
|
generate(config, nunjucks_env);
|
2023-07-13 06:11:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-14 07:56:29 +00:00
|
|
|
function serve(config, nunjucks_env) {
|
2023-07-13 06:11:06 +00:00
|
|
|
config.dev = true;
|
2023-07-14 07:56:29 +00:00
|
|
|
const rendered = nunjucks_env.render(template_path, config);
|
2023-07-13 06:11:06 +00:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
const port = 3000;
|
|
|
|
|
|
|
|
const uuid = uuidv4();
|
|
|
|
|
|
|
|
app.get("/uuid", (req, res) => {
|
|
|
|
res.set('Cache-Control', 'no-store')
|
|
|
|
res.send(uuid);
|
|
|
|
});
|
|
|
|
app.get("/", (req, res) => {
|
|
|
|
res.set('Cache-Control', 'no-store')
|
|
|
|
res.send(rendered)
|
|
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`serveing webpage at ${port}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-14 07:56:29 +00:00
|
|
|
function generate(config, nunjucks_env) {
|
|
|
|
const rendered = nunjucks_env.render(template_path, config);
|
2023-07-13 06:11:06 +00:00
|
|
|
if (! fs.existsSync(path.dirname(output_path))) {
|
|
|
|
fs.mkdirSync(fs.dirname(output_path), { recursive: true });
|
|
|
|
}
|
|
|
|
fs.writeFileSync(output_path, rendered);
|
|
|
|
}
|
|
|
|
|
2023-07-14 07:56:29 +00:00
|
|
|
// A simple function to encrypt string
|
|
|
|
// to avoid being collected by robots.
|
|
|
|
function hide(seed, str) {
|
|
|
|
const str_arr = new TextEncoder().encode(str);
|
|
|
|
const res_arr = [];
|
|
|
|
seed %= 1000000007;
|
|
|
|
|
|
|
|
for (let i = 0; i < str_arr.length; i++) {
|
|
|
|
res_arr.push((str_arr[i] + seed) % 256);
|
|
|
|
seed = seed * seed % 1000000007;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return res_arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unhide(seed, arr) {
|
|
|
|
seed %= 1000000007;
|
|
|
|
const str_arr = [];
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
str_arr.push((((arr[i] - seed) % 256) + 256) % 256)
|
|
|
|
seed = seed * seed % 1000000007;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ui8_arr = new Uint8Array(str_arr);
|
|
|
|
const str = new TextDecoder().decode(ui8_arr);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2023-07-13 06:11:06 +00:00
|
|
|
main();
|