1 ;;; org-exp-bibtex.el --- Export bibtex fragments
3 ;; Copyright (C) 2009-2011 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.
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
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:
49 ;; For LaTeX export this simply inserts the lines
50 ;; \bibliographystyle{plain}
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
62 (defvar org-export-current-backend
) ; dynamically bound in org-exp.el
63 (defun org-export-bibtex-preprocess ()
66 (save-window-excursion
67 (setq oebp-cite-plist
'())
69 ;; Convert #+BIBLIOGRAPHY: name style
70 (goto-char (point-min))
71 (while (re-search-forward "^#\\+BIBLIOGRAPHY:[ \t]+\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\([^\r\n]*\\)" nil t
)
72 (let ((file (match-string 1))
73 (style (match-string 2))
74 (opt (org-exp-bibtex-options-to-plist (match-string 3))))
77 ((eq org-export-current-backend
'html
) ;; We are exporting to HTML
78 (let (extra-args cite-list end-hook tmp-files
)
80 (when (equal "option" (car elt
))
81 (setq extra-args
(cons (cdr elt
) extra-args
))))
83 (when (assoc "limit" opt
) ;; Limit is true - collect references
84 (org-exp-bibtex-docites (lambda ()
85 (dolist (c (org-split-string (match-string 1) ","))
86 (add-to-list 'cite-list c
))))
87 ;; (message "cites: %s" cite-list)
88 (let ((tmp (make-temp-file "org-exp-bibtex")))
89 (with-temp-file tmp
(dolist (i cite-list
) (insert (concat i
"\n"))))
90 (setq tmp-files
(cons tmp tmp-files
))
91 (setq extra-args
(append extra-args
`("-citefile" ,tmp
)))))
93 (when (not (eq 0 (apply 'call-process
(append '("bibtex2html" nil nil nil
)
94 `("-a" "--nodoc" "--style" ,style
"--no-header")
96 (list (concat file
".bib"))))))
97 (error "Executing bibtex2html failed"))
99 (dolist (f tmp-files
) (delete-file f
)))
103 (insert-file-contents (concat file
".html"))
104 (goto-char (point-min))
105 (while (re-search-forward (org-re "a name=\"\\([-_[:word:]]+\\)\">\\([[:word:]]+\\)") nil t
)
106 (setq oebp-cite-plist
(cons (cons (match-string 1) (match-string 2)) oebp-cite-plist
)))
107 (goto-char (point-min))
108 (while (re-search-forward "<hr>" nil t
)
109 (replace-match "<hr/>" t t
))
110 (concat "\n#+BEGIN_HTML\n<div id=\"bibliography\">\n<h2>References</h2>\n" (buffer-string) "\n</div>\n#+END_HTML\n"))))
111 ((eq org-export-current-backend
'latex
) ;; Latex export
112 (concat "\n#+LATEX: \\bibliographystyle{" style
"}"
113 "\n#+LATEX: \\bibliography{" file
"}\n"))) t t
)))
115 ;; Convert cites to links in html
116 (when (eq org-export-current-backend
'html
)
117 ;; Split citation commands with multiple keys
118 (org-exp-bibtex-docites
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
"")
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
))))))
131 (defun org-exp-bibtex-docites (fun)
134 (goto-char (point-min))
135 (when (eq org-export-current-backend
'html
)
136 (while (re-search-forward "\\\\cite{\\([^}\n]+\\)}" nil t
)
139 (defun org-exp-bibtex-options-to-plist (options)
141 (flet ((f (o) (let ((s (split-string o
":"))) (cons (nth 0 s
) (nth 1 s
)))))
142 (mapcar 'f
(split-string options nil t
)))))
144 (add-hook 'org-export-preprocess-hook
'org-export-bibtex-preprocess
)
146 (provide 'org-exp-bibtex
)
148 ;;; org-exp-bibtex.el ends here