Skip to content

Commit 456b6f6

Browse files
committed
better regex for years
1 parent a254355 commit 456b6f6

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

libs/citations/parse-text-cite/src/lib/lexer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ console.log(moo)
33
export const lexer = moo.compile({
44
__: /[ \t ]+/u,
55
//_: /[ \t]*/u,
6-
Year: /(?:\d{4}|n\.d\.?)/u,
6+
Year: /(?:\b\d{4}(?=[a-z]|\b)|n\.d\.?)/u,
77
BCE: /B\.C\.E\.|B\.C\.|C\.E\.|A\.D\.|a\.d\.|b\.c\.e\.|b\.c\.|c\.e\./u,
88
//maybename: /[A-Z]\w*['-]?\w*/u,
99
Number: /\d+/u,

libs/citations/parse-text-cite/src/lib/parse-text-cite.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ export const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar))
3434
export const parseTextCite = (string: string, options?: Options) => {
3535
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar))
3636

37+
const splitCites = string
38+
.split(')')
39+
.map(
40+
(c: string, idx, arr) =>
41+
`${c}${arr.length > 1 && idx === arr.length - 1 ? '' : ')'}`
42+
)
43+
3744
try {
3845
parser.feed(string)
3946
} catch (err) {
@@ -71,7 +78,11 @@ function recoverOriginalCitation(cite: (string | Citation)[], ogText: string) {
7178
const originalCites = narrowString
7279
.split(')')
7380
.filter((c) => c)
74-
.map((c: string) => c + ')')
81+
.map(
82+
(c: string, idx, arr) =>
83+
`${c}${arr.length > 1 && idx === arr.length - 1 ? '' : ')'}`
84+
)
85+
console.log(originalCites)
7586

7687
let stupidCounterYouShouldKnowBetter = 0
7788

libs/citations/parse-text-cite/src/lib/testcites.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,38 @@ export const tests: TestData = {
2525
],
2626
input: 'Wow (Bautista Perpinya, 2019).',
2727
},
28+
{
29+
input:
30+
'We used Gorilla (Anwyl, 2019) to present the visual probe task online and the task is available on the open materials page to preview or clone (https://gorilla.sc/openmaterials/85021).',
31+
description: 'Complex case with a URL',
32+
result: [
33+
'We used Gorilla ',
34+
{
35+
citationId: 'CITE-X',
36+
citationItems: [
37+
{
38+
id: 'Anwyl2019',
39+
itemData: {
40+
author: [
41+
{
42+
family: 'Anwyl',
43+
},
44+
],
45+
issued: {
46+
'date-parts': [['2019']],
47+
},
48+
},
49+
},
50+
],
51+
originalText: '(Anwyl, 2019)',
52+
properties: {
53+
noteIndex: 0,
54+
},
55+
},
56+
' to present the visual probe task online and the task is available on the open materials page to preview or clone ',
57+
'(https://gorilla.sc/openmaterials/85021).',
58+
],
59+
},
2860
{
2961
description: 'Can deal with NBSP',
3062
result: [

0 commit comments

Comments
 (0)