-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
better-bibtex.ts
897 lines (765 loc) · 34 KB
/
better-bibtex.ts
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/* eslint-disable prefer-rest-params */
import { is7 } from './client'
if (is7) Components.utils.importGlobalProperties(['FormData'])
Components.utils.import('resource://gre/modules/FileUtils.jsm')
declare const FileUtils: any
declare const __estrace: any // eslint-disable-line no-underscore-dangle
import type { XUL } from '../typings/xul'
import { DebugLog } from 'zotero-plugin/debug-log'
DebugLog.register('Better BibTeX', ['extensions.zotero.translators.better-bibtex.'])
import { icons } from './icons'
import { prompt } from './prompt'
import { Elements } from './create-element'
import { newZoteroPane } from './ZoteroPane'
import { newZoteroItemPane } from './ZoteroItemPane'
import { ExportOptions } from './ExportOptions'
import { PrefPane } from './Preferences'
import { ErrorReport } from './ErrorReport'
import { patch as $patch$, unpatch as $unpatch$ } from './monkey-patch'
import { clean_pane_persist } from './clean_pane_persist'
import { flash } from './flash'
import { Deferred } from './deferred'
import { orchestrator } from './orchestrator'
import type { Reason } from './bootstrap'
import { Preference } from './prefs' // needs to be here early, initializes the prefs observer
require('./pull-export') // just require, initializes the pull-export end points
require('./json-rpc') // just require, initializes the json-rpc end point
import { AUXScanner } from './aux-scanner'
import * as Extra from './extra'
import { sentenceCase, HTMLParser, HTMLParserOptions } from './text'
import { log } from './logger'
import { Events } from './events'
import { Translators } from './translators'
import { DB as Cache } from './db/cache'
import { Serializer } from './serializer'
import { AutoExport, SQL as AE } from './auto-export'
import { KeyManager } from './key-manager'
import { TestSupport } from './test-support'
import * as l10n from './l10n'
import * as CSL from 'citeproc'
import { generateBibLaTeX } from '../translators/bibtex/biblatex'
import { generateBibTeX, parseBibTeX } from '../translators/bibtex/bibtex'
import { generateCSLYAML, parseCSLYAML } from '../translators/csl/yaml'
import { generateCSLJSON } from '../translators/csl/json'
import { Translation } from '../translators/lib/translator'
// need coroutine here because Zotero calls '.done()' on the nonexistent! result, added automagically by bluebird
if (!is7) {
$patch$(Zotero, 'shutdown', original => Zotero.Promise.coroutine(function* () { // eslint-disable-line @typescript-eslint/no-unsafe-return
try {
yield orchestrator.shutdown(Zotero.BetterBibTeX.uninstalled ? 'ADDON_UNINSTALL' : 'APP_SHUTDOWN')
}
catch (err) {
log.error('BBT shutdown: shutdown failed', err)
}
yield original.apply(this, arguments)
}))
}
declare const AddonManager: any
if (!is7) {
Components.utils.import('resource://gre/modules/AddonManager.jsm')
AddonManager.addAddonListener({
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
onUninstalling(addon: { id: string }) {
if (addon.id === 'better-bibtex@iris-advies.com') Zotero.BetterBibTeX.uninstalled = true
},
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
onDisabling(addon: { id: string }) {
if (addon.id === 'better-bibtex@iris-advies.com') Zotero.BetterBibTeX.uninstalled = true
},
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
onOperationCancelled(addon: { id: string, pendingOperations: number }) {
if (addon.id !== 'better-bibtex@iris-advies.com') return null
// eslint-disable-next-line no-bitwise
if (addon.pendingOperations & (AddonManager.PENDING_UNINSTALL | AddonManager.PENDING_DISABLE)) Zotero.BetterBibTeX.uninstalled = false
},
})
}
// MONKEY PATCHES
// zotero moved itemToCSLJSON to Zotero.Utilities.Item, jurism for the moment keeps it on ZU
$patch$(Zotero.Utilities.Item?.itemToCSLJSON ? Zotero.Utilities.Item : Zotero.Utilities, 'itemToCSLJSON', original => function itemToCSLJSON(zoteroItem: { itemID: any }) {
const cslItem = original.apply(this, arguments)
try {
if (typeof Zotero.Item !== 'undefined' && !(zoteroItem instanceof Zotero.Item)) {
const citekey = Zotero.BetterBibTeX.KeyManager.get(zoteroItem.itemID)
if (citekey) {
cslItem['citation-key'] = citekey.citationKey
}
}
}
catch (err) {
log.error('failed patching CSL-JSON:', err)
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return cslItem
})
// https://github.com/retorquere/zotero-better-bibtex/issues/1221
$patch$(Zotero.Items, 'merge', original => async function Zotero_Items_merge(item: ZoteroItem, otherItems: ZoteroItem[]) {
try {
// log.verbose = true
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
const merge = {
citationKey: Preference.extraMergeCitekeys,
tex: Preference.extraMergeTeX,
kv: Preference.extraMergeCSL,
}
if (merge.citationKey || merge.tex || merge.kv) {
const extra = Extra.get(item.getField('extra') as string, 'zotero', { citationKey: merge.citationKey, aliases: merge.citationKey, tex: merge.tex, kv: merge.kv })
if (!extra.extraFields.citationKey) { // why is the citationkey stripped from extra before we get to this point?!
const pinned = Zotero.BetterBibTeX.KeyManager.get(item.id)
if (pinned.pinned) extra.extraFields.citationKey = pinned.citationKey
}
// get citekeys of other items
if (merge.citationKey) {
const otherIDs = otherItems.map(i => i.id)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
extra.extraFields.aliases = [...extra.extraFields.aliases, ...Zotero.BetterBibTeX.KeyManager.find({ where: { itemID: { in: otherIDs } } }).map(key => key.citationKey)]
}
// add any aliases they were already holding
for (const i of otherItems) {
const otherExtra = Extra.get(i.getField('extra') as string, 'zotero', { citationKey: merge.citationKey, aliases: merge.citationKey, tex: merge.tex, kv: merge.kv })
if (merge.citationKey) {
extra.extraFields.aliases = [...extra.extraFields.aliases, ...otherExtra.extraFields.aliases]
if (otherExtra.extraFields.citationKey) extra.extraFields.aliases.push(otherExtra.extraFields.citationKey)
}
if (merge.tex) {
for (const [name, value] of Object.entries(otherExtra.extraFields.tex)) {
if (!extra.extraFields.tex[name]) extra.extraFields.tex[name] = value
}
}
if (merge.kv) {
for (const [name, value] of Object.entries(otherExtra.extraFields.kv)) {
const existing = extra.extraFields.kv[name]
if (!existing) {
extra.extraFields.kv[name] = value
}
else if (Array.isArray(existing) && Array.isArray(value)) {
for (const creator in value) {
if (!existing.includes(creator)) existing.push(creator)
}
}
}
}
}
if (merge.citationKey) {
const citekey = Zotero.BetterBibTeX.KeyManager.get(item.id).citationKey
extra.extraFields.aliases = extra.extraFields.aliases.filter(alias => alias !== citekey)
}
item.setField('extra', Extra.set(extra.extra, {
// keep pinned if it was before
citationKey: merge.citationKey ? extra.extraFields.citationKey : undefined,
aliases: merge.citationKey ? extra.extraFields.aliases : undefined,
tex: merge.tex ? extra.extraFields.tex : undefined,
kv: merge.kv ? extra.extraFields.kv : undefined,
}))
}
}
catch (err) {
log.error('Zotero.Items.merge:', err)
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return await original.apply(this, arguments)
})
// https://github.com/retorquere/zotero-better-bibtex/issues/769
function parseLibraryKeyFromCitekey(libraryKey) {
try {
const decoded = decodeURIComponent(libraryKey)
if (decoded[0] === '@') {
const item = Zotero.BetterBibTeX.KeyManager.first({ where: { citationKey: decoded.substring(1) } })
return item ? { libraryID: item.libraryID, key: item.itemKey } : false
}
const m = decoded.match(/^bbt:(?:{([0-9]+)})?(.*)/)
if (m) {
const [_libraryID, citationKey] = m.slice(1)
const libraryID: number = (!_libraryID || _libraryID === '1') ? Zotero.Libraries.userLibraryID : parseInt(_libraryID)
const item = Zotero.BetterBibTeX.KeyManager.first({ where: { libraryID, citationKey }})
return item ? { libraryID: item.libraryID, key: item.itemKey } : false
}
}
catch (err) {
log.error('parseLibraryKeyFromCitekey:', libraryKey, err)
}
return null
}
$patch$(Zotero.API, 'getResultsFromParams', original => function Zotero_API_getResultsFromParams(params: Record<string, any>) {
try {
if (params.itemKey) {
const libraryID = params.libraryID || Zotero.Libraries.userLibraryID
params.itemKey = params.itemKey.map((itemKey: string) => {
const m = itemKey.match(/^(bbt:|@)(.+)/)
if (!m) return itemKey
const citekey = Zotero.BetterBibTeX.KeyManager.first({ where: { libraryID, citationKey: m[2] }})
return citekey?.itemKey || itemKey
})
}
}
catch (err) {
log.debug('getResultsFromParams', params, err)
}
return original.apply(this, arguments) as Record<string, any>
})
if (typeof Zotero.DataObjects.prototype.parseLibraryKeyHash === 'function') {
$patch$(Zotero.DataObjects.prototype, 'parseLibraryKeyHash', original => function Zotero_DataObjects_prototype_parseLibraryKeyHash(libraryKey: string) {
const item = parseLibraryKeyFromCitekey(libraryKey)
if (item !== null) return item
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return original.apply(this, arguments)
})
}
if (typeof Zotero.DataObjects.prototype.parseLibraryKey === 'function') {
$patch$(Zotero.DataObjects.prototype, 'parseLibraryKey', original => function Zotero_DataObjects_prototype_parseLibraryKey(libraryKey: string) {
const item = parseLibraryKeyFromCitekey(libraryKey)
if (item) return item
if (item === false) return { libraryID: Zotero.Libraries.userLibraryID, key: undefined }
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return original.apply(this, arguments)
})
}
// otherwise the display of the citekey in the item pane flames out
$patch$(Zotero.ItemFields, 'isFieldOfBase', original => function Zotero_ItemFields_isFieldOfBase(field: string, _baseField: any) {
if (field === 'citationKey') return false
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return original.apply(this, arguments)
})
// because the zotero item editor does not check whether a textbox is read-only. *sigh*
$patch$(Zotero.Item.prototype, 'setField', original => function Zotero_Item_prototype_setField(field: string, value: string | undefined, _loadIn: any) {
if (field === 'citationKey') {
if (Zotero.BetterBibTeX.ready.pending) return false
const citekey = Zotero.BetterBibTeX.KeyManager.get(this.id)
if (citekey.retry) return false
if (typeof value !== 'string') value = ''
if (!value) {
this.setField('extra', Extra.get(this.getField('extra') as string, 'zotero', { citationKey: true }).extra)
Zotero.BetterBibTeX.KeyManager.update(this)
Zotero.Notifier.trigger('modify', 'item', [this.id])
return true
}
else if (value !== citekey.citationKey) {
this.setField('extra', Extra.set(this.getField('extra'), { citationKey: value }))
// citekey.pinned = true
// citekey.citekey = value
// Zotero.BetterBibTeX.KeyManager.keys.update(citekey)
return true
}
else {
return false
}
}
else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return original.apply(this, arguments)
}
})
// To show the citekey in the item list
$patch$(Zotero.Item.prototype, 'getField', original => function Zotero_Item_prototype_getField(field: any, unformatted: any, includeBaseMapped: any) {
try {
if (field === 'citationKey' || field === 'citekey') {
if (Zotero.BetterBibTeX.ready.pending) return '' // eslint-disable-line @typescript-eslint/no-use-before-define
return Zotero.BetterBibTeX.KeyManager.get(this.id).citationKey
}
}
catch (err) {
log.error('patched getField:', {field, unformatted, includeBaseMapped, err})
return ''
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return original.apply(this, arguments) as string
})
// #1579
$patch$(Zotero.Item.prototype, 'clone', original => function Zotero_Item_prototype_clone(libraryID: number, options = {}) {
const item = original.apply(this, arguments)
try {
if ((typeof libraryID === 'undefined' || this.libraryID === libraryID) && item.isRegularItem()) {
item.setField('extra', item.getField('extra').replace(/(^|\n)citation key:[^\n]*(\n|$)/i, '\n').trim())
}
}
catch (err) {
log.error('patched clone:', {libraryID, options, err})
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return item
})
if (!is7) {
const itemTree = require('zotero/itemTree')
$patch$(itemTree.prototype, 'getColumns', original => function Zotero_ItemTree_prototype_getColumns() {
const columns = original.apply(this, arguments)
try {
const insertAfter: number = columns.findIndex(column => column.dataKey === 'title')
columns.splice(insertAfter + 1, 0, {
dataKey: 'citationKey',
label: l10n.localize('better-bibtex_zotero-pane_column_citekey'),
flex: '1',
zoteroPersist: new Set(['width', 'ordinal', 'hidden', 'sortActive', 'sortDirection']),
})
}
catch (err) {
log.debug('could not install itemtree column')
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return columns
})
$patch$(itemTree.prototype, '_renderCell', original => function Zotero_ItemTree_prototype_renderCell(index, data, col) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (col.dataKey !== 'citationKey') return original.apply(this, arguments)
const item = this.getRow(index).ref
const citekey = Zotero.BetterBibTeX.KeyManager.get(item.id)
const doc: Document = this.window.document
const icon = doc.createElementNS('http://www.w3.org/1999/xhtml', 'span')
icon.innerText = citekey.pinned ? icons.pin : ''
// icon.className = 'icon icon-bg cell-icon'
const text = doc.createElementNS('http://www.w3.org/1999/xhtml', 'span')
text.className = 'cell-text'
text.innerText = data
const cell = doc.createElementNS('http://www.w3.org/1999/xhtml', 'span')
cell.className = `cell ${col.className}`
cell.append(text, icon)
return cell
})
}
import * as CAYW from './cayw'
$patch$(Zotero.Integration, 'getApplication', original => function Zotero_Integration_getApplication(agent: string, _command: any, _docId: any) {
if (agent === 'BetterBibTeX') return CAYW.Application
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return original.apply(this, arguments)
})
import * as DateParser from './dateparser'
import type { ParsedDate } from './dateparser'
Zotero.Translate.Export.prototype.Sandbox.BetterBibTeX = {
clientName: Zotero.clientName,
strToISO(_sandbox: any, str: string) { return DateParser.strToISO(str) },
getContents(_sandbox: any, path: string): string { return Zotero.BetterBibTeX.getContents(path) },
generateBibLaTeX(_sandbox: any, translation: Translation) { generateBibLaTeX(translation) },
generateBibTeX(_sandbox: any, translation: Translation) { generateBibTeX(translation) },
generateCSLYAML(_sandbox: any, translation: Translation) { generateCSLYAML(translation) },
generateCSLJSON(_sandbox: any, translation: Translation) { generateCSLJSON(translation) },
/*
cacheFetch(sandbox: { translator: { label: string }[] }, itemID: number, options: { exportNotes: boolean, useJournalAbbreviation: boolean }, prefs: any) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Cache.fetch(sandbox.translator[0].label, itemID, options, prefs)
},
cacheStore(sandbox: { translator: { label: string }[] }, itemID: number, options: { exportNotes: boolean, useJournalAbbreviation: boolean }, prefs: any, entry: any, metadata: any) {
return Cache.store(sandbox.translator[0].label, itemID, options, prefs, entry, metadata)
},
*/
parseDate(_sandbox: any, date: string): ParsedDate { return DateParser.parse(date) },
}
Zotero.Translate.Import.prototype.Sandbox.BetterBibTeX = {
clientName: Zotero.clientName,
parseHTML(_sandbox: any, text: { toString: () => any }, options: HTMLParserOptions) {
options = {
...options,
exportBraceProtection: Preference.exportBraceProtection,
csquotes: Preference.csquotes,
exportTitleCase: Preference.exportTitleCase,
}
return HTMLParser.parse(text.toString(), options)
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
parseDate(_sandbox: any, date: string): ParsedDate { return DateParser.parse(date) },
async parseBibTeX(_sandbox: any, input: string, translation: Translation) { return parseBibTeX(input, translation) },
parseCSLYAML(_sandbox: any, input: string): any { return parseCSLYAML(input) },
}
$patch$(Zotero.Utilities.Internal, 'itemToExportFormat', original => function Zotero_Utilities_Internal_itemToExportFormat(zoteroItem: any, _legacy: any, _skipChildItems: any) {
const serialized = original.apply(this, arguments)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Serializer.enrich(serialized, zoteroItem)
})
// so BBT-JSON can be imported without extra-field meddling
$patch$(Zotero.Utilities.Internal, 'extractExtraFields', original => function Zotero_Utilities_Internal_extractExtraFields(extra: string, _item: any, _additionalFields: any) {
if (extra && extra.startsWith('\x1BBBT\x1B')) {
return { itemType: null, fields: new Map(), creators: [], extra: extra.replace('\x1BBBT\x1B', '') }
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return original.apply(this, arguments)
})
$patch$(Zotero.Translate.Export.prototype, 'translate', original => function Zotero_Translate_Export_prototype_translate() {
try {
/* requested translator */
let translatorID = this.translator[0]
if (translatorID.translatorID) translatorID = translatorID.translatorID
const translator = Translators.byId[translatorID]
const displayOptions = this._displayOptions || {}
if (translator) {
if (this.location) {
if (displayOptions.exportFileData) { // when exporting file data, the user was asked to pick a directory rather than a file
displayOptions.exportDir = this.location.path
displayOptions.exportPath = OS.Path.join(this.location.path, `${this.location.leafName}.${translator.target}`)
displayOptions.cache = false
}
else {
displayOptions.exportDir = this.location.parent.path
displayOptions.exportPath = this.location.path
displayOptions.cache = true
}
}
let capture = displayOptions.keepUpdated
if (capture) {
// this should never occur -- keepUpdated should only be settable if you do a file export
if (! this.location?.path) {
flash('Auto-export not registered', 'Auto-export only supported for exports to file -- please report this, you should not have seen this message')
capture = false
}
// this should never occur -- the JS in exportOptions.ts should prevent it
if (displayOptions.exportFileData) {
flash('Auto-export not registered', 'Auto-export does not support file data export -- please report this, you should not have seen this message')
capture = false
}
if (! ['library', 'collection'].includes(this._export?.type)) {
flash('Auto-export not registered', 'Auto-export only supported for groups, collections and libraries')
capture = false
}
}
if (capture) {
void AutoExport.add({
enabled: true,
path: this.location.path,
type: this._export.type as 'collection' | 'library',
id: this._export.type === 'library' ? this._export.id : this._export.collection.id,
recursive: false,
error: '',
updated: Date.now(),
status: 'done',
translatorID,
exportNotes: displayOptions.exportNotes,
useJournalAbbreviation: displayOptions.useJournalAbbreviation,
})
}
let noworker = ''
if (this.noWait) { // noWait must be synchronous
noworker = 'noWait is active'
}
else if (!Translators.worker) {
// there wasn't an error starting a worker earlier
noworker = 'failed to start a chromeworker, disabled until restart'
}
else if (typeof translator.displayOptions.worker === 'undefined') {
noworker = `${translator.label} does not support background export`
}
else if (!displayOptions.worker) {
noworker = `user has chosen foreground export for ${translator.label}`
}
/*
else if (this.location?.path.startsWith('\\\\')) {
// check for SMB path for #1396
noworker = 'chrome workers fail on smb paths'
}
*/
else {
noworker = Object.keys(this._handlers).filter(handler => !['done', 'itemDone', 'error'].includes(handler)).join(', ')
if (noworker) noworker = `found async handlers: ${noworker}`
}
if (noworker) {
log.debug('worker export skipped,', noworker)
}
else {
const path = this.location?.path
// fake out the stuff that complete expects to be set by .translate
this._currentState = 'translate'
this.saveQueue = []
this._savingAttachments = []
return Translators.queueJob({ translatorID, displayOptions, translate: this, scope: { ...this._export, getter: this._itemGetter }, path })
.then(result => {
// eslint-disable-next-line id-blacklist
this.string = result
this.complete(result || true)
})
.catch(err => {
log.error('worker translation failed, error:', err)
this.complete(null, err)
})
}
}
}
catch (err) {
log.error('Zotero.Translate.Export::translate error:', err)
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return original.apply(this, arguments)
})
export class BetterBibTeX {
public uninstalled = false
public Orchestrator = orchestrator
public Cache = Cache
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions, @typescript-eslint/no-unsafe-return, @typescript-eslint/explicit-module-boundary-types
public CSL() { return CSL }
public TestSupport = new TestSupport
public KeyManager = KeyManager
public Text = { sentenceCase }
// panes
public ExportOptions: ExportOptions = new ExportOptions
public ErrorReport = ErrorReport
public PrefPane = new PrefPane
public Translators = Translators
public ready = new Deferred<boolean>()
public dir: string
public debugEnabledAtStart: boolean
public outOfMemory = ''
public generateCSLJSON = generateCSLJSON
constructor() {
this.debugEnabledAtStart = Zotero.Prefs.get('debug.store') || Zotero.Debug.storing
if (Zotero.isWin && !is7) Zotero.Debug.addListener(this.logListener.bind(this))
}
private logListener(message: string): void {
if (Preference.cache && !this.outOfMemory && message.match(/Translate: Translation using Better .* failed:[\s\S]*out of memory/)) {
this.outOfMemory = message
flash('Zotero is out of memory', 'Zotero is out of memory. I will turn off the cache to help release memory pressure, but this is only a temporary fix until Zotero 7 comes out')
Preference.cache = false
}
}
public async scanAUX(target: string): Promise<void> {
await this.ready
const aux = await AUXScanner.pick()
if (!aux) return
switch (target) {
case 'collection':
await AUXScanner.scan(aux)
break
case 'tag':
// eslint-disable-next-line no-case-declarations
let name = OS.Path.basename(aux)
name = name.lastIndexOf('.') > 0 ? name.substr(0, name.lastIndexOf('.')) : name
// eslint-disable-next-line no-case-declarations
const tag = prompt({
title: l10n.localize(`better-bibtex_aux-scan_title_${aux.endsWith('.aux') ? 'aux' : 'md'}`),
text: l10n.localize('better-bibtex_aux-scan_prompt'),
value: name,
})
if (!tag) return
await AUXScanner.scan(aux, { tag })
break
default:
flash(`Unsupported aux-scan target ${target}`)
break
}
}
public openDialog(url: string, title: string, properties: string, params: Record<string, any>): void {
Zotero.getMainWindow().openDialog(url, title, properties, params)
}
public setProgress(progress: number, msg: string): void {
const doc = Zotero.getMainWindow().document
if (!doc.getElementById('better-bibtex-progress')) {
const elements = new Elements(doc)
// progress bar
const progressToolbar = elements.create('hbox', {
id: 'better-bibtex-progress',
hidden: 'true',
align: 'left',
pack: 'start',
flex: '1',
})
const container = doc.getElementById('zotero-item-toolbar') || doc.getElementById('zotero-pane-progressmeter-container')
// after hbox-before-zotero-pq-buttons
container.insertBefore(progressToolbar, container.firstChild.nextSibling)
progressToolbar.appendChild(elements.create('hbox', {
id: 'better-bibtex-progress-meter',
width: '16px',
height: '16px',
style: `
position: absolute;
left: 0;
top: 0;
width: 16px;
height: 16px;
background-image: url(chrome://zotero/skin/progress_arcs.png);
background-position: 0 0;
`,
}))
progressToolbar.appendChild(elements.create('label', {
id: 'better-bibtex-progress-label',
value: 'nothing to see here',
}))
}
log.debug('progress:', progress, msg)
const progressbox = doc.getElementById('better-bibtex-progress')
if (progressbox.hidden = (progress >= 100 || progress < 0)) return
const progressmeter: XUL.Element = (doc.getElementById('better-bibtex-progress-meter') as unknown as XUL.Element)
const nArcs = 20
progressmeter.style.backgroundPosition = `-${Math.round(progress/100 * nArcs) * 16}px 0`
const progressbar: XUL.Element = (doc.getElementById('better-bibtex-progress') as unknown as XUL.Element)
progressbar.style.opacity = `${progress/200+.5}`
const label: XUL.Label = (doc.getElementById('better-bibtex-progress-label') as unknown as XUL.Label)
label.setAttribute('value', `better bibtex: ${msg}`)
}
public async startup(reason: Reason): Promise<void> {
log.debug('Loading Better BibTeX: starting...')
orchestrator.add('start', {
description: 'zotero',
startup: async () => {
// https://groups.google.com/d/msg/zotero-dev/QYNGxqTSpaQ/uvGObVNlCgAJ
// this is what really takes long
await Zotero.initializationPromise
this.dir = OS.Path.join(Zotero.DataDirectory.dir, 'better-bibtex')
await OS.File.makeDir(this.dir, { ignoreExisting: true })
await Preference.startup(this.dir)
Events.startup()
},
})
orchestrator.add('sqlite', {
startup: async () => {
await Zotero.DB.queryAsync('ATTACH DATABASE ? AS betterbibtex', [OS.Path.join(Zotero.DataDirectory.dir, 'better-bibtex.sqlite')])
const tables: Record<string, boolean> = {}
for (const table of await Zotero.DB.columnQueryAsync("SELECT LOWER(REPLACE(name, '-', '')) FROM betterbibtex.sqlite_master where type='table'")) {
tables[table] = true
}
const NoParse = { noParseParams: true }
for (const ddl of require('./db/citation-key.sql')) {
await Zotero.DB.queryAsync(ddl, [], NoParse)
}
for (const ddl of require('./db/auto-export.sql')) {
await Zotero.DB.queryAsync(ddl, [], NoParse)
}
for (const ddl of require('../gen/auto-export-triggers.sql')) {
await Zotero.DB.queryAsync(ddl, [], NoParse)
}
if (tables.betterbibtex) {
if (!(await Zotero.DB.queryAsync('PRAGMA betterbibtex.table_info("better-bibtex")')).find(info => info.name === 'migrated')) {
Zotero.DB.queryAsync('ALTER TABLE betterbibtex."better-bibtex" ADD migrated')
}
await Zotero.DB.executeTransaction(async () => {
for (let { name, data } of await Zotero.DB.queryAsync('SELECT name, data FROM betterbibtex."better-bibtex" WHERE migrated IS NULL')) {
log.debug('migrating', { name })
data = JSON.parse(data)
let migrated = name
switch (name) {
case 'better-bibtex.citekey':
log.debug('converting', { name, records: data.data.length })
try {
for (const key of data.data) {
await Zotero.DB.queryAsync('REPLACE INTO betterbibtex.citationkey (itemID, itemKey, libraryID, citationKey, pinned) VALUES (?, ?, ?, ?, ?)', [
key.itemID,
key.itemKey,
key.libraryID,
key.citekey,
key.pinned ? 1 : 0,
])
}
}
catch (err) {
log.error('not migrated:', name, err)
}
break
case 'better-bibtex.autoexport':
log.debug('converting', { name, records: data.data.length })
for (const ae of data.data) {
await AE.store({ ...ae, updated: ae.meta.updated })
}
break
default:
migrated = ''
break
}
if (migrated) await Zotero.DB.queryAsync('UPDATE betterbibtex."better-bibtex" SET migrated = 1 WHERE name = ?', [ migrated ])
}
})
const status = {}
for (const { name, migrated } of await Zotero.DB.queryAsync('SELECT name, migrated FROM betterbibtex."better-bibtex"')) {
status[name] = migrated
}
log.debug('migrated:', status)
}
},
shutdown: async () => {
await Zotero.DB.queryAsync('DETACH DATABASE betterbibtex')
},
})
orchestrator.add('done', {
description: 'user interface',
startup: async () => {
this.ready.resolve(true)
await this.load(Zotero.getMainWindow())
Zotero.Promise.delay(15000).then(() => {
log.debug('removing fallback debug logger')
DebugLog.unregister('Better BibTeX')
})
Zotero.Promise.delay(3000).then(() => {
DebugLog.convertLegacy()
})
if (is7) {
await Zotero.ItemTreeManager.registerColumns({
dataKey: 'citationKey',
label: 'Citation key',
pluginID: 'better-bibtex@iris-advies.com',
dataProvider: (item, _dataKey) => {
const citekey = Zotero.BetterBibTeX.KeyManager.get(item.id)
return citekey ? `${citekey.citationKey}${citekey.pinned ? icons.pin : ''}`.trim() : ''
},
})
}
},
shutdown: async () => { // eslint-disable-line @typescript-eslint/require-await
Events.shutdown()
Elements.removeAll()
$unpatch$()
clean_pane_persist()
Preference.shutdown()
for (const endpoint of Object.keys(Zotero.Server.Endpoints)) {
if (endpoint.startsWith('/better-bibtex/')) delete Zotero.Server.Endpoints[endpoint]
}
},
})
await orchestrator.startup(reason, (phase: string, name: string, done: number, total: number, message: string): void => {
this.setProgress(done * 100 / total, message || name)
})
this.setProgress(100, 'finished')
}
public async shutdown(reason: Reason): Promise<void> {
await orchestrator.shutdown(reason)
}
public async load(win: Window): Promise<void> {
// the zero-width-space is a marker to re-save the current default so it doesn't get replaced
// when the default changes later, which would change new keys suddenly
if (!Preference.citekeyFormat) Preference.citekeyFormat = Preference.default.citekeyFormat
Preference.citekeyFormat = Preference.citekeyFormat.replace(/\u200B/g, '')
if (typeof __estrace !== 'undefined') {
flash(
'BBT TRACE LOGGING IS ENABLED',
'BBT trace logging is enabled in this build.\nZotero will run very slowly.\nThis is intended for debugging ONLY.',
20
)
}
await this.loadUI(win)
void Events.emit('loaded')
Events.on('export-progress', ({ pct, message }) => {
this.setProgress(pct, message)
})
}
async loadUI(win: Window): Promise<void> {
try {
log.debug('loading main UI')
await newZoteroPane(win)
await newZoteroItemPane(win)
}
catch (err) {
log.debug('loadUI error:', err)
}
}
public parseDate(date: string): ParsedDate { return DateParser.parse(date) }
getContents(path: string): string {
if (!path) {
log.error('BetterBibTeX.getContents: no path')
return null
}
const file = new FileUtils.File(path)
// cannot use await OS.File.exists here because we may be invoked in noWait mod
if (!file.exists()) {
log.error('BetterBibTeX.getContents:', path, 'does not exist')
return null
}
try {
return Zotero.File.getContents(file) as string
}
catch (err) {
log.error('BetterBibTeX.getContents:', path, `${err}`)
return null
}
}
}
Events.on('window-loaded', async ({ win, href }: {win: Window, href: string}) => {
if (Preference.testing) log.debug('window-loaded:', href)
switch (href) {
case 'chrome://zotero/content/standalone/standalone.xul':
case 'chrome://zotero/content/zoteroPane.xhtml':
await Zotero.BetterBibTeX.loadUI(win)
break
}
})
Zotero.BetterBibTeX = Zotero.BetterBibTeX || new BetterBibTeX