瀏覽代碼

oc-basic: Detect malformed bibtex bibliographies

* lisp/oc-basic.el (org-cite-basic--parse-bibtex): Validate the
bibliography before parsing.  Display list of issues if any (via
`bibtex-validate`).
(org-cite-basic--parse-bibliography): Set buffer file name needed by
`bibtex-validate`.  Empty the cache in case of error.
Ihor Radchenko 3 年之前
父節點
當前提交
5b45ad083c
共有 1 個文件被更改,包括 23 次插入14 次删除
  1. 23 14
      lisp/oc-basic.el

+ 23 - 14
lisp/oc-basic.el

@@ -221,6 +221,10 @@ Return a hash table with citation references as keys and fields alist as values.
   (let ((entries (make-hash-table :test #'equal))
         (bibtex-sort-ignore-string-entries t))
     (bibtex-set-dialect dialect t)
+    ;; Throw an error if bibliography is malformed.
+    (unless (bibtex-validate)
+      (user-error "Malformed bibliography at %S"
+                  (or (buffer-file-name) (current-buffer))))
     (bibtex-map-entries
      (lambda (key &rest _)
        ;; Normalize entries: field names are turned into symbols
@@ -265,21 +269,26 @@ Optional argument INFO is the export state, as a property list."
             (when (or (org-file-has-changed-p file)
                       (not (gethash file org-cite-basic--file-id-cache)))
               (insert-file-contents file)
+              (setf (buffer-file-name) file)
               (puthash file (org-buffer-hash) org-cite-basic--file-id-cache))
-	    (let* ((file-id (cons file (gethash file org-cite-basic--file-id-cache)))
-                   (entries
-                    (or (cdr (assoc file-id org-cite-basic--bibliography-cache))
-                        (let ((table
-                               (pcase (file-name-extension file)
-                                 ("json" (org-cite-basic--parse-json))
-                                 ("bib" (org-cite-basic--parse-bibtex 'biblatex))
-                                 ("bibtex" (org-cite-basic--parse-bibtex 'BibTeX))
-                                 (ext
-                                  (user-error "Unknown bibliography extension: %S"
-                                              ext)))))
-                          (push (cons file-id table) org-cite-basic--bibliography-cache)
-                          table))))
-              (push (cons file entries) results)))))
+            (condition-case nil
+                (unwind-protect
+	            (let* ((file-id (cons file (gethash file org-cite-basic--file-id-cache)))
+                           (entries
+                            (or (cdr (assoc file-id org-cite-basic--bibliography-cache))
+                                (let ((table
+                                       (pcase (file-name-extension file)
+                                         ("json" (org-cite-basic--parse-json))
+                                         ("bib" (org-cite-basic--parse-bibtex 'biblatex))
+                                         ("bibtex" (org-cite-basic--parse-bibtex 'BibTeX))
+                                         (ext
+                                          (user-error "Unknown bibliography extension: %S"
+                                                      ext)))))
+                                  (push (cons file-id table) org-cite-basic--bibliography-cache)
+                                  table))))
+                      (push (cons file entries) results))
+                  (setf (buffer-file-name) nil))
+              (error (setq org-cite-basic--file-id-cache nil))))))
       (when info (plist-put info :cite-basic/bibliography results))
       results)))