-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreateAPKG.js
88 lines (80 loc) · 2.29 KB
/
createAPKG.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
const readline = require('readline')
const Database = require('better-sqlite3')
const ProgressBar = require('progress')
const { APKG } = require('anki-apkg')
const query = (dictdb, word) => {
const entries = dictdb
.prepare(
`SELECT meaning FROM dictionary WHERE word = (?) OR word LIKE (?) OR word LIKE (?)`
)
.all(word, `${word}|%`, `%|${word}`)
return entries
.map(entry => {
return `<div class="meaning">${entry.meaning}</div>`
})
.join('')
}
const createAPKG = ({ vocab, dict }) => {
const SQL_LOOKUPS = `
select WORDS.stem, WORDS.word, LOOKUPS.usage, LOOKUPS.timestamp
from LOOKUPS left join WORDS
on WORDS.id = LOOKUPS.word_key
left join BOOK_INFO
on BOOK_INFO.id = LOOKUPS.book_key
where BOOK_INFO.title = (?)
`
const db = new Database(vocab)
const rows = db.prepare('SELECT title from BOOK_INFO').all()
rows.forEach((row, i) => {
console.log(`[${i + 1}]`, row.title)
})
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question(
"Select the book you'd like to export (input number):",
answer => {
answer = Number(answer)
if (answer < 0 || answer > rows.length) {
return
}
const title = rows[answer - 1].title
const dictdb = new Database(dict)
const lookups = db.prepare(SQL_LOOKUPS).all(title)
const apkg = new APKG({
name: title,
card: {
fields: ['word', 'meaning', 'usage'],
template: {
question: '{{word}}',
answer: `
<div class="word">{{word}}</div>
<div class="meaning">{{meaning}}</div>
<div class="usage">{{usage}}</div>
`
}
}
})
const progress = new ProgressBar(':bar :percent', {
total: lookups.length
})
lookups.forEach(lookup => {
let { stem, word, usage, timestamp } = lookup
let meaning = query(dictdb, stem)
usage = usage.replace(word, `<strong>${word}</strong>`)
apkg.addCard({
timestamp,
content: [stem, meaning, usage]
})
progress.tick()
})
apkg.save(__dirname)
rl.close()
console.log(`${title}.apkg created.`)
}
)
}
module.exports = {
createAPKG
}