-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ooxast-util-citations.ts
executable file
·287 lines (250 loc) · 8.28 KB
/
ooxast-util-citations.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
import { SKIP, visitParents } from 'unist-util-visit-parents'
import { P, R, T, Text, Root } from 'ooxast'
import { getPStyle } from 'ooxast-util-get-style'
import { parseTextCite } from 'parse-text-cite'
import { Node } from 'unist'
import { convertElement, isElement } from 'xast-util-is-element'
import { select } from 'xast-util-select'
import { toString } from 'xast-util-to-string'
import { x } from 'xastscript'
import { Data as CSL } from 'csl-json'
import { CiteOutput, Citation, MendeleyCitation, ZoteroCitation } from './types.js'
import { detectCitePlugin } from 'ooxast-util-citation-plugin'
import { VFile } from 'vfile'
import { constructMendeleyCitation } from './constructMendeleyCitation.js'
import { constructZoteroCitation } from './constructZoteroCitation.js'
const isInstrT = convertElement<T>('w:instrText')
const isP = convertElement<P>('w:p')
export const citationTypesWithSuffixedForm = ['mendeley', 'zotero']
export interface Options {
bibliography?: CSL[]
log?: boolean
type?: 'mendeley' | 'zotero' | 'citavi' | 'word' | 'endnote'
}
export function findCitations(tree: Node, vfile?: VFile, options?: Options): Root {
const type = detectCitePlugin(tree)
if (vfile) {
vfile.data.citePlugin = type
}
let references = false
let citationCounter = 1
visitParents(tree, isP, (p: P, ancestors: Node[]) => {
// don't parse citations in tables
if (ancestors.some((parent) => isElement(parent) && /tbl/.test(parent.name))) {
return
}
if (references) {
return
}
// find references section
if (getPStyle(p)?.toLowerCase()?.includes('heading')) {
if (['references', 'citations', 'bibliography'].includes(toString(p)?.toLowerCase().trim())) {
references = true
return
}
references = false
return
}
const kids = p.children
const runs: typeof kids = []
let skipNext = false
// loop over runs
for (let idx = 0; idx < kids.length; idx++) {
const kid = kids[idx]
if (!convertElement<R>('w:r')(kid)) {
runs.push(kid)
continue
}
const instr = select('w\\:instrText', kid)
// Check if this is a Mendeley/Zotero citation, because if so we should not try to parse it ourselves
if (instr && isInstrT(instr) && toString(instr).includes('CSL_CITATION')) {
skipNext = true
runs.push(kid)
continue
}
const t = select('w\\:t', kid)
// If either one of them don't have text, don't merge them ya dummy
if (!t) {
runs.push(kid)
continue
}
const text = toString(t)
/**
* We dont care about empty runs
*/
if (text === '') {
continue
}
if (!text) {
runs.push(kid)
continue
}
if (skipNext) {
skipNext = false
runs.push(kid)
continue
}
// split the sentences to improve parsing performance
const sentences = text.split(/(?<=[a-z)][.?!])\s+(?=[A-Z]\w)/)
// re-add the spaces that were removed by the split
const sentencesWithSpaces = sentences.map((s, i) => {
if (i === sentences.length - 1) return s
return `${s} `
})
const rpr = select('w\\:rPr', kid)
for (const sentence of sentencesWithSpaces) {
try {
// split the text into sentences so that we can parse them individually
const parsedCitation = parseTextCite(sentence, {
log: options?.log,
}) as CiteOutput
parsedCitation.forEach((item) => {
if (typeof item === 'string') {
return
}
if (item?.citationItems?.[0]?.prefix) {
console.log({ item })
}
})
// if the sentence is not a citation, just add it to the runs
if (parsedCitation.every((item) => typeof item === 'string')) {
runs.push(
x('w:r', {}, [
...(rpr ? [rpr] : []),
x('w:t', {}, [{ type: 'text', value: sentence } as Text]) as T,
]) as R,
)
continue
}
const newNodes = parsedCitation.reduce((acc: typeof kids, curr: string | Citation) => {
if (typeof curr === 'string') {
acc.push(
x('w:r', {}, [
...(rpr ? [rpr] : []),
x('w:t', {}, [{ type: 'text', value: curr } as Text]) as T,
]) as R,
)
return acc
}
const { instr, citation } = constructCitation(
curr,
type,
citationCounter,
options?.bibliography,
)
citationCounter++
acc.push(
x('w:r', {}, [
...(rpr ? [rpr] : []),
x('w:instrText', {}, [{ type: 'text', value: instr } as Text]) as T,
]) as R,
)
// say this is an empty, clean thing, and we want to turn it into a text
// with mendeely citations. then we need to push the formatted citation after the main citation
if (citationTypesWithSuffixedForm.includes(type) && citation) {
acc.push(
x('w:r', {}, [
...(rpr ? [rpr] : []),
x('w:t', {}, [
{
type: 'text',
value:
type === 'mendeley'
? (citation as MendeleyCitation)?.mendeley.formattedCitation
: (citation as ZoteroCitation).properties.formattedCitation,
} as Text,
]) as T,
]) as R,
)
}
return acc
}, [])
runs.push(...newNodes)
} catch (e) {
vfile?.message(
`Text unsuccesfully parsed, treating it as non-cite.
Sentence: ${sentence}
Error: ${e}`,
kid,
'ooxast-util-citations',
)
console.warn('Text unsuccesfully parsed, treating it as non-cite')
console.warn(sentence)
console.warn(e)
// runs.push(kid)
runs.push(
x('w:r', {}, [
...(rpr ? [rpr] : []),
x('w:t', {}, [{ type: 'text', value: sentence } as Text]) as T,
]) as R,
)
continue
}
}
}
p.children = runs
return SKIP
})
return tree as Root
}
/**
* Construct a citation for Mendeley or Zotero.
* @param curr The citation to construct
* @param type The type of citation to construct
* @param index The index of the citation
* @param bibliography The bibliography to use
* @returns The constructed citation and instruction
*/
export function constructCitation(
curr: Citation,
type: string,
index: number,
bibliography?: CSL[],
message?: VFile['message'],
): { citation: MendeleyCitation | ZoteroCitation; instr: string } {
switch (type) {
case 'mendeley': {
const citation = constructMendeleyCitation(curr, index, bibliography)
return {
instr: `ADDIN CSL_CITATION ${JSON.stringify(citation)}`,
citation,
}
}
case 'zotero':
default: {
const citation = constructZoteroCitation(curr, index, bibliography, message)
return {
instr: `ADDIN ZOTERO_ITEM CSL_CITATION ${JSON.stringify(citation)}`,
citation,
}
}
}
}
// function plainMendeleyCitation(
// cite: Citation,
// index: number
// ): MendeleyCitation {
// const schema =
// 'https://github.com/citation-style-language/schema/raw/master/csl-citation.json'
// const pref = 'ADDIN CSL_CITATION'
// const mend = {
// schema,
// properties: cite.properties,
// citationItems: cite.citationItems,
// mendeley: {
// formattedCitation: cite.raw,
// previouslyFormattedCitation: cite.raw,
// plainTextFormattedCitation: cite.raw,
// },
// }
// return mend
// }
// function constructZoteroCitation(curr: Citation, index: number) {
// throw new Error('Function not implemented.')
// }
function constructEndnoteCitation(curr: Citation, index: number) {
throw new Error('Function not implemented.')
}
function constructCitaviCitation(curr: Citation, index: number) {
throw new Error('Function not implemented.')
}