@retorquere/bibtex-parser 6.4.17 → 6.4.19
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.js +1 -1
- package/package.json +5 -2
- package/sentence-case.d.ts +8 -0
- package/sentence-case.js +89 -0
package/index.js
CHANGED
|
@@ -1357,7 +1357,7 @@ class Parser {
|
|
|
1357
1357
|
convertToSentenceCase(text) {
|
|
1358
1358
|
if (!this.field.preserveRanges)
|
|
1359
1359
|
return text;
|
|
1360
|
-
const sentenceCased = (0, sentence_case_2.restore)((0, sentence_case_2.toSentenceCase)(text
|
|
1360
|
+
const sentenceCased = (0, sentence_case_2.restore)((0, sentence_case_2.toSentenceCase)(text), text, this.field.preserveRanges);
|
|
1361
1361
|
if (text !== sentenceCased)
|
|
1362
1362
|
this.entry.sentenceCased = true;
|
|
1363
1363
|
return sentenceCased;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@retorquere/bibtex-parser",
|
|
3
|
-
"version": "6.4.
|
|
3
|
+
"version": "6.4.19",
|
|
4
4
|
"description": "BibTeX parser",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"typings": "./index.d.ts",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"lint": "eslint . --ext .ts --cache --cache-location .eslintcache/",
|
|
9
9
|
"prestart": "npm run lint && peggy grammar.peggy",
|
|
10
10
|
"start": "tsc -d",
|
|
11
|
-
"
|
|
11
|
+
"tsc": "tsc -d",
|
|
12
|
+
"poststart": "echo npm run docs",
|
|
12
13
|
"pretest": "npm start",
|
|
13
14
|
"test": "tap test/tap --no-timeout --no-coverage",
|
|
14
15
|
"snap": "npm test --snapshot",
|
|
@@ -75,6 +76,8 @@
|
|
|
75
76
|
"jabref.js",
|
|
76
77
|
"README.md",
|
|
77
78
|
"crossref.json",
|
|
79
|
+
"sentence-case.js",
|
|
80
|
+
"sentence-case.d.ts",
|
|
78
81
|
"fields.json",
|
|
79
82
|
"strings.bib",
|
|
80
83
|
"unabbrev.json"
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type TextRange = {
|
|
2
|
+
start: number;
|
|
3
|
+
end: number;
|
|
4
|
+
description?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function restore(text: string, orig: string, preserve: TextRange[]): string;
|
|
7
|
+
export declare function toSentenceCase(text: string): string;
|
|
8
|
+
//# sourceMappingURL=sentence-case.d.ts.map
|
package/sentence-case.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toSentenceCase = exports.restore = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
5
|
+
const categories = require("xregexp/tools/output/categories");
|
|
6
|
+
const cat = name => categories.find(c => c.name === name).bmp;
|
|
7
|
+
const L = cat('L');
|
|
8
|
+
const Lu = cat('Lu');
|
|
9
|
+
const Ll = cat('Ll');
|
|
10
|
+
const N = cat('N');
|
|
11
|
+
const No = cat('No');
|
|
12
|
+
const Pc = cat('Pc');
|
|
13
|
+
function restore(text, orig, preserve) {
|
|
14
|
+
for (const { start, end } of preserve) {
|
|
15
|
+
text = text.substring(0, start) + orig.substring(start, end) + text.substring(end);
|
|
16
|
+
}
|
|
17
|
+
return text;
|
|
18
|
+
}
|
|
19
|
+
exports.restore = restore;
|
|
20
|
+
const re = {
|
|
21
|
+
acronym: new RegExp(`([${Lu}][.])+$`),
|
|
22
|
+
innerCaps: new RegExp(`.[${Lu}]`),
|
|
23
|
+
ident: new RegExp(`^[$p{L}]+[${N}${No}][${L}${N}${No}]*$`),
|
|
24
|
+
allCaps: new RegExp(`^[${Lu}${N}${No}]+$`),
|
|
25
|
+
skipWords: /^(but|or|yet|so|for|and|nor|a|an|the|at|by|from|in|into|of|on|to|with|updown|as)$/i,
|
|
26
|
+
// chemElements: /^(H|He|Li|Be|B|C|N|O|F|Ne|Na|Mg|Al|Si|P|S|Cl|Ar|K|Ca|Sc|Ti|V|Cr|Mn|Fe|Co|Ni|Cu|Zn|Ga|Ge|As|Se|Br|Kr|Rb|Sr|Y|Zr|Nb|Mo|Tc|Ru|Rh|Pb|Ag|Cd|In|Sn|Sb|Te|I|Xe|Cs|Ba|La|Hf|Ta|W|Re|Os|Ir|Pt|Au|Hg|Tl|Pb|Bi|Po|At|Rn|Fr|Ra|Ac|Rf|Db|Sg|Bh|Hs|Mt|Ds|Rg|Cn|Nh|Fl|Mc|Lv|Ts|Og|La|Ce|Pr|Nd|Pm|Sm|Eu|Gd|Tb|Dy|Ho|Er|Tm|Yb|Lu|Ac|Th|Pa|U|Np|Pu|Am|Cm|Bk|Cf|Es|Fm|Md|No|Lr)$/,
|
|
27
|
+
words: new RegExp(`([\uFFFD${L}${N}${No}]+([\uFFFD${Pc}${L}${N}${No}]*))|(\\s([\uFFFD${Lu}]+[.]){2,})?`, 'g'),
|
|
28
|
+
titleCase: new RegExp(`^[${Lu}][${Ll}${N}${No}]+$`),
|
|
29
|
+
// private aint = XRegExp("^\\p{L}n't(?=$|[\\P{L}])") // isn't
|
|
30
|
+
// private word = XRegExp('^\\p{L}+([-.]\\p{L}+)*') // also match gallium-nitride as one word
|
|
31
|
+
// private and = XRegExp('^\\p{Lu}&\\p{Lu}(?=$|[\\P{L}])') // Q&A
|
|
32
|
+
};
|
|
33
|
+
function lowercase(word) {
|
|
34
|
+
if (!word)
|
|
35
|
+
return word;
|
|
36
|
+
const unmasked = word.replace(/\uFFFD/g, '');
|
|
37
|
+
if (unmasked.match(re.skipWords))
|
|
38
|
+
return word.toLowerCase();
|
|
39
|
+
if (unmasked.match(re.titleCase))
|
|
40
|
+
return word.toLowerCase();
|
|
41
|
+
// if (unmasked.match(re.chemElements)) return word
|
|
42
|
+
if (unmasked.length === 1)
|
|
43
|
+
return unmasked === 'A' ? word.toLowerCase() : word;
|
|
44
|
+
if (unmasked.match(re.innerCaps))
|
|
45
|
+
return word;
|
|
46
|
+
if (unmasked.match(re.ident) || unmasked.match(re.allCaps))
|
|
47
|
+
return word;
|
|
48
|
+
return word.toLowerCase();
|
|
49
|
+
}
|
|
50
|
+
function toSentenceCase(text) {
|
|
51
|
+
const preserve = [];
|
|
52
|
+
text.replace(/“.*?”/g, (match, i) => {
|
|
53
|
+
preserve.push({ start: i, end: i + match.length, description: 'quoted' });
|
|
54
|
+
return '';
|
|
55
|
+
});
|
|
56
|
+
text.replace(/‘.*?’/g, (match, i) => {
|
|
57
|
+
preserve.push({ start: i, end: i + match.length, description: 'quoted' });
|
|
58
|
+
return '';
|
|
59
|
+
});
|
|
60
|
+
text.replace(/(["]).*?\1/g, (match, _quote, i) => {
|
|
61
|
+
preserve.push({ start: i, end: i + match.length, description: 'quoted' });
|
|
62
|
+
return '';
|
|
63
|
+
});
|
|
64
|
+
text.replace(/([.?!][\s]+)(<[^>]+>)?([A-Z])/g, (match, end, markup, char, i) => {
|
|
65
|
+
if (!text.substring(0, i + 1).match(re.acronym)) {
|
|
66
|
+
preserve.push({ start: i + end.length + ((markup === null || markup === void 0 ? void 0 : markup.length) || 0), end: i + end.length + ((markup === null || markup === void 0 ? void 0 : markup.length) || 0) + char.length, description: 'sub-sentence-start' });
|
|
67
|
+
}
|
|
68
|
+
return '';
|
|
69
|
+
});
|
|
70
|
+
text.replace(/^(<[^>]+>)?([A-Z])/, (match, markup, char) => {
|
|
71
|
+
preserve.push({ start: ((markup === null || markup === void 0 ? void 0 : markup.length) || 0), end: ((markup === null || markup === void 0 ? void 0 : markup.length) || 0) + char.length, description: 'sentence-start' });
|
|
72
|
+
return '';
|
|
73
|
+
});
|
|
74
|
+
text.replace(/<span class="nocase">.*?<\/span>|<nc>.*?<\/nc>/gi, (match, i) => {
|
|
75
|
+
preserve.push({ start: i, end: i + match.length, description: 'nocase' });
|
|
76
|
+
return '';
|
|
77
|
+
});
|
|
78
|
+
let masked = text.replace(/<[^>]+>/g, (match, i) => {
|
|
79
|
+
preserve.push({ start: i, end: i + match.length, description: 'markup' });
|
|
80
|
+
return '\uFFFD'.repeat(match.length);
|
|
81
|
+
});
|
|
82
|
+
masked = masked
|
|
83
|
+
.replace(/[;:]\uFFFD*\s+\uFFFD*A\s/g, match => match.toLowerCase())
|
|
84
|
+
.replace(/[–—]\uFFFD*\s*\uFFFD*A\s/g, match => match.toLowerCase())
|
|
85
|
+
.replace(re.words, word => lowercase(word));
|
|
86
|
+
return restore(masked, text, preserve);
|
|
87
|
+
}
|
|
88
|
+
exports.toSentenceCase = toSentenceCase;
|
|
89
|
+
//# sourceMappingURL=sentence-case.js.map
|