Skip to content

Commit

Permalink
Merge branch 'master' into gh-3057
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed Nov 24, 2024
2 parents a628acc + 6a4cf33 commit c9a918a
Show file tree
Hide file tree
Showing 27 changed files with 497 additions and 117 deletions.
6 changes: 4 additions & 2 deletions content/Preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,19 +413,21 @@ export class PrefPane {
public checkCitekeyFormat(): void {
if (!$window || Zotero.BetterBibTeX.starting) return // itemTypes not available yet

const error = Formatter.update([ Preference.citekeyFormatEditing, Preference.citekeyFormat ])
const error = Formatter.test(Preference.citekeyFormatEditing || Preference.citekeyFormat)
const editing = $window.document.getElementById('bbt-preferences-citekeyFormatEditing')
editing.classList[error ? 'add' : 'remove']('bbt-prefs-error')
editing.setAttribute(client.is7 ? 'title' : 'tooltiptext', error)
if (client.is7) editing.setAttribute('tooltip', 'html-tooltip')

const msg = $window.document.getElementById('bbt-citekeyFormat-error') as HTMLInputElement
msg.value = error
msg.value = error || (Preference.citekeyFormatEditing === '[' && 'legacy formula, will be upgraded when completed')
msg.style.display = error ? 'initial' : 'none'

const active = $window.document.getElementById('bbt-preferences-citekeyFormat')
const label = $window.document.getElementById('bbt-label-citekeyFormat')
active.style.display = label.style.display = Preference.citekeyFormat === Preference.citekeyFormatEditing ? 'none' : 'initial'

if (!error) Formatter.update([ Preference.citekeyFormatEditing, Preference.citekeyFormat ])
}

public checkPostscript(): void {
Expand Down
2 changes: 1 addition & 1 deletion content/auto-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ const queue = new class TaskQueue {
}
}

await Promise.all(jobs.map(job => Translators.queueJob(job)))
await Promise.allSettled(jobs.map(job => Translators.queueJob(job)))

await repo.push(l10n.localize('better-bibtex_preferences_auto-export_git_message', { type: translator.label.replace('Better ', '') }))

Expand Down
2 changes: 2 additions & 0 deletions content/better-bibtex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import flatMap from 'array.prototype.flatmap'
flatMap.shim()
import matchAll from 'string.prototype.matchall'
matchAll.shim()
import allSettled = require('promise.allsettled')
allSettled.shim()

import type Bluebird from 'bluebird'
const Ready = Zotero.Promise.defer()
Expand Down
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
Loading

0 comments on commit c9a918a

Please sign in to comment.