-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
110 lines (94 loc) · 2.45 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var fs = require("fs");
var process = require("process");
var MarkdownIt = require("markdown-it");
var emoji = require("markdown-it-emoji");
markdown = new MarkdownIt();
markdown.use(emoji);
function msg2html(msg) {
return `
<article class="media">
<div class="media-content">
<div class="content">
<p>
<strong>${msg.username}</strong> <small>${msg.time}</small>
<br>
${msg.text.join("\n")}
</p>
</div>
</div>
</article>`;
}
function day2html(day, messages) {
const all_messages = messages.map(msg2html).join("\n");
return `<h3 class="title is-3">${day}</h3>
<div>
${all_messages}
</div>`;
}
function json2html(filename) {
var file = fs.readFileSync(filename, "utf8");
var obj = JSON.parse(file);
var messages_by_day = {};
var last_user = null;
var last_day = null;
for (var msg of obj.messages.sort((a, b) => a.date.localeCompare(b.date))) {
var [day, time] = msg.date.split(" ");
var mbd = messages_by_day[day];
if (mbd === undefined) {
mbd = messages_by_day[day] = [];
}
var rendered = markdown.render(msg.text);
if (last_user !== msg.username || last_day !== day) {
mbd.push({
time: time,
text: [rendered],
username: msg.username
});
} else {
mbd[mbd.length - 1].text.push(rendered);
}
last_user = msg.username;
last_day = day;
}
var dayChunks = [];
for (const [day, messages] of Object.entries(messages_by_day).sort()) {
dayChunks.push(day2html(day, messages));
}
return `
<section class="section">
<div class="container">
${dayChunks.join("\n")}
</div>
</section>
`;
}
function writeTemplate(filename, content) {
// console.log(filename);
const wrappedBody = `
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/bulma.min.css">
</head>
<body>
${content}
</body>
</html>`;
fs.writeFileSync(filename, wrappedBody);
}
function copyStatic() {
fs.mkdirSync("output/static/", { recursive: true });
for (var fn of fs.readdirSync("static")) {
fs.copyFileSync(`static/${fn}`, `output/static/${fn}`);
}
}
function main() {
copyStatic();
const dirname = process.argv[2];
for (var channel of fs.readdirSync(`${dirname}/`)) {
var html = json2html(`${dirname}/${channel}`);
writeTemplate(`output/${channel.replace("json", "html")}`, html);
}
}
main();