ox-bibtex.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 to
  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. ;;; Filters
  126. (defun org-bibtex-process-bib-files (tree backend info)
  127. "Send each bibliography in parse tree to \"bibtex2html\" process.
  128. Return new parse tree."
  129. (when (org-export-derived-backend-p backend 'html)
  130. ;; Initialize dynamically scoped variables. The first one
  131. ;; contain an alist between keyword objects and their HTML
  132. ;; translation. The second one will contain an alist between
  133. ;; citation keys and names in the output (according to style).
  134. (setq org-bibtex-html-entries-alist nil
  135. org-bibtex-html-keywords-alist nil)
  136. (org-element-map tree 'keyword
  137. (lambda (keyword)
  138. (when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
  139. (let ((arguments (org-bibtex-get-arguments keyword))
  140. (file (org-bibtex-get-file keyword))
  141. temp-file)
  142. ;; limit is set: collect citations throughout the document
  143. ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
  144. ;; argument.
  145. (when (plist-get arguments :limit)
  146. (let ((citations
  147. (org-element-map tree '(latex-fragment link)
  148. (lambda (object)
  149. (and (org-bibtex-citation-p object)
  150. (org-bibtex-get-citation-key object))))))
  151. (with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
  152. (insert (mapconcat 'identity citations "\n")))
  153. (setq arguments
  154. (plist-put arguments
  155. :options
  156. (append (plist-get arguments :options)
  157. (list "-citefile" temp-file))))))
  158. ;; Call "bibtex2html" on specified file.
  159. (unless (eq 0 (apply 'call-process
  160. (append '("bibtex2html" nil nil nil)
  161. '("-a" "-nodoc" "-noheader" "-nofooter")
  162. (list "--style"
  163. (org-bibtex-get-style keyword))
  164. (plist-get arguments :options)
  165. (list (concat file ".bib")))))
  166. (error "Executing bibtex2html failed"))
  167. (and temp-file (delete-file temp-file))
  168. ;; Open produced HTML file, wrap references within a block and
  169. ;; return it.
  170. (with-temp-buffer
  171. (insert "<div id=\"bibliography\">\n<h2>References</h2>\n")
  172. (insert-file-contents (concat file ".html"))
  173. (insert "\n</div>")
  174. ;; Update `org-bibtex-html-keywords-alist'.
  175. (push (cons keyword (buffer-string))
  176. org-bibtex-html-keywords-alist)
  177. ;; Update `org-bibtex-html-entries-alist'.
  178. (goto-char (point-min))
  179. (while (re-search-forward
  180. "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t)
  181. (push (cons (match-string 1) (match-string 2))
  182. org-bibtex-html-entries-alist))))))))
  183. ;; Return parse tree unchanged.
  184. tree)
  185. (defun org-bibtex-merge-contiguous-citations (tree backend info)
  186. "Merge all contiguous citation in parse tree.
  187. As a side effect, this filter will also turn all \"cite\" links
  188. into \"\\cite{...}\" LaTeX fragments."
  189. (when (org-export-derived-backend-p backend 'html 'latex)
  190. (org-element-map tree '(link latex-fragment)
  191. (lambda (object)
  192. (when (org-bibtex-citation-p object)
  193. (let ((new-citation (list 'latex-fragment
  194. (list :value ""
  195. :post-blank (org-element-property
  196. :post-blank object)))))
  197. ;; Insert NEW-CITATION right before OBJECT.
  198. (org-element-insert-before new-citation object)
  199. ;; Remove all subsequent contiguous citations from parse
  200. ;; tree, keeping only their citation key.
  201. (let ((keys (list (org-bibtex-get-citation-key object)))
  202. next)
  203. (while (and (setq next (org-export-get-next-element object info))
  204. (or (and (stringp next)
  205. (not (org-string-match-p "\\S-" next)))
  206. (org-bibtex-citation-p next)))
  207. (unless (stringp next)
  208. (push (org-bibtex-get-citation-key next) keys))
  209. (org-element-extract-element object)
  210. (setq object next))
  211. (org-element-extract-element object)
  212. ;; Eventually merge all keys within NEW-CITATION. Also
  213. ;; ensure NEW-CITATION has the same :post-blank property
  214. ;; as the last citation removed.
  215. (org-element-put-property
  216. new-citation
  217. :post-blank (org-element-property :post-blank object))
  218. (org-element-put-property
  219. new-citation
  220. :value (format "\\cite{%s}"
  221. (mapconcat 'identity (nreverse keys) ",")))))))))
  222. tree)
  223. (eval-after-load 'ox
  224. '(progn (add-to-list 'org-export-filter-parse-tree-functions
  225. 'org-bibtex-process-bib-files)
  226. (add-to-list 'org-export-filter-parse-tree-functions
  227. 'org-bibtex-merge-contiguous-citations)))
  228. ;;; LaTeX Part
  229. (defadvice org-latex-keyword (around bibtex-keyword)
  230. "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
  231. Fallback to `latex' back-end for other keywords."
  232. (let ((keyword (ad-get-arg 0)))
  233. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  234. ad-do-it
  235. (let ((file (org-bibtex-get-file keyword))
  236. (style (org-bibtex-get-style keyword)))
  237. (setq ad-return-value
  238. (when file
  239. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  240. (format "\\bibliography{%s}" file))))))))
  241. (ad-activate 'org-latex-keyword)
  242. ;;; HTML Part
  243. (defvar org-bibtex-html-entries-alist nil) ; Dynamically scoped.
  244. (defvar org-bibtex-html-keywords-alist nil) ; Dynamically scoped.
  245. ;;;; Advices
  246. (defadvice org-html-keyword (around bibtex-keyword)
  247. "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
  248. Fallback to `html' back-end for other keywords."
  249. (let ((keyword (ad-get-arg 0)))
  250. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  251. ad-do-it
  252. (setq ad-return-value
  253. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  254. (defadvice org-html-latex-fragment (around bibtex-citation)
  255. "Translate \"\\cite\" LaTeX fragments into HTML syntax.
  256. Fallback to `html' back-end for other keywords."
  257. (let ((fragment (ad-get-arg 0)))
  258. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  259. (setq ad-return-value
  260. (format "[%s]"
  261. (mapconcat
  262. (lambda (key)
  263. (format "<a href=\"#%s\">%s</a>"
  264. key
  265. (or (cdr (assoc key org-bibtex-html-entries-alist))
  266. key)))
  267. (org-split-string
  268. (org-bibtex-get-citation-key fragment) ",") ","))))))
  269. (ad-activate 'org-html-keyword)
  270. (ad-activate 'org-html-latex-fragment)
  271. (provide 'ox-bibtex)
  272. ;;; ox-bibtex.el ends here