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."
254 (when (org-export-derived-backend-p backend
'html
'latex
'ascii
)
255 (org-element-map tree
'(link latex-fragment
)
257 (when (org-bibtex-citation-p object
)
258 (let ((new-citation (list 'latex-fragment
260 :post-blank
(org-element-property
261 :post-blank object
)))))
262 ;; Insert NEW-CITATION right before OBJECT.
263 (org-element-insert-before new-citation object
)
264 ;; Remove all subsequent contiguous citations from parse
265 ;; tree, keeping only their citation key.
266 (let ((keys (list (org-bibtex-get-citation-key object
)))
268 (while (and (setq next
(org-export-get-next-element object info
))
269 (or (and (stringp next
)
270 (not (org-string-match-p "\\S-" next
)))
271 (org-bibtex-citation-p next
)))
272 (unless (stringp next
)
273 (push (org-bibtex-get-citation-key next
) keys
))
274 (org-element-extract-element object
)
276 (org-element-extract-element object
)
277 ;; Eventually merge all keys within NEW-CITATION. Also
278 ;; ensure NEW-CITATION has the same :post-blank property
279 ;; as the last citation removed.
280 (org-element-put-property
282 :post-blank
(org-element-property :post-blank object
))
283 (org-element-put-property
285 :value
(format "\\cite{%s}"
286 (mapconcat 'identity
(nreverse keys
) ",")))))))))
290 '(progn (add-to-list 'org-export-filter-parse-tree-functions
291 'org-bibtex-process-bib-files
)
292 (add-to-list 'org-export-filter-parse-tree-functions
293 'org-bibtex-merge-contiguous-citations
)))
299 (defadvice org-latex-keyword
(around bibtex-keyword
)
300 "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
301 Fallback to `latex' back-end for other keywords."
302 (let ((keyword (ad-get-arg 0)))
303 (if (not (equal (org-element-property :key keyword
) "BIBLIOGRAPHY"))
305 (let ((file (org-bibtex-get-file keyword
))
306 (style (org-not-nil (org-bibtex-get-style keyword
))))
307 (setq ad-return-value
309 (concat (and style
(format "\\bibliographystyle{%s}\n" style
))
310 (format "\\bibliography{%s}" file
))))))))
312 (ad-activate 'org-latex-keyword
)
318 (defvar org-bibtex-html-entries-alist nil
) ; Dynamically scoped.
319 (defvar org-bibtex-html-keywords-alist nil
) ; Dynamically scoped.
324 (defadvice org-html-keyword
(around bibtex-keyword
)
325 "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
326 Fallback to `html' back-end for other keywords."
327 (let ((keyword (ad-get-arg 0)))
328 (if (not (equal (org-element-property :key keyword
) "BIBLIOGRAPHY"))
330 (setq ad-return-value
331 (cdr (assq keyword org-bibtex-html-keywords-alist
))))))
333 (defadvice org-html-latex-fragment
(around bibtex-citation
)
334 "Translate \"\\cite\" LaTeX fragments into HTML syntax.
335 Fallback to `html' back-end for other keywords."
336 (let ((fragment (ad-get-arg 0)))
337 (if (not (org-bibtex-citation-p fragment
)) ad-do-it
338 (setq ad-return-value
342 (format "<a href=\"#%s\">%s</a>"
344 (or (cdr (assoc key org-bibtex-html-entries-alist
))
347 (org-bibtex-get-citation-key fragment
) ",") ","))))))
349 (ad-activate 'org-html-keyword
)
350 (ad-activate 'org-html-latex-fragment
)
354 (defadvice org-ascii-keyword
(around bibtex-keyword
)
355 "Translate \"BIBLIOGRAPHY\" keywords into ascii syntax.
356 Fallback to `ascii' back-end for other keywords."
357 (let ((keyword (ad-get-arg 0)))
358 (if (not (equal (org-element-property :key keyword
) "BIBLIOGRAPHY"))
360 (setq ad-return-value
361 (cdr (assq keyword org-bibtex-html-keywords-alist
))))))
363 (defadvice org-ascii-latex-fragment
(around bibtex-citation
)
364 "Translate \"\\cite\" LaTeX fragments into ascii syntax.
365 Fallback to `ascii' back-end for other keywords."
366 (let ((fragment (ad-get-arg 0)))
367 (if (not (org-bibtex-citation-p fragment
)) ad-do-it
368 (setq ad-return-value
372 (or (cdr (assoc key org-bibtex-html-entries-alist
))
375 (org-bibtex-get-citation-key fragment
) ",") ","))))))
377 (ad-activate 'org-ascii-keyword
)
378 (ad-activate 'org-ascii-latex-fragment
)
382 ;;; ox-bibtex.el ends here