genanki-js 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/genanki.js CHANGED
@@ -1,536 +1,1270 @@
1
- /**
2
- * [mkanki]{@link https://github.com/nornagon/mkanki}
3
- * @copyright Copyright (c) 2018 Jeremy Apthorp
4
- * @license AGPL-3.0 License
5
- *
6
- *
7
- * [genanki]{@link https://github.com/kerrickstaley/genanki}
8
- * @copyright Copyright (c) Kerrick Staley 2021
9
- * @license The MIT License
10
- *
11
- *
12
- * [genanki-js]{@link https://github.com/krmanik/genanki-js}
13
- * @copyright Copyright (c) 2021 Mani
14
- * @license AGPL-3.0 License
15
- */
16
-
17
- import { saveAs } from "file-saver";
18
- import { sha256 } from "js-sha256";
19
- import JSZip from "jszip";
20
- import bigInt from "big-integer";
21
-
22
- const BASE91_TABLE = [
23
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
24
- 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
25
- 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4',
26
- '5', '6', '7', '8', '9', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', ':',
27
- ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~']
28
-
29
- function ankiHash(fields) {
30
- const str = fields.join('__')
31
- const h = sha256.create();
32
- h.update(str)
33
- const hex = h.digest()
34
-
35
- let hash_int = bigInt();
36
- for (let i = 0; i < 8; i++) {
37
- hash_int *= bigInt("256");
38
- hash_int += bigInt(hex[i])
39
- }
40
-
41
- // convert to the weird base91 format that Anki uses
42
- let rv_reversed = []
43
- while (hash_int > 0) {
44
- rv_reversed.push(BASE91_TABLE[hash_int % bigInt("91")])
45
- hash_int = (hash_int / bigInt("91"))
46
- }
47
-
48
- return rv_reversed.reverse().join('')
49
- }
50
-
51
- const MODEL_STD = 0
52
- const MODEL_CLOZE = 1
53
-
54
- export class Model {
55
- constructor(props) {
56
- this.props = {
57
- ...defaultModel,
58
- ...props,
59
- flds: props.flds.map((f, i) => ({ ...defaultField, ord: i, ...f })),
60
- tmpls: props.tmpls.map((t, i) => ({ ...defaultTemplate, ord: i, name: `Card ${i + 1}`, ...t })),
61
- mod: new Date().getTime()
62
- }
63
- this.fieldNameToOrd = {}
64
- this.props.flds.forEach(f => { this.fieldNameToOrd[f.name] = f.ord })
65
- }
66
-
67
- note(fields, tags, guid = null) {
68
- if (Array.isArray(fields)) {
69
- if (fields.length !== this.props.flds.length) {
70
- throw new Error(`Expected ${this.props.flds.length} fields for model '${this.props.name}' but got ${fields.length}`)
71
- }
72
- return new Note(this, fields, tags, guid)
73
- } else {
74
- const field_names = Object.keys(fields)
75
- const fields_list = []
76
- field_names.forEach(field_name => {
77
- const ord = this.fieldNameToOrd[field_name]
78
- if (ord == null) throw new Error(`Field '${field_name}' does not exist in the model`)
79
- fields_list[ord] = fields[field_name]
80
- })
81
- return new Note(this, fields_list, tags, guid)
82
- }
83
- }
84
- }
85
-
86
- export class ClozeModel extends Model {
87
- constructor(props) {
88
- super({
89
- type: MODEL_CLOZE,
90
- css: `
91
- .card {
92
- font-family: arial;
93
- font-size: 20px;
94
- text-align: center;
95
- color: black;
96
- background-color: white;
97
- }
98
-
99
- .cloze {
100
- font-weight: bold;
101
- color: blue;
102
- }
103
- `,
104
- tmpls: [{ name: "Cloze", ...props.tmpl }],
105
- ...props
106
- })
107
- }
108
- }
109
-
110
- export const defaultModel = {
111
- sortf: 0, // sort field
112
- did: 1, // deck id
113
- latexPre: `\\documentclass[12pt]{article}
114
- \\special{papersize=3in,5in}
115
- \\usepackage[utf8]{inputenc}
116
- \\usepackage{amssymb,amsmath}
117
- \\pagestyle{empty}
118
- \\setlength{\\parindent}{0in}
119
- \\begin{document}`,
120
- latexPost: "\\end{document}",
121
- mod: 0, // modification time
122
- usn: 0, // unsure, something to do with sync?
123
- vers: [], // seems to be unused
124
- type: MODEL_STD,
125
- css: `.card {
126
- font-family: arial;
127
- font-size: 20px;
128
- text-align: center;
129
- color: black;
130
- background-color: white;
131
- }`,
132
- /* also:
133
- name: string,
134
- flds: [Field],
135
- tmpls: [Template],
136
- tags: [??],
137
- id: string
138
- */
139
- tags: [],
140
- }
141
-
142
- export const defaultField = {
143
- name: "",
144
- ord: null,
145
- sticky: false,
146
- rtl: false,
147
- font: "Arial",
148
- size: 20,
149
- media: [],
150
- }
151
-
152
- export const defaultTemplate = {
153
- name: "",
154
- ord: null,
155
- qfmt: "",
156
- afmt: "",
157
- did: null,
158
- bqfmt: "",
159
- bafmt: "",
160
- }
161
-
162
- // whether new cards should be mixed with reviews, or shown first or last
163
- const NEW_CARDS_DISTRIBUTE = 0
164
- const NEW_CARDS_LAST = 1
165
- const NEW_CARDS_FIRST = 2
166
-
167
- export const defaultConf = {
168
- // review options
169
- 'activeDecks': [1],
170
- 'curDeck': 1,
171
- 'newSpread': NEW_CARDS_DISTRIBUTE,
172
- 'collapseTime': 1200,
173
- 'timeLim': 0,
174
- 'estTimes': true,
175
- 'dueCounts': true,
176
- // other config
177
- 'curModel': null,
178
- 'nextPos': 1,
179
- 'sortType': "noteFld",
180
- 'sortBackwards': false,
181
- 'addToCur': true, // add new to currently selected deck?
182
- 'dayLearnFirst': false,
183
- }
184
-
185
-
186
- // new card insertion order
187
- const NEW_CARDS_RANDOM = 0
188
- const NEW_CARDS_DUE = 1
189
-
190
- const STARTING_FACTOR = 2500
191
-
192
- export const defaultDeckConf = {
193
- 'name': "Default",
194
- 'new': {
195
- 'delays': [1, 10],
196
- 'ints': [1, 4, 7], // 7 is not currently used
197
- 'initialFactor': STARTING_FACTOR,
198
- 'separate': true,
199
- 'order': NEW_CARDS_DUE,
200
- 'perDay': 20,
201
- // may not be set on old decks
202
- 'bury': false,
203
- },
204
- 'lapse': {
205
- 'delays': [10],
206
- 'mult': 0,
207
- 'minInt': 1,
208
- 'leechFails': 8,
209
- // type 0=suspend, 1=tagonly
210
- 'leechAction': 0,
211
- },
212
- 'rev': {
213
- 'perDay': 200,
214
- 'ease4': 1.3,
215
- 'fuzz': 0.05,
216
- 'minSpace': 1, // not currently used
217
- 'ivlFct': 1,
218
- 'maxIvl': 36500,
219
- // may not be set on old decks
220
- 'bury': false,
221
- 'hardFactor': 1.2,
222
- },
223
- 'maxTaken': 60,
224
- 'timer': 0,
225
- 'autoplay': true,
226
- 'replayq': true,
227
- 'mod': 0,
228
- 'usn': 0,
229
- }
230
-
231
- export const defaultDeck = {
232
- newToday: [0, 0], // currentDay, count
233
- revToday: [0, 0],
234
- lrnToday: [0, 0],
235
- timeToday: [0, 0], // time in ms
236
- conf: 1,
237
- usn: 0,
238
- desc: "",
239
- dyn: 0, // anki uses int/bool interchangably here
240
- collapsed: false,
241
- // added in beta11
242
- extendNew: 10,
243
- extendRev: 50,
244
- }
245
-
246
- export class Deck {
247
- constructor(id, name, desc="") {
248
- this.id = id
249
- this.name = name
250
- this.desc = desc
251
- this.notes = []
252
- }
253
-
254
- addNote(note) {
255
- this.notes.push(note)
256
- }
257
- }
258
-
259
- export class Note {
260
- constructor(model, fields, tags = null, guid = null) {
261
- this.model = model
262
- this.fields = fields
263
- this.tags = tags
264
- this._guid = guid
265
- }
266
-
267
- get guid() {
268
- return this._guid ? this._guid : ankiHash(this.fields);
269
- }
270
-
271
- get cards() {
272
- if (this.model.props.type === MODEL_STD) {
273
- const isEmpty = f => {
274
- return !f || f.toString().trim().length === 0
275
- }
276
- const rv = []
277
- for (const [card_ord, any_or_all, required_field_ords] of this.model.props.req) {
278
- const op = any_or_all === "any" ? "some" : "every"
279
- if (required_field_ords[op](f => !isEmpty(this.fields[f]))) {
280
- rv.push(card_ord)
281
- }
282
- }
283
- return rv
284
- } else {
285
- // the below logic is copied from anki's ModelManager._availClozeOrds
286
- const ords = new Set()
287
- const matches = []
288
- const curliesRe = /{{[^}]*?cloze:(?:[^}]?:)*(.+?)}}/g
289
- const percentRe = /<%cloze:(.+?)%>/g
290
- const { qfmt } = this.model.props.tmpls[0] // cloze models only have 1 template
291
- let m;
292
- while (m = curliesRe.exec(qfmt))
293
- matches.push(m[1])
294
- while (m = percentRe.exec(qfmt))
295
- matches.push(m[1])
296
- const map = {}
297
- this.model.props.flds.forEach((fld, i) => {
298
- map[fld.name] = [i, fld]
299
- })
300
- for (const fname of matches) {
301
- if (!(fname in map)) continue
302
- const ord = map[fname][0]
303
- const re = /{{c(\d+)::.+?}}/gs
304
- while (m = re.exec(this.fields[ord])) {
305
- const i = parseInt(m[1])
306
- if (i > 0)
307
- ords.add(i - 1)
308
- }
309
- }
310
- if (ords.size === 0) {
311
- // empty clozes use first ord
312
- return [0]
313
- }
314
- return Array.from(ords)
315
- }
316
- }
317
- }
318
-
319
- export class Package {
320
- constructor() {
321
- this.db = null;
322
- this.decks = []
323
- this.media = []
324
- }
325
-
326
- setSqlJs(SQL) {
327
- this.db = SQL;
328
- }
329
-
330
- addDeck(deck) {
331
- this.decks.push(deck)
332
- }
333
-
334
- addMedia(data, name) {
335
- this.media.push({ name, data })
336
- }
337
-
338
- addMediaFile(filename, name = null) {
339
- this.media.push({ name: name || filename, filename })
340
- }
341
-
342
- writeToFile(filename) {
343
- let db = this.db;
344
- db.run(APKG_SCHEMA);
345
-
346
- this.write(db)
347
-
348
- let zip = new JSZip();
349
-
350
- const data = db.export();
351
- const buffer = new Uint8Array(data).buffer;
352
-
353
- zip.file("collection.anki2", buffer);
354
-
355
- const media_info = {}
356
-
357
- this.media.forEach((m, i) => {
358
- if (m.filename != null) {
359
- zip.file(i.toString(), m.filename)
360
- } else {
361
- zip.file(i.toString(), m.data)
362
- }
363
-
364
- media_info[i] = m.name
365
- })
366
-
367
- zip.file('media', JSON.stringify(media_info))
368
-
369
- zip.generateAsync({ type: "blob", mimeType: "application/apkg" }).then(function (content) {
370
- // see FileSaver.js
371
- saveAs(content, filename);
372
- });
373
- }
374
-
375
-
376
- write(db) {
377
- const now = new Date
378
- const models = {}
379
- const decks = {}
380
-
381
- // AnkiDroid failed to import subdeck, So add a Default deck
382
- decks["1"] = {...defaultDeck, id: 1, name: "Default"}
383
-
384
- this.decks.forEach(d => {
385
- d.notes.forEach(n => models[n.model.props.id] = n.model.props)
386
- decks[d.id] = {
387
- ...defaultDeck,
388
- id: d.id,
389
- name: d.name,
390
- desc: d.desc,
391
- }
392
- })
393
-
394
- const col = [
395
- null, // id
396
- (+now / 1000) | 0, // crt
397
- +now, // mod
398
- +now, // scm
399
- 11, // ver
400
- 0, // dty
401
- 0, // usn
402
- 0, // ls
403
- JSON.stringify(defaultConf), // conf
404
- JSON.stringify(models), // models
405
- JSON.stringify(decks), // decks
406
- JSON.stringify({ 1: { id: 1, ...defaultDeckConf } }), // dconf
407
- JSON.stringify({}), // tags
408
- ]
409
-
410
- db.prepare(`INSERT INTO col
411
- (id, crt, mod, scm, ver, dty, usn, ls, conf, models, decks, dconf, tags)
412
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(col)
413
-
414
- const insert_notes = db.prepare(`INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)
415
- VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, '')`)
416
-
417
- const insert_cards = db.prepare(`INSERT INTO cards (id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data)
418
- VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, 0, 0, 0, 0, 0, 0, '')`)
419
-
420
- for (const deck of this.decks) {
421
- for (const note of deck.notes) {
422
- let tags = note.tags == null ? '' : note.tags.join(' ')
423
- insert_notes.run(
424
- [
425
- note.guid, // guid
426
- note.model.props.id, // mid
427
- (+now / 1000) | 0, // mod
428
- -1, // usn
429
- tags, // tags
430
- note.fields.join('\x1f'), // flds
431
- 0, // sfld
432
- ])
433
-
434
- let rowID = db.exec("select last_insert_rowid();")
435
- let note_id = rowID[0]['values'][0][0];
436
-
437
- for (const card_ord of note.cards) {
438
- insert_cards.run(
439
- [
440
- note_id, // nid
441
- deck.id, // did
442
- card_ord, // ord
443
- (+now / 1000) | 0, // mod
444
- -1, // usn
445
- 0, // type 0=new, 1=learning, 2=due
446
- 0, // queue -1 for suspended
447
- ])
448
- }
449
- }
450
- }
451
- }
452
- }
453
-
454
- /*
455
- * [genanki]{@link https://github.com/kerrickstaley/genanki}
456
- * @copyright Copyright (c) Kerrick Staley 2021
457
- * @license The MIT License
458
- */
459
-
460
- export const APKG_SCHEMA = `
461
- PRAGMA foreign_keys=OFF;
462
- BEGIN TRANSACTION;
463
-
464
- CREATE TABLE col (
465
- id integer primary key,
466
- crt integer not null,
467
- mod integer not null,
468
- scm integer not null,
469
- ver integer not null,
470
- dty integer not null,
471
- usn integer not null,
472
- ls integer not null,
473
- conf text not null,
474
- models text not null,
475
- decks text not null,
476
- dconf text not null,
477
- tags text not null
478
- );
479
- CREATE TABLE notes (
480
- id integer primary key, /* 0 */
481
- guid text not null, /* 1 */
482
- mid integer not null, /* 2 */
483
- mod integer not null, /* 3 */
484
- usn integer not null, /* 4 */
485
- tags text not null, /* 5 */
486
- flds text not null, /* 6 */
487
- sfld integer not null, /* 7 */
488
- csum integer not null, /* 8 */
489
- flags integer not null, /* 9 */
490
- data text not null /* 10 */
491
- );
492
- CREATE TABLE cards (
493
- id integer primary key, /* 0 */
494
- nid integer not null, /* 1 */
495
- did integer not null, /* 2 */
496
- ord integer not null, /* 3 */
497
- mod integer not null, /* 4 */
498
- usn integer not null, /* 5 */
499
- type integer not null, /* 6 */
500
- queue integer not null, /* 7 */
501
- due integer not null, /* 8 */
502
- ivl integer not null, /* 9 */
503
- factor integer not null, /* 10 */
504
- reps integer not null, /* 11 */
505
- lapses integer not null, /* 12 */
506
- left integer not null, /* 13 */
507
- odue integer not null, /* 14 */
508
- odid integer not null, /* 15 */
509
- flags integer not null, /* 16 */
510
- data text not null /* 17 */
511
- );
512
- CREATE TABLE revlog (
513
- id integer primary key,
514
- cid integer not null,
515
- usn integer not null,
516
- ease integer not null,
517
- ivl integer not null,
518
- lastIvl integer not null,
519
- factor integer not null,
520
- time integer not null,
521
- type integer not null
522
- );
523
- CREATE TABLE graves (
524
- usn integer not null,
525
- oid integer not null,
526
- type integer not null
527
- );
528
- CREATE INDEX ix_notes_usn on notes (usn);
529
- CREATE INDEX ix_cards_usn on cards (usn);
530
- CREATE INDEX ix_revlog_usn on revlog (usn);
531
- CREATE INDEX ix_cards_nid on cards (nid);
532
- CREATE INDEX ix_cards_sched on cards (did, queue, due);
533
- CREATE INDEX ix_revlog_cid on revlog (cid);
534
- CREATE INDEX ix_notes_csum on notes (csum);
535
- COMMIT;
536
- `;
1
+ /**
2
+ * [mkanki]{@link https://github.com/nornagon/mkanki}
3
+ * @copyright Copyright (c) 2018 Jeremy Apthorp
4
+ * @license AGPL-3.0 License
5
+ *
6
+ *
7
+ * [genanki]{@link https://github.com/kerrickstaley/genanki}
8
+ * @copyright Copyright (c) Kerrick Staley 2021
9
+ * @license The MIT License
10
+ *
11
+ *
12
+ * [genanki-js]{@link https://github.com/krmanik/genanki-js}
13
+ * @copyright Copyright (c) 2021 Mani
14
+ * @license AGPL-3.0 License
15
+ */
16
+ const BASE91_TABLE = [
17
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
18
+ 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
19
+ 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4',
20
+ '5', '6', '7', '8', '9', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', ':',
21
+ ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~']
22
+
23
+ function ankiHash(fields) {
24
+ const str = fields.join('__')
25
+ const h = sha256.create();
26
+ h.update(str)
27
+ const hex = h.digest()
28
+
29
+ // The first 8 bytes of the digest form a big-endian unsigned 64-bit integer,
30
+ // which is converted to the weird base91 format that Anki uses.
31
+ // Long division over the byte array is used instead of BigInt, because
32
+ // BigInt (and the `0n` literal syntax) is not supported on older Safari / iOS.
33
+ const digits = hex.slice(0, 8)
34
+ let rv_reversed = []
35
+ while (digits.some(d => d !== 0)) {
36
+ let remainder = 0
37
+ for (let i = 0; i < digits.length; i++) {
38
+ const acc = remainder * 256 + digits[i]
39
+ digits[i] = Math.floor(acc / 91)
40
+ remainder = acc % 91
41
+ }
42
+ rv_reversed.push(BASE91_TABLE[remainder])
43
+ }
44
+
45
+ return rv_reversed.reverse().join('')
46
+ }
47
+ const MODEL_STD = 0
48
+ const MODEL_CLOZE = 1
49
+
50
+ class Model {
51
+ constructor(props) {
52
+ this.props = {
53
+ ...defaultModel,
54
+ ...props,
55
+ flds: props.flds.map((f, i) => ({ ...defaultField, ord: i, ...f })),
56
+ tmpls: props.tmpls.map((t, i) => ({ ...defaultTemplate, ord: i, name: `Card ${i + 1}`, ...t })),
57
+ mod: new Date().getTime()
58
+ }
59
+ this.fieldNameToOrd = {}
60
+ this.props.flds.forEach(f => { this.fieldNameToOrd[f.name] = f.ord })
61
+ }
62
+
63
+ note(fields, tags, guid = null) {
64
+ if (Array.isArray(fields)) {
65
+ if (fields.length !== this.props.flds.length) {
66
+ throw new Error(`Expected ${this.props.flds.length} fields for model '${this.props.name}' but got ${fields.length}`)
67
+ }
68
+ return new Note(this, fields, tags, guid)
69
+ } else {
70
+ const field_names = Object.keys(fields)
71
+ const fields_list = []
72
+ field_names.forEach(field_name => {
73
+ const ord = this.fieldNameToOrd[field_name]
74
+ if (ord == null) throw new Error(`Field '${field_name}' does not exist in the model`)
75
+ fields_list[ord] = fields[field_name]
76
+ })
77
+ return new Note(this, fields_list, tags, guid)
78
+ }
79
+ }
80
+ }
81
+
82
+ class ClozeModel extends Model {
83
+ constructor(props) {
84
+ // `tmpl` is only used to build the single cloze template,
85
+ // it must not end up in the note type json
86
+ const { tmpl, ...rest } = props
87
+ super({
88
+ type: MODEL_CLOZE,
89
+ css: `
90
+ .card {
91
+ font-family: arial;
92
+ font-size: 20px;
93
+ text-align: center;
94
+ color: black;
95
+ background-color: white;
96
+ }
97
+
98
+ .cloze {
99
+ font-weight: bold;
100
+ color: blue;
101
+ }
102
+ `,
103
+ tmpls: [{ name: "Cloze", ...tmpl }],
104
+ ...rest
105
+ })
106
+ }
107
+ }
108
+ const defaultModel = {
109
+ sortf: 0, // sort field
110
+ did: 1, // deck id
111
+ latexPre: `\\documentclass[12pt]{article}
112
+ \\special{papersize=3in,5in}
113
+ \\usepackage[utf8]{inputenc}
114
+ \\usepackage{amssymb,amsmath}
115
+ \\pagestyle{empty}
116
+ \\setlength{\\parindent}{0in}
117
+ \\begin{document}`,
118
+ latexPost: "\\end{document}",
119
+ mod: 0, // modification time
120
+ usn: 0, // unsure, something to do with sync?
121
+ vers: [], // seems to be unused
122
+ type: MODEL_STD,
123
+ css: `.card {
124
+ font-family: arial;
125
+ font-size: 20px;
126
+ text-align: center;
127
+ color: black;
128
+ background-color: white;
129
+ }`,
130
+ /* also:
131
+ name: string,
132
+ flds: [Field],
133
+ tmpls: [Template],
134
+ tags: [??],
135
+ id: string
136
+ */
137
+ tags: [],
138
+ }
139
+
140
+ const defaultField = {
141
+ name: "",
142
+ ord: null,
143
+ sticky: false,
144
+ rtl: false,
145
+ font: "Arial",
146
+ size: 20,
147
+ media: [],
148
+ }
149
+
150
+ const defaultTemplate = {
151
+ name: "",
152
+ ord: null,
153
+ qfmt: "",
154
+ afmt: "",
155
+ did: null,
156
+ bqfmt: "",
157
+ bafmt: "",
158
+ }
159
+
160
+ // whether new cards should be mixed with reviews, or shown first or last
161
+ const NEW_CARDS_DISTRIBUTE = 0
162
+ const NEW_CARDS_LAST = 1
163
+ const NEW_CARDS_FIRST = 2
164
+
165
+ const defaultConf = {
166
+ // review options
167
+ 'activeDecks': [1],
168
+ 'curDeck': 1,
169
+ 'newSpread': NEW_CARDS_DISTRIBUTE,
170
+ 'collapseTime': 1200,
171
+ 'timeLim': 0,
172
+ 'estTimes': true,
173
+ 'dueCounts': true,
174
+ // other config
175
+ 'curModel': null,
176
+ 'nextPos': 1,
177
+ 'sortType': "noteFld",
178
+ 'sortBackwards': false,
179
+ 'addToCur': true, // add new to currently selected deck?
180
+ 'dayLearnFirst': false,
181
+ }
182
+
183
+
184
+ // new card insertion order
185
+ const NEW_CARDS_RANDOM = 0
186
+ const NEW_CARDS_DUE = 1
187
+
188
+ const STARTING_FACTOR = 2500
189
+
190
+ const defaultDeckConf = {
191
+ 'name': "Default",
192
+ 'new': {
193
+ 'delays': [1, 10],
194
+ 'ints': [1, 4, 7], // 7 is not currently used
195
+ 'initialFactor': STARTING_FACTOR,
196
+ 'separate': true,
197
+ 'order': NEW_CARDS_DUE,
198
+ 'perDay': 20,
199
+ // may not be set on old decks
200
+ 'bury': false,
201
+ },
202
+ 'lapse': {
203
+ 'delays': [10],
204
+ 'mult': 0,
205
+ 'minInt': 1,
206
+ 'leechFails': 8,
207
+ // type 0=suspend, 1=tagonly
208
+ 'leechAction': 0,
209
+ },
210
+ 'rev': {
211
+ 'perDay': 200,
212
+ 'ease4': 1.3,
213
+ 'fuzz': 0.05,
214
+ 'minSpace': 1, // not currently used
215
+ 'ivlFct': 1,
216
+ 'maxIvl': 36500,
217
+ // may not be set on old decks
218
+ 'bury': false,
219
+ 'hardFactor': 1.2,
220
+ },
221
+ 'maxTaken': 60,
222
+ 'timer': 0,
223
+ 'autoplay': true,
224
+ 'replayq': true,
225
+ 'mod': 0,
226
+ 'usn': 0,
227
+ }
228
+
229
+ const defaultDeck = {
230
+ newToday: [0, 0], // currentDay, count
231
+ revToday: [0, 0],
232
+ lrnToday: [0, 0],
233
+ timeToday: [0, 0], // time in ms
234
+ conf: 1,
235
+ usn: 0,
236
+ desc: "",
237
+ dyn: 0, // anki uses int/bool interchangably here
238
+ collapsed: false,
239
+ // added in beta11
240
+ extendNew: 10,
241
+ extendRev: 50,
242
+ }
243
+ class Deck {
244
+ constructor(id, name, desc="") {
245
+ this.id = id
246
+ this.name = name
247
+ this.desc = desc
248
+ this.notes = []
249
+ }
250
+
251
+ addNote(note) {
252
+ this.notes.push(note)
253
+ }
254
+ }
255
+ class Note {
256
+ constructor(model, fields, tags = null, guid = null) {
257
+ this.model = model
258
+ this.fields = fields
259
+ this.tags = tags
260
+ this._guid = guid
261
+ }
262
+
263
+ get guid() {
264
+ return this._guid ? this._guid : ankiHash(this.fields);
265
+ }
266
+
267
+ get cards() {
268
+ if (this.model.props.type === MODEL_STD) {
269
+ const isEmpty = f => {
270
+ return !f || f.toString().trim().length === 0
271
+ }
272
+ const rv = []
273
+ for (const [card_ord, any_or_all, required_field_ords] of this.model.props.req) {
274
+ const op = any_or_all === "any" ? "some" : "every"
275
+ if (required_field_ords[op](f => !isEmpty(this.fields[f]))) {
276
+ rv.push(card_ord)
277
+ }
278
+ }
279
+ return rv
280
+ } else {
281
+ // the below logic is copied from anki's ModelManager._availClozeOrds
282
+ const ords = new Set()
283
+ const matches = []
284
+ const curliesRe = /{{[^}]*?cloze:(?:[^}]?:)*(.+?)}}/g
285
+ const percentRe = /<%cloze:(.+?)%>/g
286
+ const { qfmt } = this.model.props.tmpls[0] // cloze models only have 1 template
287
+ let m;
288
+ while (m = curliesRe.exec(qfmt))
289
+ matches.push(m[1])
290
+ while (m = percentRe.exec(qfmt))
291
+ matches.push(m[1])
292
+ const map = {}
293
+ this.model.props.flds.forEach((fld, i) => {
294
+ map[fld.name] = [i, fld]
295
+ })
296
+ for (const fname of matches) {
297
+ if (!(fname in map)) continue
298
+ const ord = map[fname][0]
299
+ // matches {{c1::text}} and {{c1,2::text}} (one deletion shown on several cards)
300
+ const re = /{{c(\d+(?:,\d+)*)::[\s\S]+?}}/g
301
+ while (m = re.exec(this.fields[ord])) {
302
+ for (const n of m[1].split(',')) {
303
+ const i = parseInt(n)
304
+ if (i > 0)
305
+ ords.add(i - 1)
306
+ }
307
+ }
308
+ }
309
+ if (ords.size === 0) {
310
+ // empty clozes use first ord
311
+ return [0]
312
+ }
313
+ return Array.from(ords)
314
+ }
315
+ }
316
+ }
317
+ // Dependencies used to create the package.
318
+ // When genanki.js is loaded with a <script> tag, the global `SQL` (or `initSqlJs`),
319
+ // `JSZip` and `saveAs` (FileSaver.js, optional) are used.
320
+ // The npm builds (ES module / CommonJS) set these from the installed packages.
321
+ const genankiDeps = {
322
+ SQL: null, // initialized sql.js, see setSqlJs()
323
+ sqlPromise: null, // Promise<SQL> while sql.js is being loaded
324
+ loadSqlJs: null, // () => Promise<SQL>
325
+ JSZip: null,
326
+ saveAs: null,
327
+ }
328
+
329
+ /**
330
+ * Use an initialized sql.js instance for all packages.
331
+ * `SQL` is the value `initSqlJs()` resolves to.
332
+ */
333
+ function setSqlJs(SQL) {
334
+ genankiDeps.SQL = SQL
335
+ }
336
+
337
+ function resolveSqlJs(packageSQL) {
338
+ if (packageSQL) return Promise.resolve(packageSQL)
339
+ if (genankiDeps.SQL) return Promise.resolve(genankiDeps.SQL)
340
+ if (typeof SQL !== "undefined" && SQL) return Promise.resolve(SQL)
341
+
342
+ if (!genankiDeps.sqlPromise) {
343
+ let load = genankiDeps.loadSqlJs
344
+ if (!load && typeof initSqlJs === "function") {
345
+ load = () => initSqlJs()
346
+ }
347
+ if (!load) {
348
+ return Promise.reject(new Error("sql.js is not loaded. Add sql.js to the page and assign the result " +
349
+ "of initSqlJs() to a global `SQL` variable, or pass it with setSqlJs(SQL)."))
350
+ }
351
+ genankiDeps.sqlPromise = load().then(sql => {
352
+ genankiDeps.SQL = sql
353
+ return sql
354
+ }, err => {
355
+ genankiDeps.sqlPromise = null
356
+ throw err
357
+ })
358
+ }
359
+ return genankiDeps.sqlPromise
360
+ }
361
+
362
+ function resolveJSZip() {
363
+ if (genankiDeps.JSZip) return genankiDeps.JSZip
364
+ if (typeof JSZip !== "undefined" && JSZip) return JSZip
365
+ throw new Error("JSZip is not loaded. Add jszip.min.js to the page.")
366
+ }
367
+
368
+ function saveFile(content, filename) {
369
+ const saver = genankiDeps.saveAs || (typeof saveAs === "function" ? saveAs : null)
370
+ if (saver) {
371
+ saver(content, filename)
372
+ return
373
+ }
374
+
375
+ // FileSaver.js is optional, download with a temporary link instead
376
+ if (typeof document !== "undefined" && typeof URL !== "undefined" && URL.createObjectURL) {
377
+ const url = URL.createObjectURL(content)
378
+ const a = document.createElement("a")
379
+ a.href = url
380
+ a.download = filename
381
+ a.style.display = "none"
382
+ document.body.appendChild(a)
383
+ a.click()
384
+ document.body.removeChild(a)
385
+ setTimeout(() => URL.revokeObjectURL(url), 40000)
386
+ return
387
+ }
388
+
389
+ throw new Error("writeToFile() can only download files in a browser. " +
390
+ "Use package.generate('nodebuffer') or package.generate('uint8array') and write the result to a file.")
391
+ }
392
+
393
+ class Package {
394
+ constructor() {
395
+ this.decks = []
396
+ this.media = []
397
+ this.SQL = null
398
+ }
399
+
400
+ /**
401
+ * Use an initialized sql.js instance for this package.
402
+ */
403
+ setSqlJs(SQL) {
404
+ this.SQL = SQL
405
+ return this
406
+ }
407
+
408
+ addDeck(deck) {
409
+ // adding the same deck twice would duplicate all of its notes
410
+ if (!this.decks.includes(deck)) {
411
+ this.decks.push(deck)
412
+ }
413
+ }
414
+
415
+ addMedia(data, name) {
416
+ this.media.push({ name, data })
417
+ }
418
+
419
+ addMediaFile(filename, name = null) {
420
+ this.media.push({ name: name || filename, filename })
421
+ }
422
+
423
+ /**
424
+ * Create the .apkg file content.
425
+ * `type` is any JSZip output type: "blob" (default), "uint8array", "arraybuffer", "base64",
426
+ * or "nodebuffer" (Node.js only).
427
+ * Returns a Promise.
428
+ */
429
+ generate(type = "blob") {
430
+ return resolveSqlJs(this.SQL).then(SQL => {
431
+ const ZipClass = resolveJSZip()
432
+
433
+ const db = new SQL.Database()
434
+ let data
435
+ try {
436
+ db.run(APKG_SCHEMA)
437
+ this.write(db)
438
+ data = db.export()
439
+ } finally {
440
+ db.close()
441
+ }
442
+
443
+ const zip = new ZipClass()
444
+ zip.file("collection.anki2", data)
445
+
446
+ const media_info = {}
447
+
448
+ this.media.forEach((m, i) => {
449
+ if (m.filename != null) {
450
+ zip.file(i.toString(), m.filename)
451
+ } else {
452
+ zip.file(i.toString(), m.data)
453
+ }
454
+
455
+ media_info[i] = m.name
456
+ })
457
+
458
+ zip.file('media', JSON.stringify(media_info))
459
+
460
+ return zip.generateAsync({ type, mimeType: "application/apkg" })
461
+ })
462
+ }
463
+
464
+ /**
465
+ * Download the package as `filename` (browser only).
466
+ * Returns a Promise that resolves with the generated Blob.
467
+ */
468
+ writeToFile(filename) {
469
+ return this.generate("blob").then(content => {
470
+ saveFile(content, filename)
471
+ return content
472
+ })
473
+ }
474
+
475
+
476
+ write(db) {
477
+ const now = new Date
478
+ const models = {}
479
+ const decks = {}
480
+
481
+ // AnkiDroid failed to import subdeck, So add a Default deck
482
+ decks["1"] = {...defaultDeck, id: 1, name: "Default"}
483
+
484
+ this.decks.forEach(d => {
485
+ d.notes.forEach(n => models[n.model.props.id] = n.model.props)
486
+ decks[d.id] = {
487
+ ...defaultDeck,
488
+ id: d.id,
489
+ name: d.name,
490
+ desc: d.desc,
491
+ }
492
+ })
493
+
494
+ const col = [
495
+ null, // id
496
+ (+now / 1000) | 0, // crt
497
+ +now, // mod
498
+ +now, // scm
499
+ 11, // ver
500
+ 0, // dty
501
+ 0, // usn
502
+ 0, // ls
503
+ JSON.stringify(defaultConf), // conf
504
+ JSON.stringify(models), // models
505
+ JSON.stringify(decks), // decks
506
+ JSON.stringify({ 1: { id: 1, ...defaultDeckConf } }), // dconf
507
+ JSON.stringify({}), // tags
508
+ ]
509
+
510
+ db.prepare(`INSERT INTO col
511
+ (id, crt, mod, scm, ver, dty, usn, ls, conf, models, decks, dconf, tags)
512
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(col)
513
+
514
+ const insert_notes = db.prepare(`INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)
515
+ VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, '')`)
516
+
517
+ const insert_cards = db.prepare(`INSERT INTO cards (id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data)
518
+ VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, 0, 0, 0, 0, 0, 0, '')`)
519
+
520
+ for (const deck of this.decks) {
521
+ for (const note of deck.notes) {
522
+ var tags = note.tags == null ? '' : note.tags.join(' ')
523
+ insert_notes.run(
524
+ [
525
+ note.guid, // guid
526
+ note.model.props.id, // mid
527
+ (+now / 1000) | 0, // mod
528
+ -1, // usn
529
+ tags, // tags
530
+ note.fields.join('\x1f'), // flds
531
+ 0, // sfld
532
+ ])
533
+
534
+ var rowID = db.exec("select last_insert_rowid();")
535
+ var note_id = rowID[0]['values'][0][0];
536
+
537
+ for (const card_ord of note.cards) {
538
+ insert_cards.run(
539
+ [
540
+ note_id, // nid
541
+ deck.id, // did
542
+ card_ord, // ord
543
+ (+now / 1000) | 0, // mod
544
+ -1, // usn
545
+ 0, // type 0=new, 1=learning, 2=due
546
+ 0, // queue -1 for suspended
547
+ ])
548
+ }
549
+ }
550
+ }
551
+
552
+ insert_notes.free()
553
+ insert_cards.free()
554
+ }
555
+ }
556
+ var sha256 = (function () {
557
+ var module = { exports: {} }, define, process;
558
+ /**
559
+ * [js-sha256]{@link https://github.com/emn178/js-sha256}
560
+ *
561
+ * @version 0.9.0
562
+ * @author Chen, Yi-Cyuan [emn178@gmail.com]
563
+ * @copyright Chen, Yi-Cyuan 2014-2017
564
+ * @license MIT
565
+ */
566
+ /*jslint bitwise: true */
567
+ (function () {
568
+ 'use strict';
569
+
570
+ var ERROR = 'input is invalid type';
571
+ var WINDOW = typeof window === 'object';
572
+ var root = WINDOW ? window : {};
573
+ if (root.JS_SHA256_NO_WINDOW) {
574
+ WINDOW = false;
575
+ }
576
+ var WEB_WORKER = !WINDOW && typeof self === 'object';
577
+ var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
578
+ if (NODE_JS) {
579
+ root = global;
580
+ } else if (WEB_WORKER) {
581
+ root = self;
582
+ }
583
+ var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports;
584
+ var AMD = typeof define === 'function' && define.amd;
585
+ var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
586
+ var HEX_CHARS = '0123456789abcdef'.split('');
587
+ var EXTRA = [-2147483648, 8388608, 32768, 128];
588
+ var SHIFT = [24, 16, 8, 0];
589
+ var K = [
590
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
591
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
592
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
593
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
594
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
595
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
596
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
597
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
598
+ ];
599
+ var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
600
+
601
+ var blocks = [];
602
+
603
+ if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) {
604
+ Array.isArray = function (obj) {
605
+ return Object.prototype.toString.call(obj) === '[object Array]';
606
+ };
607
+ }
608
+
609
+ if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
610
+ ArrayBuffer.isView = function (obj) {
611
+ return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
612
+ };
613
+ }
614
+
615
+ var createOutputMethod = function (outputType, is224) {
616
+ return function (message) {
617
+ return new Sha256(is224, true).update(message)[outputType]();
618
+ };
619
+ };
620
+
621
+ var createMethod = function (is224) {
622
+ var method = createOutputMethod('hex', is224);
623
+ if (NODE_JS) {
624
+ method = nodeWrap(method, is224);
625
+ }
626
+ method.create = function () {
627
+ return new Sha256(is224);
628
+ };
629
+ method.update = function (message) {
630
+ return method.create().update(message);
631
+ };
632
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
633
+ var type = OUTPUT_TYPES[i];
634
+ method[type] = createOutputMethod(type, is224);
635
+ }
636
+ return method;
637
+ };
638
+
639
+ var nodeWrap = function (method, is224) {
640
+ var crypto = eval("require('crypto')");
641
+ var Buffer = eval("require('buffer').Buffer");
642
+ var algorithm = is224 ? 'sha224' : 'sha256';
643
+ var nodeMethod = function (message) {
644
+ if (typeof message === 'string') {
645
+ return crypto.createHash(algorithm).update(message, 'utf8').digest('hex');
646
+ } else {
647
+ if (message === null || message === undefined) {
648
+ throw new Error(ERROR);
649
+ } else if (message.constructor === ArrayBuffer) {
650
+ message = new Uint8Array(message);
651
+ }
652
+ }
653
+ if (Array.isArray(message) || ArrayBuffer.isView(message) ||
654
+ message.constructor === Buffer) {
655
+ return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
656
+ } else {
657
+ return method(message);
658
+ }
659
+ };
660
+ return nodeMethod;
661
+ };
662
+
663
+ var createHmacOutputMethod = function (outputType, is224) {
664
+ return function (key, message) {
665
+ return new HmacSha256(key, is224, true).update(message)[outputType]();
666
+ };
667
+ };
668
+
669
+ var createHmacMethod = function (is224) {
670
+ var method = createHmacOutputMethod('hex', is224);
671
+ method.create = function (key) {
672
+ return new HmacSha256(key, is224);
673
+ };
674
+ method.update = function (key, message) {
675
+ return method.create(key).update(message);
676
+ };
677
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
678
+ var type = OUTPUT_TYPES[i];
679
+ method[type] = createHmacOutputMethod(type, is224);
680
+ }
681
+ return method;
682
+ };
683
+
684
+ function Sha256(is224, sharedMemory) {
685
+ if (sharedMemory) {
686
+ blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
687
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
688
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
689
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
690
+ this.blocks = blocks;
691
+ } else {
692
+ this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
693
+ }
694
+
695
+ if (is224) {
696
+ this.h0 = 0xc1059ed8;
697
+ this.h1 = 0x367cd507;
698
+ this.h2 = 0x3070dd17;
699
+ this.h3 = 0xf70e5939;
700
+ this.h4 = 0xffc00b31;
701
+ this.h5 = 0x68581511;
702
+ this.h6 = 0x64f98fa7;
703
+ this.h7 = 0xbefa4fa4;
704
+ } else { // 256
705
+ this.h0 = 0x6a09e667;
706
+ this.h1 = 0xbb67ae85;
707
+ this.h2 = 0x3c6ef372;
708
+ this.h3 = 0xa54ff53a;
709
+ this.h4 = 0x510e527f;
710
+ this.h5 = 0x9b05688c;
711
+ this.h6 = 0x1f83d9ab;
712
+ this.h7 = 0x5be0cd19;
713
+ }
714
+
715
+ this.block = this.start = this.bytes = this.hBytes = 0;
716
+ this.finalized = this.hashed = false;
717
+ this.first = true;
718
+ this.is224 = is224;
719
+ }
720
+
721
+ Sha256.prototype.update = function (message) {
722
+ if (this.finalized) {
723
+ return;
724
+ }
725
+ var notString, type = typeof message;
726
+ if (type !== 'string') {
727
+ if (type === 'object') {
728
+ if (message === null) {
729
+ throw new Error(ERROR);
730
+ } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
731
+ message = new Uint8Array(message);
732
+ } else if (!Array.isArray(message)) {
733
+ if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
734
+ throw new Error(ERROR);
735
+ }
736
+ }
737
+ } else {
738
+ throw new Error(ERROR);
739
+ }
740
+ notString = true;
741
+ }
742
+ var code, index = 0, i, length = message.length, blocks = this.blocks;
743
+
744
+ while (index < length) {
745
+ if (this.hashed) {
746
+ this.hashed = false;
747
+ blocks[0] = this.block;
748
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
749
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
750
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
751
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
752
+ }
753
+
754
+ if (notString) {
755
+ for (i = this.start; index < length && i < 64; ++index) {
756
+ blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
757
+ }
758
+ } else {
759
+ for (i = this.start; index < length && i < 64; ++index) {
760
+ code = message.charCodeAt(index);
761
+ if (code < 0x80) {
762
+ blocks[i >> 2] |= code << SHIFT[i++ & 3];
763
+ } else if (code < 0x800) {
764
+ blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
765
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
766
+ } else if (code < 0xd800 || code >= 0xe000) {
767
+ blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
768
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
769
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
770
+ } else {
771
+ code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
772
+ blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
773
+ blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
774
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
775
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
776
+ }
777
+ }
778
+ }
779
+
780
+ this.lastByteIndex = i;
781
+ this.bytes += i - this.start;
782
+ if (i >= 64) {
783
+ this.block = blocks[16];
784
+ this.start = i - 64;
785
+ this.hash();
786
+ this.hashed = true;
787
+ } else {
788
+ this.start = i;
789
+ }
790
+ }
791
+ if (this.bytes > 4294967295) {
792
+ this.hBytes += this.bytes / 4294967296 << 0;
793
+ this.bytes = this.bytes % 4294967296;
794
+ }
795
+ return this;
796
+ };
797
+
798
+ Sha256.prototype.finalize = function () {
799
+ if (this.finalized) {
800
+ return;
801
+ }
802
+ this.finalized = true;
803
+ var blocks = this.blocks, i = this.lastByteIndex;
804
+ blocks[16] = this.block;
805
+ blocks[i >> 2] |= EXTRA[i & 3];
806
+ this.block = blocks[16];
807
+ if (i >= 56) {
808
+ if (!this.hashed) {
809
+ this.hash();
810
+ }
811
+ blocks[0] = this.block;
812
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
813
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
814
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
815
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
816
+ }
817
+ blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
818
+ blocks[15] = this.bytes << 3;
819
+ this.hash();
820
+ };
821
+
822
+ Sha256.prototype.hash = function () {
823
+ var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6,
824
+ h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc;
825
+
826
+ for (j = 16; j < 64; ++j) {
827
+ // rightrotate
828
+ t1 = blocks[j - 15];
829
+ s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
830
+ t1 = blocks[j - 2];
831
+ s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
832
+ blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
833
+ }
834
+
835
+ bc = b & c;
836
+ for (j = 0; j < 64; j += 4) {
837
+ if (this.first) {
838
+ if (this.is224) {
839
+ ab = 300032;
840
+ t1 = blocks[0] - 1413257819;
841
+ h = t1 - 150054599 << 0;
842
+ d = t1 + 24177077 << 0;
843
+ } else {
844
+ ab = 704751109;
845
+ t1 = blocks[0] - 210244248;
846
+ h = t1 - 1521486534 << 0;
847
+ d = t1 + 143694565 << 0;
848
+ }
849
+ this.first = false;
850
+ } else {
851
+ s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
852
+ s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
853
+ ab = a & b;
854
+ maj = ab ^ (a & c) ^ bc;
855
+ ch = (e & f) ^ (~e & g);
856
+ t1 = h + s1 + ch + K[j] + blocks[j];
857
+ t2 = s0 + maj;
858
+ h = d + t1 << 0;
859
+ d = t1 + t2 << 0;
860
+ }
861
+ s0 = ((d >>> 2) | (d << 30)) ^ ((d >>> 13) | (d << 19)) ^ ((d >>> 22) | (d << 10));
862
+ s1 = ((h >>> 6) | (h << 26)) ^ ((h >>> 11) | (h << 21)) ^ ((h >>> 25) | (h << 7));
863
+ da = d & a;
864
+ maj = da ^ (d & b) ^ ab;
865
+ ch = (h & e) ^ (~h & f);
866
+ t1 = g + s1 + ch + K[j + 1] + blocks[j + 1];
867
+ t2 = s0 + maj;
868
+ g = c + t1 << 0;
869
+ c = t1 + t2 << 0;
870
+ s0 = ((c >>> 2) | (c << 30)) ^ ((c >>> 13) | (c << 19)) ^ ((c >>> 22) | (c << 10));
871
+ s1 = ((g >>> 6) | (g << 26)) ^ ((g >>> 11) | (g << 21)) ^ ((g >>> 25) | (g << 7));
872
+ cd = c & d;
873
+ maj = cd ^ (c & a) ^ da;
874
+ ch = (g & h) ^ (~g & e);
875
+ t1 = f + s1 + ch + K[j + 2] + blocks[j + 2];
876
+ t2 = s0 + maj;
877
+ f = b + t1 << 0;
878
+ b = t1 + t2 << 0;
879
+ s0 = ((b >>> 2) | (b << 30)) ^ ((b >>> 13) | (b << 19)) ^ ((b >>> 22) | (b << 10));
880
+ s1 = ((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7));
881
+ bc = b & c;
882
+ maj = bc ^ (b & d) ^ cd;
883
+ ch = (f & g) ^ (~f & h);
884
+ t1 = e + s1 + ch + K[j + 3] + blocks[j + 3];
885
+ t2 = s0 + maj;
886
+ e = a + t1 << 0;
887
+ a = t1 + t2 << 0;
888
+ }
889
+
890
+ this.h0 = this.h0 + a << 0;
891
+ this.h1 = this.h1 + b << 0;
892
+ this.h2 = this.h2 + c << 0;
893
+ this.h3 = this.h3 + d << 0;
894
+ this.h4 = this.h4 + e << 0;
895
+ this.h5 = this.h5 + f << 0;
896
+ this.h6 = this.h6 + g << 0;
897
+ this.h7 = this.h7 + h << 0;
898
+ };
899
+
900
+ Sha256.prototype.hex = function () {
901
+ this.finalize();
902
+
903
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
904
+ h6 = this.h6, h7 = this.h7;
905
+
906
+ var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
907
+ HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
908
+ HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
909
+ HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
910
+ HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
911
+ HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
912
+ HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
913
+ HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
914
+ HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
915
+ HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
916
+ HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
917
+ HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
918
+ HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
919
+ HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
920
+ HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
921
+ HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
922
+ HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
923
+ HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
924
+ HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
925
+ HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
926
+ HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
927
+ HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
928
+ HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
929
+ HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
930
+ HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
931
+ HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
932
+ HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
933
+ HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
934
+ if (!this.is224) {
935
+ hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
936
+ HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
937
+ HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
938
+ HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
939
+ }
940
+ return hex;
941
+ };
942
+
943
+ Sha256.prototype.toString = Sha256.prototype.hex;
944
+
945
+ Sha256.prototype.digest = function () {
946
+ this.finalize();
947
+
948
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
949
+ h6 = this.h6, h7 = this.h7;
950
+
951
+ var arr = [
952
+ (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
953
+ (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
954
+ (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
955
+ (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
956
+ (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
957
+ (h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
958
+ (h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
959
+ ];
960
+ if (!this.is224) {
961
+ arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
962
+ }
963
+ return arr;
964
+ };
965
+
966
+ Sha256.prototype.array = Sha256.prototype.digest;
967
+
968
+ Sha256.prototype.arrayBuffer = function () {
969
+ this.finalize();
970
+
971
+ var buffer = new ArrayBuffer(this.is224 ? 28 : 32);
972
+ var dataView = new DataView(buffer);
973
+ dataView.setUint32(0, this.h0);
974
+ dataView.setUint32(4, this.h1);
975
+ dataView.setUint32(8, this.h2);
976
+ dataView.setUint32(12, this.h3);
977
+ dataView.setUint32(16, this.h4);
978
+ dataView.setUint32(20, this.h5);
979
+ dataView.setUint32(24, this.h6);
980
+ if (!this.is224) {
981
+ dataView.setUint32(28, this.h7);
982
+ }
983
+ return buffer;
984
+ };
985
+
986
+ function HmacSha256(key, is224, sharedMemory) {
987
+ var i, type = typeof key;
988
+ if (type === 'string') {
989
+ var bytes = [], length = key.length, index = 0, code;
990
+ for (i = 0; i < length; ++i) {
991
+ code = key.charCodeAt(i);
992
+ if (code < 0x80) {
993
+ bytes[index++] = code;
994
+ } else if (code < 0x800) {
995
+ bytes[index++] = (0xc0 | (code >> 6));
996
+ bytes[index++] = (0x80 | (code & 0x3f));
997
+ } else if (code < 0xd800 || code >= 0xe000) {
998
+ bytes[index++] = (0xe0 | (code >> 12));
999
+ bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
1000
+ bytes[index++] = (0x80 | (code & 0x3f));
1001
+ } else {
1002
+ code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
1003
+ bytes[index++] = (0xf0 | (code >> 18));
1004
+ bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
1005
+ bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
1006
+ bytes[index++] = (0x80 | (code & 0x3f));
1007
+ }
1008
+ }
1009
+ key = bytes;
1010
+ } else {
1011
+ if (type === 'object') {
1012
+ if (key === null) {
1013
+ throw new Error(ERROR);
1014
+ } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
1015
+ key = new Uint8Array(key);
1016
+ } else if (!Array.isArray(key)) {
1017
+ if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
1018
+ throw new Error(ERROR);
1019
+ }
1020
+ }
1021
+ } else {
1022
+ throw new Error(ERROR);
1023
+ }
1024
+ }
1025
+
1026
+ if (key.length > 64) {
1027
+ key = (new Sha256(is224, true)).update(key).array();
1028
+ }
1029
+
1030
+ var oKeyPad = [], iKeyPad = [];
1031
+ for (i = 0; i < 64; ++i) {
1032
+ var b = key[i] || 0;
1033
+ oKeyPad[i] = 0x5c ^ b;
1034
+ iKeyPad[i] = 0x36 ^ b;
1035
+ }
1036
+
1037
+ Sha256.call(this, is224, sharedMemory);
1038
+
1039
+ this.update(iKeyPad);
1040
+ this.oKeyPad = oKeyPad;
1041
+ this.inner = true;
1042
+ this.sharedMemory = sharedMemory;
1043
+ }
1044
+ HmacSha256.prototype = new Sha256();
1045
+
1046
+ HmacSha256.prototype.finalize = function () {
1047
+ Sha256.prototype.finalize.call(this);
1048
+ if (this.inner) {
1049
+ this.inner = false;
1050
+ var innerHash = this.array();
1051
+ Sha256.call(this, this.is224, this.sharedMemory);
1052
+ this.update(this.oKeyPad);
1053
+ this.update(innerHash);
1054
+ Sha256.prototype.finalize.call(this);
1055
+ }
1056
+ };
1057
+
1058
+ var exports = createMethod();
1059
+ exports.sha256 = exports;
1060
+ exports.sha224 = createMethod(true);
1061
+ exports.sha256.hmac = createHmacMethod();
1062
+ exports.sha224.hmac = createHmacMethod(true);
1063
+
1064
+ if (COMMON_JS) {
1065
+ module.exports = exports;
1066
+ } else {
1067
+ root.sha256 = exports.sha256;
1068
+ root.sha224 = exports.sha224;
1069
+ if (AMD) {
1070
+ define(function () {
1071
+ return exports;
1072
+ });
1073
+ }
1074
+ }
1075
+ })();
1076
+ return module.exports;
1077
+ })();
1078
+ /*
1079
+ * [genanki]{@link https://github.com/kerrickstaley/genanki}
1080
+ * @copyright Copyright (c) Kerrick Staley 2021
1081
+ * @license The MIT License
1082
+ */
1083
+
1084
+ const APKG_SCHEMA = `
1085
+ PRAGMA foreign_keys=OFF;
1086
+ BEGIN TRANSACTION;
1087
+
1088
+ CREATE TABLE col (
1089
+ id integer primary key,
1090
+ crt integer not null,
1091
+ mod integer not null,
1092
+ scm integer not null,
1093
+ ver integer not null,
1094
+ dty integer not null,
1095
+ usn integer not null,
1096
+ ls integer not null,
1097
+ conf text not null,
1098
+ models text not null,
1099
+ decks text not null,
1100
+ dconf text not null,
1101
+ tags text not null
1102
+ );
1103
+ CREATE TABLE notes (
1104
+ id integer primary key, /* 0 */
1105
+ guid text not null, /* 1 */
1106
+ mid integer not null, /* 2 */
1107
+ mod integer not null, /* 3 */
1108
+ usn integer not null, /* 4 */
1109
+ tags text not null, /* 5 */
1110
+ flds text not null, /* 6 */
1111
+ sfld integer not null, /* 7 */
1112
+ csum integer not null, /* 8 */
1113
+ flags integer not null, /* 9 */
1114
+ data text not null /* 10 */
1115
+ );
1116
+ CREATE TABLE cards (
1117
+ id integer primary key, /* 0 */
1118
+ nid integer not null, /* 1 */
1119
+ did integer not null, /* 2 */
1120
+ ord integer not null, /* 3 */
1121
+ mod integer not null, /* 4 */
1122
+ usn integer not null, /* 5 */
1123
+ type integer not null, /* 6 */
1124
+ queue integer not null, /* 7 */
1125
+ due integer not null, /* 8 */
1126
+ ivl integer not null, /* 9 */
1127
+ factor integer not null, /* 10 */
1128
+ reps integer not null, /* 11 */
1129
+ lapses integer not null, /* 12 */
1130
+ left integer not null, /* 13 */
1131
+ odue integer not null, /* 14 */
1132
+ odid integer not null, /* 15 */
1133
+ flags integer not null, /* 16 */
1134
+ data text not null /* 17 */
1135
+ );
1136
+ CREATE TABLE revlog (
1137
+ id integer primary key,
1138
+ cid integer not null,
1139
+ usn integer not null,
1140
+ ease integer not null,
1141
+ ivl integer not null,
1142
+ lastIvl integer not null,
1143
+ factor integer not null,
1144
+ time integer not null,
1145
+ type integer not null
1146
+ );
1147
+ CREATE TABLE graves (
1148
+ usn integer not null,
1149
+ oid integer not null,
1150
+ type integer not null
1151
+ );
1152
+ CREATE INDEX ix_notes_usn on notes (usn);
1153
+ CREATE INDEX ix_cards_usn on cards (usn);
1154
+ CREATE INDEX ix_revlog_usn on revlog (usn);
1155
+ CREATE INDEX ix_cards_nid on cards (nid);
1156
+ CREATE INDEX ix_cards_sched on cards (did, queue, due);
1157
+ CREATE INDEX ix_revlog_cid on revlog (cid);
1158
+ CREATE INDEX ix_notes_csum on notes (csum);
1159
+ COMMIT;
1160
+ `;
1161
+ var APKG_COL = `
1162
+ INSERT INTO col VALUES(
1163
+ null,
1164
+ 1411124400,
1165
+ 1425279151694,
1166
+ 1425279151690,
1167
+ 11,
1168
+ 0,
1169
+ 0,
1170
+ 0,
1171
+ '{
1172
+ "activeDecks": [
1173
+ 1
1174
+ ],
1175
+ "addToCur": true,
1176
+ "collapseTime": 1200,
1177
+ "curDeck": 1,
1178
+ "curModel": "1425279151691",
1179
+ "dueCounts": true,
1180
+ "estTimes": true,
1181
+ "newBury": true,
1182
+ "newSpread": 0,
1183
+ "nextPos": 1,
1184
+ "sortBackwards": false,
1185
+ "sortType": "noteFld",
1186
+ "timeLim": 0
1187
+ }',
1188
+ '{}',
1189
+ '{
1190
+ "1": {
1191
+ "collapsed": false,
1192
+ "conf": 1,
1193
+ "desc": "",
1194
+ "dyn": 0,
1195
+ "extendNew": 10,
1196
+ "extendRev": 50,
1197
+ "id": 1,
1198
+ "lrnToday": [
1199
+ 0,
1200
+ 0
1201
+ ],
1202
+ "mod": 1425279151,
1203
+ "name": "Default",
1204
+ "newToday": [
1205
+ 0,
1206
+ 0
1207
+ ],
1208
+ "revToday": [
1209
+ 0,
1210
+ 0
1211
+ ],
1212
+ "timeToday": [
1213
+ 0,
1214
+ 0
1215
+ ],
1216
+ "usn": 0
1217
+ }
1218
+ }',
1219
+ '{
1220
+ "1": {
1221
+ "autoplay": true,
1222
+ "id": 1,
1223
+ "lapse": {
1224
+ "delays": [
1225
+ 10
1226
+ ],
1227
+ "leechAction": 0,
1228
+ "leechFails": 8,
1229
+ "minInt": 1,
1230
+ "mult": 0
1231
+ },
1232
+ "maxTaken": 60,
1233
+ "mod": 0,
1234
+ "name": "Default",
1235
+ "new": {
1236
+ "bury": true,
1237
+ "delays": [
1238
+ 1,
1239
+ 10
1240
+ ],
1241
+ "initialFactor": 2500,
1242
+ "ints": [
1243
+ 1,
1244
+ 4,
1245
+ 7
1246
+ ],
1247
+ "order": 1,
1248
+ "perDay": 20,
1249
+ "separate": true
1250
+ },
1251
+ "replayq": true,
1252
+ "rev": {
1253
+ "bury": true,
1254
+ "ease4": 1.3,
1255
+ "fuzz": 0.05,
1256
+ "ivlFct": 1,
1257
+ "maxIvl": 36500,
1258
+ "minSpace": 1,
1259
+ "perDay": 100
1260
+ },
1261
+ "timer": 0,
1262
+ "usn": 0
1263
+ }
1264
+ }',
1265
+ '{}'
1266
+ );
1267
+ `;
1268
+
1269
+ // namespace for the <script> build, e.g. new genanki.Model({...})
1270
+ var genanki = { Model, ClozeModel, Deck, Note, Package, setSqlJs, ankiHash, sha256, MODEL_STD, MODEL_CLOZE }