ox-bibtex.el 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. ;; The usage is as follows:
  26. ;;
  27. ;; #+BIBLIOGRAPHY: bibfilebasename stylename optional-options
  28. ;;
  29. ;; e.g. given foo.bib and using style plain:
  30. ;;
  31. ;; #+BIBLIOGRAPHY: foo plain option:-d
  32. ;;
  33. ;; Optional options are of the form:
  34. ;;
  35. ;; option:-foobar pass '-foobar' to bibtex2html
  36. ;;
  37. ;; e.g.,
  38. ;;
  39. ;; option:-d sort by date
  40. ;; option:-a sort as BibTeX (usually by author) *default*
  41. ;; option:-u unsorted i.e. same order as in .bib file
  42. ;; option:-r reverse the sort
  43. ;;
  44. ;; See the bibtex2html man page for more. Multiple options can be
  45. ;; combined like:
  46. ;;
  47. ;; option:-d option:-r
  48. ;;
  49. ;; Limiting to only the entries cited in the document:
  50. ;;
  51. ;; limit:t
  52. ;;
  53. ;; For LaTeX export this simply inserts the lines
  54. ;;
  55. ;; \bibliographystyle{plain}
  56. ;; \bibliography{foo}
  57. ;;
  58. ;; into the TeX file when exporting.
  59. ;;
  60. ;; For HTML export it:
  61. ;; 1) converts all \cite{foo} to links to the bibliography,
  62. ;; 2) creates a foo.html and foo_bib.html,
  63. ;; 3) includes the contents of foo.html in the exported HTML file.
  64. ;;; Internal Functions
  65. (defun org-bibtex-get-file (keyword)
  66. "Return bibliography file as a string.
  67. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no file is found,
  68. return nil instead."
  69. (let ((value (org-element-property :value keyword)))
  70. (and value
  71. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  72. (match-string 1 value))))
  73. (defun org-bibtex-get-style (keyword)
  74. "Return bibliography style as a string.
  75. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no style is found,
  76. return nil instead."
  77. (let ((value (org-element-property :value keyword)))
  78. (and value
  79. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  80. (match-string 2 value))))
  81. (defun org-bibtex-get-arguments (keyword)
  82. "Return \"bibtex2html\" arguments specified by the user.
  83. KEYWORD is a \"BIBLIOGRAPHY\" keyword. Return value is a plist
  84. containing `:options' and `:limit' properties. The former
  85. contains a list of strings to be passed as options ot
  86. \"bibtex2html\" process. The latter contains a boolean."
  87. (let ((value (org-element-property :value keyword)))
  88. (and value
  89. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  90. (let (options limit)
  91. (dolist (arg (org-split-string (match-string 3 value))
  92. ;; Return value.
  93. (list :options (nreverse options) :limit limit))
  94. (let* ((s (split-string arg ":"))
  95. (key (car s))
  96. (value (nth 1 s)))
  97. (cond ((equal "limit" key)
  98. (setq limit (not (equal "nil" value))))
  99. ((equal "option" key) (push value options)))))))))
  100. (defun org-bibtex-citation-p (fragment)
  101. "Non-nil when a LaTeX macro is a citation.
  102. FRAGMENT is a `latex-fragment' type object."
  103. (string-match "\\`\\\\cite{" (org-element-property :value fragment)))
  104. (defun org-bibtex-get-citation-key (citation)
  105. "Return key for a given citation, as a string.
  106. CITATION is a `latex-fragment' type object satisfying to
  107. `org-bibtex-citation-p' predicate."
  108. (let ((value (org-element-property :value citation)))
  109. (and (string-match "\\`\\\\cite{" value)
  110. (substring value (match-end 0) -1))))
  111. ;;; LaTeX Part
  112. (defadvice org-latex-keyword (around bibtex-keyword)
  113. "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
  114. Fallback to `latex' back-end for other keywords."
  115. (let ((keyword (ad-get-arg 0)))
  116. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  117. ad-do-it
  118. (let ((file (org-bibtex-get-file keyword))
  119. (style (org-bibtex-get-style keyword)))
  120. (setq ad-return-value
  121. (when file
  122. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  123. (format "\\bibliography{%s}" file))))))))
  124. (ad-activate 'org-latex-keyword)
  125. ;;; HTML Part
  126. (defvar org-bibtex-html-entries-alist nil) ; Dynamically scoped.
  127. (defvar org-bibtex-html-keywords-alist nil) ; Dynamically scoped.
  128. ;;;; Advices
  129. (defadvice org-html-keyword (around bibtex-keyword)
  130. "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
  131. Fallback to `html' back-end for other keywords."
  132. (let ((keyword (ad-get-arg 0)))
  133. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  134. ad-do-it
  135. (setq ad-return-value
  136. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  137. (defadvice org-html-latex-fragment (around bibtex-citation)
  138. "Translate \"\\cite\" LaTeX fragments into HTML syntax.
  139. Fallback to `html' back-end for other keywords."
  140. (let ((fragment (ad-get-arg 0)))
  141. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  142. (setq ad-return-value
  143. (mapconcat
  144. (lambda (key)
  145. (let ((key (org-trim key)))
  146. (format "[<a href=\"#%s\">%s</a>]"
  147. key
  148. (or (cdr (assoc key org-bibtex-html-entries-alist))
  149. key))))
  150. (org-split-string (org-bibtex-get-citation-key fragment) ",")
  151. "")))))
  152. (ad-activate 'org-html-keyword)
  153. (ad-activate 'org-html-latex-fragment)
  154. ;;;; Filter
  155. (defun org-bibtex-process-bib-files (tree backend info)
  156. "Send each bibliography in parse tree to \"bibtex2html\" process.
  157. Return new parse tree. This function assumes current back-end is HTML."
  158. ;; Initialize dynamically scoped variables. The first one
  159. ;; contain an alist between keyword objects and their HTML
  160. ;; translation. The second one will contain an alist between
  161. ;; citation keys and names in the output (according to style).
  162. (setq org-bibtex-html-entries-alist nil
  163. org-bibtex-html-keywords-alist nil)
  164. (org-element-map tree 'keyword
  165. (lambda (keyword)
  166. (when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
  167. (let ((arguments (org-bibtex-get-arguments keyword))
  168. (file (org-bibtex-get-file keyword))
  169. temp-file)
  170. ;; limit is set: collect citations throughout the document
  171. ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
  172. ;; argument.
  173. (when (plist-get arguments :limit)
  174. (let ((citations
  175. (org-element-map tree 'latex-fragment
  176. (lambda (fragment)
  177. (and (org-bibtex-citation-p fragment)
  178. (org-bibtex-get-citation-key fragment))))))
  179. (with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
  180. (insert (mapconcat 'identity citations "\n")))
  181. (setq arguments
  182. (plist-put arguments
  183. :options
  184. (append (plist-get arguments :options)
  185. (list "-citefile" temp-file))))))
  186. ;; Call "bibtex2html" on specified file.
  187. (unless (eq 0 (apply 'call-process
  188. (append '("bibtex2html" nil nil nil)
  189. '("-a" "-nodoc" "-noheader" "-nofooter")
  190. (list "--style"
  191. (org-bibtex-get-style keyword))
  192. (plist-get arguments :options)
  193. (list (concat file ".bib")))))
  194. (error "Executing bibtex2html failed"))
  195. (and temp-file (delete-file temp-file))
  196. ;; Open produced HTML file, wrap references within a block and
  197. ;; return it.
  198. (with-temp-buffer
  199. (insert "<div id=\"bibliography\">\n<h2>References</h2>\n")
  200. (insert-file-contents (concat file ".html"))
  201. (insert "\n</div>")
  202. ;; Update `org-bibtex-html-keywords-alist'.
  203. (push (cons keyword (buffer-string))
  204. org-bibtex-html-keywords-alist)
  205. ;; Update `org-bibtex-html-entries-alist'.
  206. (goto-char (point-min))
  207. (while (re-search-forward
  208. "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t)
  209. (push (cons (match-string 1) (match-string 2))
  210. org-bibtex-html-entries-alist)))))))
  211. ;; Return parse tree unchanged.
  212. tree)
  213. (eval-after-load 'ox
  214. '(add-to-list 'org-export-filter-parse-tree-functions
  215. 'org-bibtex-process-bib-files))
  216. (provide 'ox-bibtex)
  217. ;;; ox-bibtex.el ends here