1 ;;; ox-bibtex.el --- Export bibtex fragments
3 ;; Copyright (C) 2009-2013 Taru Karttunen
5 ;; Author: Taru Karttunen <taruti@taruti.net>
6 ;; Nicolas Goaziou <n dot goaziou at gmail dot com>
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 LaTeX, html and ascii
27 ;; exports. For HTML and ascii it uses the bibtex2html software from:
29 ;; http://www.lri.fr/~filliatr/bibtex2html/
31 ;; For ascii it uses the pandoc software from:
33 ;; http://johnmacfarlane.net/pandoc/
35 ;; It also introduces "cite" syntax for Org links.
37 ;; The usage is as follows:
39 ;; #+BIBLIOGRAPHY: bibfilebasename stylename optional-options
41 ;; e.g. given foo.bib and using style plain:
43 ;; #+BIBLIOGRAPHY: foo plain option:-d
45 ;; Optional options are of the form:
47 ;; option:-foobar pass '-foobar' to bibtex2html
51 ;; option:-d sort by date
52 ;; option:-a sort as BibTeX (usually by author) *default*
53 ;; option:-u unsorted i.e. same order as in .bib file
54 ;; option:-r reverse the sort
56 ;; See the bibtex2html man page for more. Multiple options can be
59 ;; option:-d option:-r
61 ;; Limiting to only the entries cited in the document:
65 ;; For LaTeX export this simply inserts the lines
67 ;; \bibliographystyle{plain}
70 ;; into the TeX file when exporting.
72 ;; For HTML export it:
73 ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
75 ;; 2) creates a foo.html and foo_bib.html,
76 ;; 3) includes the contents of foo.html in the exported HTML file.
78 ;; For ascii export it:
79 ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
81 ;; 2) creates a foo.txt and foo_bib.html,
82 ;; 3) includes the contents of foo.txt in the exported ascii file.
84 ;; For LaTeX export it:
85 ;; 1) converts all [[cite:foo]] to \cite{foo}.
89 (eval-when-compile (require 'cl
))
90 (let ((jump-fn (car (org-remove-if-not #'fboundp
'(ebib obe-goto-citation
)))))
91 (org-add-link-type "cite" jump-fn
))
93 ;;; Internal Functions
95 (defun org-bibtex-get-file (keyword)
96 "Return bibliography file as a string.
97 KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no file is found,
99 (let ((value (org-element-property :value keyword
)))
101 (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value
)
102 (match-string 1 value
))))
104 (defun org-bibtex-get-style (keyword)
105 "Return bibliography style as a string.
106 KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no style is found,
108 (let ((value (org-element-property :value keyword
)))
110 (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value
)
111 (match-string 2 value
))))
113 (defun org-bibtex-get-arguments (keyword)
114 "Return \"bibtex2html\" arguments specified by the user.
115 KEYWORD is a \"BIBLIOGRAPHY\" keyword. Return value is a plist
116 containing `:options' and `:limit' properties. The former
117 contains a list of strings to be passed as options to
118 \"bibtex2html\" process. The latter contains a boolean."
119 (let ((value (org-element-property :value keyword
)))
121 (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value
)
123 (dolist (arg (org-split-string (match-string 3 value
))
125 (list :options
(nreverse options
) :limit limit
))
126 (let* ((s (split-string arg
":"))
129 (cond ((equal "limit" key
)
130 (setq limit
(not (equal "nil" value
))))
131 ((equal "option" key
) (push value options
)))))))))
133 (defun org-bibtex-citation-p (object)
134 "Non-nil when OBJECT is a citation."
135 (case (org-element-type object
)
136 (link (equal (org-element-property :type object
) "cite"))
138 (string-match "\\`\\\\cite{" (org-element-property :value object
)))))
140 (defun org-bibtex-get-citation-key (citation)
141 "Return key for a given citation, as a string.
142 CITATION is a `latex-fragment' or `link' type object satisfying
143 to `org-bibtex-citation-p' predicate."
144 (if (eq (org-element-type citation
) 'link
)
145 (org-element-property :path citation
)
146 (let ((value (org-element-property :value citation
)))
147 (and (string-match "\\`\\\\cite{" value
)
148 (substring value
(match-end 0) -
1)))))
154 (defun org-bibtex-process-bib-files (tree backend info
)
155 "Send each bibliography in parse tree to \"bibtex2html\" process.
156 Return new parse tree."
157 (when (org-export-derived-backend-p backend
'ascii
'html
)
158 ;; Initialize dynamically scoped variables. The first one
159 ;; contain an alist between keyword objects and their HTML
160 ;; translation. The second one will contain an alist between
161 ;; citation keys and names in the output (according to style).
162 (setq org-bibtex-html-entries-alist nil
163 org-bibtex-html-keywords-alist nil
)
164 (org-element-map tree
'keyword
166 (when (equal (org-element-property :key keyword
) "BIBLIOGRAPHY")
167 (let ((arguments (org-bibtex-get-arguments keyword
))
168 (file (org-bibtex-get-file keyword
))
170 ;; limit is set: collect citations throughout the document
171 ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
173 (when (plist-get arguments
:limit
)
175 (org-element-map tree
'(latex-fragment link
)
177 (and (org-bibtex-citation-p object
)
178 (org-bibtex-get-citation-key object
))))))
179 (with-temp-file (setq temp-file
(make-temp-file "ox-bibtex"))
180 (insert (mapconcat 'identity citations
"\n")))
184 (append (plist-get arguments
:options
)
185 (list "-citefile" temp-file
))))))
186 ;; Call "bibtex2html" on specified file.
187 (unless (eq 0 (apply 'call-process
188 (append '("bibtex2html" nil nil nil
)
189 '("-a" "-nodoc" "-noheader" "-nofooter")
191 (org-bibtex-get-style keyword
))
192 (plist-get arguments
:options
)
193 (list (concat file
".bib")))))
194 (error "Executing bibtex2html failed"))
195 (and temp-file
(delete-file temp-file
))
196 ;; Open produced HTML file, and collect Bibtex key names
198 (insert-file-contents (concat file
".html"))
199 ;; Update `org-bibtex-html-entries-alist'.
200 (goto-char (point-min))
201 (while (re-search-forward
202 "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t
)
203 (push (cons (match-string 1) (match-string 2))
204 org-bibtex-html-entries-alist
)))
205 ;; Open produced HTML file, wrap references within a block and
209 ((org-export-derived-backend-p backend
'html
)
210 (insert "<div id=\"bibliography\">\n<h2>References</h2>\n")
211 (insert-file-contents (concat file
".html"))
213 ((org-export-derived-backend-p backend
'ascii
)
214 ;; convert HTML references to text w/pandoc
215 (unless (eq 0 (call-process "pandoc" nil nil nil
216 (concat file
".html")
218 (concat file
".txt")))
219 (error "Executing pandoc failed"))
220 (insert "References\n==========\n\n")
221 (insert-file-contents (concat file
".txt"))
222 (goto-char (point-min))
223 (while (re-search-forward
224 "\\[ \\[bib\\][^ ]+ \\(\\]\\||[\n\r]\\)" nil t
)
226 (goto-char (point-min))
227 (while (re-search-forward "\\( \\]\\| \\]\\| |\\)" nil t
)
229 (goto-char (point-min))
230 (while (re-search-forward "[\n\r]\\([\n\r][\n\r]\\)" nil t
)
231 (replace-match "\\1"))))
232 ;; Update `org-bibtex-html-keywords-alist'.
233 (push (cons keyword
(buffer-string))
234 org-bibtex-html-keywords-alist
)))))))
235 ;; Return parse tree unchanged.
238 (defun org-bibtex-merge-contiguous-citations (tree backend info
)
239 "Merge all contiguous citation in parse tree.
240 As a side effect, this filter will also turn all \"cite\" links
241 into \"\\cite{...}\" LaTeX fragments."
242 (when (org-export-derived-backend-p backend
'html
'latex
'ascii
)
243 (org-element-map tree
'(link latex-fragment
)
245 (when (org-bibtex-citation-p object
)
246 (let ((new-citation (list 'latex-fragment
248 :post-blank
(org-element-property
249 :post-blank object
)))))
250 ;; Insert NEW-CITATION right before OBJECT.
251 (org-element-insert-before new-citation object
)
252 ;; Remove all subsequent contiguous citations from parse
253 ;; tree, keeping only their citation key.
254 (let ((keys (list (org-bibtex-get-citation-key object
)))
256 (while (and (setq next
(org-export-get-next-element object info
))
257 (or (and (stringp next
)
258 (not (org-string-match-p "\\S-" next
)))
259 (org-bibtex-citation-p next
)))
260 (unless (stringp next
)
261 (push (org-bibtex-get-citation-key next
) keys
))
262 (org-element-extract-element object
)
264 (org-element-extract-element object
)
265 ;; Eventually merge all keys within NEW-CITATION. Also
266 ;; ensure NEW-CITATION has the same :post-blank property
267 ;; as the last citation removed.
268 (org-element-put-property
270 :post-blank
(org-element-property :post-blank object
))
271 (org-element-put-property
273 :value
(format "\\cite{%s}"
274 (mapconcat 'identity
(nreverse keys
) ",")))))))))
278 '(progn (add-to-list 'org-export-filter-parse-tree-functions
279 'org-bibtex-process-bib-files
)
280 (add-to-list 'org-export-filter-parse-tree-functions
281 'org-bibtex-merge-contiguous-citations
)))
287 (defadvice org-latex-keyword
(around bibtex-keyword
)
288 "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
289 Fallback to `latex' back-end for other keywords."
290 (let ((keyword (ad-get-arg 0)))
291 (if (not (equal (org-element-property :key keyword
) "BIBLIOGRAPHY"))
293 (let ((file (org-bibtex-get-file keyword
))
294 (style (org-bibtex-get-style keyword
)))
295 (setq ad-return-value
297 (concat (and style
(format "\\bibliographystyle{%s}\n" style
))
298 (format "\\bibliography{%s}" file
))))))))
300 (ad-activate 'org-latex-keyword
)
306 (defvar org-bibtex-html-entries-alist nil
) ; Dynamically scoped.
307 (defvar org-bibtex-html-keywords-alist nil
) ; Dynamically scoped.
312 (defadvice org-html-keyword
(around bibtex-keyword
)
313 "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
314 Fallback to `html' back-end for other keywords."
315 (let ((keyword (ad-get-arg 0)))
316 (if (not (equal (org-element-property :key keyword
) "BIBLIOGRAPHY"))
318 (setq ad-return-value
319 (cdr (assq keyword org-bibtex-html-keywords-alist
))))))
321 (defadvice org-html-latex-fragment
(around bibtex-citation
)
322 "Translate \"\\cite\" LaTeX fragments into HTML syntax.
323 Fallback to `html' back-end for other keywords."
324 (let ((fragment (ad-get-arg 0)))
325 (if (not (org-bibtex-citation-p fragment
)) ad-do-it
326 (setq ad-return-value
330 (format "<a href=\"#%s\">%s</a>"
332 (or (cdr (assoc key org-bibtex-html-entries-alist
))
335 (org-bibtex-get-citation-key fragment
) ",") ","))))))
337 (ad-activate 'org-html-keyword
)
338 (ad-activate 'org-html-latex-fragment
)
342 (defadvice org-ascii-keyword
(around bibtex-keyword
)
343 "Translate \"BIBLIOGRAPHY\" keywords into ascii syntax.
344 Fallback to `ascii' back-end for other keywords."
345 (let ((keyword (ad-get-arg 0)))
346 (if (not (equal (org-element-property :key keyword
) "BIBLIOGRAPHY"))
348 (setq ad-return-value
349 (cdr (assq keyword org-bibtex-html-keywords-alist
))))))
351 (defadvice org-ascii-latex-fragment
(around bibtex-citation
)
352 "Translate \"\\cite\" LaTeX fragments into ascii syntax.
353 Fallback to `ascii' back-end for other keywords."
354 (let ((fragment (ad-get-arg 0)))
355 (if (not (org-bibtex-citation-p fragment
)) ad-do-it
356 (setq ad-return-value
360 (or (cdr (assoc key org-bibtex-html-entries-alist
))
363 (org-bibtex-get-citation-key fragment
) ",") ","))))))
365 (ad-activate 'org-ascii-keyword
)
366 (ad-activate 'org-ascii-latex-fragment
)
370 ;;; ox-bibtex.el ends here