-
Notifications
You must be signed in to change notification settings - Fork 2
/
quizcard_webserver.js
211 lines (194 loc) · 6.27 KB
/
quizcard_webserver.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const { AnkiNote } = require('./quizcard-generator/anki/anki_generator')
const { QuizCardGenerator, opt } = require('./quizcard-generator/quizcard_generator')
const { Express } = require('express')
const fs = require('fs/promises')
const md2html = require('./md2html')
const path = require('path')
const DIR = 'quizcard-generator'
const MAIN_PAGE = 'quizcard_generator.html'
const ANKI_DIR = `out/webserver/anki`
/**
*
* @param {Express} server Existing server to which quizcard is added.
* @param {string} public_dir
*/
function main(server, public_dir) {
const DELETE_EXPORTS = process.env.DELETE_EXPORTS || true
const EXPORT_DELETE_DELAY_MIN = process.env.EXPORT_DELETE_DELAY_MIN || 1
// compile quizgen markdown pages
md2html.compile(
path.join(DIR, 'readme.md'),
path.join(public_dir, 'out/webserver', DIR, 'readme.html')
)
// compile translated markdown pages
for (let locale_path of [
'spa/readme.md'
]) {
fs.mkdir(
path.join(public_dir, 'out/webserver', DIR, 'translations', path.dirname(locale_path)),
{ recursive: true }
)
.then(() => {
md2html.compile(
path.join(DIR, 'i18n', locale_path),
path.join(
public_dir, 'out/webserver', DIR, 'translations',
path.dirname(locale_path),
path.basename(locale_path, '.md') + '.html'
)
)
})
}
// main page
server.get(`/${DIR}`, function(_req, res) {
console.log(`info quizgen root path to ${MAIN_PAGE}`)
res.sendFile(`./${DIR}/${MAIN_PAGE}`, {
root: public_dir
})
})
// api/preview
server.post(`/${DIR}/api/preview`, function(req, res) {
console.log(`debug content-type header = ${req.headers['content-type']}`)
generator(req.body)
.then((anki_notes) => {
res.json({
anki_notes: anki_notes.map(
(note) => note.toString(
undefined,
req.body[opt.OPT_TAG]?.join(AnkiNote.SEPARATOR)
)
),
anki_notes_header: AnkiNote.header(
anki_notes.length,
undefined,
opt.clean_opts(req.body)
)
})
})
})
// api/generate
server.post(`/${DIR}/api/generate`, function(req, res) {
/**
* @type {opt.OptArgv}
*/
const opts = req.body
let notes_name = opts[opt.OPT_NOTES_NAME]
if (notes_name === undefined) {
notes_name = 'quizgen-anki-notes'
}
generator(opts)
.then((anki_notes) => {
// add unique suffix
notes_name += '-' + new Date().toISOString().replace(/[:\s]/g, '-')
console.log(`info anki notes name = ${notes_name}`)
// create local anki notes file
return AnkiNote.export(
anki_notes,
notes_name,
`${public_dir}/${ANKI_DIR}`,
undefined,
opts[opt.OPT_TAG],
opt.clean_opts(opts)
)
})
.then((export_bytes) => {
// send anki notes file for download
const file_path = `/${ANKI_DIR}/${notes_name}.txt`
res.json({
file_path: file_path,
file_size: export_bytes,
file_size_unit: 'B',
file_expiry: EXPORT_DELETE_DELAY_MIN,
file_expiry_unit: 'minute'
})
if (DELETE_EXPORTS) {
setTimeout(
() => delete_file(`${public_dir}${file_path}`),
// delete file after so many minutes
EXPORT_DELETE_DELAY_MIN * (1000 * 60)
)
}
else {
console.log(`info skip delete ${file_path} after send`)
}
})
})
}
exports.main = main
/**
* Instantiate a QuizCardGenerator given options provided by a web client.
*
* @param {opt.OptArgv} opts
* @param quizgen
*/
function generator(opts) {
console.log(`debug generator(${JSON.stringify(
opts,
(key, val) => {
if (key === opt.OPT_INPUT_FILE_CONTENT) {
// show reduced preview
return val.substring(0, 1500) + '...'
}
else {
return val
}
},
2
)})`)
const input_file_path = opts[opt.OPT_INPUT_FILE]
console.log('debug new quizgen instance')
const qg = new QuizCardGenerator(
opts[opt.OPT_INPUT_FILE_CONTENT],
input_file_path,
opts[opt.OPT_EXCLUDE_WORD]
// and parse strings, regexp
?.map((exclude) => {
if (exclude.startsWith('/') && exclude.endsWith('/')) {
return new RegExp(exclude.slice(1, exclude.lastIndexOf('/')))
}
else {
return exclude
}
}),
opts[opt.OPT_SENTENCE_WORDS_MIN],
opts[opt.OPT_SENTENCE_TOKENS_MAX]
)
AnkiNote.set_choices_max(opts[opt.OPT_CHOICES_MAX])
return qg.finish_calculation
.then(
() => {
console.log(`info calculations complete for ${input_file_path}`)
// console.log(`debug sentences = ${qg.get_sentences().join(',')}`)
return qg.generate_anki_notes(
opts[opt.OPT_LIMIT],
opts[opt.OPT_WORD_FREQUENCY_MIN],
opts[opt.OPT_WORD_LENGTH_MIN],
opts[opt.OPT_WORD_FREQUENCY_ORDINAL_MAX],
opts[opt.OPT_WORD_FREQUENCY_ORDINAL_MIN],
opts[opt.OPT_PROLOGUE],
opts[opt.OPT_EPILOGUE],
opts[opt.OPT_CHOICE_VARIATION]
)
},
(err) => {
return Promise.reject(err)
}
)
}
/**
* Delete a local file.
*
* @param {string} file_path
* @returns {Promise<void>}
*/
function delete_file(file_path) {
return fs.unlink(file_path)
.then(
() => {
console.log(`debug deleted file ${file_path}`)
},
(err) => {
console.log(`error failed to delete file ${file_path}. ${err}. ${err.stack}`)
}
)
}