org-cite-basic-export-citation: Fix prefix/suffix being ignored

* lisp/oc-basic.el (org-cite-basic-export-citation): Honour prefix and
suffix in citation references and citations.
* testing/examples/Basic.bib: New example bibliography used by tests.
* testing/lisp/test-oc-basic.el:
(test-org-cite-basic/parse-bibliography):
(test-org-cite-basic/export-citation): New file, adding tests for
oc-basic processor.

Reported-by: William Denton <william@williamdenton.org>
Link: https://orgmode.org/list/FMaPNvYf_8jqVxcU2L5kgiMuejm0NqK3e9D7CzhSzG-fmESTZKgtAJy1LG_zNDLS6o_oQ-gF_G-6DXlXA2iQ_yAjNjLgeB86JTU46Sv8Wec=@williamdenton.org
This commit is contained in:
Ihor Radchenko 2024-01-16 13:38:27 +01:00
parent e6e8f0933a
commit 87da1ff15b
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
3 changed files with 190 additions and 13 deletions

View File

@ -667,22 +667,30 @@ export communication channel, as a property list."
;; "author" style.
(`(,(or "author" "a") . ,variant)
(let ((caps (member variant '("caps" "c"))))
(org-export-data
(org-cite-mapconcat
(lambda (key)
(or
(let ((author (org-cite-basic--get-author key info)))
(if caps (org-cite-capitalize author) author))
"??"))
(org-cite-get-references citation t)
org-cite-basic-author-year-separator)
(org-cite-basic--format-author-year
citation
(lambda (p c s) (org-cite-concat p c s))
(lambda (prefix author _ suffix)
(org-cite-concat
prefix
(if caps (org-cite-capitalize author) author)
suffix))
info)))
;; "noauthor" style.
(`(,(or "noauthor" "na") . ,variant)
(format (if (funcall has-variant-p variant 'bare) "%s" "(%s)")
(mapconcat (lambda (key) (or (org-cite-basic--get-year key info) "????"))
(org-cite-get-references citation t)
org-cite-basic-author-year-separator)))
(let ((bare? (funcall has-variant-p variant 'bare)))
(org-cite-basic--format-author-year
citation
(lambda (prefix contents suffix)
(org-cite-concat
(unless bare? "(")
prefix
contents
suffix
(unless bare? ")")))
(lambda (prefix _ year suffix)
(org-cite-concat prefix year suffix))
info)))
;; "nocite" style.
(`(,(or "nocite" "n") . ,_) nil)
;; "text" and "note" styles.

View File

@ -0,0 +1,9 @@
@book{friends,
title = {{{LaTeX}} and Friends},
author = {van Dongen, M.R.C.},
date = {2012},
location = {Berlin},
publisher = {Springer},
doi = {10.1007/978-3-642-23816-1},
isbn = {9783642238161}
}

View File

@ -0,0 +1,160 @@
;;; test-oc-basic.el --- Tests for Org Cite basic processor -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Ihor Radchenko
;; Author: Ihor Radchenko <yantar92 at posteo dot net>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Unit tests for Org cite basic processor.
;;; Code:
(require 'oc-basic)
(ert-deftest test-org-cite-basic/parse-bibliography ()
"Test `org-cite-basic--parse-bibliography'."
;; Bibtex bibliography.
(org-test-with-temp-text
(format "#+bibliography: %s"
(expand-file-name "examples/Basic.bib" org-test-dir))
(let ((data (org-cite-basic--parse-bibliography)))
(should (= 1 (length data)))
(should (equal (expand-file-name "examples/Basic.bib" org-test-dir)
(caar data)))
(dolist (k (hash-table-keys (cdar data)))
(when (equal k "friends")
(should (equal (gethash k (cdar data))
'((type . "book")
(id . "friends")
(title . "{{LaTeX}} and Friends")
(author . "van Dongen, M.R.C.")
(date . "2012")
(location . "Berlin")
(publisher . "Springer")
(doi . "10.1007/978-3-642-23816-1")
(isbn . "9783642238161")))))))))
(ert-deftest test-org-cite-basic/export-citation ()
"Test `org-cite-basic-export-citation'."
;; Default "nil" citation style.
(org-test-with-temp-text
(format
"#+bibliography: %s
#+cite_export: basic
Default: [cite:Citing ; @friends; and @friends also; is duplication.]"
(expand-file-name "examples/Basic.bib" org-test-dir))
(let ((export-buffer "*Test ASCII Export*")
(org-export-show-temporary-export-buffer nil))
(org-export-to-buffer 'ascii export-buffer)
(with-current-buffer export-buffer
(let ((case-fold-search t))
(search-forward "Default: (Citing van Dongen, M.R.C., 2012, and van Dongen, M.R.C., 2012
also is duplication.)" nil t)))))
;; "author" citation style.
(org-test-with-temp-text
(format
"#+bibliography: %s
#+cite_export: basic
Author: [cite/a:Citing ; @friends; and @friends also; is duplication.]"
(expand-file-name "examples/Basic.bib" org-test-dir))
(let ((export-buffer "*Test ASCII Export*")
(org-export-show-temporary-export-buffer nil))
(org-export-to-buffer 'ascii export-buffer)
(with-current-buffer export-buffer
(let ((case-fold-search t))
(should
(search-forward "Author: Citing van Dongen, M.R.C., and van Dongen, M.R.C. also is
duplication." nil t))))))
;; "note" citation style.
(org-test-with-temp-text
(format
"#+bibliography: %s
#+cite_export: basic
Note: [cite/ft:Citing ; @friends; and @friends also; is duplication.]"
(expand-file-name "examples/Basic.bib" org-test-dir))
(let ((export-buffer "*Test ASCII Export*")
(org-export-show-temporary-export-buffer nil))
(org-export-to-buffer 'ascii export-buffer)
(with-current-buffer export-buffer
(let ((case-fold-search t))
(should
(search-forward "[1] Citing van Dongen, M.R.C. (2012), and van Dongen, M.R.C. (2012) also
is duplication." nil t))))))
;; "nocite" citation style.
(org-test-with-temp-text
(format
"#+bibliography: %s
#+cite_export: basic
Nocite (should be blank): [cite/n:Citing ; @friends; and @friends also; is duplication.]"
(expand-file-name "examples/Basic.bib" org-test-dir))
(let ((export-buffer "*Test ASCII Export*")
(org-export-show-temporary-export-buffer nil))
(org-export-to-buffer 'ascii export-buffer)
(with-current-buffer export-buffer
(let ((case-fold-search t))
(should (search-forward "Nocite (should be blank):\n" nil t))
(goto-char (point-min))
(should-not (search-forward "2012" nil t))
(goto-char (point-min))
(should-not (search-forward "Dongen" nil t))))))
;; "noauthor" citation style.
(org-test-with-temp-text
(format
"#+bibliography: %s
#+cite_export: basic
Noauthor: [cite/na:Citing ; @friends; and @friends also; is duplication.]"
(expand-file-name "examples/Basic.bib" org-test-dir))
(let ((export-buffer "*Test ASCII Export*")
(org-export-show-temporary-export-buffer nil))
(org-export-to-buffer 'ascii export-buffer)
(with-current-buffer export-buffer
(let ((case-fold-search t))
(should
(search-forward "Noauthor: (Citing 2012, and 2012 also is duplication.)" nil t))))))
;; "numeric" citation style.
(org-test-with-temp-text
(format
"#+bibliography: %s
#+cite_export: basic
Numeric (should \"use global affixes and ignore local ones\"): [cite/nb:Citing ; @friends; and @friends also; is duplication.]"
(expand-file-name "examples/Basic.bib" org-test-dir))
(let ((export-buffer "*Test ASCII Export*")
(org-export-show-temporary-export-buffer nil))
(org-export-to-buffer 'ascii export-buffer)
(with-current-buffer export-buffer
(let ((case-fold-search t))
(should
(search-forward "Numeric (should \"use global affixes and ignore local ones\"): (Citing 1,
1 is duplication.)" nil t))))))
;; "text" citation style.
(org-test-with-temp-text
(format
"#+bibliography: %s
#+cite_export: basic
Text: [cite/t: Citing ; @friends; and @friends also; is duplication.]"
(expand-file-name "examples/Basic.bib" org-test-dir))
(let ((export-buffer "*Test ASCII Export*")
(org-export-show-temporary-export-buffer nil))
(org-export-to-buffer 'ascii export-buffer)
(with-current-buffer export-buffer
(let ((case-fold-search t))
(should
(search-forward "Text: Citing van Dongen, M.R.C. (2012), and van Dongen, M.R.C. (2012)
also is duplication." nil t)))))))
(provide 'test-oc-basic)
;;; test-oc-basic.el ends here