@retorquere/bibtex-parser 6.1.4 → 6.3.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.
Files changed (3) hide show
  1. package/index.d.ts +1 -0
  2. package/index.js +92 -14
  3. package/package.json +12 -12
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as bibtex from './grammar';
2
2
  import { JabRefMetadata } from './jabref';
3
+ export declare function toSentenceCase(text: string, ignoreHTML?: boolean): string;
3
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;
4
5
  export interface Name {
5
6
  /**
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.jabref = exports.chunker = exports.promises = exports.ast = exports.parse = exports.fields = void 0;
3
+ exports.jabref = exports.chunker = exports.promises = exports.ast = exports.parse = exports.fields = exports.toSentenceCase = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  /* eslint-disable @typescript-eslint/no-unsafe-argument */
6
6
  const bibtex = tslib_1.__importStar(require("./grammar"));
@@ -8,6 +8,95 @@ 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
+ }
18
+ class SentenceCaser {
19
+ constructor() {
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
+ }
27
+ convert(text, ignoreHTML = false) {
28
+ this.input = text;
29
+ this.result = '';
30
+ this.sentenceStart = true;
31
+ const preserve = [];
32
+ if (ignoreHTML) {
33
+ let replace = true;
34
+ while (replace) {
35
+ replace = false;
36
+ // always keep the leading char, but skip markup
37
+ this.input = this.input.replace(/[^<>]<[^>]+>/, (match, i) => {
38
+ preserve.push({ start: i + 1, end: i + match.length });
39
+ replace = true;
40
+ return match[0].repeat(match.length);
41
+ });
42
+ }
43
+ replace = true;
44
+ while (replace) {
45
+ replace = false;
46
+ // always keep the leading char, but skip markup
47
+ this.input = this.input.replace(/<[^>]+>[^<>]/, (match, i) => {
48
+ preserve.push({ start: i, end: i + match.length - 1 });
49
+ replace = true;
50
+ return match[match.length - 1].repeat(match.length);
51
+ });
52
+ }
53
+ }
54
+ let m;
55
+ while (this.input) {
56
+ if (m = xregexp_1.default.exec(this.input, this.quoted)) { // "Hello There"
57
+ this.add(m[0], true);
58
+ }
59
+ else if (m = xregexp_1.default.exec(this.input, this.acronym)) { // U.S.
60
+ this.add(m[0], m[0].length > 2);
61
+ }
62
+ else if (m = xregexp_1.default.exec(this.input, this.aint)) { // isn't
63
+ this.add(m[0]);
64
+ }
65
+ else if (m = xregexp_1.default.exec(this.input, this.word)) {
66
+ this.add(m[0]);
67
+ }
68
+ else {
69
+ this.add(this.input[0]);
70
+ }
71
+ }
72
+ return restore(this.result, text, preserve);
73
+ }
74
+ add(word, keep = false) {
75
+ word = word.split('-').map((part, i, parts) => {
76
+ if ((keep || this.sentenceStart) && i === 0)
77
+ return part;
78
+ if (xregexp_1.default.exec(part, this.acronym) && part.length === 2)
79
+ return part.toLowerCase(); // name initial?
80
+ if (part === 'A' && parts.length === 1)
81
+ return part.toLowerCase();
82
+ if (xregexp_1.default.exec(part, this.innerCaps))
83
+ return part;
84
+ if (xregexp_1.default.exec(part, this.allCaps))
85
+ return part;
86
+ return part.toLowerCase();
87
+ }).join('-');
88
+ this.result += word;
89
+ this.input = this.input.substr(word.length);
90
+ if (!word.match(/^\s+$/)) {
91
+ this.sentenceStart = !!word.match(/^[.?!]$/);
92
+ }
93
+ }
94
+ }
95
+ const sentenceCaser = new SentenceCaser;
96
+ function toSentenceCase(text, ignoreHTML = false) {
97
+ return sentenceCaser.convert(text, ignoreHTML);
98
+ }
99
+ exports.toSentenceCase = toSentenceCase;
11
100
  class ParserError extends Error {
12
101
  constructor(message, node) {
13
102
  super(message); // 'Error' breaks prototype chain here
@@ -353,7 +442,7 @@ class Parser {
353
442
  if (start < last.start) throw new Error(JSON.stringify({...last, new: { start, end, reason }, text: this.field.text}))
354
443
  }
355
444
  */
356
- this.field.preserveRanges.push({ start, end, reason });
445
+ this.field.preserveRanges.push({ start, end, description: reason });
357
446
  }
358
447
  parseChunk(chunk) {
359
448
  this.chunk = chunk.text;
@@ -1276,18 +1365,7 @@ class Parser {
1276
1365
  convertToSentenceCase(text) {
1277
1366
  if (!this.field.preserveRanges)
1278
1367
  return text;
1279
- // always keep the leading char, but skip markup
1280
- const lead = text.match(/^(<[^>]+>)*./);
1281
- if (lead) {
1282
- this.preserve(lead[0].length - 1, lead[0].length);
1283
- }
1284
- else {
1285
- this.preserve(0, 1);
1286
- }
1287
- let sentenceCased = text.toLowerCase().replace(/(([?!]\s*|^)(['"¡¿“‘„«\s]+)?[^\s])/g, x => x.toUpperCase());
1288
- for (const { start, end } of this.field.preserveRanges) {
1289
- sentenceCased = sentenceCased.substring(0, start) + text.substring(start, end) + sentenceCased.substring(end);
1290
- }
1368
+ const sentenceCased = restore(sentenceCaser.convert(text, true), text, this.field.preserveRanges);
1291
1369
  if (text !== sentenceCased)
1292
1370
  this.entry.sentenceCased = true;
1293
1371
  return sentenceCased;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retorquere/bibtex-parser",
3
- "version": "6.1.4",
3
+ "version": "6.3.2",
4
4
  "description": "BibTeX parser",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,27 +32,27 @@
32
32
  "author": "Emiliano Heyns",
33
33
  "license": "ISC",
34
34
  "devDependencies": {
35
- "@types/node": "^17.0.21",
36
- "@typescript-eslint/eslint-plugin": "^5.14.0",
37
- "@typescript-eslint/parser": "^5.14.0",
35
+ "@types/node": "^17.0.25",
36
+ "@typescript-eslint/eslint-plugin": "^5.20.0",
37
+ "@typescript-eslint/parser": "^5.20.0",
38
38
  "command-line-args": "^5.2.1",
39
39
  "csv": "^6.0.5",
40
- "digital-tree": "^2.0.3",
40
+ "digital-tree": "^2.0.4",
41
41
  "ejs": "^3.1.6",
42
- "eslint": "^8.10.0",
43
- "eslint-plugin-import": "^2.25.4",
42
+ "eslint": "^8.13.0",
43
+ "eslint-plugin-import": "^2.26.0",
44
44
  "eslint-plugin-prefer-arrow": "^1.2.3",
45
- "glob": "^7.2.0",
45
+ "glob": "^8.0.1",
46
46
  "mkdirp": "^1.0.4",
47
- "node-fetch": "^3.2.2",
47
+ "node-fetch": "^3.2.3",
48
48
  "peggy": "^1.2.0",
49
49
  "piscina": "^3.2.0",
50
50
  "slug": "^5.3.0",
51
- "tap": "^16.0.0",
51
+ "tap": "^16.0.1",
52
52
  "ts-node": "^10.7.0",
53
53
  "tslib": "^2.3.1",
54
- "typedoc": "^0.22.13",
55
- "typescript": "^4.6.2"
54
+ "typedoc": "^0.22.15",
55
+ "typescript": "^4.6.3"
56
56
  },
57
57
  "dependencies": {
58
58
  "unicode2latex": "^3.0.0",