emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/org d2df9624ce 02/10: oc-basic.el: Drop {...} when rend


From: ELPA Syncer
Subject: [elpa] externals/org d2df9624ce 02/10: oc-basic.el: Drop {...} when rendering Bibtex entries
Date: Thu, 29 Feb 2024 06:58:53 -0500 (EST)

branch: externals/org
commit d2df9624ce021a6da9ae272618f452b890c362be
Author: Ihor Radchenko <yantar92@posteo.net>
Commit: Ihor Radchenko <yantar92@posteo.net>

    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
---
 lisp/oc-basic.el              | 76 +++++++++++++++++++++++++++++++------------
 testing/examples/Basic.bib    | 11 +++++++
 testing/lisp/test-oc-basic.el | 19 +++++++++++
 3 files changed, 85 insertions(+), 21 deletions(-)

diff --git a/lisp/oc-basic.el b/lisp/oc-basic.el
index d795b09124..0ea124c993 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 addc70e528..7f0c66ba41 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 f0bf9d0d0f..b7510dc66d 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]