diff --git a/lisp/oc-basic.el b/lisp/oc-basic.el index d795b0912..0ea124c99 100644 --- a/lisp/oc-basic.el +++ b/lisp/oc-basic.el @@ -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 diff --git a/testing/examples/Basic.bib b/testing/examples/Basic.bib index addc70e52..7f0c66ba4 100644 --- a/testing/examples/Basic.bib +++ b/testing/examples/Basic.bib @@ -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, +} diff --git a/testing/lisp/test-oc-basic.el b/testing/lisp/test-oc-basic.el index f0bf9d0d0..b7510dc66 100644 --- a/testing/lisp/test-oc-basic.el +++ b/testing/lisp/test-oc-basic.el @@ -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