@retorquere/bibtex-parser 6.3.0 → 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 -1
  2. package/index.js +60 -34
  3. 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,46 +8,83 @@ 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 = /^[A-Z][.]([A-Z][.])+(?=$|[^\w])/;
14
- this.quoted = /^"[^"]+"(?=$|[^\w])/;
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
15
26
  }
16
- convert(text) {
27
+ convert(text, ignoreHTML = false) {
17
28
  this.input = text;
18
29
  this.result = '';
19
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
+ }
20
54
  let m;
21
55
  while (this.input) {
22
- if (m = this.input.match(this.quoted)) { // "Hello There"
56
+ if (m = xregexp_1.default.exec(this.input, this.quoted)) { // "Hello There"
23
57
  this.add(m[0], true);
24
58
  }
25
- else if (m = this.input.match(this.acronym)) { // U.S.
26
- this.add(m[0], true);
59
+ else if (m = xregexp_1.default.exec(this.input, this.acronym)) { // U.S.
60
+ this.add(m[0], m[0].length > 2);
27
61
  }
28
- else if (m = this.input.match(/^[a-z]+n't\b/i)) { // isn't
62
+ else if (m = xregexp_1.default.exec(this.input, this.aint)) { // isn't
29
63
  this.add(m[0]);
30
64
  }
31
- else if (m = this.input.match(/^[\w-]+/)) { // gallium-nitride
65
+ else if (m = xregexp_1.default.exec(this.input, this.word)) {
32
66
  this.add(m[0]);
33
67
  }
34
68
  else {
35
69
  this.add(this.input[0]);
36
70
  }
37
71
  }
38
- return this.result;
72
+ return restore(this.result, text, preserve);
39
73
  }
40
74
  add(word, keep = false) {
41
- const parts = word.split('-');
42
- const result = [];
43
- if (this.sentenceStart || keep)
44
- result.push(parts.shift());
45
- for (word of parts) {
46
- if (!word.match(/[a-z][A-Z]/))
47
- word = word.toLowerCase();
48
- result.push(word);
49
- }
50
- word = result.join('-');
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('-');
51
88
  this.result += word;
52
89
  this.input = this.input.substr(word.length);
53
90
  if (!word.match(/^\s+$/)) {
@@ -56,8 +93,8 @@ class SentenceCaser {
56
93
  }
57
94
  }
58
95
  const sentenceCaser = new SentenceCaser;
59
- function toSentenceCase(text) {
60
- return sentenceCaser.convert(text);
96
+ function toSentenceCase(text, ignoreHTML = false) {
97
+ return sentenceCaser.convert(text, ignoreHTML);
61
98
  }
62
99
  exports.toSentenceCase = toSentenceCase;
63
100
  class ParserError extends Error {
@@ -405,7 +442,7 @@ class Parser {
405
442
  if (start < last.start) throw new Error(JSON.stringify({...last, new: { start, end, reason }, text: this.field.text}))
406
443
  }
407
444
  */
408
- this.field.preserveRanges.push({ start, end, reason });
445
+ this.field.preserveRanges.push({ start, end, description: reason });
409
446
  }
410
447
  parseChunk(chunk) {
411
448
  this.chunk = chunk.text;
@@ -1328,18 +1365,7 @@ class Parser {
1328
1365
  convertToSentenceCase(text) {
1329
1366
  if (!this.field.preserveRanges)
1330
1367
  return text;
1331
- // always keep the leading char, but skip markup
1332
- const lead = text.match(/^(<[^>]+>)*./);
1333
- if (lead) {
1334
- this.preserve(lead[0].length - 1, lead[0].length);
1335
- }
1336
- else {
1337
- this.preserve(0, 1);
1338
- }
1339
- let sentenceCased = toSentenceCase(text);
1340
- for (const { start, end } of this.field.preserveRanges) {
1341
- sentenceCased = sentenceCased.substring(0, start) + text.substring(start, end) + sentenceCased.substring(end);
1342
- }
1368
+ const sentenceCased = restore(sentenceCaser.convert(text, true), text, this.field.preserveRanges);
1343
1369
  if (text !== sentenceCased)
1344
1370
  this.entry.sentenceCased = true;
1345
1371
  return sentenceCased;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retorquere/bibtex-parser",
3
- "version": "6.3.0",
3
+ "version": "6.3.2",
4
4
  "description": "BibTeX parser",
5
5
  "main": "index.js",
6
6
  "scripts": {