-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·195 lines (173 loc) · 7 KB
/
index.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
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
#!/usr/bin/env node
import process from 'node:process';
import path from 'path';
import { listFiles, createDirectoryIfNotExists, writeFile, readYamlFile, findFile } from './src/util.js';
import { download, checkDependencies } from './src/youtube-dl.js';
import ejs from 'ejs';
import yargs from "yargs";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
async function main() {
const { yamlDir, templatesPath, outPath } = parseArgs();
const { config, slides, worksheetCategories } = await parseYamlFiles(yamlDir);
const processedSlides = await downloadYoutubeMedia(slides, yamlDir);
await createSlides(processedSlides, worksheetCategories, config, templatesPath, outPath);
}
function parseArgs() {
const argv = yargs(process.argv.slice(2))
.usage('Usage: $0 -d [directory]')
.alias('d', 'directory')
.nargs('d', 1)
.describe('d', 'Path to the question directory')
.demandOption(['d'])
.alias('o', 'output')
.default('o', path.resolve(__dirname + '/output/'))
.nargs('o', 1)
.describe('o', 'Path to output directory, where the HTML is written to')
.alias('t', 'templates')
.default('t', path.resolve(__dirname + '/templates/'))
.nargs('t', 1)
.describe('t', 'Path to template directory')
.argv;
return {
yamlDir: path.resolve(argv.directory),
templatesPath: path.resolve(argv.templates),
outPath: path.resolve(argv.output)
};
}
async function parseYamlFiles(path) {
const filesInPath = await listFiles(path);
const yamlFiles = filesInPath.filter(f => f.match(/^.+\.yaml$/));
// remove and handle config.yaml
const configIndex = yamlFiles.indexOf('config.yaml');
let config = null;
if (configIndex !== -1) {
const configFile = yamlFiles.splice(configIndex, 1);
config = await readYamlFile(path + '/' + configFile);
config = config || {};
config.title = config.title || 'Pub Quiz';
config.revealjs_path = config.revealjs_path || '../node_modules/reveal.js/';
config.revealjs_theme = config.revealjs_theme || 'black';
}
const categories = await Promise.all(
yamlFiles.map(file => readYamlFile(path + '/' + file))
);
const slides = [];
const worksheetCategories = [];
let categoryCount = 0;
for(const category of categories) {
let hasQuestions = false;
if (Object.prototype.hasOwnProperty.call(category, 'questions') && category.questions.length > 0) {
categoryCount++;
hasQuestions = true;
}
// add category title page first
if (Object.prototype.hasOwnProperty.call(category, 'title_pages')) {
// in case of multiple title pages
slides.push(...category.title_pages.map(page => {
page.type = 'title';
if (hasQuestions) {
page.category_number = categoryCount;
}
return page;
}));
} else {
// single title page
slides.push({
type: 'title',
category_number: hasQuestions ? categoryCount: undefined,
...category.title_page
});
}
if (hasQuestions) {
// add numbering to questions
category.questions.forEach((question, index) => {
question.category_number = categoryCount;
question.question_number = index + 1;
});
// add questions
slides.push(...category.questions);
const solutions = category.questions.map(question => question.answer);
worksheetCategories.push(solutions);
}
}
return {
config,
slides,
worksheetCategories
};
}
async function createSlides(slides, worksheetCategories, config, templatesPath, outPath){
await createDirectoryIfNotExists(outPath);
const quiz = await render(templatesPath + '/index.ejs', { questions: slides, isAnswer: false, config }, {});
const answers = await render(templatesPath + '/index.ejs', { questions: slides, isAnswer: true, config }, {});
const worksheet = await render(templatesPath + '/worksheet.ejs', { worksheetCategories: worksheetCategories, isAnswer: false, config }, {});
const solutionsheet = await render(templatesPath + '/worksheet.ejs', { worksheetCategories: worksheetCategories, isAnswer: true, config }, {});
await writeFile(quiz, outPath + '/index.html');
await writeFile(answers, outPath + '/answers.html');
await writeFile(worksheet, outPath + '/worksheet.html');
await writeFile(solutionsheet, outPath + '/solutionsheet.html');
}
async function downloadYoutubeMedia(slides, yamlDir) {
let dependenciesInstalled = false;
for(let index = 0; index < slides.length; index++) {
const question = slides[index];
// skip if non-youtube
if (question.type !== 'youtube-dl') continue;
// check dependencies on first run
if(!dependenciesInstalled) {
dependenciesInstalled = await checkDependencies()
}
const fileDirectory = path.resolve(yamlDir + '/' +question.outputPath);
// check if already downloaded
let fileName = await findFile(fileDirectory, question.fileBaseName);
if (fileName === null) {
const properties = {
outputPath: fileDirectory,
fileBaseName: question.fileBaseName,
};
if (question.start) {
properties.start = question.start;
}
if (question.end) {
properties.end = question.end;
}
properties.hasVideo = question.hasVideo ?? true;
properties.hasAudio = question.hasAudio ?? true;
await download(question.url, properties);
fileName = await findFile(fileDirectory, question.fileBaseName);
}
const transformed = Object.assign({}, question);
// transform question
const fileExtension = String(fileName).split('.').pop();
const filePath = transformed.outputPath + '/' + transformed.fileBaseName + '.' + fileExtension;
if (transformed.hasVideo) {
transformed.type = 'video';
transformed.video = filePath;
transformed.video_type = 'video/' + fileExtension;
} else {
transformed.type = 'audio';
transformed.audio = filePath;
transformed.audio_type = 'audio/' + fileExtension;
}
slides[index] = transformed;
}
return slides;
}
async function render(template, data, options = {}) {
return new Promise(((resolve, reject) => {
ejs.renderFile(template, data, options, (err, str) => {
if (err) {
reject(err);
} else {
resolve(str);
}
})
}));
}
(async function(){
await main();
process.exit(0);
})();