Revert "ox-latex.el: Resurrect `org-latex-with-hyperref'"
[org-mode.git] / contrib / lisp / ox-bibtex.el
blob071986691a9b47a7d2add57080f05e54974a5d32
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.
24 ;;; Commentary:
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: bibfilename 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 ;; Full filepaths are also possible:
49 ;; #+BIBLIOGRAPHY: /home/user/Literature/foo.bib plain option:-d
51 ;; Optional options are of the form:
53 ;; option:-foobar pass '-foobar' to bibtex2html
55 ;; e.g.,
57 ;; option:-d sort by date
58 ;; option:-a sort as BibTeX (usually by author) *default*
59 ;; option:-u unsorted i.e. same order as in .bib file
60 ;; option:-r reverse the sort
62 ;; See the bibtex2html man page for more. Multiple options can be
63 ;; combined like:
65 ;; option:-d option:-r
67 ;; Limiting to only the entries cited in the document:
69 ;; limit:t
71 ;; For LaTeX export this simply inserts the lines
73 ;; \bibliographystyle{plain}
74 ;; \bibliography{foo}
76 ;; into the TeX file when exporting.
78 ;; For HTML export it:
79 ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
80 ;; bibliography,
81 ;; 2) creates a foo.html and foo_bib.html,
82 ;; 3) includes the contents of foo.html in the exported HTML file.
84 ;; For ascii export it:
85 ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
86 ;; bibliography,
87 ;; 2) creates a foo.txt and foo_bib.html,
88 ;; 3) includes the contents of foo.txt in the exported ascii file.
90 ;; For LaTeX export it:
91 ;; 1) converts all [[cite:foo]] to \cite{foo}.
93 ;; Initialization
95 (eval-when-compile (require 'cl))
97 ;;; Internal Functions
99 (defun org-bibtex-get-file (keyword)
100 "Return bibliography file as a string.
101 KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no file is found,
102 return nil instead."
103 (let ((value (org-element-property :value keyword)))
104 (and value
105 (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
106 (match-string 1 value))))
108 (defun org-bibtex-get-style (keyword)
109 "Return bibliography style as a string.
110 KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no style is found,
111 return nil instead."
112 (let ((value (org-element-property :value keyword)))
113 (and value
114 (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
115 (match-string 2 value))))
117 (defun org-bibtex-get-arguments (keyword)
118 "Return \"bibtex2html\" arguments specified by the user.
119 KEYWORD is a \"BIBLIOGRAPHY\" keyword. Return value is a plist
120 containing `:options' and `:limit' properties. The former
121 contains a list of strings to be passed as options to
122 \"bibtex2html\" process. The latter contains a boolean."
123 (let ((value (org-element-property :value keyword)))
124 (and value
125 (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
126 (let (options limit)
127 (dolist (arg (org-split-string (match-string 3 value))
128 ;; Return value.
129 (list :options (nreverse options) :limit limit))
130 (let* ((s (split-string arg ":"))
131 (key (car s))
132 (value (nth 1 s)))
133 (cond ((equal "limit" key)
134 (setq limit (not (equal "nil" value))))
135 ((equal "option" key) (push value options)))))))))
137 (defun org-bibtex-citation-p (object)
138 "Non-nil when OBJECT is a citation."
139 (case (org-element-type object)
140 (link (equal (org-element-property :type object) "cite"))
141 (latex-fragment
142 (string-match "\\`\\\\cite{" (org-element-property :value object)))))
144 (defun org-bibtex-get-citation-key (citation)
145 "Return key for a given citation, as a string.
146 CITATION is a `latex-fragment' or `link' type object satisfying
147 to `org-bibtex-citation-p' predicate."
148 (if (eq (org-element-type citation) 'link)
149 (org-element-property :path citation)
150 (let ((value (org-element-property :value citation)))
151 (and (string-match "\\`\\\\cite{" value)
152 (substring value (match-end 0) -1)))))
155 ;;; Follow cite: links
157 (defun org-bibtex-file nil "Org-mode file of bibtex entries.")
159 (defun org-bibtex-goto-citation (&optional citation)
160 "Visit a citation given its ID."
161 (interactive)
162 (let ((citation (or citation
163 (org-icompleting-read "Citation: "
164 (obe-citations)))))
165 (find-file (or org-bibtex-file
166 (error "`org-bibtex-file' has not been configured")))
167 (goto-char (point-min))
168 (when (re-search-forward (format " :CUSTOM_ID: %s" citation) nil t)
169 (outline-previous-visible-heading 1)
170 t)))
172 (let ((jump-fn (car (org-remove-if-not #'fboundp '(ebib org-bibtex-goto-citation)))))
173 (org-add-link-type "cite" jump-fn))
177 ;;; Filters
179 (defun org-bibtex-process-bib-files (tree backend info)
180 "Send each bibliography in parse tree to \"bibtex2html\" process.
181 Return new parse tree."
182 (when (org-export-derived-backend-p backend 'ascii 'html)
183 ;; Initialize dynamically scoped variables. The first one
184 ;; contain an alist between keyword objects and their HTML
185 ;; translation. The second one will contain an alist between
186 ;; citation keys and names in the output (according to style).
187 (setq org-bibtex-html-entries-alist nil
188 org-bibtex-html-keywords-alist nil)
189 (org-element-map tree 'keyword
190 (lambda (keyword)
191 (when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
192 (let ((arguments (org-bibtex-get-arguments keyword))
193 (file (org-bibtex-get-file keyword))
194 temp-file
195 out-file)
196 ;; Test if filename is given with .bib-extension and strip
197 ;; it off. Filenames with another extensions will be
198 ;; untouched and will finally rise an error in bibtex2html.
199 (setq file (if (equal (file-name-extension file) "bib")
200 (file-name-sans-extension file) file))
201 ;; Outpufiles of bibtex2html will be put into current working directory
202 ;; so define a variable for this.
203 (setq out-file (file-name-sans-extension
204 (file-name-nondirectory file)))
205 ;; limit is set: collect citations throughout the document
206 ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
207 ;; argument.
208 (when (plist-get arguments :limit)
209 (let ((citations
210 (org-element-map tree '(latex-fragment link)
211 (lambda (object)
212 (and (org-bibtex-citation-p object)
213 (org-bibtex-get-citation-key object))))))
214 (with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
215 (insert (mapconcat 'identity citations "\n")))
216 (setq arguments
217 (plist-put arguments
218 :options
219 (append (plist-get arguments :options)
220 (list "-citefile" temp-file))))))
221 ;; Call "bibtex2html" on specified file.
222 (unless (eq 0 (apply
223 'call-process
224 (append '("bibtex2html" nil nil nil)
225 '("-a" "-nodoc" "-noheader" "-nofooter")
226 (let ((style
227 (org-not-nil
228 (org-bibtex-get-style keyword))))
229 (and style (list "--style" style)))
230 (plist-get arguments :options)
231 (list (concat file ".bib")))))
232 (error "Executing bibtex2html failed"))
233 (and temp-file (delete-file temp-file))
234 ;; Open produced HTML file, and collect Bibtex key names
235 (with-temp-buffer
236 (insert-file-contents (concat out-file ".html"))
237 ;; Update `org-bibtex-html-entries-alist'.
238 (goto-char (point-min))
239 (while (re-search-forward
240 "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t)
241 (push (cons (match-string 1) (match-string 2))
242 org-bibtex-html-entries-alist)))
243 ;; Open produced HTML file, wrap references within a block and
244 ;; return it.
245 (with-temp-buffer
246 (cond
247 ((org-export-derived-backend-p backend 'html)
248 (insert (format "<div id=\"bibliography\">\n<h2>%s</h2>\n"
249 (org-export-translate "References" :html info)))
250 (insert-file-contents (concat out-file ".html"))
251 (insert "\n</div>"))
252 ((org-export-derived-backend-p backend 'ascii)
253 ;; convert HTML references to text w/pandoc
254 (unless (eq 0 (call-process "pandoc" nil nil nil
255 (concat out-file ".html")
256 "-o"
257 (concat out-file ".txt")))
258 (error "Executing pandoc failed"))
259 (insert
260 (format
261 "%s\n==========\n\n"
262 (org-export-translate
263 "References"
264 (intern (format ":%s" (plist-get info :ascii-charset)))
265 info)))
266 (insert-file-contents (concat out-file ".txt"))
267 (goto-char (point-min))
268 (while (re-search-forward
269 "\\\\[bib\\][^ ]+ \\(\\]\\||[\n\r]\\)" nil t)
270 (replace-match ""))
271 (goto-char (point-min))
272 (while (re-search-forward "\\\\]\\\\]\\| |\\)" nil t)
273 (replace-match ""))
274 (goto-char (point-min))
275 (while (re-search-forward "[\n\r]\\([\n\r][\n\r]\\)" nil t)
276 (replace-match "\\1"))))
277 ;; Update `org-bibtex-html-keywords-alist'.
278 (push (cons keyword (buffer-string))
279 org-bibtex-html-keywords-alist)))))))
280 ;; Return parse tree unchanged.
281 tree)
283 (defun org-bibtex-merge-contiguous-citations (tree backend info)
284 "Merge all contiguous citation in parse tree.
285 As a side effect, this filter will also turn all \"cite\" links
286 into \"\\cite{...}\" LaTeX fragments and will extract options.
287 Cite options are placed into square brackets at the beginning of
288 the \"\\cite\" command for the LaTeX backend, and are removed for
289 the HTML and ASCII backends."
290 (when (org-export-derived-backend-p backend 'html 'latex 'ascii)
291 (org-element-map tree '(link latex-fragment)
292 (lambda (object)
293 (when (org-bibtex-citation-p object)
294 (let ((new-citation (list 'latex-fragment
295 (list :value ""
296 :post-blank (org-element-property
297 :post-blank object))))
298 option)
299 ;; Insert NEW-CITATION right before OBJECT.
300 (org-element-insert-before new-citation object)
301 ;; Remove all subsequent contiguous citations from parse
302 ;; tree, keeping only their citation key.
303 (let ((keys (list (org-bibtex-get-citation-key object)))
304 next)
305 (while (and (setq next (org-export-get-next-element object info))
306 (or (and (stringp next)
307 (not (org-string-match-p "\\S-" next)))
308 (org-bibtex-citation-p next)))
309 (unless (stringp next)
310 (push (org-bibtex-get-citation-key next) keys))
311 (org-element-extract-element object)
312 (setq object next))
313 ;; Find any options in keys, e.g., "(Chapter 2)key" has
314 ;; the option "Chapter 2".
315 (setq keys
316 (mapcar
317 (lambda (k)
318 (if (string-match "^(\\([^)]\+\\))\\(.*\\)" k)
319 (progn
320 (when (org-export-derived-backend-p backend 'latex)
321 (setq option (format "[%s]" (match-string 1 k))))
322 (match-string 2 k))
324 keys))
325 (org-element-extract-element object)
326 ;; Eventually merge all keys within NEW-CITATION. Also
327 ;; ensure NEW-CITATION has the same :post-blank property
328 ;; as the last citation removed.
329 (org-element-put-property
330 new-citation
331 :post-blank (org-element-property :post-blank object))
332 (org-element-put-property
333 new-citation
334 :value (format "\\cite%s{%s}"
335 (or option "")
336 (mapconcat 'identity (nreverse keys) ",")))))))))
337 tree)
339 (eval-after-load 'ox
340 '(progn (add-to-list 'org-export-filter-parse-tree-functions
341 'org-bibtex-process-bib-files)
342 (add-to-list 'org-export-filter-parse-tree-functions
343 'org-bibtex-merge-contiguous-citations)))
347 ;;; LaTeX Part
349 (defadvice org-latex-keyword (around bibtex-keyword)
350 "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
351 Fallback to `latex' back-end for other keywords."
352 (let ((keyword (ad-get-arg 0)))
353 (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
354 ad-do-it
355 (let ((file (org-bibtex-get-file keyword))
356 (style (org-not-nil (org-bibtex-get-style keyword))))
357 (setq ad-return-value
358 (when file
359 (concat (and style (format "\\bibliographystyle{%s}\n" style))
360 (format "\\bibliography{%s}" file))))))))
362 (ad-activate 'org-latex-keyword)
366 ;;; HTML Part
368 (defvar org-bibtex-html-entries-alist nil) ; Dynamically scoped.
369 (defvar org-bibtex-html-keywords-alist nil) ; Dynamically scoped.
372 ;;;; Advices
374 (defadvice org-html-keyword (around bibtex-keyword)
375 "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
376 Fallback to `html' back-end for other keywords."
377 (let ((keyword (ad-get-arg 0)))
378 (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
379 ad-do-it
380 (setq ad-return-value
381 (cdr (assq keyword org-bibtex-html-keywords-alist))))))
383 (defadvice org-html-latex-fragment (around bibtex-citation)
384 "Translate \"\\cite\" LaTeX fragments into HTML syntax.
385 Fallback to `html' back-end for other keywords."
386 (let ((fragment (ad-get-arg 0)))
387 (if (not (org-bibtex-citation-p fragment)) ad-do-it
388 (setq ad-return-value
389 (format "[%s]"
390 (mapconcat
391 (lambda (key)
392 (format "<a href=\"#%s\">%s</a>"
394 (or (cdr (assoc key org-bibtex-html-entries-alist))
395 key)))
396 (org-split-string
397 (org-bibtex-get-citation-key fragment) ",") ","))))))
399 (ad-activate 'org-html-keyword)
400 (ad-activate 'org-html-latex-fragment)
403 ;;; Ascii Part
404 (defadvice org-ascii-keyword (around bibtex-keyword)
405 "Translate \"BIBLIOGRAPHY\" keywords into ascii syntax.
406 Fallback to `ascii' back-end for other keywords."
407 (let ((keyword (ad-get-arg 0)))
408 (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
409 ad-do-it
410 (setq ad-return-value
411 (cdr (assq keyword org-bibtex-html-keywords-alist))))))
413 (defadvice org-ascii-latex-fragment (around bibtex-citation)
414 "Translate \"\\cite\" LaTeX fragments into ascii syntax.
415 Fallback to `ascii' back-end for other keywords."
416 (let ((fragment (ad-get-arg 0)))
417 (if (not (org-bibtex-citation-p fragment)) ad-do-it
418 (setq ad-return-value
419 (format "[%s]"
420 (mapconcat
421 (lambda (key)
422 (or (cdr (assoc key org-bibtex-html-entries-alist))
423 key))
424 (org-split-string
425 (org-bibtex-get-citation-key fragment) ",") ","))))))
427 (ad-activate 'org-ascii-keyword)
428 (ad-activate 'org-ascii-latex-fragment)
430 (provide 'ox-bibtex)
432 ;;; ox-bibtex.el ends here