@iebh/reflib 2.6.1 → 2.6.3
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/modules/bibtex.js +26 -3
- package/package.json +1 -1
package/modules/bibtex.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import Emitter from '../shared/emitter.js';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Generate a citation key from first author + year
|
|
4
|
+
* Example: "Roomruangwong2020"
|
|
5
|
+
*/
|
|
6
|
+
function generateCitationKey(ref) {
|
|
7
|
+
let author = ref.authors?.[0] || ref.author?.split(/,|\sand\s/)[0] || 'Anon';
|
|
8
|
+
author = author.replace(/\s+/g, '').replace(/[^a-zA-Z]/g, ''); // remove spaces & non-letters
|
|
9
|
+
let year = ref.year || new Date().getFullYear();
|
|
10
|
+
return `${author}${year}`;
|
|
11
|
+
}
|
|
3
12
|
/**
|
|
4
13
|
* Lookup enum for the current parser mode we are in
|
|
5
14
|
*
|
|
@@ -22,6 +31,7 @@ const MODES = {
|
|
|
22
31
|
* @param {Object} [options] Additional options to use when parsing
|
|
23
32
|
* @param {Boolean} [options.recNumberNumeric=true] Only process the BibTeX ID into a recNumber if its a finite numeric, otherwise disguard
|
|
24
33
|
* @param {Boolean} [options.recNumberRNPrefix=true] Accept `RN${NUMBER}` as recNumber if present
|
|
34
|
+
* @param {Boolean} [options.recNumberKey=true] If the reference key cannot be otherwise parsed store it in `key<String>` instead
|
|
25
35
|
* @param {Boolean} [options.omitUnkown=false] If true, only keep known reconised fields
|
|
26
36
|
* @param {String} [options.fallbackType='unkown'] Reflib fallback type if the incoming type is unrecognised or unsupported
|
|
27
37
|
* @param {Set<String>} [options.fieldsOverwrite] Set of field names where the value is clobbered rather than appended if discovered more than once
|
|
@@ -32,6 +42,7 @@ export function readStream(stream, options) {
|
|
|
32
42
|
let settings = {
|
|
33
43
|
recNumberNumeric: true,
|
|
34
44
|
recNumberRNPrefix: true,
|
|
45
|
+
recNumberKey: true,
|
|
35
46
|
omitUnknown: false,
|
|
36
47
|
fallbackType: 'unknown',
|
|
37
48
|
fieldsOverwrite: new Set(['type']),
|
|
@@ -55,14 +66,23 @@ export function readStream(stream, options) {
|
|
|
55
66
|
|
|
56
67
|
while (true) {
|
|
57
68
|
let match; // Regex storage for match groups
|
|
69
|
+
console.log("Here 0")
|
|
58
70
|
if ((mode == MODES.REF) && (match = /^\s*@(?<type>\w+?)\s*\{(?<id>.*?),/s.exec(buffer))) {
|
|
71
|
+
console.log("Here 1")
|
|
59
72
|
if (settings.recNumberNumeric && isFinite(match.groups.id)) { // Accept numeric recNumber
|
|
60
73
|
ref.recNumber = +match.groups.id;
|
|
61
74
|
} else if (settings.recNumberRNPrefix && /^RN\d+$/.test(match.groups.id)) {
|
|
62
75
|
ref.recNumber = +match.groups.id.slice(2);
|
|
63
76
|
} else if (!settings.recNumberNumeric && match.groups.id) { // Non numeric / finite ID - but we're allowed to accept it anyway
|
|
64
77
|
ref.recNumber = +match.groups.id;
|
|
78
|
+
} else if (settings.recNumberKey) { // Non numeric, custom looking key, stash in 'key' instead
|
|
79
|
+
ref.key = match.groups.id;
|
|
80
|
+
console.log("Here 2")
|
|
65
81
|
} // Implied else - No ID, ignore
|
|
82
|
+
else {
|
|
83
|
+
console.log("Here 3")
|
|
84
|
+
ref.key = generateCitationKey(ref);
|
|
85
|
+
}
|
|
66
86
|
|
|
67
87
|
ref.type = match.groups.type;
|
|
68
88
|
mode = MODES.FIELDS;
|
|
@@ -193,6 +213,7 @@ export function escape(str) {
|
|
|
193
213
|
* @param {Boolean} [options.omitUnkown=false] If true, only keep known reconised fields
|
|
194
214
|
* @param {Set} [options.omitFields] Set of special fields to always omit, either because we are ignoring or because we have special treatment for them
|
|
195
215
|
* @param {Boolean} [options.recNumberRNPrefix=true] Rewrite recNumber fields as `RN${NUMBER}`
|
|
216
|
+
* @param {Boolean} [options.recNumberKey=true] If the reference `recNumber` is empty use `key<String>` instead
|
|
196
217
|
*
|
|
197
218
|
* @returns {Object} A writable stream analogue defined in `modules/interface.js`
|
|
198
219
|
*/
|
|
@@ -201,8 +222,9 @@ export function writeStream(stream, options) {
|
|
|
201
222
|
defaultType: 'Misc',
|
|
202
223
|
delimeter: '\n',
|
|
203
224
|
omitUnkown: false,
|
|
204
|
-
omitFields: new Set(['recNumber', 'type']),
|
|
225
|
+
omitFields: new Set(['key', 'recNumber', 'type']),
|
|
205
226
|
recNumberRNPrefix: true,
|
|
227
|
+
recNumberKey: true,
|
|
206
228
|
...options,
|
|
207
229
|
};
|
|
208
230
|
|
|
@@ -212,7 +234,7 @@ export function writeStream(stream, options) {
|
|
|
212
234
|
},
|
|
213
235
|
write: ref => {
|
|
214
236
|
// Fetch Reflib type definition
|
|
215
|
-
let rlType = ref.type && translations.types.rlMap.get(ref.type.toLowerCase());
|
|
237
|
+
let rlType = (ref.type || settings.defaultType) && translations.types.rlMap.get(ref.type.toLowerCase());
|
|
216
238
|
let btType = rlType?.bt || settings.defaultType;
|
|
217
239
|
|
|
218
240
|
stream.write(
|
|
@@ -220,6 +242,7 @@ export function writeStream(stream, options) {
|
|
|
220
242
|
+ (
|
|
221
243
|
ref.recNumber && settings.recNumberRNPrefix ? `RN${ref.recNumber},`
|
|
222
244
|
: ref.recNumber ? `${ref.recNumber},`
|
|
245
|
+
: ref.key ? `${ref.key},`
|
|
223
246
|
: ''
|
|
224
247
|
) + '\n'
|
|
225
248
|
+ Object.entries(ref)
|