-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender-txt.js
93 lines (82 loc) · 2.85 KB
/
render-txt.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
'use strict';
let fs = require('fs');
let path = require('path');
let { root, historicalRoot, rooms, sanitizeRoomName } = require('./utils.js');
for (let { room, historical } of rooms) {
let roomDir = path.join('logs', 'docs', sanitizeRoomName(room), 'plaintext');
fs.mkdirSync(roomDir, { recursive: true });
let roomJsonDir = path.join(historical ? historicalRoot : root, room);
let days = fs
.readdirSync(roomJsonDir)
.filter(f => /^[0-9]{4}-[0-9]{2}-[0-9]{2}\.json$/.test(f))
.map(d => d.replace(/\.json$/, ''))
.sort();
if (days.length === 0) {
continue;
}
// gotta get `groupBy` added... https://github.com/tc39/proposal-array-filtering/issues/2#issuecomment-810594446
let months = new Map();
for (let day of days) {
let month = day.substring(0, 7);
if (!months.has(month)) {
months.set(month, []);
}
months.get(month).push(day);
}
fs.readdirSync(roomDir)
.filter(f => /^[0-9]{4}-[0-9]{2}\.txt$/.test(f))
.map(d => d.substring(0, 7))
.slice(0, -1) // always do at least the last month
.forEach(m => months.delete(m));
for (let [month, days] of months) {
let contents = [];
for (let day of days) {
contents.push(`${contents.length === 0 ? '' : '\n'}${day}\n`);
let events = JSON.parse(fs.readFileSync(path.join(roomJsonDir, day + '.json'), 'utf8'));
for (let event of events) {
let { msgtype } = event.content;
if (msgtype == null) {
// message was deleted
continue;
}
if (msgtype !== 'm.text' && msgtype !== 'm.emote') {
throw new Error('unknown event message type ' + msgtype);
}
let date = new Date(event.ts);
let ts = date.getHours().toString().padStart(2, '0') + ':' + date.getMinutes().toString().padStart(2, '0') + ':' + date.getSeconds().toString().padStart(2, '0') + '.' + date.getMilliseconds().toString().padStart(4, '0');
let { senderName } = event;
let shortNameMatch = senderName.match(/(.*) \(@[^\):\s]+:[^\):\s]+\.[^\):\s]+\)$/);
if (shortNameMatch != null) {
senderName = shortNameMatch[1];
}
contents.push(`[${ts}] <${senderName}>\n${msgtype === 'm.emote' ? '/me ' : ''}${event.content.body}\n\n`);
}
}
fs.writeFileSync(path.join(roomDir, month + '.txt'), contents.join(''), 'utf8');
}
fs.writeFileSync(path.join(roomDir, 'index.html'), makeIndex(room, roomDir), 'utf8');
}
function makeIndex(room, roomDir) {
let months = fs.readdirSync(roomDir)
.filter(f => /^[0-9]{4}-[0-9]{2}\.txt$/.test(f));
return `<!doctype html>
<head>
<title>Logs for ${room}</title>
<style>
body {
background-color: #fafafa;
padding: 1.5em;
}
ul {
line-height: 1.5;
}
</style>
</head>
<body>
Logs for ${room}:
<ul>
${months.map(m => `<li><a href="${m}">${m}</a></li>`).join('\n')}
</ul>
</body>
`;
}