-
Notifications
You must be signed in to change notification settings - Fork 1
/
schedule.mjs
97 lines (91 loc) · 2.64 KB
/
schedule.mjs
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
import fs from 'fs';
import yaml from 'js-yaml';
// This stuff is terrible and should be in a mini library, sorry.
function text(value) {
return { type: 'text', value };
}
function link(value, url) {
return { type: 'link', url, children: [text(value)] };
}
function tableCell(value, opts) {
const children = typeof value === 'string' ? [text(value)] : value;
return { type: 'tableCell', children, ...opts };
}
function span(value, style) {
const children = typeof value === 'string' ? [text(value)] : value;
return { type: 'span', children, style };
}
function tableRow(cells) {
return { type: 'tableRow', children: cells };
}
// We don't have custom css quite yet :(
const classes = {
lecture: {
background: '#4E66F6',
borderRadius: 8,
color: 'white',
padding: 5
},
participation: {
background: '#7A77B4',
borderRadius: 8,
color: 'white',
padding: 5
},
lab: {
background: '#B83BC0',
borderRadius: 8,
color: 'white',
padding: 5
},
homework: { background: '#D43B21',
borderRadius: 8,
color: 'white',
padding: 5
},
};
const scheduleDirective = {
name: 'schedule',
doc: 'Schedule directive presents a schedule based on a YAML file',
// The YAML file that contains the schedule
arg: { type: String },
options: {
// size: { type: String },
},
run(data) {
// ## Week 1
// Aug 24 [Lecture 1] PDF Document (note)
// [Exercise 1.1] PDF Document
const weeks = yaml.load(fs.readFileSync(data.arg).toString());
const children = weeks.map(({ week, days }) => {
const renderedDays = days
.map((day) => {
const rows = day.items.map(({ type, id, name, href, auxil }) =>
tableRow([
tableCell([span(`${type} ${id}`, classes[type.toLowerCase()])]),
tableCell([link(name, href)]),
auxil ? tableCell([link(auxil.id, auxil.href)]) : tableCell([]),
])
);
// Put a header on the first row that spans all of them!
rows[0].children.unshift(tableCell(day.date, { rowspan: day.items.length }));
return rows;
})
.flat(); // turns this into a flat list of children
return {
type: 'card',
identifier: `week${week}`, // Can link to this and show a preview
children: [
{
type: 'header',
children: [{ type: 'text', value: `Week ${week}` }],
},
{ type: 'table', children: renderedDays },
],
};
});
return children.flat();
},
};
const plugin = { name: 'Schedule Directive', directives: [scheduleDirective] };
export default plugin;