oc-bibtex.el (2902B)
1 ;;; oc-bibtex.el --- Vanilla citation processor for LaTeX -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2021-2024 Free Software Foundation, Inc. 4 5 ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr> 6 7 ;; This file is part of GNU Emacs. 8 9 ;; GNU Emacs is free software: you can redistribute it and/or modify 10 ;; it under the terms of the GNU General Public License as published by 11 ;; the Free Software Foundation, either version 3 of the License, or 12 ;; (at your option) any later version. 13 14 ;; GNU Emacs is distributed in the hope that it will be useful, 15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 ;; GNU General Public License for more details. 18 19 ;; You should have received a copy of the GNU General Public License 20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 21 22 ;;; Commentary: 23 24 ;; This library registers the `bibtex' citation processor, which 25 ;; provides the "export" capability for citations. It doesn't require 26 ;; any LaTeX package. 27 ;; 28 ;; It supports the following citation styles: 29 ;; 30 ;; - nocite (n), 31 ;; - default. 32 ;; 33 ;; Only suffixes are supported. Prefixes are ignored. 34 ;; 35 ;; Bibliography should consist of ".bib" files only. 36 37 ;;; Code: 38 39 (require 'org-macs) 40 (org-assert-version) 41 42 (require 'oc) 43 44 (declare-function org-element-property "org-element-ast" (property node)) 45 46 (declare-function org-export-data "org-export" (data info)) 47 48 49 ;;; Export capability 50 (defun org-cite-bibtex-export-bibliography (_keys files style &rest _) 51 "Print references from bibliography FILES. 52 FILES is a list of absolute file names. STYLE is the bibliography style, as 53 a string or nil." 54 (concat (and style (format "\\bibliographystyle{%s}\n" style)) 55 (format "\\bibliography{%s}" 56 (mapconcat #'file-name-sans-extension 57 files 58 ",")))) 59 60 (defun org-cite-bibtex-export-citation (citation style _ info) 61 "Export CITATION object. 62 STYLE is the citation style, as a pair of strings or nil. INFO is the export 63 state, as a property list." 64 (let ((references (org-cite-get-references citation))) 65 (format "\\%s%s{%s}" 66 (pcase style 67 (`(,(or "nocite" "n") . ,_) "nocite") 68 (_ "cite")) 69 (let ((suffix (cdr (org-cite-main-affixes citation)))) 70 (if suffix 71 (format "[%s]" (org-trim (org-export-data suffix info))) 72 "")) 73 (mapconcat (lambda (r) (org-element-property :key r)) 74 references 75 ",")))) 76 77 78 ;;; Register `bibtex' processor 79 (org-cite-register-processor 'bibtex 80 :export-bibliography #'org-cite-bibtex-export-bibliography 81 :export-citation #'org-cite-bibtex-export-citation 82 :cite-styles 83 '((("nocite" "n")) 84 (("nil")))) 85 86 (provide 'oc-bibtex) 87 ;;; oc-bibtex.el ends here