@iebh/reflib 2.6.1 → 2.6.2

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