-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmake-schedule.js
More file actions
198 lines (166 loc) · 6.42 KB
/
make-schedule.js
File metadata and controls
198 lines (166 loc) · 6.42 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
const fs = require('fs'), process = require('process');
const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
/* TODO’s:
* Type (talk, workshop, break, etc.)
* Day change furigana (!!)
* Time zone change!!
* Title cards ...
* Prettify presentations JSON before writing it out
* Dichotomy
* Equivalent of break with forEach?
* Timer for current
* Write to presentations.dat after reformatting!
* Rename to whats-on.html */
const parseDatabase = (datafile) => {
const database = fs.readFileSync(datafile, 'utf8');
let data = [{ }];
database.split("\n").forEach((line) => {
if (line === '') {
data.unshift({ });
return;
}
const chunks = line.split(':');
const key = chunks.shift();
const value = chunks.join(':').trim();
if (key === 'author' || key === 'slides' || key === 'preprint' || key === 'sources' || key === 'links' || key === 'videos') data[0][key] = value.split(',');
else data[0][key] = value;
});
return data.filter((entry) => {
return entry.title;
}).sort((a, b) => Date.parse(b.time) - Date.parse(a.time));
};
const formatDuration = ((duration) => {
if (duration < HOUR) {
return `${duration / MINUTE} minutes`;
};
const hours = Math.floor(duration / HOUR);
const remainder = duration - hours * HOUR;
return `${hours} hour${hours === 1 ? '' : 's'} ${remainder ? formatDuration(remainder) : ''}`
});
const formatData = (data) => {
let dataFormatted = '';
let nextTalkTime;
data.forEach((pres) => {
if (!pres.time) return;
const thisTalkTime = Date.parse(pres.time);
if (pres.pres === 'break') {
klass = 'accordion-break';
const duration = nextTalkTime - thisTalkTime;
pres.title = `Break, ${formatDuration(duration)}`.trim();
} else {
klass = 'accordion';
}
dataFormatted = `</h2>
<div class="panel">
<p>
${pres.abstract}
</p>
</div>\n\n` + dataFormatted;
if (pres.videos) {
pres.videos.forEach((videosFile) => {
dataFormatted = `<a class="presLink" href="${videosFile}"><img class="presLink" src="assets/img/video.png" alt="Video" /></a>\n` + dataFormatted;
});
}
if (pres.links) {
pres.links.forEach((linksFile) => {
dataFormatted = `<a class="presLink" target="_blank" href="${linksFile}"><img class="presLink" src="assets/img/external-link-symbol-24.png" alt="Link" /></a>\n` + dataFormatted;
});
}
// Copy input files (sources, slides, preprint) and add links to them in the HTML
if (pres.pres != 'break') {
copyInputFiles(pres, 'sources').forEach((path) => {
dataFormatted = `<a href="${path}"><img src="assets/img/tgz.png" height="24" alt="Sources" /></a>\n` + dataFormatted;
});
copyInputFiles(pres, 'slides').forEach((path) => {
dataFormatted = `<a class="presLink" href="${path}"><img class="presLink" src="assets/img/Slides.png" alt="Slides" /></a>\n` + dataFormatted;
});
copyInputFiles(pres, 'preprint').forEach((path) => {
dataFormatted = `<a class="pdfLink" href="${path}"><img src="assets/img/PDF_24.png" alt="Preprint"/></a>` + dataFormatted;
});
}
dataFormatted = `<font class="serif">${pres.title}</font>\n` + dataFormatted;
// if (pres.author) dataFormatted = `| ${pres.author} ` + dataFormatted;
if (pres.author) {
const authorsLine = new Intl.ListFormat('en-GB', { style: 'short', type: 'conjunction' }).format(pres.author);
dataFormatted = `| ${authorsLine} ` + dataFormatted;
}
dataFormatted = `<h2 class="${klass}" id="${pres.time}">
<span class="timeboxL" data-datetime="${pres.time}"></span>\n` + dataFormatted;
nextTalkTime = thisTalkTime;
});
//console.log(data);
fs.writeFileSync('presentations.js', `const presentations = ${JSON.stringify(data, null, 2)}\n`, 'utf8');
return dataFormatted;
};
/**
* Given a file name and the presentation record, get the long name for this file.
* The returned string represents only the file name itself (not the path).
*
* @param {Object} pres
* @param {String} fileName
* @returns the long file name
*/
const getLongFileName = (pres, fileName) => {
const authors = pres.author;
let normalizedAuthorsList = [];
pres.author.forEach((author) => {
normalizedAuthorsList.push(normalizeAuthorName(author));
});
const normalizedAuthorsString = normalizedAuthorsList.join("-");
return `${normalizedAuthorsString}-TUG2022-${pres.pres}-${fileName}`;
}
const normalizeAuthorName = (author) => {
return author.replaceAll(" ", "_");
}
/**
* Copy all files from the given 'assetType' directory into the 'served' folder.
* The files are named based on their authors.
*
* @param {*} pres the presentation record
* @param {*} assetType the asset type (sources / slides / preprint), which corresponds to a sub-folder under the 'tag' folder
* @returns list of the copied files (paths of their new file names)
*/
const copyInputFiles = (pres, assetType) => {
const incomingTagDir = `assets/incoming/${pres.pres}`
const incomingAssetTypeDir = `${incomingTagDir}/${assetType}`;
// Create the incoming asset-type folder, if it doesn't exist
createDirIfNotExists(incomingTagDir);
createDirIfNotExists(incomingAssetTypeDir);
let destPaths = [];
fs.readdirSync(incomingAssetTypeDir).forEach(file => {
const destFileName = getLongFileName(pres, file);
const destPath = `assets/served/${destFileName}`;
// Copy the file to the 'served' folder
fs.copyFile(`${incomingAssetTypeDir}/${file}`, destPath, (err) => {
if (err) {
console.log("Error copying file:", err);
}
});
destPaths.push(destPath);
});
return destPaths;
}
const createDirIfNotExists = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
const writeout = (environment) => {
createDirIfNotExists("assets/served");
const inSchedule = fs.readFileSync('program.html.in', 'utf8');
const outSchedule = inSchedule.replace(
'<list-of-presentations />',
formatData(parseDatabase('pres.dat')));
fs.writeFileSync('program.html', outSchedule, 'utf8');
}
/*
const data = parseDatabase('pres.dat');
for (let i in data) { console.log('i =', i, ', data[i] =', data[i]); } // “let in data” valid??
for (let j in data) { // but “let i data” invalid :-P
const i = parseInt(j);
console.log('i = ', i, ', data[i] =', data[i], ', data[i+1] =', data[i+1]);
}
*/
writeout();