@iebh/reflib 2.7.2 → 2.8.1
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/.ignore +1 -1
- package/LICENSE +20 -20
- package/README.md +297 -274
- package/app.js +87 -0
- package/lib/browser.js +30 -30
- package/lib/default.js +30 -30
- package/lib/downloadFile.js +94 -94
- package/lib/fields.js +158 -158
- package/lib/formats.js +85 -85
- package/lib/getModule.js +39 -39
- package/lib/getRefDoi.js +16 -16
- package/lib/identifyFormat.js +13 -13
- package/lib/readFile.js +63 -63
- package/lib/readStream.js +21 -21
- package/lib/uploadFile.js +71 -71
- package/lib/writeFile.js +32 -32
- package/lib/writeStream.js +16 -16
- package/modules/bibtex.js +401 -401
- package/modules/default.js +7 -7
- package/modules/endnoteEnl.js +237 -237
- package/modules/endnoteEnlX.js +85 -85
- package/modules/endnoteXml.js +410 -474
- package/modules/interface.js +47 -47
- package/modules/json.js +109 -79
- package/modules/medline.js +638 -638
- package/modules/ris.js +383 -383
- package/modules/shims/JSONStream-browser.js +43 -43
- package/modules/shims/WritableStream-browser.js +52 -52
- package/package.json +68 -66
- package/shared/camelCase.js +17 -17
- package/shared/emitter.js +23 -23
- package/shared/parseArgs.js +104 -0
- package/shared/streamEmitter.js +61 -61
package/modules/endnoteXml.js
CHANGED
|
@@ -1,474 +1,410 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
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
|
-
stack
|
|
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
|
-
+ (ref.
|
|
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
|
-
{rl: '
|
|
370
|
-
{rl: '
|
|
371
|
-
{rl: '
|
|
372
|
-
{rl: '
|
|
373
|
-
{rl: '
|
|
374
|
-
{rl: '
|
|
375
|
-
{rl: '
|
|
376
|
-
{rl: '
|
|
377
|
-
{rl: '
|
|
378
|
-
{rl: '
|
|
379
|
-
{rl: '
|
|
380
|
-
{rl: '
|
|
381
|
-
{rl: '
|
|
382
|
-
{rl: '
|
|
383
|
-
{rl: '
|
|
384
|
-
{rl: '
|
|
385
|
-
{rl: '
|
|
386
|
-
{rl: '
|
|
387
|
-
{rl: '
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
//
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
{rl: 'bookSection', rawText: 'Book Section', rawId: 5},
|
|
412
|
-
{rl: 'case', rawText: 'Case', rawId: 7},
|
|
413
|
-
{rl: 'catalog', rawText: 'Catalog', rawId: 8},
|
|
414
|
-
{rl: 'chartOrTable', rawText: 'Chart or Table', rawId: 38},
|
|
415
|
-
{rl: 'classicalWork', rawText: 'Classical Work', rawId: 49},
|
|
416
|
-
{rl: 'computerProgram', rawText: 'Computer Program', rawId: 9},
|
|
417
|
-
{rl: 'conferencePaper', rawText: 'Conference Paper', rawId: 47},
|
|
418
|
-
{rl: 'conferenceProceedings', rawText: 'Conference Proceedings', rawId: 10},
|
|
419
|
-
{rl: 'dataset', rawText: 'Dataset', rawId: 59},
|
|
420
|
-
{rl: 'dictionary', rawText: 'Dictionary', rawId: 52},
|
|
421
|
-
{rl: 'editedBook', rawText: 'Edited Book', rawId: 28},
|
|
422
|
-
{rl: 'electronicArticle', rawText: 'Electronic Article', rawId: 43},
|
|
423
|
-
{rl: 'electronicBook', rawText: 'Electronic Book', rawId: 44},
|
|
424
|
-
{rl: 'electronicBookSection', rawText: 'Electronic Book Section', rawId: 60},
|
|
425
|
-
{rl: 'encyclopedia', rawText: 'Encyclopedia', rawId: 53},
|
|
426
|
-
{rl: 'equation', rawText: 'Equation', rawId: 39},
|
|
427
|
-
{rl: 'figure', rawText: 'Figure', rawId: 37},
|
|
428
|
-
{rl: 'filmOrBroadcast', rawText: 'Film or Broadcast', rawId: 21},
|
|
429
|
-
{rl: 'generic', rawText: 'Generic', rawId: 13},
|
|
430
|
-
{rl: 'governmentDocument', rawText: 'Government Document', rawId: 46},
|
|
431
|
-
{rl: 'grant', rawText: 'Grant', rawId: 54},
|
|
432
|
-
{rl: 'hearing', rawText: 'Hearing', rawId: 14},
|
|
433
|
-
{rl: 'journalArticle', rawText: 'Journal Article', rawId: 17},
|
|
434
|
-
{rl: 'legalRuleOrRegulation', rawText: 'Legal Rule or Regulation', rawId: 50},
|
|
435
|
-
{rl: 'magazineArticle', rawText: 'Magazine Article', rawId: 19},
|
|
436
|
-
{rl: 'manuscript', rawText: 'Manuscript', rawId: 36},
|
|
437
|
-
{rl: 'map', rawText: 'Map', rawId: 20},
|
|
438
|
-
{rl: 'music', rawText: 'Music', rawId: 61},
|
|
439
|
-
{rl: 'newspaperArticle', rawText: 'Newspaper Article', rawId: 23},
|
|
440
|
-
{rl: 'onlineDatabase', rawText: 'Online Database', rawId: 45},
|
|
441
|
-
{rl: 'onlineMultimedia', rawText: 'Online Multimedia', rawId: 48},
|
|
442
|
-
{rl: 'pamphlet', rawText: 'Pamphlet', rawId: 24},
|
|
443
|
-
{rl: 'patent', rawText: 'Patent', rawId: 25},
|
|
444
|
-
{rl: 'personalCommunication', rawText: 'Personal Communication', rawId: 26},
|
|
445
|
-
{rl: 'report', rawText: 'Report', rawId: 27},
|
|
446
|
-
{rl: 'serial', rawText: 'Serial', rawId: 57},
|
|
447
|
-
{rl: 'standard', rawText: 'Standard', rawId: 58},
|
|
448
|
-
{rl: 'statute', rawText: 'Statute', rawId: 31},
|
|
449
|
-
{rl: 'thesis', rawText: 'Thesis', rawId: 32},
|
|
450
|
-
{rl: 'unpublished', rawText: 'Unpublished Work', rawId: 34},
|
|
451
|
-
{rl: 'web', rawText: 'Web Page', rawId: 12},
|
|
452
|
-
],
|
|
453
|
-
rlMap: new Map(), // Calculated later for quicker lookup
|
|
454
|
-
rawMap: new Map(), // Calculated later for quicker lookup
|
|
455
|
-
},
|
|
456
|
-
// }}}
|
|
457
|
-
};
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* @see modules/interface.js
|
|
462
|
-
*/
|
|
463
|
-
export function setup() {
|
|
464
|
-
// Create lookup object of translations.field translations
|
|
465
|
-
translations.fields.collection.forEach(c => {
|
|
466
|
-
translations.fields.rawMap.set(c.raw, c);
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
// Create lookup object of translations.types with key as .rl / val as the full object
|
|
470
|
-
translations.types.collection.forEach(c => {
|
|
471
|
-
translations.types.rlMap.set(c.rl, c);
|
|
472
|
-
translations.types.rawMap.set(c.rawId, c);
|
|
473
|
-
});
|
|
474
|
-
}
|
|
1
|
+
import Emitter from '../shared/emitter.js';
|
|
2
|
+
import XMLParser from '@iebh/cacx';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Read an EndnoteXML file, returning an Emitter analogue
|
|
6
|
+
*
|
|
7
|
+
* @see modules/inhterface.js
|
|
8
|
+
* @param {Stream} stream Stream primative to encapsulate
|
|
9
|
+
* @returns {Object} An Emitter analogue defined in `../shared/Emitter.js`
|
|
10
|
+
*/
|
|
11
|
+
export function readStream(stream) {
|
|
12
|
+
let emitter = Emitter();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The current reference being appended to
|
|
16
|
+
* @type {Object}
|
|
17
|
+
*/
|
|
18
|
+
let ref = {};
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
// Setup the XML parser
|
|
22
|
+
let parserOptions = {
|
|
23
|
+
flattenText: false,
|
|
24
|
+
flattenChildren: false,
|
|
25
|
+
flattenAttrs: false,
|
|
26
|
+
onTagClose(node, stack) {
|
|
27
|
+
if (node.tag == 'title' && node.text) {
|
|
28
|
+
ref.title = node.text;
|
|
29
|
+
} else if (node.tag == 'author' && node.text) {
|
|
30
|
+
if (!ref.authors) ref.authors = [];
|
|
31
|
+
ref.authors.push(node.text);
|
|
32
|
+
} else if (node.tag == 'keyword' && node.text) {
|
|
33
|
+
if (!ref.keywords) ref.keywords = [];
|
|
34
|
+
ref.keywords.push(node.text);
|
|
35
|
+
} else if (node.tag == 'url' && node.text) {
|
|
36
|
+
if (!ref.urls) ref.urls = [];
|
|
37
|
+
ref.urls.push(node.text);
|
|
38
|
+
} else if (
|
|
39
|
+
!['authors', 'keywords', 'urls'].includes(node.tag) // Not one of the above collectors
|
|
40
|
+
&& translations.fields.rawMap.has(node.tag) // AND other supported field mapping
|
|
41
|
+
&& node.text // AND there is some content to populate
|
|
42
|
+
) {
|
|
43
|
+
ref[translations.fields.rawMap.get(node.tag).rl] = node.text;
|
|
44
|
+
} else if (node.tag == 'ref-type') { // Special EndnoteXML reference lookup
|
|
45
|
+
let rlType = translations.types.rawMap.get(node.attrs.name);
|
|
46
|
+
ref.type = rlType?.rl || 'journalArticle'; // It should never happen that we have an unknown type but default to something sane if we ever see one
|
|
47
|
+
} else if (node.tag == 'secondary-title' && node.text) { // Zotero "Journal" field translation
|
|
48
|
+
let rlType = translations.types.rawMap.get(node.text);
|
|
49
|
+
ref.type = rlType?.rl || 'journalArticle'; // It should never happen that we have an unknown type but default to something sane if we ever see one
|
|
50
|
+
} else if (node.tag == 'style' && node.text) { // Embedded <style> tag Endnote seems to wrap all inner values with these even though they dont serve any purpose
|
|
51
|
+
// Re-call onTagClose() using the parent node instead
|
|
52
|
+
let outerNode = { // Create shallow copy of 'real' parent node
|
|
53
|
+
...stack.at(-2),
|
|
54
|
+
text: node.text, // ... but copy in this nodes text as if this inner `<style>` wrapper didn't exist
|
|
55
|
+
};
|
|
56
|
+
if (!outerNode) throw new Error('<style> tag with orphaned children!');
|
|
57
|
+
parserOptions.onTagClose(outerNode, stack.slice(0, -1));
|
|
58
|
+
} else if (node.tag == 'record') { // End of record - emit the ref and clear tracking state
|
|
59
|
+
emitter.emit('ref', ref);
|
|
60
|
+
ref = {};
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
let parser = new XMLParser(parserOptions);
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
// Queue up the parser in the next tick (so we can return the emitter first)
|
|
68
|
+
setTimeout(() => {
|
|
69
|
+
if (typeof stream.pipe === 'function') {
|
|
70
|
+
stream.on('data', data => {
|
|
71
|
+
parser.append(data.toString()) // Push new data onto XML decode stack (converting from Buffer -> String)
|
|
72
|
+
emitter.emit('progress', stream.bytesRead);
|
|
73
|
+
})
|
|
74
|
+
stream.on('end', ()=> {
|
|
75
|
+
parser.exec();
|
|
76
|
+
emitter.emit('end');
|
|
77
|
+
});
|
|
78
|
+
stream.on('error', e => emitter.emit('error', e))
|
|
79
|
+
return;
|
|
80
|
+
} else {
|
|
81
|
+
console.error('Error with stream, check "streamEmitter.js" if on browser')
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return emitter;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Write references to a file
|
|
91
|
+
*
|
|
92
|
+
* @see modules/interface.js
|
|
93
|
+
*
|
|
94
|
+
* @param {Stream} stream Writable stream to output to
|
|
95
|
+
* @param {Object} [options] Additional options to use when parsing
|
|
96
|
+
* @param {string} [options.defaultType='journalArticle'] Default citation type to assume when no other type is specified
|
|
97
|
+
* @param {string} [options.filePath="c:\\"] "Fake" internal source file path the citation library was exported from, must end with backslashes
|
|
98
|
+
* @param {string} [options.fileName="Endnote.enl"] "Fake" internal source file name the citation library was exported from
|
|
99
|
+
* @param {function} [options.formatDate] Date formatter to translate between a JS Date object and the Endnote YYYY-MM-DD format
|
|
100
|
+
*
|
|
101
|
+
* @returns {Object} A writable stream analogue defined in `modules/interface.js`
|
|
102
|
+
*/
|
|
103
|
+
export function writeStream(stream, options) {
|
|
104
|
+
let settings = {
|
|
105
|
+
defaultType: 'journalArticle',
|
|
106
|
+
filePath: 'c:\\',
|
|
107
|
+
fileName: 'Endnote.enl',
|
|
108
|
+
formatDate: value => value instanceof Date ? value.toISOString().slice(0, 10) : value,
|
|
109
|
+
...options,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// Cached values so we don't need to keep recomputing
|
|
113
|
+
let encodedName = xmlEscape(settings.fileName);
|
|
114
|
+
let refsSeen = 0;
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
start: ()=> {
|
|
118
|
+
stream.write('<?xml version="1.0" encoding="UTF-8" ?><xml><records>');
|
|
119
|
+
return Promise.resolve();
|
|
120
|
+
},
|
|
121
|
+
write: ref => {
|
|
122
|
+
let refType = translations.types.rlMap.get(ref.type || settings.defaultType);
|
|
123
|
+
if (!refType) {
|
|
124
|
+
console.warn(`Invalid reference type: "${ref.type}", defaulting to journal article`);
|
|
125
|
+
refType = translations.types.rlMap.get('journalArticle')
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
refsSeen++;
|
|
129
|
+
let recNumber = ref.recNumber || refsSeen;
|
|
130
|
+
|
|
131
|
+
stream.write(
|
|
132
|
+
'<record>'
|
|
133
|
+
// Preamble
|
|
134
|
+
+ `<database name="${settings.fileName}" path="${settings.filePath}${settings.fileName}">${encodedName}</database>`
|
|
135
|
+
+ `<source-app name="EndNote" version="16.0">EndNote</source-app>`
|
|
136
|
+
+ `<rec-number>${recNumber}</rec-number>`
|
|
137
|
+
+ `<foreign-keys><key app="EN" db-id="s55prpsswfsepue0xz25pxai2p909xtzszzv">${recNumber}</key></foreign-keys>`
|
|
138
|
+
|
|
139
|
+
// Type
|
|
140
|
+
+ `<ref-type name="${refType.rawText}">${refType.rawId}</ref-type>`
|
|
141
|
+
|
|
142
|
+
// Authors
|
|
143
|
+
+ '<contributors><authors>'
|
|
144
|
+
+ (ref.authors || []).map(author => `<author><style face="normal" font="default" size="100%">${xmlEscape(author)}</style></author>`)
|
|
145
|
+
+ '</authors></contributors>'
|
|
146
|
+
|
|
147
|
+
// Titles
|
|
148
|
+
+ '<titles>'
|
|
149
|
+
+ (ref.title ? `<title><style face="normal" font="default" size="100%">${xmlEscape(ref.title)}</style></title>` : '')
|
|
150
|
+
+ (ref.journal ? `<secondary-title><style face="normal" font="default" size="100%">${xmlEscape(ref.journal)}</style></secondary-title>` : '')
|
|
151
|
+
+ (ref.titleShort ? `<short-title><style face="normal" font="default" size="100%">${xmlEscape(ref.titleShort)}</style></short-title>` : '')
|
|
152
|
+
+ (ref.journalAlt ? `<alt-title><style face="normal" font="default" size="100%">${xmlEscape(ref.journalAlt)}</style></alt-title>` : '')
|
|
153
|
+
+ '</titles>'
|
|
154
|
+
|
|
155
|
+
// Periodical
|
|
156
|
+
+ (ref.periodical ? `<periodical><full-title><style face="normal" font="default" size="100%">${xmlEscape(ref.periodical)}</style></full-title></periodical>` : '')
|
|
157
|
+
|
|
158
|
+
// Simple field key/vals
|
|
159
|
+
+ [
|
|
160
|
+
['abstract', 'abstract'],
|
|
161
|
+
['accessDate', 'access-date'],
|
|
162
|
+
['accession', 'accession-num'],
|
|
163
|
+
['address', 'auth-address'],
|
|
164
|
+
['caption', 'caption'],
|
|
165
|
+
['databaseProvider', 'remote-database-provider'],
|
|
166
|
+
['database', 'remote-database-name'],
|
|
167
|
+
['doi', 'electronic-resource-num'],
|
|
168
|
+
['isbn', 'isbn'],
|
|
169
|
+
['accessionNum', 'accession-num'],
|
|
170
|
+
['label', 'label'],
|
|
171
|
+
['language', 'language'],
|
|
172
|
+
['notes', 'notes'],
|
|
173
|
+
['number', 'number'],
|
|
174
|
+
['pages', 'pages'],
|
|
175
|
+
['researchNotes', 'research-notes'],
|
|
176
|
+
['section', 'section'],
|
|
177
|
+
['volume', 'volume'],
|
|
178
|
+
['workType', 'work-type'],
|
|
179
|
+
['custom1', 'custom1'],
|
|
180
|
+
['custom2', 'custom2'],
|
|
181
|
+
['custom3', 'custom3'],
|
|
182
|
+
['custom4', 'custom4'],
|
|
183
|
+
['custom5', 'custom5'],
|
|
184
|
+
['custom6', 'custom6'],
|
|
185
|
+
['custom7', 'custom7'],
|
|
186
|
+
]
|
|
187
|
+
.filter(([rlKey]) => ref[rlKey]) // Remove empty fields
|
|
188
|
+
.map(([rlKey, rawKey]) =>
|
|
189
|
+
`<${rawKey}><style face="normal" font="default" size="100%">${xmlEscape(ref[rlKey])}</style></${rawKey}>`
|
|
190
|
+
)
|
|
191
|
+
.join('')
|
|
192
|
+
|
|
193
|
+
// Dates
|
|
194
|
+
+ (
|
|
195
|
+
ref.date && ref.year && ref.date instanceof Date ?
|
|
196
|
+
`<dates><year><style face="normal" font="default" size="100%">${xmlEscape(ref.year)}</style></year>`
|
|
197
|
+
+ `<pub-dates><date><style face="normal" font="default" size="100%">${settings.formatDate(ref.date)}</style></date></pub-dates></dates>`
|
|
198
|
+
: ref.date && ref.year ?
|
|
199
|
+
`<dates><year><style face="normal" font="default" size="100%">${xmlEscape(ref.year)}</style></year>`
|
|
200
|
+
+ `<pub-dates><date><style face="normal" font="default" size="100%">${ref.date}</style></date></pub-dates></dates>`
|
|
201
|
+
: ref.date ?
|
|
202
|
+
`<dates><pub-dates><date><style face="normal" font="default" size="100%">${xmlEscape(ref.date)}</style></date></pub-dates></dates>`
|
|
203
|
+
: ref.year ?
|
|
204
|
+
`<dates><year><style face="normal" font="default" size="100%">${xmlEscape(ref.year)}</style></year></dates>`
|
|
205
|
+
: ''
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
// Urls
|
|
209
|
+
+ (ref.urls ?
|
|
210
|
+
'<urls><related-urls>'
|
|
211
|
+
+ [].concat(ref.urls || [])
|
|
212
|
+
.map(url => `<url><style face="normal" font="default" size="100%">${xmlEscape(url)}</style></url>`)
|
|
213
|
+
.join('')
|
|
214
|
+
+ '</related-urls></urls>'
|
|
215
|
+
: '')
|
|
216
|
+
|
|
217
|
+
// Keywords
|
|
218
|
+
+ (ref.keywords ?
|
|
219
|
+
'<keywords>'
|
|
220
|
+
+ [].concat(ref.keywords || [])
|
|
221
|
+
.map(keyword => `<keyword><style face="normal" font="default" size="100%">${xmlEscape(keyword)}</style></keyword>`)
|
|
222
|
+
.join('')
|
|
223
|
+
+ '</keywords>'
|
|
224
|
+
: '')
|
|
225
|
+
|
|
226
|
+
+ '</record>'
|
|
227
|
+
);
|
|
228
|
+
return Promise.resolve();
|
|
229
|
+
},
|
|
230
|
+
end: ()=> {
|
|
231
|
+
stream.write('</records></xml>');
|
|
232
|
+
return new Promise((resolve, reject) =>
|
|
233
|
+
stream.end(err => err ? reject(err) : resolve())
|
|
234
|
+
);
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Utility function to take the raw XML output object and translate it into a Reflib object
|
|
242
|
+
* @param {Object} xRef Raw XML object to process
|
|
243
|
+
* @returns {Object} The translated Reflib object output
|
|
244
|
+
*/
|
|
245
|
+
export function translateRawToRef(xRef) {
|
|
246
|
+
let recOut = {
|
|
247
|
+
...Object.fromEntries(
|
|
248
|
+
translations.fields.collection
|
|
249
|
+
.filter(field => xRef[field.raw]) // Only include fields we have a value for
|
|
250
|
+
.map(field => [ // Translate Raw -> Reflib spec
|
|
251
|
+
field.rl,
|
|
252
|
+
Array.isArray(xRef[field.raw]) ? xRef[field.raw].map(xmlUnescape)
|
|
253
|
+
: xmlUnescape(xRef[field.raw])
|
|
254
|
+
])
|
|
255
|
+
),
|
|
256
|
+
type: translations.types.rawMap.get(+xRef.refType || 17)?.rl,
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
return recOut;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Default string -> XML encoder
|
|
265
|
+
* @param {string} str The input string to encode
|
|
266
|
+
* @returns {string} The XML "safe" string
|
|
267
|
+
*/
|
|
268
|
+
export function xmlEscape(str) {
|
|
269
|
+
return ('' + str)
|
|
270
|
+
.replace(/&/g, '&')
|
|
271
|
+
.replace(/\r/g, ' ')
|
|
272
|
+
.replace(/</g, '<')
|
|
273
|
+
.replace(/>/g, '>')
|
|
274
|
+
.replace(/"/g, '"')
|
|
275
|
+
.replace(/'/g, ''');
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Default XML -> string decodeer
|
|
281
|
+
* @param {string} str The input string to decode
|
|
282
|
+
* @returns {string} The actual string
|
|
283
|
+
*/
|
|
284
|
+
export function xmlUnescape(str) {
|
|
285
|
+
return ('' + str)
|
|
286
|
+
.replace(/&/g, '&')
|
|
287
|
+
.replace(/&#(xD|13);/g, '\r')
|
|
288
|
+
.replace(/</g, '<')
|
|
289
|
+
.replace(/>/g, '>')
|
|
290
|
+
.replace(/"/g, '"')
|
|
291
|
+
.replace(/'/g, "'")
|
|
292
|
+
.replace(/\s+$/gm, '') // Trim line-end whitespace
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Lookup tables for this module
|
|
298
|
+
* @type {Object}
|
|
299
|
+
* @property {array<Object>} fields Field translations between Reflib (`rl`) and the raw format (`raw`)
|
|
300
|
+
* @property {array<Object>} types Field translations between Reflib (`rl`) and the raw format types as raw text (`rawText`) and numeric ID (`rawId`)
|
|
301
|
+
*/
|
|
302
|
+
export let translations = {
|
|
303
|
+
// Field translations {{{
|
|
304
|
+
fields: {
|
|
305
|
+
collection: [
|
|
306
|
+
// Field translations in priority order (Endnote first then Zotero)
|
|
307
|
+
{rl: 'recNumber', raw: 'rec-number'},
|
|
308
|
+
{rl: 'title', raw: 'title'},
|
|
309
|
+
{rl: 'journal', raw: 'secondary-title'},
|
|
310
|
+
{rl: 'address', raw: 'auth-address'},
|
|
311
|
+
{rl: 'researchNotes', raw: 'research-notes'},
|
|
312
|
+
{rl: 'authors', raw: 'authors'},
|
|
313
|
+
{rl: 'pages', raw: 'pages'},
|
|
314
|
+
{rl: 'volume', raw: 'volume'},
|
|
315
|
+
{rl: 'number', raw: 'number'},
|
|
316
|
+
{rl: 'isbn', raw: 'isbn'},
|
|
317
|
+
{rl: 'accessionNum', raw: 'accession-num'},
|
|
318
|
+
{rl: 'abstract', raw: 'abstract'},
|
|
319
|
+
{rl: 'label', raw: 'label'},
|
|
320
|
+
{rl: 'caption', raw: 'caption'},
|
|
321
|
+
{rl: 'notes', raw: 'notes'},
|
|
322
|
+
{rl: 'custom1', raw: 'custom1'},
|
|
323
|
+
{rl: 'custom2', raw: 'custom2'},
|
|
324
|
+
{rl: 'custom3', raw: 'custom3'},
|
|
325
|
+
{rl: 'custom4', raw: 'custom4'},
|
|
326
|
+
{rl: 'custom5', raw: 'custom5'},
|
|
327
|
+
{rl: 'custom6', raw: 'custom6'},
|
|
328
|
+
{rl: 'custom7', raw: 'custom7'},
|
|
329
|
+
{rl: 'doi', raw: 'electronic-resource-num'},
|
|
330
|
+
{rl: 'year', raw: 'year'},
|
|
331
|
+
{rl: 'date', raw: 'date'},
|
|
332
|
+
{rl: 'language', raw: 'language'},
|
|
333
|
+
],
|
|
334
|
+
rawMap: new Map(), // Calculated later for quicker lookup
|
|
335
|
+
},
|
|
336
|
+
// }}}
|
|
337
|
+
// Ref type translations {{{
|
|
338
|
+
types: {
|
|
339
|
+
collection: [
|
|
340
|
+
{rl: 'aggregatedDatabase', rawText: 'Aggregated Database', rawId: 55},
|
|
341
|
+
{rl: 'ancientText', rawText: 'Ancient Text', rawId: 51},
|
|
342
|
+
{rl: 'artwork', rawText: 'Artwork', rawId: 2},
|
|
343
|
+
{rl: 'audioVisualMaterial', rawText: 'Audiovisual Material', rawId: 3},
|
|
344
|
+
{rl: 'bill', rawText: 'Bill', rawId: 4},
|
|
345
|
+
{rl: 'blog', rawText: 'Blog', rawId: 56},
|
|
346
|
+
{rl: 'book', rawText: 'Book', rawId: 6},
|
|
347
|
+
{rl: 'bookSection', rawText: 'Book Section', rawId: 5},
|
|
348
|
+
{rl: 'case', rawText: 'Case', rawId: 7},
|
|
349
|
+
{rl: 'catalog', rawText: 'Catalog', rawId: 8},
|
|
350
|
+
{rl: 'chartOrTable', rawText: 'Chart or Table', rawId: 38},
|
|
351
|
+
{rl: 'classicalWork', rawText: 'Classical Work', rawId: 49},
|
|
352
|
+
{rl: 'computerProgram', rawText: 'Computer Program', rawId: 9},
|
|
353
|
+
{rl: 'conferencePaper', rawText: 'Conference Paper', rawId: 47},
|
|
354
|
+
{rl: 'conferenceProceedings', rawText: 'Conference Proceedings', rawId: 10},
|
|
355
|
+
{rl: 'dataset', rawText: 'Dataset', rawId: 59},
|
|
356
|
+
{rl: 'dictionary', rawText: 'Dictionary', rawId: 52},
|
|
357
|
+
{rl: 'editedBook', rawText: 'Edited Book', rawId: 28},
|
|
358
|
+
{rl: 'electronicArticle', rawText: 'Electronic Article', rawId: 43},
|
|
359
|
+
{rl: 'electronicBook', rawText: 'Electronic Book', rawId: 44},
|
|
360
|
+
{rl: 'electronicBookSection', rawText: 'Electronic Book Section', rawId: 60},
|
|
361
|
+
{rl: 'encyclopedia', rawText: 'Encyclopedia', rawId: 53},
|
|
362
|
+
{rl: 'equation', rawText: 'Equation', rawId: 39},
|
|
363
|
+
{rl: 'figure', rawText: 'Figure', rawId: 37},
|
|
364
|
+
{rl: 'filmOrBroadcast', rawText: 'Film or Broadcast', rawId: 21},
|
|
365
|
+
{rl: 'generic', rawText: 'Generic', rawId: 13},
|
|
366
|
+
{rl: 'governmentDocument', rawText: 'Government Document', rawId: 46},
|
|
367
|
+
{rl: 'grant', rawText: 'Grant', rawId: 54},
|
|
368
|
+
{rl: 'hearing', rawText: 'Hearing', rawId: 14},
|
|
369
|
+
{rl: 'journalArticle', rawText: 'Journal Article', rawId: 17},
|
|
370
|
+
{rl: 'legalRuleOrRegulation', rawText: 'Legal Rule or Regulation', rawId: 50},
|
|
371
|
+
{rl: 'magazineArticle', rawText: 'Magazine Article', rawId: 19},
|
|
372
|
+
{rl: 'manuscript', rawText: 'Manuscript', rawId: 36},
|
|
373
|
+
{rl: 'map', rawText: 'Map', rawId: 20},
|
|
374
|
+
{rl: 'music', rawText: 'Music', rawId: 61},
|
|
375
|
+
{rl: 'newspaperArticle', rawText: 'Newspaper Article', rawId: 23},
|
|
376
|
+
{rl: 'onlineDatabase', rawText: 'Online Database', rawId: 45},
|
|
377
|
+
{rl: 'onlineMultimedia', rawText: 'Online Multimedia', rawId: 48},
|
|
378
|
+
{rl: 'pamphlet', rawText: 'Pamphlet', rawId: 24},
|
|
379
|
+
{rl: 'patent', rawText: 'Patent', rawId: 25},
|
|
380
|
+
{rl: 'personalCommunication', rawText: 'Personal Communication', rawId: 26},
|
|
381
|
+
{rl: 'report', rawText: 'Report', rawId: 27},
|
|
382
|
+
{rl: 'serial', rawText: 'Serial', rawId: 57},
|
|
383
|
+
{rl: 'standard', rawText: 'Standard', rawId: 58},
|
|
384
|
+
{rl: 'statute', rawText: 'Statute', rawId: 31},
|
|
385
|
+
{rl: 'thesis', rawText: 'Thesis', rawId: 32},
|
|
386
|
+
{rl: 'unpublished', rawText: 'Unpublished Work', rawId: 34},
|
|
387
|
+
{rl: 'web', rawText: 'Web Page', rawId: 12},
|
|
388
|
+
],
|
|
389
|
+
rlMap: new Map(), // Calculated later for quicker lookup
|
|
390
|
+
rawMap: new Map(), // Calculated later for quicker lookup
|
|
391
|
+
},
|
|
392
|
+
// }}}
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* @see modules/interface.js
|
|
398
|
+
*/
|
|
399
|
+
export function setup() {
|
|
400
|
+
// Create lookup object of translations.field translations
|
|
401
|
+
translations.fields.collection.forEach(c => {
|
|
402
|
+
translations.fields.rawMap.set(c.raw, c);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
// Create lookup object of translations.types with key as .rl / val as the full object
|
|
406
|
+
translations.types.collection.forEach(c => {
|
|
407
|
+
translations.types.rlMap.set(c.rl, c);
|
|
408
|
+
translations.types.rawMap.set(c.rawId, c);
|
|
409
|
+
});
|
|
410
|
+
}
|