Make org-exp-bibtex.el require org and org-exp
[rgr-org-mode.git] / contrib / lisp / org-exp-bibtex.el
blobab6a6b00668b25f9dfd4c8dddfdaf2a6a6d0c26a
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
59 (require 'org)
60 (require 'org-exp)
61 (defun org-export-bibtex-preprocess ()
62 "Export all BibTeX."
63 (interactive)
64 (save-window-excursion
65 (setq oebp-cite-plist '())
67 ;; Convert #+BIBLIOGRAPHY: name style
68 (goto-char (point-min))
69 (while (re-search-forward "^#\\+BIBLIOGRAPHY:[ \t]+\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\([^\r\n]*\\)" nil t)
70 (let ((file (match-string 1))
71 (style (match-string 2))
72 (opt (org-exp-bibtex-options-to-plist (match-string 3))))
73 (replace-match
74 (cond
75 (htmlp ;; We are exporting to HTML
76 (let (extra-args cite-list end-hook tmp-files)
77 (dolist (elt opt)
78 (when (equal "option" (car elt))
79 (setq extra-args (cons (cdr elt) extra-args))))
82 (when (assoc "limit" opt) ;; Limit is true - collect references
83 (org-exp-bibtex-docites (lambda ()
84 (dolist (c (org-split-string (match-string 1) ","))
85 (add-to-list 'cite-list c))))
86 ;; (message "cites: %s" cite-list)
87 (let ((tmp (make-temp-file "org-exp-bibtex")))
88 (with-temp-file tmp (dolist (i cite-list) (insert (concat i "\n"))))
89 (setq tmp-files (cons tmp tmp-files))
90 (setq extra-args (append extra-args `("-citefile" ,tmp)))))
92 (when (not (eq 0 (apply 'call-process (append '("bibtex2html" nil nil nil)
93 `("-a" "--nodoc" "--style" ,style "--no-header")
94 extra-args
95 (list (concat file ".bib"))))))
96 (error "Executing bibtex2html failed"))
98 (dolist (f tmp-files) (delete-file f)))
100 (with-temp-buffer
101 (save-match-data
102 (insert-file-contents (concat file ".html"))
103 (goto-char (point-min))
104 (while (re-search-forward "a name=\"\\(\\w+\\)\">\\(\\w+\\)" nil t)
105 (setq oebp-cite-plist (cons (cons (match-string 1) (match-string 2)) oebp-cite-plist)))
106 (goto-char (point-min))
107 (while (re-search-forward "<hr>" nil t)
108 (replace-match "<hr/>" t t))
109 (concat "\n#+BEGIN_HTML\n<div id=\"bibliography\">\n" (buffer-string) "\n</div>\n#+END_HTML\n"))))
110 (latexp ;; Latex export
111 (concat "\n#+LATEX: \\bibliographystyle{" style "}"
112 "\n#+LATEX: \\bibliography{" file "}\n"))) t t)))
115 ;; Convert cites to links in html
116 (when htmlp
117 ;; Split citation commands with multiple keys
118 (org-exp-bibtex-docites
119 (lambda ()
120 (let ((keys (save-match-data (org-split-string (match-string 1) ","))))
121 (when (> (length keys) 1)
122 (replace-match (mapconcat (lambda (k) (format "\\cite{%s}" k)) keys "")
123 t t)))))
124 ;; Replace the citation commands with links
125 (org-exp-bibtex-docites
126 (lambda () (let* ((cn (match-string 1))
127 (cv (assoc cn oebp-cite-plist)))
128 ;; (message "L: %s" (concat "\[_{}[[" cn "][" (if cv (cdr cv) cn) "]]\]"))
129 (replace-match (concat "\[_{}[[#" cn "][" (if cv (cdr cv) cn) "]]\]")) t t))))
134 (defun org-exp-bibtex-docites (fun)
135 (save-excursion
136 (save-match-data
137 (goto-char (point-min))
138 (when htmlp
139 (while (re-search-forward "\\\\cite{\\([^}\n]+\\)}" nil t)
140 (apply fun nil))))))
143 (defun org-exp-bibtex-options-to-plist (options)
144 (save-match-data
145 (flet ((f (o) (let ((s (split-string o ":"))) (cons (nth 0 s) (nth 1 s)))))
146 (mapcar 'f (split-string options nil t)))))
151 (add-hook 'org-export-preprocess-hook 'org-export-bibtex-preprocess)
153 (provide 'org-exp-bibtex)
155 ;;; org-exp-bibtex.el ends here