Skip to content

Commit

Permalink
Merge branch 'gh-3067'
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed Nov 24, 2024
2 parents caabfeb + 356ef38 commit 7599914
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
36 changes: 24 additions & 12 deletions content/dateparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import edtfy = require('edtfy')
// import escapeStringRegexp = require('escape-string-regexp')

import * as months from '../gen/dateparser-months.json'
const Month = new class {
private months = months
private re = new RegExp(Object.keys(months).sort((a, b) => b.length - a.length).map(month => `${month}[.]?`).join('|'), 'i')

english(date: string): string {
return date.replace(this.re, (month: string) => this.months[month.toLowerCase().replace('.', '')] as string || month)
}
}

import { getLocaleDateOrder } from '../submodules/zotero-utilities/date'

Expand Down Expand Up @@ -34,8 +42,6 @@ export type ParsedDate = {
approximate?: boolean
}

const months_re = new RegExp(Object.keys(months).sort((a, b) => b.length - a.length).join('|'), 'i')

const Season = new class {
private ranges = [
[ 13, 14, 15, 16 ],
Expand Down Expand Up @@ -130,7 +136,7 @@ function swap_day_month(day: number, month: number, fix_only = false): number[]
return [ day, month ]
}

function parseEDTF(value: string): ParsedDate {
function parseEDTF(value: string, english: string): ParsedDate {
// 2378 + 2275
let date = value

Expand All @@ -148,17 +154,12 @@ function parseEDTF(value: string): ParsedDate {
catch {}

try {
const edtf = normalize_edtf(EDTF.parse(edtfy(date
.normalize('NFC')
.replace(/\. /, ' ') // 8. july 2011
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
.replace(months_re, _ => months[_.toLowerCase()] || _)
)))
const edtf = normalize_edtf(EDTF.parse(edtfy(english)))
if (edtf) return edtf
}
catch {}

return { verbatim: value }
return null
}

export function parse(value: string, try_range = true): ParsedDate {
Expand Down Expand Up @@ -191,6 +192,17 @@ export function parse(value: string, try_range = true): ParsedDate {
if (date.type === 'date') return date
}

const english = Month.english(value.normalize('NFC').replace(/[.] /, ' ')) // 8. july 2011

if (m = (/^([a-z]+)[-/]([0-9]+)$/i).exec(english)) {
const [ , month, year ] = m
if (months[month.toLowerCase()]) {
date = parse(`${ month } ${ year }`, false)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (date.type === 'date') return date
}
}

// '[origdate] date'
if (try_range && (m = /^\[(.+)\]\s*(.+)$/.exec(value))) {
const [ , _orig, _date ] = m
Expand Down Expand Up @@ -325,11 +337,11 @@ export function parse(value: string, try_range = true): ParsedDate {
return { type: 'date', year: parseInt(date_only), ...time_doubt }
}

if (!(date = parseEDTF(value)).verbatim) return date
if (date = parseEDTF(value, english)) return date

// https://github.com/retorquere/zotero-better-bibtex/issues/868
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (m = /^([0-9]{3,})\s([^0-9]+)(?:\s+([0-9]+))?$/.exec(value.normalize('NFC').replace(months_re, _ => months[_.toLowerCase()] || _))) {
if (m = /^([0-9]{3,})\s([^0-9]+)(?:\s+([0-9]+))?$/.exec(english)) {
const [ , year, month, day ] = m
if (months[month]) {
try {
Expand Down
1 change: 1 addition & 0 deletions test/features/export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Feature: Export

Examples:
| file | references |
| Better BibLatex copied year column as string if character is found #3067 | 1 |
| Cannot change citation key formula #3058 | 1 |
| Citation keys are missing certain words if hyphens are used #3059 | 1 |
| lastpage not work in better bitex #3050 | 1 |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@article{leungMultibitSigmaDelta1992,
title = {Multibit {{Sigma}} - {{Delta A}}/{{D}} Converter Incorporating a Novel Class of Dynamic Element Matching Techniques},
author = {Leung, B.H. and Sutarja, S.},
date = {1992-01},
journaltitle = {IEEE Transactions on Circuits and Systems II: Analog and Digital Signal Processing},
shortjournal = {IEEE Trans. Circuits Syst. II},
volume = {39},
number = {1},
pages = {35--51},
issn = {10577130},
doi = {10.1109/82.204108},
url = {http://ieeexplore.ieee.org/document/204108/},
urldate = {2024-11-21}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"config": {
"id": "36a3b0b5-bad0-4a04-b79b-441c7cef77db",
"label": "BetterBibTeX JSON",
"preferences": {
"kuroshiro": true
}
},
"items": [
{
"DOI": "10.1109/82.204108",
"ISSN": "10577130",
"accessDate": "2024-11-21T08:43:57Z",
"citationKey": "leungMultibitSigmaDelta1992",
"creators": [
{
"creatorType": "author",
"firstName": "B.H.",
"lastName": "Leung"
},
{
"creatorType": "author",
"firstName": "S.",
"lastName": "Sutarja"
}
],
"date": "Jan./1992",
"issue": "1",
"itemID": 1,
"itemType": "journalArticle",
"journalAbbreviation": "IEEE Trans. Circuits Syst. II",
"libraryCatalog": "DOI.org (Crossref)",
"pages": "35-51",
"publicationTitle": "IEEE Transactions on Circuits and Systems II: Analog and Digital Signal Processing",
"rights": "https://ieeexplore.ieee.org/Xplorehelp/downloads/license-information/IEEE.html",
"title": "Multibit Sigma - Delta A/D converter incorporating a novel class of dynamic element matching techniques",
"url": "http://ieeexplore.ieee.org/document/204108/",
"volume": "39"
}
]
}

0 comments on commit 7599914

Please sign in to comment.