@retorquere/bibtex-parser 6.4.19 → 7.0.0

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 CHANGED
@@ -205,6 +205,10 @@ export interface ParserOptions {
205
205
  * If you pass an empty array, or `false`, no sentence casing will be applied (even when there's no language field).
206
206
  */
207
207
  sentenceCase?: string[] | boolean;
208
+ /**
209
+ * If you have sentence-casing on, you can independently choose whether quoted titles within a title are preserved as-is (true) or also sentence-cased(false)
210
+ */
211
+ sentenceCasePreserveQuoted?: boolean;
208
212
  /**
209
213
  * Some bibtex has titles in sentence case, or all-uppercase. If this is on, and there is a field that would normally have sentence-casing applied in which more words are all-`X`case
210
214
  * (where `X` is either lower or upper) than mixed-case, it is assumed that you want them this way, and no sentence-casing will be applied to that field
package/index.js CHANGED
@@ -1228,6 +1228,12 @@ class Parser {
1228
1228
  }
1229
1229
  return parsed;
1230
1230
  }
1231
+ restore(text, orig, preserve) {
1232
+ for (const { start, end } of preserve) {
1233
+ text = text.substring(0, start) + orig.substring(start, end) + text.substring(end);
1234
+ }
1235
+ return text;
1236
+ }
1231
1237
  convert_entry(node) {
1232
1238
  this.entry = {
1233
1239
  key: node.id,
@@ -1357,7 +1363,7 @@ class Parser {
1357
1363
  convertToSentenceCase(text) {
1358
1364
  if (!this.field.preserveRanges)
1359
1365
  return text;
1360
- const sentenceCased = (0, sentence_case_2.restore)((0, sentence_case_2.toSentenceCase)(text), text, this.field.preserveRanges);
1366
+ const sentenceCased = this.restore((0, sentence_case_2.toSentenceCase)(text, this.options.sentenceCasePreserveQuoted || false), text, this.field.preserveRanges);
1361
1367
  if (text !== sentenceCased)
1362
1368
  this.entry.sentenceCased = true;
1363
1369
  return sentenceCased;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retorquere/bibtex-parser",
3
- "version": "6.4.19",
3
+ "version": "7.0.0",
4
4
  "description": "BibTeX parser",
5
5
  "main": "index.js",
6
6
  "typings": "./index.d.ts",
@@ -9,7 +9,7 @@
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
+ "poststart": "npm run docs",
13
13
  "pretest": "npm start",
14
14
  "test": "tap test/tap --no-timeout --no-coverage",
15
15
  "snap": "npm test --snapshot",
@@ -1,8 +1,2 @@
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;
1
+ export declare function toSentenceCase(sentence: string, preserveQuoted?: boolean): string;
8
2
  //# sourceMappingURL=sentence-case.d.ts.map
package/sentence-case.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toSentenceCase = exports.restore = void 0;
3
+ exports.toSentenceCase = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-unsafe-argument */
5
5
  const categories = require("xregexp/tools/output/categories");
6
6
  const cat = name => categories.find(c => c.name === name).bmp;
@@ -10,13 +10,6 @@ const Ll = cat('Ll');
10
10
  const N = cat('N');
11
11
  const No = cat('No');
12
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
13
  const re = {
21
14
  acronym: new RegExp(`([${Lu}][.])+$`),
22
15
  innerCaps: new RegExp(`.[${Lu}]`),
@@ -47,43 +40,46 @@ function lowercase(word) {
47
40
  return word;
48
41
  return word.toLowerCase();
49
42
  }
50
- function toSentenceCase(text) {
43
+ function toSentenceCase(sentence, preserveQuoted = false) {
51
44
  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' });
45
+ sentence.replace(/([.?!][\s]+)(<[^>]+>)?([A-Z])/g, (match, end, markup, char, i) => {
46
+ if (!sentence.substring(0, i + 1).match(re.acronym)) {
47
+ preserve.push({ pos: i + end.length + ((markup === null || markup === void 0 ? void 0 : markup.length) || 0), text: char, description: 'sub-sentence-start' });
67
48
  }
68
49
  return '';
69
50
  });
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' });
51
+ sentence.replace(/^(<[^>]+>)?([A-Z])/, (match, markup, char) => {
52
+ preserve.push({ pos: ((markup === null || markup === void 0 ? void 0 : markup.length) || 0), text: char, description: 'sentence-start' });
72
53
  return '';
73
54
  });
74
- text.replace(/<span class="nocase">.*?<\/span>|<nc>.*?<\/nc>/gi, (match, i) => {
75
- preserve.push({ start: i, end: i + match.length, description: 'nocase' });
55
+ sentence.replace(/<span class="nocase">.*?<\/span>|<nc>.*?<\/nc>/gi, (text, pos) => {
56
+ preserve.push({ pos, text, description: 'nocase' });
76
57
  return '';
77
58
  });
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);
59
+ let masked = sentence.replace(/<[^>]+>/g, (text, pos) => {
60
+ preserve.push({ pos, text, description: 'markup' });
61
+ return '\uFFFD'.repeat(text.length);
81
62
  });
63
+ // last because we're potentially modifying the original
64
+ for (const q of [/(“)(.*?)”/g, /(‘)(.*?)’/g, /(["])(.*?)\1/g]) {
65
+ sentence.replace(q, (text, quote, quoted, pos) => {
66
+ preserve.push({
67
+ pos: pos + (preserveQuoted ? 0 : quote.length),
68
+ text: preserveQuoted ? text : toSentenceCase(quoted, preserveQuoted),
69
+ description: `quoted with ${q}`,
70
+ });
71
+ return '';
72
+ });
73
+ }
82
74
  masked = masked
83
75
  .replace(/[;:]\uFFFD*\s+\uFFFD*A\s/g, match => match.toLowerCase())
84
76
  .replace(/[–—]\uFFFD*\s*\uFFFD*A\s/g, match => match.toLowerCase())
85
77
  .replace(re.words, word => lowercase(word));
86
- return restore(masked, text, preserve);
78
+ sentence = masked;
79
+ for (const { pos, text } of preserve) {
80
+ sentence = sentence.substring(0, pos) + text + sentence.substring(pos + text.length);
81
+ }
82
+ return sentence;
87
83
  }
88
84
  exports.toSentenceCase = toSentenceCase;
89
85
  //# sourceMappingURL=sentence-case.js.map