@retorquere/bibtex-parser 6.2.2 → 6.3.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/index.d.ts +1 -1
- package/index.js +73 -35
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as bibtex from './grammar';
|
|
2
2
|
import { JabRefMetadata } from './jabref';
|
|
3
|
-
export declare function toSentenceCase(text: string): string;
|
|
3
|
+
export declare function toSentenceCase(text: string, ignoreHTML?: boolean): string;
|
|
4
4
|
declare type Node = bibtex.Bibliography | bibtex.Math | bibtex.Environment | bibtex.Block | bibtex.RegularCommand | bibtex.DiacriticCommand | bibtex.Entry | bibtex.Field | bibtex.NumberValue | bibtex.StringDeclaration | bibtex.StringReference | bibtex.SubscriptCommand | bibtex.SuperscriptCommand | bibtex.SymbolCommand | bibtex.TextValue | bibtex.PreambleExpression | bibtex.BracedComment | bibtex.LineComment | bibtex.Markup | bibtex.NonEntryText;
|
|
5
5
|
export interface Name {
|
|
6
6
|
/**
|
package/index.js
CHANGED
|
@@ -8,52 +8,101 @@ const chunker = tslib_1.__importStar(require("./chunker"));
|
|
|
8
8
|
const jabref_1 = require("./jabref");
|
|
9
9
|
const unicode2latex_1 = require("unicode2latex");
|
|
10
10
|
const crossref_json_1 = tslib_1.__importDefault(require("./crossref.json"));
|
|
11
|
+
const xregexp_1 = tslib_1.__importDefault(require("xregexp"));
|
|
12
|
+
function restore(text, orig, preserve) {
|
|
13
|
+
for (const { start, end } of preserve) {
|
|
14
|
+
text = text.substring(0, start) + orig.substring(start, end) + text.substring(end);
|
|
15
|
+
}
|
|
16
|
+
return text;
|
|
17
|
+
}
|
|
11
18
|
class SentenceCaser {
|
|
12
19
|
constructor() {
|
|
13
|
-
this.acronym =
|
|
20
|
+
this.acronym = (0, xregexp_1.default)('^(\\p{Lu}[.])+(?=$|[\\P{L}])');
|
|
21
|
+
this.quoted = (0, xregexp_1.default)('^"[^"]+"(?=$|[\\P{L}])');
|
|
22
|
+
this.innerCaps = (0, xregexp_1.default)('\\p{Ll}\\p{Lu}');
|
|
23
|
+
this.allCaps = (0, xregexp_1.default)('^\\p{Lu}+$');
|
|
24
|
+
this.aint = (0, xregexp_1.default)("^\\p{L}n't(?=$|[\\P{L}])"); // isn't
|
|
25
|
+
this.word = (0, xregexp_1.default)('^\\p{L}+(-\\p{L}+)*'); // also match gallium-nitride as one word
|
|
26
|
+
this.and = (0, xregexp_1.default)('^\\p{Lu}&\\p{Lu}(?=$|[\\P{L}])'); // Q&A
|
|
14
27
|
}
|
|
15
|
-
convert(text) {
|
|
28
|
+
convert(text, ignoreHTML = false) {
|
|
16
29
|
this.input = text;
|
|
17
30
|
this.result = '';
|
|
18
31
|
this.sentenceStart = true;
|
|
32
|
+
const preserve = [];
|
|
33
|
+
if (ignoreHTML) {
|
|
34
|
+
let replace = true;
|
|
35
|
+
while (replace) {
|
|
36
|
+
replace = false;
|
|
37
|
+
// always keep the leading char, but skip markup
|
|
38
|
+
this.input = this.input.replace(/[^<>]<[^>]+>/, (match, i) => {
|
|
39
|
+
preserve.push({ start: i + 1, end: i + match.length });
|
|
40
|
+
replace = true;
|
|
41
|
+
return match[0].repeat(match.length);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
replace = true;
|
|
45
|
+
while (replace) {
|
|
46
|
+
replace = false;
|
|
47
|
+
// always keep the leading char, but skip markup
|
|
48
|
+
this.input = this.input.replace(/<[^>]+>[^<>]/, (match, i) => {
|
|
49
|
+
preserve.push({ start: i, end: i + match.length - 1 });
|
|
50
|
+
replace = true;
|
|
51
|
+
return match[match.length - 1].repeat(match.length);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
this.input = this.input.replace(/[;:]\s+A\s/g, match => match.toLowerCase());
|
|
56
|
+
this.input = this.input.replace(/[–—]\s*A\s/g, match => match.toLowerCase());
|
|
19
57
|
let m;
|
|
20
58
|
while (this.input) {
|
|
21
|
-
if (m =
|
|
22
|
-
this.add(m[0], true);
|
|
59
|
+
if (m = xregexp_1.default.exec(this.input, this.quoted)) { // "Hello There"
|
|
60
|
+
this.add(m[0], undefined, true);
|
|
61
|
+
}
|
|
62
|
+
else if (m = xregexp_1.default.exec(this.input, this.acronym)) { // U.S.
|
|
63
|
+
this.add(m[0], undefined, true);
|
|
64
|
+
}
|
|
65
|
+
else if (m = xregexp_1.default.exec(this.input, this.aint)) { // isn't
|
|
66
|
+
this.add(m[0], undefined, false);
|
|
23
67
|
}
|
|
24
|
-
else if (m = this.input.
|
|
25
|
-
this.add(m[0]);
|
|
68
|
+
else if (m = xregexp_1.default.exec(this.input, this.word)) {
|
|
69
|
+
this.add(m[0], '-', false);
|
|
26
70
|
}
|
|
27
|
-
else if (m = this.input.
|
|
28
|
-
this.add(m[0]);
|
|
71
|
+
else if (m = xregexp_1.default.exec(this.input, this.and)) {
|
|
72
|
+
this.add(m[0], undefined, true);
|
|
29
73
|
}
|
|
30
74
|
else {
|
|
31
|
-
this.add(this.input[0]);
|
|
75
|
+
this.add(this.input[0], undefined, false);
|
|
32
76
|
}
|
|
33
77
|
}
|
|
34
|
-
return this.result;
|
|
78
|
+
return restore(this.result, text, preserve);
|
|
35
79
|
}
|
|
36
|
-
add(word, keep
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
80
|
+
add(word, splitter, keep) {
|
|
81
|
+
if (splitter) {
|
|
82
|
+
word = word.split(splitter).map((part, i) => {
|
|
83
|
+
if ((keep || this.sentenceStart) && i === 0)
|
|
84
|
+
return part;
|
|
85
|
+
if (xregexp_1.default.exec(part, this.innerCaps))
|
|
86
|
+
return part;
|
|
87
|
+
if (xregexp_1.default.exec(part, this.allCaps))
|
|
88
|
+
return part;
|
|
89
|
+
return part.toLowerCase();
|
|
90
|
+
}).join(splitter);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
if (!keep)
|
|
43
94
|
word = word.toLowerCase();
|
|
44
|
-
result.push(word);
|
|
45
95
|
}
|
|
46
|
-
word = result.join('-');
|
|
47
96
|
this.result += word;
|
|
48
97
|
this.input = this.input.substr(word.length);
|
|
49
98
|
if (!word.match(/^\s+$/)) {
|
|
50
|
-
this.sentenceStart = !!word.match(/^[.?!]$/);
|
|
99
|
+
this.sentenceStart = !!word.match(/^[.?!]$/) || (word.length === 2 && word[1] === '.'); // Vitamin A. Vitamin B.
|
|
51
100
|
}
|
|
52
101
|
}
|
|
53
102
|
}
|
|
54
103
|
const sentenceCaser = new SentenceCaser;
|
|
55
|
-
function toSentenceCase(text) {
|
|
56
|
-
return sentenceCaser.convert(text);
|
|
104
|
+
function toSentenceCase(text, ignoreHTML = false) {
|
|
105
|
+
return sentenceCaser.convert(text, ignoreHTML);
|
|
57
106
|
}
|
|
58
107
|
exports.toSentenceCase = toSentenceCase;
|
|
59
108
|
class ParserError extends Error {
|
|
@@ -401,7 +450,7 @@ class Parser {
|
|
|
401
450
|
if (start < last.start) throw new Error(JSON.stringify({...last, new: { start, end, reason }, text: this.field.text}))
|
|
402
451
|
}
|
|
403
452
|
*/
|
|
404
|
-
this.field.preserveRanges.push({ start, end, reason });
|
|
453
|
+
this.field.preserveRanges.push({ start, end, description: reason });
|
|
405
454
|
}
|
|
406
455
|
parseChunk(chunk) {
|
|
407
456
|
this.chunk = chunk.text;
|
|
@@ -1324,18 +1373,7 @@ class Parser {
|
|
|
1324
1373
|
convertToSentenceCase(text) {
|
|
1325
1374
|
if (!this.field.preserveRanges)
|
|
1326
1375
|
return text;
|
|
1327
|
-
|
|
1328
|
-
const lead = text.match(/^(<[^>]+>)*./);
|
|
1329
|
-
if (lead) {
|
|
1330
|
-
this.preserve(lead[0].length - 1, lead[0].length);
|
|
1331
|
-
}
|
|
1332
|
-
else {
|
|
1333
|
-
this.preserve(0, 1);
|
|
1334
|
-
}
|
|
1335
|
-
let sentenceCased = toSentenceCase(text);
|
|
1336
|
-
for (const { start, end } of this.field.preserveRanges) {
|
|
1337
|
-
sentenceCased = sentenceCased.substring(0, start) + text.substring(start, end) + sentenceCased.substring(end);
|
|
1338
|
-
}
|
|
1376
|
+
const sentenceCased = restore(sentenceCaser.convert(text, true), text, this.field.preserveRanges);
|
|
1339
1377
|
if (text !== sentenceCased)
|
|
1340
1378
|
this.entry.sentenceCased = true;
|
|
1341
1379
|
return sentenceCased;
|