ox-bibtex.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. ;;; ox-bibtex.el --- Export bibtex fragments
  2. ;; Copyright (C) 2009-2013 Taru Karttunen
  3. ;; Author: Taru Karttunen <taruti@taruti.net>
  4. ;; Nicolas Goaziou <n dot goaziou at gmail dot com>
  5. ;; This file is not currently part of GNU Emacs.
  6. ;; This program is free software; you can redistribute it and/or
  7. ;; modify it under the terms of the GNU General Public License as
  8. ;; published by the Free Software Foundation; either version 2, or (at
  9. ;; your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful, but
  11. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;; General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program ; see the file COPYING. If not, write to
  16. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. ;; Boston, MA 02111-1307, USA.
  18. ;;; Commentary:
  19. ;;
  20. ;; This is an utility to handle BibTeX export to both LaTeX and html
  21. ;; exports. It uses the bibtex2html software from:
  22. ;;
  23. ;; http://www.lri.fr/~filliatr/bibtex2html/
  24. ;;
  25. ;; It also introduces "cite" syntax for Org links.
  26. ;;
  27. ;; The usage is as follows:
  28. ;;
  29. ;; #+BIBLIOGRAPHY: bibfilebasename stylename optional-options
  30. ;;
  31. ;; e.g. given foo.bib and using style plain:
  32. ;;
  33. ;; #+BIBLIOGRAPHY: foo plain option:-d
  34. ;;
  35. ;; Optional options are of the form:
  36. ;;
  37. ;; option:-foobar pass '-foobar' to bibtex2html
  38. ;;
  39. ;; e.g.,
  40. ;;
  41. ;; option:-d sort by date
  42. ;; option:-a sort as BibTeX (usually by author) *default*
  43. ;; option:-u unsorted i.e. same order as in .bib file
  44. ;; option:-r reverse the sort
  45. ;;
  46. ;; See the bibtex2html man page for more. Multiple options can be
  47. ;; combined like:
  48. ;;
  49. ;; option:-d option:-r
  50. ;;
  51. ;; Limiting to only the entries cited in the document:
  52. ;;
  53. ;; limit:t
  54. ;;
  55. ;; For LaTeX export this simply inserts the lines
  56. ;;
  57. ;; \bibliographystyle{plain}
  58. ;; \bibliography{foo}
  59. ;;
  60. ;; into the TeX file when exporting.
  61. ;;
  62. ;; For HTML export it:
  63. ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
  64. ;; bibliography,
  65. ;; 2) creates a foo.html and foo_bib.html,
  66. ;; 3) includes the contents of foo.html in the exported HTML file.
  67. ;;
  68. ;; For LaTeX export it:
  69. ;; 1) converts all [[cite:foo]] to \cite{foo}.
  70. ;; Initialization
  71. (eval-when-compile (require 'cl))
  72. (let ((jump-fn (car (org-remove-if-not #'fboundp '(ebib obe-goto-citation)))))
  73. (org-add-link-type "cite" jump-fn))
  74. ;;; Internal Functions
  75. (defun org-bibtex-get-file (keyword)
  76. "Return bibliography file as a string.
  77. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no file is found,
  78. return nil instead."
  79. (let ((value (org-element-property :value keyword)))
  80. (and value
  81. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  82. (match-string 1 value))))
  83. (defun org-bibtex-get-style (keyword)
  84. "Return bibliography style as a string.
  85. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no style is found,
  86. return nil instead."
  87. (let ((value (org-element-property :value keyword)))
  88. (and value
  89. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  90. (match-string 2 value))))
  91. (defun org-bibtex-get-arguments (keyword)
  92. "Return \"bibtex2html\" arguments specified by the user.
  93. KEYWORD is a \"BIBLIOGRAPHY\" keyword. Return value is a plist
  94. containing `:options' and `:limit' properties. The former
  95. contains a list of strings to be passed as options ot
  96. \"bibtex2html\" process. The latter contains a boolean."
  97. (let ((value (org-element-property :value keyword)))
  98. (and value
  99. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  100. (let (options limit)
  101. (dolist (arg (org-split-string (match-string 3 value))
  102. ;; Return value.
  103. (list :options (nreverse options) :limit limit))
  104. (let* ((s (split-string arg ":"))
  105. (key (car s))
  106. (value (nth 1 s)))
  107. (cond ((equal "limit" key)
  108. (setq limit (not (equal "nil" value))))
  109. ((equal "option" key) (push value options)))))))))
  110. (defun org-bibtex-citation-p (object)
  111. "Non-nil when OBJECT is a citation."
  112. (case (org-element-type object)
  113. (link (equal (org-element-property :type object) "cite"))
  114. (latex-fragment
  115. (string-match "\\`\\\\cite{" (org-element-property :value object)))))
  116. (defun org-bibtex-get-citation-key (citation)
  117. "Return key for a given citation, as a string.
  118. CITATION is a `latex-fragment' or `link' type object satisfying
  119. to `org-bibtex-citation-p' predicate."
  120. (if (eq (org-element-type citation) 'link)
  121. (org-element-property :path citation)
  122. (let ((value (org-element-property :value citation)))
  123. (and (string-match "\\`\\\\cite{" value)
  124. (substring value (match-end 0) -1)))))
  125. ;;; LaTeX Part
  126. (defadvice org-latex-keyword (around bibtex-keyword)
  127. "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
  128. Fallback to `latex' back-end for other keywords."
  129. (let ((keyword (ad-get-arg 0)))
  130. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  131. ad-do-it
  132. (let ((file (org-bibtex-get-file keyword))
  133. (style (org-bibtex-get-style keyword)))
  134. (setq ad-return-value
  135. (when file
  136. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  137. (format "\\bibliography{%s}" file))))))))
  138. (defadvice org-latex-link (around bibtex-link)
  139. "Translate \"cite\" type links into LaTeX syntax.
  140. Fallback to `latex' back-end for other keywords."
  141. (let ((link (ad-get-arg 0)))
  142. (if (not (org-bibtex-citation-p link)) ad-do-it
  143. (setq ad-return-value
  144. (format "\\cite{%s}" (org-bibtex-get-citation-key link))))))
  145. (ad-activate 'org-latex-keyword)
  146. (ad-activate 'org-latex-link)
  147. ;;; HTML Part
  148. (defvar org-bibtex-html-entries-alist nil) ; Dynamically scoped.
  149. (defvar org-bibtex-html-keywords-alist nil) ; Dynamically scoped.
  150. ;;;; Advices
  151. (defadvice org-html-keyword (around bibtex-keyword)
  152. "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
  153. Fallback to `html' back-end for other keywords."
  154. (let ((keyword (ad-get-arg 0)))
  155. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  156. ad-do-it
  157. (setq ad-return-value
  158. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  159. (defadvice org-html-latex-fragment (around bibtex-citation)
  160. "Translate \"\\cite\" LaTeX fragments into HTML syntax.
  161. Fallback to `html' back-end for other keywords."
  162. (let ((fragment (ad-get-arg 0)))
  163. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  164. (setq ad-return-value
  165. (mapconcat
  166. (lambda (key)
  167. (let ((key (org-trim key)))
  168. (format "[<a href=\"#%s\">%s</a>]"
  169. key
  170. (or (cdr (assoc key org-bibtex-html-entries-alist))
  171. key))))
  172. (org-split-string (org-bibtex-get-citation-key fragment) ",")
  173. "")))))
  174. (defadvice org-html-link (around bibtex-link)
  175. "Translate \"cite:\" type links into HTML syntax.
  176. Fallback to `html' back-end for other types."
  177. (let ((link (ad-get-arg 0)))
  178. (if (not (org-bibtex-citation-p link)) ad-do-it
  179. (setq ad-return-value
  180. (mapconcat
  181. (lambda (key)
  182. (format "[<a href=\"#%s\">%s</a>]"
  183. key
  184. (or (cdr (assoc key org-bibtex-html-entries-alist))
  185. key)))
  186. (org-split-string (org-bibtex-get-citation-key link)
  187. "[ \t]*,[ \t]*")
  188. "")))))
  189. (ad-activate 'org-html-keyword)
  190. (ad-activate 'org-html-latex-fragment)
  191. (ad-activate 'org-html-link)
  192. ;;;; Filter
  193. (defun org-bibtex-process-bib-files (tree backend info)
  194. "Send each bibliography in parse tree to \"bibtex2html\" process.
  195. Return new parse tree. This function assumes current back-end is HTML."
  196. ;; Initialize dynamically scoped variables. The first one
  197. ;; contain an alist between keyword objects and their HTML
  198. ;; translation. The second one will contain an alist between
  199. ;; citation keys and names in the output (according to style).
  200. (setq org-bibtex-html-entries-alist nil
  201. org-bibtex-html-keywords-alist nil)
  202. (org-element-map tree 'keyword
  203. (lambda (keyword)
  204. (when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
  205. (let ((arguments (org-bibtex-get-arguments keyword))
  206. (file (org-bibtex-get-file keyword))
  207. temp-file)
  208. ;; limit is set: collect citations throughout the document
  209. ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
  210. ;; argument.
  211. (when (plist-get arguments :limit)
  212. (let ((citations
  213. (org-element-map tree '(latex-fragment link)
  214. (lambda (object)
  215. (and (org-bibtex-citation-p object)
  216. (org-bibtex-get-citation-key object))))))
  217. (with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
  218. (insert (mapconcat 'identity citations "\n")))
  219. (setq arguments
  220. (plist-put arguments
  221. :options
  222. (append (plist-get arguments :options)
  223. (list "-citefile" temp-file))))))
  224. ;; Call "bibtex2html" on specified file.
  225. (unless (eq 0 (apply 'call-process
  226. (append '("bibtex2html" nil nil nil)
  227. '("-a" "-nodoc" "-noheader" "-nofooter")
  228. (list "--style"
  229. (org-bibtex-get-style keyword))
  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, wrap references within a block and
  235. ;; return it.
  236. (with-temp-buffer
  237. (insert "<div id=\"bibliography\">\n<h2>References</h2>\n")
  238. (insert-file-contents (concat file ".html"))
  239. (insert "\n</div>")
  240. ;; Update `org-bibtex-html-keywords-alist'.
  241. (push (cons keyword (buffer-string))
  242. org-bibtex-html-keywords-alist)
  243. ;; Update `org-bibtex-html-entries-alist'.
  244. (goto-char (point-min))
  245. (while (re-search-forward
  246. "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t)
  247. (push (cons (match-string 1) (match-string 2))
  248. org-bibtex-html-entries-alist)))))))
  249. ;; Return parse tree unchanged.
  250. tree)
  251. (eval-after-load 'ox
  252. '(add-to-list 'org-export-filter-parse-tree-functions
  253. (lambda (e b i) (when (eql b 'html)
  254. (org-bibtex-process-bib-files e b i)))))
  255. (provide 'ox-bibtex)
  256. ;;; ox-bibtex.el ends here