-
-
Notifications
You must be signed in to change notification settings - Fork 502
Expand file tree
/
Copy pathmdParser.js
More file actions
258 lines (233 loc) · 8.52 KB
/
mdParser.js
File metadata and controls
258 lines (233 loc) · 8.52 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const fs = require("fs");
const ROOT = "../";
const MAIN_INPUT = ROOT + "README.md";
const TAGS_INPUT = ROOT + "TAGS.csv";
const MAIN_OUTPUT = ROOT + "page/src/misc/all-events.json";
const CFP_OUTPUT = ROOT + "page/src/misc/all-cfps.json";
const MONTHS_NAMES =
"january,february,march,april,may,june,july,august,september,october,november,december".split(
","
);
const MONTHS_SHORTNAMES = MONTHS_NAMES.map((m) => m.slice(0, 3));
const getTimeStamp = (year,month,day) => new Date(Date.UTC(year,month,day,0,0,0)).getTime()
const formatDateHumanReadable = (timestamp) => {
const date = new Date(timestamp);
const year = date.getUTCFullYear();
const month = MONTHS_NAMES[date.getUTCMonth()];
const day = date.getUTCDate();
return `${month.charAt(0).toUpperCase() + month.slice(1)}-${day}-${year}`;
}
const parseTags = () => {
try {
const tagsContent = fs.readFileSync(TAGS_INPUT, 'utf8');
const lines = tagsContent.split('\n').filter(line => line.trim() !== '');
const tagsMap = new Map();
for (let i = 1; i < lines.length; i++) { // Skip header row
const line = lines[i].trim();
if (!line) continue;
const [eventId, ...tagsParts] = line.split(',');
const tagsString = tagsParts.join(',');
const tags = tagsString.split(',')
.map(tag => tag.trim())
.filter(tag => tag !== '' && tag.includes(':'))
.map(tag => {
const [key, value] = tag.split(':');
return { key: key.trim(), value: value.trim() };
});
tagsMap.set(eventId, tags);
}
return tagsMap;
} catch (error) {
console.warn('TAGS.csv not found or invalid, continuing without tags');
return new Map();
}
}
const generateEventId = (conf) => {
// Generate ISO date from the first date in the date array
const firstDate = new Date(conf.date[0]);
const isoDate = firstDate.toISOString().split('T')[0];
return `${isoDate}-${conf.name}`;
}
const extractArchiveFiles = (
markdown //eg: " * [2017](archives/2017.md)"
) =>
[...markdown.matchAll(/^\s*\*\s*\[.*\]\(archives\/.*\.md\)\s*$/gm)]
.map((match) => match[0])
.map(
(archiveLine) =>
ROOT + archiveLine.trim().replaceAll(/^.*(archives\/.*\.md).*$/g, "$1")
);
const extractConfs = (markdown) =>
extractYearBlocks(markdown).flatMap((y) =>
extractMonthBlocks(y.markdown).flatMap((m) =>
extractEvents(m.markdown, y.year, m.month)
)
);
const extractYearBlocks = (markdown) => {
const years = [...markdown.matchAll(/^## \d+$/gm)].map((m) => ({
start: m.index,
year: m[0].replaceAll(/^\D*(\d+)\D*$/g, "$1"),
}));
if (!years) return;
for (let index = 0; index < years.length - 1; index++) {
const year = years[index];
year.markdown = markdown.slice(year.start, years[index + 1].start);
}
const lastYear = years[years.length - 1];
lastYear.markdown = markdown.slice(lastYear.start);
return years;
};
const extractMonthBlocks = (yearMarkdown) => {
const months = [...yearMarkdown.matchAll(/^### \w+$/gm)]
.map((m) => ({
start: m.index,
month_en: m[0].replaceAll(/^\W*(\w+)\W*$/g, "$1"),
}))
.map((month) => ({
...month,
month: MONTHS_NAMES.indexOf(month.month_en.toLowerCase()),
}));
if (!months) return;
for (let index = 0; index < months.length - 1; index++) {
const month = months[index];
month.markdown = yearMarkdown.slice(month.start, months[index + 1].start);
}
const lastMonth = months[months.length - 1];
lastMonth.markdown = yearMarkdown.slice(lastMonth.start);
return months;
};
const extractEvents = (monthMarkdown, year, month) => {
// '* 31-03/02: [SnowCamp](https://snowcamp.io/fr/) - Grenoble (France)\n'
const eventLines = monthMarkdown
.match(/^\s*\*\s*(\[[^\]]*\])?\s*[0-9\/-]+:?.*$/gm)
if (!eventLines) return [];
return eventLines.map((eventLine) => {
const links = eventLine.match(/<a[^>]*>.*?<\/a>/g) || [];
const sponsoringLink = links.find(link => link.includes('alt="Sponsoring"')) || "";
const sponsoringUrl = sponsoringLink.match(/href="([^"]+)"/)?.[1];
const cfpLink = links.find(link => link.includes('alt="CFP"')) || "";
const miscContent = eventLine.includes("</a>")
? eventLine.trim().replaceAll(/^.*?(<a.*a>.*)$/g, "$1")
: "";
const misc = miscContent.replace(sponsoringLink, "").replace(cfpLink, "").trim();
const dateArray = getTimeSpan(
year,
month,
eventLine.trim().replaceAll(/^\s*\*\s*([0-9\/-]*).*$/g, "$1")
);
const event = {
name: eventLine.trim().replaceAll(/^.*[?0-9\/\-]+.*\[(.*)\].*$/g, "$1"),
date: dateArray,
humanReadableDate: dateArray.length === 1
? formatDateHumanReadable(dateArray[0])
: `${formatDateHumanReadable(dateArray[0])} - ${formatDateHumanReadable(dateArray[1])}`,
hyperlink: eventLine.trim().replaceAll(/^.*\]\(([^)]*)\).*$/g, "$1"),
location: eventLine
.trim()
.replaceAll(/^[^\]]*[^)]*[\P{Letter}]*([^<]*).*$/ug, "$1")
.trim(),
city: eventLine
.trim()
.replaceAll(/^[^\]]*[^)]*[\P{Letter}]*([^<]*).*$/ug, "$1")
.trim()
.replaceAll(/ \& Online/g, "")
.replaceAll(/^([^(]*)\(.*$/g, "$1")
.trim(),
country: eventLine
.trim()
.replaceAll(/^[^\]]*[^)]*[\P{Letter}]*([^<]*).*$/ug, "$1")
.trim()
.replaceAll(/ \& Online/g, "")
.replaceAll(/^[^(]*\(([^)]*)\)$/g, "$1")
.trim(),
misc: misc,
cfp: extractCfp(misc),
sponsoring: sponsoringUrl,
closedCaptions: eventLine.trim().match(/^.*(<img alt=.Closed Captions.).*$/) !== null,
scholarship: eventLine.trim().match(/^.*(<img alt=.Scholarship.).*$/) !== null,
sponsoringBadge: eventLine.trim().match(/^.*(<img alt=.Sponsoring.).*$/) !== null,
status: eventLine.trim().startsWith("* [")
? eventLine.trim().replaceAll(/^[^[]*\[([\w\s]*)\].*$/g, "$1")
: "open",
};
return event;
});
}
const getTimeSpan = (year, month, datespan) => {
const [startDay, endDay] = datespan.split("-").map((d) => d.trim());
if (!endDay) {
return [ getTimeStamp(year,month,+startDay) ]
}
if (endDay.includes("/")) {
//event ends next month "31-02/04"
return [ getTimeStamp(year,month,+startDay), getTimeStamp(year,month+1,+endDay.split('/')[0])]
}
if (+startDay > +endDay) {
//event ends next month "31-02"
return [ getTimeStamp(year,month,+startDay), getTimeStamp(year,month+1,+endDay)]
}
return [ getTimeStamp(year,month,+startDay), getTimeStamp(year,month,+endDay,0,0,0)]
};
const extractCfp = (shieldCode) => {
if (!shieldCode.includes("shields.io")) return {};
const label = shieldCode.replaceAll(/^.*label=([^&]*)&.*$/g, "$1");
if (!label.match(/cfp/i)) return {};
const message = decodeURI(
shieldCode.replaceAll(/^.*&message=([^&]*)&.*$/g, "$1")
);
const untilStr = decodeURI(
shieldCode.replaceAll(/^.*&message=([^&]*)&.*$/g, "$1")
)
.replace(/^.*(until|to)\s/gi, "")
.trim();
const year = untilStr.replaceAll(/^.*(\d{4})$/g, "$1");
const monthStr = untilStr.replaceAll(/[^a-zA-Z]/g, "");
const month = MONTHS_SHORTNAMES.indexOf(monthStr.slice(0, 3).toLowerCase());
const day = untilStr.replaceAll(/^\D*(\d{1,2}).*$/g, "$1");
const untilDate = getTimeStamp(year, month, day)
return {
link: shieldCode.includes("href=")
? shieldCode.replaceAll(/^.*href="([^"]*)".*$/g, "$1")
: "",
until: untilStr,
untilDate: untilDate,
};
};
//main file parsing
const mainContent = fs.readFileSync(MAIN_INPUT).toString();
const currentConfs = extractConfs(mainContent);
//archives parsing
const archives = extractArchiveFiles(mainContent);
const archiveConfs = archives.flatMap((archive) =>
extractConfs(fs.readFileSync(archive).toString())
);
//tags parsing
const tagsMap = parseTags();
//aggregation and tags integration
const allConfs = archiveConfs.concat(currentConfs).map((conf) => {
const eventId = generateEventId(conf);
const tags = tagsMap.get(eventId) || [];
return {
...conf,
tags: tags
};
});
try {
fs.writeFileSync(MAIN_OUTPUT, JSON.stringify(allConfs));
} catch (error) {
console.error("Error writing to file:", error);
}
const allCFPs = allConfs
.filter((conf) => conf.cfp.untilDate)
.map((conf) => ({
...conf.cfp,
conf: {
name: conf.name,
date: conf.date,
hyperlink: conf.hyperlink,
status: conf.status,
location: conf.location,
},
}))
.sort((a, b) => a.untilDate - b.untilDate);
fs.writeFileSync(CFP_OUTPUT, JSON.stringify(allCFPs));