oc-basic.el: Drop {...} when rendering Bibtex entries

* lisp/oc-basic.el (org-cite-basic--print-bibtex-string): New function
approximately parsing Bibtex strings and dropping {...}.
(org-cite-basic--print-entry): Use the new function.
* testing/examples/Basic.bib:
*
testing/lisp/test-oc-basic.el (test-org-cite-basic/export-bibliography):
Add test.

Reported-by: Dominik Schrempf <dominik.schrempf@gmail.com>
Link: https://orgmode.org/list/CAF+0kSg8O3RQBG1wXoHjMEHwnGFz0gaDkTTSGv+ZaOt4d4myCA@mail.gmail.com
This commit is contained in:
Ihor Radchenko 2024-02-29 12:21:08 +03:00
parent 286a8fb798
commit d2df9624ce
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
3 changed files with 85 additions and 21 deletions

View File

@ -463,6 +463,38 @@ necessary, unless optional argument NO-SUFFIX is non-nil."
new))))
(if no-suffix year (concat year suffix)))))))
(defun org-cite-basic--print-bibtex-string (element &optional info)
"Print Bibtex formatted string ELEMENT, according to Bibtex syntax.
Remove all the {...} that are not a part of LaTeX macros and parse the
LaTeX fragments. Do nothing when current backend is derived from
LaTeX, according to INFO.
Return updated ELEMENT."
(if (org-export-derived-backend-p (plist-get info :back-end) 'latex)
;; Derived from LaTeX, no need to use manual ad-hoc LaTeX
;; parser.
element
;; Convert ELEMENT to anonymous when ELEMENT is string.
;; Otherwise, we cannot modify ELEMENT by side effect.
(when (org-element-type-p element 'plain-text)
(setq element (org-element-create 'anonymous nil element)))
;; Approximately parse LaTeX fragments, assuming Org mode syntax
;; (it is close to original LaTeX, and we do not want to
;; re-implement complete LaTeX parser here))
(org-element-map element t
(lambda (str)
(when (stringp str)
(org-element-set
str
(org-element-parse-secondary-string
str '(latex-fragment entity))))))
;; Strip the remaining { and }.
(org-element-map element t
(lambda (str)
(when (stringp str)
(org-element-set str (replace-regexp-in-string "[{}]" "" str)))))
element))
(defun org-cite-basic--print-entry (entry style &optional info)
"Format ENTRY according to STYLE string.
ENTRY is an alist, as returned by `org-cite-basic--get-entry'.
@ -474,27 +506,29 @@ Optional argument INFO is the export state, as a property list."
(org-cite-basic--get-field 'journal entry info)
(org-cite-basic--get-field 'institution entry info)
(org-cite-basic--get-field 'school entry info))))
(pcase style
("plain"
(let ((year (org-cite-basic--get-year entry info 'no-suffix)))
(org-cite-concat
(org-cite-basic--shorten-names author) ". "
title (and from (list ", " from)) ", " year ".")))
("numeric"
(let ((n (org-cite-basic--key-number (cdr (assq 'id entry)) info))
(year (org-cite-basic--get-year entry info 'no-suffix)))
(org-cite-concat
(format "[%d] " n) author ", "
(org-cite-emphasize 'italic title)
(and from (list ", " from)) ", "
year ".")))
;; Default to author-year. Use year disambiguation there.
(_
(let ((year (org-cite-basic--get-year entry info)))
(org-cite-concat
author " (" year "). "
(org-cite-emphasize 'italic title)
(and from (list ", " from)) "."))))))
(org-cite-basic--print-bibtex-string
(pcase style
("plain"
(let ((year (org-cite-basic--get-year entry info 'no-suffix)))
(org-cite-concat
(org-cite-basic--shorten-names author) ". "
title (and from (list ", " from)) ", " year ".")))
("numeric"
(let ((n (org-cite-basic--key-number (cdr (assq 'id entry)) info))
(year (org-cite-basic--get-year entry info 'no-suffix)))
(org-cite-concat
(format "[%d] " n) author ", "
(org-cite-emphasize 'italic title)
(and from (list ", " from)) ", "
year ".")))
;; Default to author-year. Use year disambiguation there.
(_
(let ((year (org-cite-basic--get-year entry info)))
(org-cite-concat
author " (" year "). "
(org-cite-emphasize 'italic title)
(and from (list ", " from)) "."))))
info)))
;;; "Activate" capability

View File

@ -7,3 +7,14 @@
doi = {10.1007/978-3-642-23816-1},
isbn = {9783642238161}
}
@InCollection{Geyer2011,
author = {Geyer, Charles J},
title = {{Introduction to Markov\plus Chain Monte Carlo}},
year = 2011,
booktitle = {{Handbook of Markov Chain Monte Carlo}},
editor = {Brooks, Steve and Gelman, Andrew and Jones, Galin and Meng,
Xiao-Li},
publisher = {CRC press},
pages = 45,
}

View File

@ -156,5 +156,24 @@ Text: [cite/t: Citing ; @friends; and @friends also; is duplication.]"
(search-forward "Text: Citing van Dongen, M.R.C. (2012), and van Dongen, M.R.C. (2012)
also is duplication." nil t)))))))
(ert-deftest test-org-cite-basic/export-bibliography ()
"Test `org-cite-basic-export-bibliography'."
;; Drop {...} Bibtex brackets and render entities.
(org-test-with-temp-text
(format
"#+bibliography: %s
#+cite_export: basic
Foo [cite/plain:@Geyer2011]
#+print_bibliography:"
(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
;; Rendered from {Introduction to Markov\plus Chain Monte Carlo}
(search-forward "Introduction to Markov+ Chain Monte Carlo" nil t)))))))
(provide 'test-oc-basic)
;;; test-oc-basic.el ends here