From 5bec7025f66eb65f13a701dc616aca2440110c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Simonyi?= Date: Wed, 26 Oct 2022 12:15:42 +0200 Subject: [PATCH] oc-csl.el: Improve reference parsing * lisp/oc-csl.el (org-cite-csl--export-backend): New constant to provide a trivial export back-end for exporting reference affixes and locators with the simple html-based markup expected by citeproc. (org-cite-csl--parse-reference): Do not construct the reference locator and include it in the result, since citeproc does not make use of it. Start the suffix immediately after the locator's ending, skipping the ending comma if necessary. Use `org-cite-csl--export-backend' to export reference affixes and locators. --- lisp/oc-csl.el | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/lisp/oc-csl.el b/lisp/oc-csl.el index 1ccb74e92..30eac9f1a 100644 --- a/lisp/oc-csl.el +++ b/lisp/oc-csl.el @@ -140,9 +140,10 @@ (declare-function org-element-property "org-element" (property element)) (declare-function org-element-put-property "org-element" (element property value)) -(declare-function org-export-data "org-export" (data info)) +(declare-function org-export-data-with-backend "org-export" (data backend info)) (declare-function org-export-derived-backend-p "org-export" (backend &rest backends)) (declare-function org-export-get-footnote-number "org-export" (footnote info &optional data body-first)) +(declare-function org-export-create-backend "org-export" (&key transcoders)) ;;; Customization @@ -310,6 +311,16 @@ If nil then the Chicago author-date style is used as a fallback.") "Regexp matching a label in a citation reference suffix. Label is in match group 1.") +(defconst org-cite-csl--export-backend + (org-export-create-backend + :transcoders + '((plain-text . (lambda (text _info) text)) + (bold . (lambda (_bold contents _info) (concat "" contents ""))) + (italic . (lambda (_italic contents _info) (concat "" contents ""))) + (underline . (lambda (_underline contents _info) + (concat "" contents ""))))) + "Custom backend for exporting citation affixes and locators.") + ;;; Internal functions (defun org-cite-csl--barf-without-citeproc () @@ -476,11 +487,10 @@ property in INFO." INFO is the export state, as a property list. The result is a association list. Keys are: `id', `prefix',`suffix', -`location', `locator' and `label'." - (let (label location-start locator-start location locator prefix suffix) +`locator' and `label'." + (let (label location-start locator-start locator prefix suffix) ;; Parse suffix. Insert it in a temporary buffer to find - ;; different parts: pre-label, label, locator, location (label + - ;; locator), and suffix. + ;; different parts: pre-label, label, locator, and suffix. (with-temp-buffer (save-excursion (insert (org-element-interpret-data @@ -506,12 +516,15 @@ The result is a association list. Keys are: `id', `prefix',`suffix', (let ((re (rx (or "," (group digit))))) (when (re-search-backward re location-start t) (goto-char (or (match-end 1) (match-beginning 0))) - (setq location (buffer-substring location-start (point))) - (setq locator (org-trim (buffer-substring locator-start (point)))) + (setq locator + (org-cite-parse-objects + (buffer-substring locator-start (point)) + t)) ;; Skip comma in suffix. + (when (= (following-char) ?,) (forward-char)) (setq suffix (org-cite-parse-objects - (buffer-substring (match-end 0) (point-max)) + (buffer-substring (point) (point-max)) t))))) (setq prefix (org-cite-concat @@ -525,18 +538,16 @@ The result is a association list. Keys are: `id', `prefix',`suffix', (lambda (data) (org-string-nw-p (org-trim - ;; When Citeproc exports to Org syntax, avoid mix and - ;; matching output formats by also generating Org - ;; syntax for prefix and suffix. - (if (eq 'org (org-cite-csl--output-format info)) - (org-element-interpret-data data) - (org-export-data data info))))))) + ;; Export the parsed prefix, suffix, and locator + ;; with a custom backend, which produces the simple + ;; html markup expected by citeproc. + (org-export-data-with-backend + data org-cite-csl--export-backend info)))))) `((id . ,(org-element-property :key reference)) (prefix . ,(funcall export prefix)) (suffix . ,(funcall export suffix)) - (locator . ,locator) - (label . ,label) - (location . ,location))))) + (locator . ,(funcall export locator)) + (label . ,label))))) (defun org-cite-csl--create-structure (citation info) "Create Citeproc structure for CITATION object. -- 2.25.1