1
0
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-11-22 07:09:47 +00:00

oc: Refactor affixes extraction

* lisp/oc.el (org-cite-main-affixes): New function.
* lisp/oc-natbib.el (org-cite-natbib--build-optional-arguments): Use
new function.
* testing/lisp/test-oc.el (test-org-cite/main-affixes): New test.
This commit is contained in:
Nicolas Goaziou 2021-11-03 16:27:53 +01:00
parent 37c99e0874
commit ad40282860
3 changed files with 58 additions and 5 deletions

View File

@ -119,11 +119,7 @@ If \"natbib\" package is already required in the document, e.g., through
(defun org-cite-natbib--build-optional-arguments (citation info)
"Build optional arguments for citation command.
CITATION is the citation object. INFO is the export state, as a property list."
(let* ((origin (pcase (org-cite-get-references citation)
(`(,reference) reference)
(_ citation)))
(suffix (org-element-property :suffix origin))
(prefix (org-element-property :prefix origin)))
(pcase-let ((`(,prefix . ,suffix) (org-cite-main-affixes citation)))
(concat (and prefix (format "[%s]" (org-trim (org-export-data prefix info))))
(cond
(suffix (format "[%s]" (org-trim (org-export-data suffix info))))

View File

@ -638,6 +638,24 @@ in the current buffer. Positions include leading \"@\" character."
(re-search-forward org-element-citation-key-re end t)
(cons (match-beginning 0) (match-end 0)))))
(defun org-cite-main-affixes (citation)
"Return main affixes for CITATION object.
Some export back-ends only support a single pair of affixes per
citation, even if it contains multiple keys. This function
decides what affixes are the most appropriate.
Return a pair (PREFIX . SUFFIX) where PREFIX and SUFFIX are
parsed data."
(let ((source
;; When there are multiple references, use global affixes.
;; Otherwise, local affixes have priority.
(pcase (org-cite-get-references citation)
(`(,reference) reference)
(_ citation))))
(cons (org-element-property :prefix source)
(org-element-property :suffix source))))
(defun org-cite-supported-styles (&optional processors)
"List of supported citation styles and variants.

View File

@ -251,6 +251,45 @@
(car boundaries)
(cdr boundaries)))))))
(ert-deftest test-org-cite/main-affixes ()
"Test`org-cite-main-affixes'."
(should
(equal '(nil . nil)
(org-test-with-temp-text "[cite:@key]"
(org-cite-main-affixes (org-element-context)))))
(should
(equal '(nil . nil)
(org-test-with-temp-text "[cite:@key1;@key2]"
(org-cite-main-affixes (org-element-context)))))
(should
(equal '(("pre ") . nil)
(org-test-with-temp-text "[cite:pre @key]"
(org-cite-main-affixes (org-element-context)))))
(should
(equal '(("pre ") . (" post"))
(org-test-with-temp-text "[cite:pre @key post]"
(org-cite-main-affixes (org-element-context)))))
(should
(equal '(("pre ") . nil)
(org-test-with-temp-text "[cite:global pre;pre @key]"
(org-cite-main-affixes (org-element-context)))))
(should
(equal '(nil . (" post"))
(org-test-with-temp-text "[cite:@key post;global post]"
(org-cite-main-affixes (org-element-context)))))
(should
(equal '(("global pre") . ("global post"))
(org-test-with-temp-text "[cite:global pre;@key1;@key2;global post]"
(org-cite-main-affixes (org-element-context)))))
(should
(equal '(("global pre") . nil)
(org-test-with-temp-text "[cite:global pre;pre1 @key1;pre2 @key2]"
(org-cite-main-affixes (org-element-context)))))
(should
(equal '(nil . ("global post"))
(org-test-with-temp-text "[cite:@key1 post1;@key2 post2; global post]"
(org-cite-main-affixes (org-element-context))))))
(ert-deftest test-org-cite/supported-styles ()
"Test `org-cite-supported-styles'."
;; Default behavior is to use export processors.