oc-basic: Better handling of CSL-JSON dates

* lisp/oc-basic.el (org-cite-basic--parse-json): Make date-parsing and
year extraction more resilient.  Provide more informative errors when it
fails.

A string-based date is not only indicated by the key 'raw, but also
possibly by the key 'literal.

String-based dates come in various formats, not necessarily yyyy-mm-dd.
So extracting the first sequence of 4 digits is arguably a better
heuristic for getting the publication year than splitting the string on
- and getting the car of that.

On error, include `value' in the message, which contains the original
value with actionable information, whereas the previously included
`date' is always nil in that case.

TINYCHANGE
This commit is contained in:
David Lukes 2022-02-25 14:21:44 +01:00 committed by Nicolas Goaziou
parent ea6b7451b8
commit 83c6eccaee
1 changed files with 14 additions and 6 deletions

View File

@ -178,21 +178,29 @@ Return a hash table with citation references as keys and fields alist as values.
" and "))) " and ")))
('issued ('issued
;; Date are expressed as an array ;; Date are expressed as an array
;; (`date-parts') or a "string (`raw'). ;; (`date-parts') or a "string (`raw'
;; In both cases, extract the year and ;; or `literal'). In both cases,
;; associate it to `year' field, for ;; extract the year and associate it
;; compatibility with BibTeX format. ;; to `year' field, for compatibility
;; with BibTeX format.
(let ((date (or (alist-get 'date-parts value) (let ((date (or (alist-get 'date-parts value)
(alist-get 'literal value)
(alist-get 'raw value)))) (alist-get 'raw value))))
(cons 'year (cons 'year
(cond (cond
((consp date) ((consp date)
(caar date)) (caar date))
((stringp date) ((stringp date)
(car (split-string date "-"))) (replace-regexp-in-string
(rx
(minimal-match (zero-or-more anything))
(group-n 1 (repeat 4 digit))
(zero-or-more anything))
(rx (backref 1))
date))
(t (t
(error "Unknown CSL-JSON date format: %S" (error "Unknown CSL-JSON date format: %S"
date)))))) value))))))
(_ (_
(cons field value)))) (cons field value))))
item) item)