1 ;;; ox-bibtex.el --- Export bibtex fragments
3 ;; Copyright (C) 2009-2014 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 ;; "stylename" can also be "nil", in which case no style will be used.
47 ;; Optional options are of the form:
49 ;; option:-foobar pass '-foobar' to bibtex2html
53 ;; option:-d sort by date
54 ;; option:-a sort as BibTeX (usually by author) *default*
55 ;; option:-u unsorted i.e. same order as in .bib file
56 ;; option:-r reverse the sort
58 ;; See the bibtex2html man page for more. Multiple options can be
61 ;; option:-d option:-r
63 ;; Limiting to only the entries cited in the document:
67 ;; For LaTeX export this simply inserts the lines
69 ;; \bibliographystyle{plain}
72 ;; into the TeX file when exporting.
74 ;; For HTML export it:
75 ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
77 ;; 2) creates a foo.html and foo_bib.html,
78 ;; 3) includes the contents of foo.html in the exported HTML file.
80 ;; For ascii export it:
81 ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
83 ;; 2) creates a foo.txt and foo_bib.html,
84 ;; 3) includes the contents of foo.txt in the exported ascii file.
86 ;; For LaTeX export it:
87 ;; 1) converts all [[cite:foo]] to \cite{foo}.
91 (eval-when-compile (require 'cl
))
92 (let ((jump-fn (car (org-remove-if-not #'fboundp
'(ebib obe-goto-citation
)))))
93 (org-add-link-type "cite" jump-fn
))
95 ;;; Internal Functions
97 (defun org-bibtex-get-file (keyword)
98 "Return bibliography file as a string.
99 KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no file is found,
101 (let ((value (org-element-property :value keyword
)))
103 (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value
)
104 (match-string 1 value
))))
106 (defun org-bibtex-get-style (keyword)
107 "Return bibliography style as a string.
108 KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no style is found,
110 (let ((value (org-element-property :value keyword
)))
112 (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value
)
113 (match-string 2 value
))))
115 (defun org-bibtex-get-arguments (keyword)
116 "Return \"bibtex2html\" arguments specified by the user.
117 KEYWORD is a \"BIBLIOGRAPHY\" keyword. Return value is a plist
118 containing `:options' and `:limit' properties. The former
119 contains a list of strings to be passed as options to
120 \"bibtex2html\" process. The latter contains a boolean."
121 (let ((value (org-element-property :value keyword
)))
123 (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value
)
125 (dolist (arg (org-split-string (match-string 3 value
))
127 (list :options
(nreverse options
) :limit limit
))
128 (let* ((s (split-string arg
":"))
131 (cond ((equal "limit" key
)
132 (setq limit
(not (equal "nil" value
))))
133 ((equal "option" key
) (push value options
)))))))))
135 (defun org-bibtex-citation-p (object)
136 "Non-nil when OBJECT is a citation."
137 (case (org-element-type object
)
138 (link (equal (org-element-property :type object
) "cite"))
140 (string-match "\\`\\\\cite{" (org-element-property :value object
)))))
142 (defun org-bibtex-get-citation-key (citation)
143 "Return key for a given citation, as a string.
144 CITATION is a `latex-fragment' or `link' type object satisfying
145 to `org-bibtex-citation-p' predicate."
146 (if (eq (org-element-type citation
) 'link
)
147 (org-element-property :path citation
)
148 (let ((value (org-element-property :value citation
)))
149 (and (string-match "\\`\\\\cite{" value
)
150 (substring value
(match-end 0) -
1)))))
156 (defun org-bibtex-process-bib-files (tree backend info
)
157 "Send each bibliography in parse tree to \"bibtex2html\" process.
158 Return new parse tree."
159 (when (org-export-derived-backend-p backend
'ascii
'html
)
160 ;; Initialize dynamically scoped variables. The first one
161 ;; contain an alist between keyword objects and their HTML
162 ;; translation. The second one will contain an alist between
163 ;; citation keys and names in the output (according to style).
164 (setq org-bibtex-html-entries-alist nil
165 org-bibtex-html-keywords-alist nil
)
166 (org-element-map tree
'keyword
168 (when (equal (org-element-property :key keyword
) "BIBLIOGRAPHY")
169 (let ((arguments (org-bibtex-get-arguments keyword
))
170 (file (org-bibtex-get-file keyword
))
172 ;; limit is set: collect citations throughout the document
173 ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
175 (when (plist-get arguments
:limit
)
177 (org-element-map tree
'(latex-fragment link
)
179 (and (org-bibtex-citation-p object
)
180 (org-bibtex-get-citation-key object
))))))
181 (with-temp-file (setq temp-file
(make-temp-file "ox-bibtex"))
182 (insert (mapconcat 'identity citations
"\n")))
186 (append (plist-get arguments
:options
)
187 (list "-citefile" temp-file
))))))
188 ;; Call "bibtex2html" on specified file.
191 (append '("bibtex2html" nil nil nil
)
192 '("-a" "-nodoc" "-noheader" "-nofooter")
195 (org-bibtex-get-style keyword
))))
196 (and style
(list "--style" style
)))
197 (plist-get arguments
:options
)
198 (list (concat file
".bib")))))
199 (error "Executing bibtex2html failed"))
200 (and temp-file
(delete-file temp-file
))
201 ;; Open produced HTML file, and collect Bibtex key names
203 (insert-file-contents (concat file
".html"))
204 ;; Update `org-bibtex-html-entries-alist'.
205 (goto-char (point-min))
206 (while (re-search-forward
207 "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t
)
208 (push (cons (match-string 1) (match-string 2))
209 org-bibtex-html-entries-alist
)))
210 ;; Open produced HTML file, wrap references within a block and
214 ((org-export-derived-backend-p backend
'html
)
215 (insert (format "<div id=\"bibliography\">\n<h2>%s</h2>\n"
216 (org-export-translate "References" :html info
)))
217 (insert-file-contents (concat file
".html"))
219 ((org-export-derived-backend-p backend
'ascii
)
220 ;; convert HTML references to text w/pandoc
221 (unless (eq 0 (call-process "pandoc" nil nil nil
222 (concat file
".html")
224 (concat file
".txt")))
225 (error "Executing pandoc failed"))
229 (org-export-translate
231 (intern (format ":%s" (plist-get info
:ascii-charset
)))
233 (insert-file-contents (concat file
".txt"))
234 (goto-char (point-min))
235 (while (re-search-forward
236 "\\[ \\[bib\\][^ ]+ \\(\\]\\||[\n\r]\\)" nil t
)
238 (goto-char (point-min))
239 (while (re-search-forward "\\( \\]\\| \\]\\| |\\)" nil t
)
241 (goto-char (point-min))
242 (while (re-search-forward "[\n\r]\\([\n\r][\n\r]\\)" nil t
)
243 (replace-match "\\1"))))
244 ;; Update `org-bibtex-html-keywords-alist'.
245 (push (cons keyword
(buffer-string))
246 org-bibtex-html-keywords-alist
)))))))
247 ;; Return parse tree unchanged.
250 (defun org-bibtex-merge-contiguous-citations (tree backend info
)
251 "Merge all contiguous citation in parse tree.
252 As a side effect, this filter will also turn all \"cite\" links
253 into \"\\cite{...}\" LaTeX fragments and will extract options.
254 Cite options are placed into square brackets at the beginning of
255 the \"\\cite\" command for the LaTeX backend, and are removed for
256 the HTML and ASCII backends."
257 (when (org-export-derived-backend-p backend
'html
'latex
'ascii
)
258 (org-element-map tree
'(link latex-fragment
)
260 (when (org-bibtex-citation-p object
)
261 (let ((new-citation (list 'latex-fragment
263 :post-blank
(org-element-property
264 :post-blank object
))))
266 ;; Insert NEW-CITATION right before OBJECT.
267 (org-element-insert-before new-citation object
)
268 ;; Remove all subsequent contiguous citations from parse
269 ;; tree, keeping only their citation key.
270 (let ((keys (list (org-bibtex-get-citation-key object
)))
272 (while (and (setq next
(org-export-get-next-element object info
))
273 (or (and (stringp next
)
274 (not (org-string-match-p "\\S-" next
)))
275 (org-bibtex-citation-p next
)))
276 (unless (stringp next
)
277 (push (org-bibtex-get-citation-key next
) keys
))
278 (org-element-extract-element object
)
280 ;; Find any options in keys, e.g., "(Chapter 2)key" has
281 ;; the option "Chapter 2".
285 (if (string-match "^(\\([^)]\+\\))\\(.*\\)" k
)
287 (when (org-export-derived-backend-p backend
'latex
)
288 (setq option
(format "[%s]" (match-string 1 k
))))
292 (org-element-extract-element object
)
293 ;; Eventually merge all keys within NEW-CITATION. Also
294 ;; ensure NEW-CITATION has the same :post-blank property
295 ;; as the last citation removed.
296 (org-element-put-property
298 :post-blank
(org-element-property :post-blank object
))
299 (org-element-put-property
301 :value
(format "\\cite%s{%s}"
303 (mapconcat 'identity
(nreverse keys
) ",")))))))))
307 '(progn (add-to-list 'org-export-filter-parse-tree-functions
308 'org-bibtex-process-bib-files
)
309 (add-to-list 'org-export-filter-parse-tree-functions
310 'org-bibtex-merge-contiguous-citations
)))
316 (defadvice org-latex-keyword
(around bibtex-keyword
)
317 "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
318 Fallback to `latex' back-end for other keywords."
319 (let ((keyword (ad-get-arg 0)))
320 (if (not (equal (org-element-property :key keyword
) "BIBLIOGRAPHY"))
322 (let ((file (org-bibtex-get-file keyword
))
323 (style (org-not-nil (org-bibtex-get-style keyword
))))
324 (setq ad-return-value
326 (concat (and style
(format "\\bibliographystyle{%s}\n" style
))
327 (format "\\bibliography{%s}" file
))))))))
329 (ad-activate 'org-latex-keyword
)
335 (defvar org-bibtex-html-entries-alist nil
) ; Dynamically scoped.
336 (defvar org-bibtex-html-keywords-alist nil
) ; Dynamically scoped.
341 (defadvice org-html-keyword
(around bibtex-keyword
)
342 "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
343 Fallback to `html' back-end for other keywords."
344 (let ((keyword (ad-get-arg 0)))
345 (if (not (equal (org-element-property :key keyword
) "BIBLIOGRAPHY"))
347 (setq ad-return-value
348 (cdr (assq keyword org-bibtex-html-keywords-alist
))))))
350 (defadvice org-html-latex-fragment
(around bibtex-citation
)
351 "Translate \"\\cite\" LaTeX fragments into HTML syntax.
352 Fallback to `html' back-end for other keywords."
353 (let ((fragment (ad-get-arg 0)))
354 (if (not (org-bibtex-citation-p fragment
)) ad-do-it
355 (setq ad-return-value
359 (format "<a href=\"#%s\">%s</a>"
361 (or (cdr (assoc key org-bibtex-html-entries-alist
))
364 (org-bibtex-get-citation-key fragment
) ",") ","))))))
366 (ad-activate 'org-html-keyword
)
367 (ad-activate 'org-html-latex-fragment
)
371 (defadvice org-ascii-keyword
(around bibtex-keyword
)
372 "Translate \"BIBLIOGRAPHY\" keywords into ascii syntax.
373 Fallback to `ascii' back-end for other keywords."
374 (let ((keyword (ad-get-arg 0)))
375 (if (not (equal (org-element-property :key keyword
) "BIBLIOGRAPHY"))
377 (setq ad-return-value
378 (cdr (assq keyword org-bibtex-html-keywords-alist
))))))
380 (defadvice org-ascii-latex-fragment
(around bibtex-citation
)
381 "Translate \"\\cite\" LaTeX fragments into ascii syntax.
382 Fallback to `ascii' back-end for other keywords."
383 (let ((fragment (ad-get-arg 0)))
384 (if (not (org-bibtex-citation-p fragment
)) ad-do-it
385 (setq ad-return-value
389 (or (cdr (assoc key org-bibtex-html-entries-alist
))
392 (org-bibtex-get-citation-key fragment
) ",") ","))))))
394 (ad-activate 'org-ascii-keyword
)
395 (ad-activate 'org-ascii-latex-fragment
)
399 ;;; ox-bibtex.el ends here