This commit is contained in:
Flummi
2020-04-08 02:19:20 +02:00
parent c97b08a7c4
commit 46082780e1
11 changed files with 97 additions and 130 deletions

View File

@ -43,7 +43,8 @@ router.get(/^\/([0-9]+)$/, async (req, res) => {
timestamp: new Date(query.stamp * 1000).toISOString()
},
next: query.next ? query.next : null,
prev: query.prev ? query.prev : null
prev: query.prev ? query.prev : null,
title: `${query.id} - f0ck.me`
};
res.reply({ body: tpl.render("views/item", data) });
});

View File

@ -2,10 +2,6 @@ import router from "../router.mjs";
import url from "url";
import util from "util";
import sql from "../sql.mjs";
import lib from "../lib.mjs";
import tpl from "../tpl.mjs";
tpl.readdir("views");
router.get("/stats", async (req, res) => {
const query = await sql.query("select src from items");
@ -15,10 +11,7 @@ router.get("/stats", async (req, res) => {
hosts[host] ? hosts[host]++ : hosts[host] = 1;
});
const sorted = Object.keys(hosts).sort((a, b) => hosts[b] - hosts[a]).map(k => ({ [k]: hosts[k] })).reduce((a, b) => ({ ...a, ...b }));
res.reply({
body: "<pre>" + util.inspect(sorted) + "</pre>"
body: "<pre>" + util.inspect(Object.keys(hosts).sort((a, b) => hosts[b] - hosts[a]).map(k => ({ [k]: hosts[k] })).reduce((a, b) => ({ ...a, ...b }))) + "</pre>"
});
//res.reply({ body: tpl.render("views/index", data) });
});

View File

@ -4,13 +4,13 @@ import path from "path";
export default new class {
#templates = {};
#syntax = [
[ "each", (t, args = t.slice(4).trim().split(" ")) => `util.forEach(${args[0]},(${(args[1] === "as" && args[2]) ? args[2] : "value"},key)=>{` ],
[ "each", (t, _, args = t.slice(4).trim().split(" ")) => `util.forEach(${args[0]},(${(args[1] === "as" && args[2]) ? args[2] : "value"},key)=>{` ],
[ "/each", () => "});" ],
[ "if", t => `if(${t.slice(2).trim()}){` ],
[ "elseif", t => `}else if(${t.slice(6).trim()}){` ],
[ "else", () => "}else{" ],
[ "/if", () => "}" ],
[ "include", t => `html+=util.tpl["${t.slice(7).trim()}"];`],
[ "include", (t, data) => `html+='${this.render(t.slice(7).trim(), data)}';` ], //`html+=util.include("${t.slice(7).trim()}", data);`], // parse them aswell
[ "=", t => `html+=${t.slice(1).trim()};` ]
];
readdir(dir, root = dir, rel = dir.replace(`${root}/`, "")) {
@ -33,8 +33,8 @@ export default new class {
.replace(/[\n\r]/g, "")
.split(/{{\s*([^}]+)\s*}}/)
.filter(Boolean)
.map(t => !(f = this.#syntax.filter(s => t.startsWith(s[0]))[0]) ? `html+='${t}';` : f[1](t))
.map(t => !(f = this.#syntax.filter(s => t.startsWith(s[0]))[0]) ? `html+='${t}';` : f[1](t, data))
.join("") + "}return html.trim().replace(/>[\\n\\r\\s]*?</g, '><')"
).bind(null, { forEach: this.forEach, tpl: this.#templates })(data);
).bind(null, { forEach: this.forEach })(data);
}
};