Remove comma after means in many docstrings
[rgr-org-mode.git] / contrib / lisp / org-exp-bibtex.el
blobc68575244045beb10bd9740a74a2bc06795f3906
1 ;;; org-exp-bibtex.el --- Export bibtex fragments
3 ;; Copyright (C) 2009 Taru Karttunen
5 ;; Author: Taru Karttunen <taruti@taruti.net >
7 ;; This file is not currently part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation; either version 2, or (at
12 ;; your option) any later version.
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program ; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;; This is an utility to handle BibTeX export to both LaTeX and html
27 ;; exports. It uses the bibtex2html software from
28 ;; http://www.lri.fr/~filliatr/bibtex2html/
30 ;; The usage is as follows:
31 ;; #+BIBLIOGRAPHY: bibfilebasename stylename optional-options
32 ;; e.g. given foo.bib and using style plain:
33 ;; #+BIBLIOGRAPHY: foo plain option:-d
35 ;; Optional options are of the form:
37 ;; option:-foobar pass '-foobar' to bibtex2html
38 ;; e.g.
39 ;; option:-d sort by date.
40 ;; option:-a sort as BibTeX (usually by author) *default*
41 ;; option:-u unsorted i.e. same order as in .bib file
42 ;; option:-r reverse the sort.
43 ;; see the bibtex2html man page for more. Multiple options can be combined like:
44 ;; option:-d option:-r
46 ;; Limiting to only the entries cited in the document:
47 ;; limit:t
49 ;; For LaTeX export this simply inserts the lines
50 ;; \bibliographystyle{plain}
51 ;; \bibliography{foo}
52 ;; into the tex-file when exporting.
54 ;; For Html export it:
55 ;; 1) converts all \cite{foo} to links to the bibliography
56 ;; 2) creates a foo.html and foo_bib.html
57 ;; 3) includes the contents of foo.html in the exported html file
60 (defun org-export-bibtex-preprocess ()
61 "Export all BibTeX."
62 (interactive)
63 (save-window-excursion
64 (setq oebp-cite-plist '())
66 ;; Convert #+BIBLIOGRAPHY: name style
67 (goto-char (point-min))
68 (while (re-search-forward "^#\\+BIBLIOGRAPHY:[ \t]+\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\([^\r\n]*\\)" nil t)
69 (let ((file (match-string 1))
70 (style (match-string 2))
71 (opt (org-exp-bibtex-options-to-plist (match-string 3))))
72 (replace-match
73 (cond
74 (htmlp ;; We are exporting to HTML
75 (let (extra-args cite-list end-hook tmp-files)
76 (dolist (elt opt)
77 (when (equal "option" (car elt))
78 (setq extra-args (cons (cdr elt) extra-args))))
81 (when (assoc "limit" opt) ;; Limit is true - collect references
82 (org-exp-bibtex-docites (lambda ()
83 (dolist (c (org-split-string (match-string 1) ","))
84 (add-to-list 'cite-list c))))
85 ;; (message "cites: %s" cite-list)
86 (let ((tmp (make-temp-file "org-exp-bibtex")))
87 (with-temp-file tmp (dolist (i cite-list) (insert (concat i "\n"))))
88 (setq tmp-files (cons tmp tmp-files))
89 (setq extra-args (append extra-args `("-citefile" ,tmp)))))
91 (when (not (eq 0 (apply 'call-process (append '("bibtex2html" nil nil nil)
92 `("-a" "--nodoc" "--style" ,style "--no-header")
93 extra-args
94 (list (concat file ".bib"))))))
95 (error "Executing bibtex2html failed"))
97 (dolist (f tmp-files) (delete-file f)))
99 (with-temp-buffer
100 (save-match-data
101 (insert-file-contents (concat file ".html"))
102 (goto-char (point-min))
103 (while (re-search-forward "a name=\"\\(\\w+\\)\">\\(\\w+\\)" nil t)
104 (setq oebp-cite-plist (cons (cons (match-string 1) (match-string 2)) oebp-cite-plist)))
105 (goto-char (point-min))
106 (while (re-search-forward "<hr>" nil t)
107 (replace-match "<hr/>" t t))
108 (concat "\n#+BEGIN_HTML\n<div id=\"bibliography\">\n" (buffer-string) "\n</div>\n#+END_HTML\n"))))
109 (latexp ;; Latex export
110 (concat "\n#+LATEX: \\bibliographystyle{" style "}"
111 "\n#+LATEX: \\bibliography{" file "}\n"))) t t)))
114 ;; Convert cites to links in html
115 (when htmlp
116 ;; Split citation commands with multiple keys
117 (org-exp-bibtex-docites
118 (lambda ()
119 (let ((keys (save-match-data (org-split-string (match-string 1) ","))))
120 (when (> (length keys) 1)
121 (replace-match (mapconcat (lambda (k) (format "\\cite{%s}" k)) keys "")
122 t t)))))
123 ;; Replace the citation commands with links
124 (org-exp-bibtex-docites
125 (lambda () (let* ((cn (match-string 1))
126 (cv (assoc cn oebp-cite-plist)))
127 ;; (message "L: %s" (concat "\[_{}[[" cn "][" (if cv (cdr cv) cn) "]]\]"))
128 (replace-match (concat "\[_{}[[#" cn "][" (if cv (cdr cv) cn) "]]\]")) t t))))
133 (defun org-exp-bibtex-docites (fun)
134 (save-excursion
135 (save-match-data
136 (goto-char (point-min))
137 (when htmlp
138 (while (re-search-forward "\\\\cite{\\([^}\n]+\\)}" nil t)
139 (apply fun nil))))))
142 (defun org-exp-bibtex-options-to-plist (options)
143 (save-match-data
144 (flet ((f (o) (let ((s (split-string o ":"))) (cons (nth 0 s) (nth 1 s)))))
145 (mapcar 'f (split-string options nil t)))))
150 (add-hook 'org-export-preprocess-hook 'org-export-bibtex-preprocess)
152 (provide 'org-exp-bibtex)
154 ;;; org-exp-bibtex.el ends here