-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
formatter.ts
300 lines (252 loc) · 11 KB
/
formatter.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
/* eslint-disable @typescript-eslint/require-await, @typescript-eslint/no-unsafe-return, @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-shadow */
import { Translators } from '../translators'
import { getItemsAsync } from '../get-items-async'
import { Preference } from '../prefs'
import { html as escapeHTML } from '../escape'
import { scannableCite } from '../../gen/ScannableCite'
import { citeCreators, yearFromDate } from '../../translators/Better BibTeX Citation Key Quick Copy'
import { Eta } from 'eta'
const eta = new Eta({ autoEscape: true })
import { simplifyForExport } from '../../gen/items/simplify'
import { Transform } from 'unicode2latex'
function serialized(item) {
if (item) {
const ser = simplifyForExport(Zotero.Utilities.Internal.itemToExportFormat(item, false, true))
ser.uri = Zotero.URI.getItemURI(item)
ser.itemID = item.id
return ser
}
return undefined
}
function shortLabel(label: string, options): string {
if (typeof options[label] === 'string') return options[label]
return {
article: 'art.',
chapter: 'ch.',
subchapter: 'subch.',
column: 'col.',
figure: 'fig.',
line: 'l.',
note: 'n.',
issue: 'no.',
opus: 'op.',
page: 'p.',
paragraph: 'para.',
subparagraph: 'subpara.',
part: 'pt.',
rule: 'r.',
section: 'sec.',
subsection: 'subsec.',
Section: 'Sec.',
'sub verbo': 'sv.',
schedule: 'sch.',
title: 'tit.',
verse: 'vrs.',
volume: 'vol.',
}[label] || label
}
const latextx = new Transform('minimal')
function citation2latex(citation, options) {
let formatted = ''
// despite Mozilla's claim that trimStart === trimLeft, and that trimStart should be preferred, trimStart does not seem to exist in FF chrome code.
const label = (`${ shortLabel(citation.label, { page: '', ...options }) } `).trimLeft()
if (citation.prefix) formatted += `[${ latextx.tolatex(citation.prefix) }]`
if (citation.locator && citation.suffix) {
formatted += `[${ latextx.tolatex(label) }${ latextx.tolatex(citation.locator) }, ${ latextx.tolatex(citation.suffix) }]`
}
else if (citation.locator) {
formatted += `[${ latextx.tolatex(label) }${ latextx.tolatex(citation.locator) }]`
}
else if (citation.suffix) {
formatted += `[${ latextx.tolatex(citation.suffix) }]`
}
else if (citation.prefix) {
formatted += '[]'
}
formatted += `{${ citation.citationKey }}`
return formatted
}
// export singleton: https://k94n.com/es6-modules-single-instance-pattern
export const Formatter = new class { // eslint-disable-line @typescript-eslint/naming-convention,no-underscore-dangle,id-blacklist,id-match
public async typst(citations, options): Promise<string> {
const cite = citation => {
let cited = citation.citationKey.match(/^[a-z0-9_\-:.]+$/i) ? `<${ citation.citationKey }>` : `label(${ JSON.stringify(citation.citationKey) })`
if (citation.locator) cited += `,${ JSON.stringify(`${ shortLabel(citation.label, options) } ${ citation.locator }`) }`
if (citation.suppressAuthor) cited += citation.locator ? ',"year"' : ', form: "year"'
return `#cite(${ cited })`
}
return await citations.map(cite).join('')
}
public async citationLinks(citations, _options): Promise<string> {
return await citations.map(citation => `cites: ${ citation.citationKey }`).join('\n')
}
public async cite(citations, options) { return this.natbib(citations, options) }
public async citet(citations, options) { return this.natbib(citations, { command: 'citet', ...options }) }
public async citep(citations, options) { return this.natbib(citations, { command: 'citep', ...options }) }
public async latex(citations, options) { return this.natbib(citations, options) }
public async natbib(citations, options) {
if (!options.command) options.command = 'cite'
if (citations.length === 0) return ''
// test for simple case where multiple entries can be put in a single cite
if (citations.length > 1) {
const state = citations.reduce((acc, cit) => {
for (const field of [ 'prefix', 'suffix', 'suppressAuthor', 'locator', 'label' ]) {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
acc[field] = (acc[field] || 0) + (cit[field] ? 1 : 0)
}
return acc
}, {})
if (state.suffix === 0 && state.prefix === 0 && state.locator === 0 && (state.suppressAuthor === 0 || state.suppressAuthor === citations.length)) {
return `\\${ citations[0].suppressAuthor ? 'citeyear' : options.command }{${ citations.map(citation => citation.citationKey).join(',') }}`
}
}
let formatted = ''
for (const citation of citations) {
formatted += `\\${ citation.suppressAuthor ? 'citeyear' : options.command }${ citation2latex(citation, options) }`
}
return formatted
}
public async biblatex(citations, options) {
if (citations.length === 0) return ''
let command = options.command ? options.command : 'autocite'
if (citations.length === 1) {
const citation = citations[0]
// NOTE: suppressAuthor is only honored when citations.length === 1 and
// the command is one of \cite, \autocite or \parencite; for other
// commands, suppressAuthor doesn't make sense, and for multiple
// citations, biblatex doesn't support suppressing authors on a case by
// case basis
const suppressAuthor = citation.suppressAuthor && /^(auto|paren|)cite$/.exec(command) ? '*' : ''
return `\\${ command }${ suppressAuthor }${ citation2latex(citation, options) }`
}
citations = citations.map(citation2latex).join('')
if (citations.includes('[')) {
// there are some pre/post notes → generate a full \XYcites command
command = command.endsWith('s') ? command : `${ command }s`
}
else {
// there are no pre/post-notes, the citations can be a simple
// comma-separated list of keys
citations = citations.replace(/\}\{/g, ',')
}
return `\\${ command }${ citations }`
}
public async mmd(citations, _options) {
const formatted = []
for (const citation of citations) {
if (citation.prefix) {
formatted.push(`[${ citation.prefix }][#${ citation.citationKey }]`)
}
else {
formatted.push(`[#${ citation.citationKey }][]`)
}
}
return formatted.join('')
}
public async jekyll(citations, _options) {
return citations.map(cit => `{% cite ${ cit.citationKey } %}`).join('')
}
public async pandoc(citations, options) {
const formatted = []
for (const citation of citations) {
let cite = ''
if (citation.prefix) cite += `${ citation.prefix } `
if (citation.suppressAuthor) cite += '-'
cite += `@${ citation.citationKey }`
if (citation.locator) cite += `, ${ shortLabel(citation.label, options) } ${ citation.locator }`.replace(/\s+/, ' ')
if (citation.suffix) cite += ` ${ citation.suffix }`
formatted.push(cite)
}
return `${ options.brackets ? '[' : '' }${ formatted.join('; ') }${ options.brackets ? ']' : '' }`
}
public async 'asciidoctor-bibtex'(citations, options) {
const formatted = []
for (const citation of citations) {
let cite = citation.citationKey
if (citation.locator) {
const label = `${ shortLabel(citation.label, { page: '', ...options }) } ${ citation.locator }`.trim()
cite += `(${ label })`
}
formatted.push(cite)
}
return `${ options.cite || 'cite' }:[${ formatted.join(', ') }]`
}
public async 'scannable-cite'(citations, options) {
let markers = ''
for (const citation of citations) {
const scannable = scannableCite(await getItemsAsync(citation.id))
const enriched = [
citation.prefix || '',
`${ citation.suppressAuthor ? '-' : '' }${ scannable.label }`,
citation.locator ? `${ shortLabel(citation.label, options) } ${ citation.locator }`.trim() : '',
citation.suffix || '',
Preference.testing ? 'zu:0:ITEMKEY' : scannable.id,
].join(' | ').replace(/ +/g, ' ')
markers += `{ ${ enriched.trim() } }`
}
return markers
}
public async 'formatted-citation'(citations, options) {
const format = {
mode: 'bibliography',
contentType: options.contentType,
id: options.style,
locale: options.locale,
}
// items must be pre-loaded for the citation processor
await getItemsAsync(citations.map(item => item.id))
const csl = Zotero.Styles.get(format.id).getCiteProc(format.locale)
csl.updateItems(citations.map(item => item.id))
const citation = {
citationItems: citations.map(item => ({ ...item, 'suppress-author': item.suppressAuthor })),
properties: {},
}
const output = csl.previewCitationCluster(citation, [], [], format.contentType)
return output
}
public async 'formatted-bibliography'(citations, options) {
const format = {
mode: 'bibliography',
contentType: options.contentType,
id: options.style,
locale: options.locale,
}
const items = await getItemsAsync(citations.map(item => item.id))
return Zotero.QuickCopy.getContentFromItems(items, format, null, false)[format.contentType]
}
public async jupyter(citations, _options) {
const items = await getItemsAsync(citations.map(cit => cit.id))
let picked = ''
for (const cit of citations) {
const i = items.find(item => item.id === cit.id)
const item = i ? { creators: i.getCreatorsJSON(), date: i.getField('date') } : {}
picked += `<cite data-cite="${ escapeHTML(cit.citationKey) }">(${ escapeHTML(citeCreators(item.creators)) }, ${ escapeHTML(yearFromDate(item.date)) })</cite>`
}
return picked
}
public async eta(citations, options) {
if (!options.template) throw new Error('No template provided')
const items = await getItemsAsync(citations.map(cit => cit.id))
for (const cit of citations) {
cit.item = serialized(items.find(item => item.id === cit.id))
}
return eta.renderString(options.template, { items: citations })
}
public async translate(citations, options) {
const items = await getItemsAsync(citations.map(citation => citation.id))
const label = (options.translator || 'biblatex').replace(/\s/g, '').toLowerCase().replace('better', '')
const translatorID = Object.keys(Translators.byId).find(id => Translators.byId[id].label.replace(/\s/g, '').toLowerCase().replace('better', '') === label) || options.translator
const displayOptions = {
exportNotes: [ 'yes', 'y', 'true' ].includes((options.exportNotes || '').toLowerCase()),
useJournalAbbreviation: [ 'yes', 'y', 'true' ].includes((options.useJournalAbbreviation || '').toLowerCase()),
}
return await Translators.exportItems({ translatorID, displayOptions, scope: { type: 'items', items }})
}
public async json(citations, _options) {
const items = await getItemsAsync(citations.map(cit => cit.id))
for (const cit of citations) {
cit.item = serialized(items.find(item => item.id === cit.id))
}
return JSON.stringify(citations)
}
}