forked from nbswords/web-design-final-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
63 lines (57 loc) · 2.09 KB
/
generate.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
const ejs = require("ejs");
const fs = require("fs");
const moment = require("moment");
const axios = require("axios");
moment.locale("zh-tw");
const renderPage = (src, dst, resources) => {
ejs.renderFile(src, resources, null, (err, html) => {
if (err) {
console.log(err);
return;
}
// console.log(html);
fs.writeFileSync(dst, html, "utf8");
});
}
// events
const events_url = "https://ccns.kktix.cc/events.json";
const renderEvents = (src, dst) => {
axios.get(events_url).then((response) => {
var events = response.data;
var events = events.entry.map((e) => {
var content = e.content;
var spl = content.split("\n");
var time_origin = spl[0].split(":")[1].split("~")[0];
var place = spl[1].split(":")[1].split("/")[0];
var time = moment(time_origin, "YYYY/MM/DD HH:mm(Z)").format(
"YYYY年M月D日 Ah點"
);
e.time = time;
e.place = place;
return e;
});
renderPage(src, dst, { events: events });
}).catch((error) => {
console.log(error)
console.log("Failed to fetch events.");
});
};
const members = require("./resources/members.json");
const projects = require("./resources/projects.json");
const timeline = require("./resources/timeline.json");
fs.mkdirSync("./public/frameworks/", { recursive: true })
const pages = [
{ src: "./ejs/events.ejs", dst: "./public/events.html", renderer: renderEvents },
{ src: "./ejs/members.ejs", dst: "./public/members.html", resources: { members: members } },
{ src: "./ejs/projects.ejs", dst: "./public/projects.html", resources: { projects: projects } },
{ src: "./ejs/intro.ejs", dst: "./public/intro.html", resources: { timeline: timeline } },
{ src: "./ejs/env.ejs", dst: "./public/env.html" },
{ src: "./ejs/food.ejs", dst: "./public/food.html" },
{ src: "./ejs/index.ejs", dst: "./public/index.html" },
{ src: "./resources/maplibre.html", dst: "./public/maplibre.html" },
];
pages.forEach((e, i) => {
if (e.renderer && e.renderer.call && e.renderer.apply)
return e.renderer(e.src, e.dst);
renderPage(e.src, e.dst, e.resources || {});
});