ox-bibtex.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ;;; ox-bibtex.el --- Export bibtex fragments
  2. ;; Copyright (C) 2009-2014 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. (org-add-link-type "cite" 'ebib)
  73. ;;; Internal Functions
  74. (defun org-bibtex-get-file (keyword)
  75. "Return bibliography file as a string.
  76. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no file is found,
  77. return nil instead."
  78. (let ((value (org-element-property :value keyword)))
  79. (and value
  80. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  81. (match-string 1 value))))
  82. (defun org-bibtex-get-style (keyword)
  83. "Return bibliography style as a string.
  84. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no style is found,
  85. return nil instead."
  86. (let ((value (org-element-property :value keyword)))
  87. (and value
  88. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  89. (match-string 2 value))))
  90. (defun org-bibtex-get-arguments (keyword)
  91. "Return \"bibtex2html\" arguments specified by the user.
  92. KEYWORD is a \"BIBLIOGRAPHY\" keyword. Return value is a plist
  93. containing `:options' and `:limit' properties. The former
  94. contains a list of strings to be passed as options ot
  95. \"bibtex2html\" process. The latter contains a boolean."
  96. (let ((value (org-element-property :value keyword)))
  97. (and value
  98. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  99. (let (options limit)
  100. (dolist (arg (org-split-string (match-string 3 value))
  101. ;; Return value.
  102. (list :options (nreverse options) :limit limit))
  103. (let* ((s (split-string arg ":"))
  104. (key (car s))
  105. (value (nth 1 s)))
  106. (cond ((equal "limit" key)
  107. (setq limit (not (equal "nil" value))))
  108. ((equal "option" key) (push value options)))))))))
  109. (defun org-bibtex-citation-p (object)
  110. "Non-nil when OBJECT is a citation."
  111. (case (org-element-type object)
  112. (link (equal (org-element-property :type object) "cite"))
  113. (latex-fragment
  114. (string-match "\\`\\\\cite{" (org-element-property :value object)))))
  115. (defun org-bibtex-get-citation-key (citation)
  116. "Return key for a given citation, as a string.
  117. CITATION is a `latex-fragment' or `link' type object satisfying
  118. to `org-bibtex-citation-p' predicate."
  119. (if (eq (org-element-type citation) 'link)
  120. (org-element-property :path citation)
  121. (let ((value (org-element-property :value citation)))
  122. (and (string-match "\\`\\\\cite{" value)
  123. (substring value (match-end 0) -1)))))
  124. ;;; LaTeX Part
  125. (defadvice org-latex-keyword (around bibtex-keyword)
  126. "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
  127. Fallback to `latex' back-end for other keywords."
  128. (let ((keyword (ad-get-arg 0)))
  129. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  130. ad-do-it
  131. (let ((file (org-bibtex-get-file keyword))
  132. (style (org-bibtex-get-style keyword)))
  133. (setq ad-return-value
  134. (when file
  135. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  136. (format "\\bibliography{%s}" file))))))))
  137. (defadvice org-latex-link (around bibtex-link)
  138. "Translate \"cite\" type links into LaTeX syntax.
  139. Fallback to `latex' back-end for other keywords."
  140. (let ((link (ad-get-arg 0)))
  141. (if (not (org-bibtex-citation-p link)) ad-do-it
  142. (setq ad-return-value
  143. (format "\\cite{%s}" (org-bibtex-get-citation-key link))))))
  144. (ad-activate 'org-latex-keyword)
  145. (ad-activate 'org-latex-link)
  146. ;;; HTML Part
  147. (defvar org-bibtex-html-entries-alist nil) ; Dynamically scoped.
  148. (defvar org-bibtex-html-keywords-alist nil) ; Dynamically scoped.
  149. ;;;; Advices
  150. (defadvice org-html-keyword (around bibtex-keyword)
  151. "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
  152. Fallback to `html' back-end for other keywords."
  153. (let ((keyword (ad-get-arg 0)))
  154. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  155. ad-do-it
  156. (setq ad-return-value
  157. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  158. (defadvice org-html-latex-fragment (around bibtex-citation)
  159. "Translate \"\\cite\" LaTeX fragments into HTML syntax.
  160. Fallback to `html' back-end for other keywords."
  161. (let ((fragment (ad-get-arg 0)))
  162. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  163. (setq ad-return-value
  164. (mapconcat
  165. (lambda (key)
  166. (let ((key (org-trim key)))
  167. (format "[<a href=\"#%s\">%s</a>]"
  168. key
  169. (or (cdr (assoc key org-bibtex-html-entries-alist))
  170. key))))
  171. (org-split-string (org-bibtex-get-citation-key fragment) ",")
  172. "")))))
  173. (defadvice org-html-link (around bibtex-link)
  174. "Translate \"cite:\" type links into HTML syntax.
  175. Fallback to `html' back-end for other types."
  176. (let ((link (ad-get-arg 0)))
  177. (if (not (org-bibtex-citation-p link)) ad-do-it
  178. (setq ad-return-value
  179. (mapconcat
  180. (lambda (key)
  181. (format "[<a href=\"#%s\">%s</a>]"
  182. key
  183. (or (cdr (assoc key org-bibtex-html-entries-alist))
  184. key)))
  185. (org-split-string (org-bibtex-get-citation-key link)
  186. "[ \t]*,[ \t]*")
  187. "")))))
  188. (ad-activate 'org-html-keyword)
  189. (ad-activate 'org-html-latex-fragment)
  190. (ad-activate 'org-html-link)
  191. ;;;; Filter
  192. (defun org-bibtex-process-bib-files (tree backend info)
  193. "Send each bibliography in parse tree to \"bibtex2html\" process.
  194. Return new parse tree. This function assumes current back-end is HTML."
  195. ;; Initialize dynamically scoped variables. The first one
  196. ;; contain an alist between keyword objects and their HTML
  197. ;; translation. The second one will contain an alist between
  198. ;; citation keys and names in the output (according to style).
  199. (setq org-bibtex-html-entries-alist nil
  200. org-bibtex-html-keywords-alist nil)
  201. (org-element-map tree 'keyword
  202. (lambda (keyword)
  203. (when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
  204. (let ((arguments (org-bibtex-get-arguments keyword))
  205. (file (org-bibtex-get-file keyword))
  206. temp-file)
  207. ;; limit is set: collect citations throughout the document
  208. ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
  209. ;; argument.
  210. (when (plist-get arguments :limit)
  211. (let ((citations
  212. (org-element-map tree '(latex-fragment link)
  213. (lambda (object)
  214. (and (org-bibtex-citation-p object)
  215. (org-bibtex-get-citation-key object))))))
  216. (with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
  217. (insert (mapconcat 'identity citations "\n")))
  218. (setq arguments
  219. (plist-put arguments
  220. :options
  221. (append (plist-get arguments :options)
  222. (list "-citefile" temp-file))))))
  223. ;; Call "bibtex2html" on specified file.
  224. (unless (eq 0 (apply 'call-process
  225. (append '("bibtex2html" nil nil nil)
  226. '("-a" "-nodoc" "-noheader" "-nofooter")
  227. (list "--style"
  228. (org-bibtex-get-style keyword))
  229. (plist-get arguments :options)
  230. (list (concat file ".bib")))))
  231. (error "Executing bibtex2html failed"))
  232. (and temp-file (delete-file temp-file))
  233. ;; Open produced HTML file, wrap references within a block and
  234. ;; return it.
  235. (with-temp-buffer
  236. (insert "<div id=\"bibliography\">\n<h2>References</h2>\n")
  237. (insert-file-contents (concat file ".html"))
  238. (insert "\n</div>")
  239. ;; Update `org-bibtex-html-keywords-alist'.
  240. (push (cons keyword (buffer-string))
  241. org-bibtex-html-keywords-alist)
  242. ;; Update `org-bibtex-html-entries-alist'.
  243. (goto-char (point-min))
  244. (while (re-search-forward
  245. "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t)
  246. (push (cons (match-string 1) (match-string 2))
  247. org-bibtex-html-entries-alist)))))))
  248. ;; Return parse tree unchanged.
  249. tree)
  250. (eval-after-load 'ox
  251. '(add-to-list 'org-export-filter-parse-tree-functions
  252. 'org-bibtex-process-bib-files))
  253. (provide 'ox-bibtex)
  254. ;;; ox-bibtex.el ends here