-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.ts
79 lines (63 loc) · 1.99 KB
/
parser.ts
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
import { Day, HTMLDocument } from "./deps.ts";
export const DAYS: Day[] = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
export function parseTimetable(document: HTMLDocument) {
const customDays: Day[] = [];
const lessonDict: { [title: string]: Lesson } = {};
const table = document.querySelector(".maintable")!;
const tbody = table.firstElementChild!;
const rows = tbody.children;
for (let row_i = 0; row_i < rows.length; ++row_i) {
if (row_i === 0) {
// First row (days of the week)
for (const cell of rows[row_i].children) {
const day = cell.textContent.trim().slice(0, 2).toUpperCase() as Day;
const colSpan = cell.getAttribute("colspan") ?? "1";
let i = parseInt(colSpan);
do {
customDays.push(day);
} while (--i);
}
}
// Walk through every row
const row = rows[row_i];
// Time will be set later
let time = "";
const cells = row.children;
for (let col_i = 0; col_i < cells.length; ++col_i) {
const day = customDays[col_i]!;
// Walk through every cell
const cell = cells[col_i];
// Get start time
if (col_i === 0) {
time = cell.textContent;
continue;
}
// Handle lesson cells
if (cell.className.includes("nonemptycell")) {
const details = cell.firstElementChild!.firstElementChild!.children;
const title = details[0].textContent.trim();
const lessonRef = lessonDict[title];
if (lessonRef === undefined) {
const lesson: Lesson = {
teachers: details[1].textContent.trim(),
location: details[2].textContent.trim(),
weeks: details[3].textContent.trim(),
day,
time: [time],
};
lessonDict[title] = lesson;
} else {
lessonRef.time.push(time);
}
}
}
}
return lessonDict;
}
export type Lesson = {
teachers: string;
location: string;
weeks: string;
day: Day;
time: string[];
};