ox-bibtex.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 LaTeX, html and ascii
  21. ;; exports. For HTML and ascii it uses the bibtex2html software from:
  22. ;;
  23. ;; http://www.lri.fr/~filliatr/bibtex2html/
  24. ;;
  25. ;; For ascii it uses the pandoc software from:
  26. ;;
  27. ;; http://johnmacfarlane.net/pandoc/
  28. ;;
  29. ;; It also introduces "cite" syntax for Org links.
  30. ;;
  31. ;; The usage is as follows:
  32. ;;
  33. ;; #+BIBLIOGRAPHY: bibfilebasename stylename optional-options
  34. ;;
  35. ;; e.g. given foo.bib and using style plain:
  36. ;;
  37. ;; #+BIBLIOGRAPHY: foo plain option:-d
  38. ;;
  39. ;; Optional options are of the form:
  40. ;;
  41. ;; option:-foobar pass '-foobar' to bibtex2html
  42. ;;
  43. ;; e.g.,
  44. ;;
  45. ;; option:-d sort by date
  46. ;; option:-a sort as BibTeX (usually by author) *default*
  47. ;; option:-u unsorted i.e. same order as in .bib file
  48. ;; option:-r reverse the sort
  49. ;;
  50. ;; See the bibtex2html man page for more. Multiple options can be
  51. ;; combined like:
  52. ;;
  53. ;; option:-d option:-r
  54. ;;
  55. ;; Limiting to only the entries cited in the document:
  56. ;;
  57. ;; limit:t
  58. ;;
  59. ;; For LaTeX export this simply inserts the lines
  60. ;;
  61. ;; \bibliographystyle{plain}
  62. ;; \bibliography{foo}
  63. ;;
  64. ;; into the TeX file when exporting.
  65. ;;
  66. ;; For HTML export it:
  67. ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
  68. ;; bibliography,
  69. ;; 2) creates a foo.html and foo_bib.html,
  70. ;; 3) includes the contents of foo.html in the exported HTML file.
  71. ;;
  72. ;; For ascii export it:
  73. ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
  74. ;; bibliography,
  75. ;; 2) creates a foo.txt and foo_bib.html,
  76. ;; 3) includes the contents of foo.txt in the exported ascii file.
  77. ;;
  78. ;; For LaTeX export it:
  79. ;; 1) converts all [[cite:foo]] to \cite{foo}.
  80. ;; Initialization
  81. (eval-when-compile (require 'cl))
  82. (let ((jump-fn (car (org-remove-if-not #'fboundp '(ebib obe-goto-citation)))))
  83. (org-add-link-type "cite" jump-fn))
  84. ;;; Internal Functions
  85. (defun org-bibtex-get-file (keyword)
  86. "Return bibliography file as a string.
  87. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no file is found,
  88. return nil instead."
  89. (let ((value (org-element-property :value keyword)))
  90. (and value
  91. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  92. (match-string 1 value))))
  93. (defun org-bibtex-get-style (keyword)
  94. "Return bibliography style as a string.
  95. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no style is found,
  96. return nil instead."
  97. (let ((value (org-element-property :value keyword)))
  98. (and value
  99. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  100. (match-string 2 value))))
  101. (defun org-bibtex-get-arguments (keyword)
  102. "Return \"bibtex2html\" arguments specified by the user.
  103. KEYWORD is a \"BIBLIOGRAPHY\" keyword. Return value is a plist
  104. containing `:options' and `:limit' properties. The former
  105. contains a list of strings to be passed as options to
  106. \"bibtex2html\" process. The latter contains a boolean."
  107. (let ((value (org-element-property :value keyword)))
  108. (and value
  109. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  110. (let (options limit)
  111. (dolist (arg (org-split-string (match-string 3 value))
  112. ;; Return value.
  113. (list :options (nreverse options) :limit limit))
  114. (let* ((s (split-string arg ":"))
  115. (key (car s))
  116. (value (nth 1 s)))
  117. (cond ((equal "limit" key)
  118. (setq limit (not (equal "nil" value))))
  119. ((equal "option" key) (push value options)))))))))
  120. (defun org-bibtex-citation-p (object)
  121. "Non-nil when OBJECT is a citation."
  122. (case (org-element-type object)
  123. (link (equal (org-element-property :type object) "cite"))
  124. (latex-fragment
  125. (string-match "\\`\\\\cite{" (org-element-property :value object)))))
  126. (defun org-bibtex-get-citation-key (citation)
  127. "Return key for a given citation, as a string.
  128. CITATION is a `latex-fragment' or `link' type object satisfying
  129. to `org-bibtex-citation-p' predicate."
  130. (if (eq (org-element-type citation) 'link)
  131. (org-element-property :path citation)
  132. (let ((value (org-element-property :value citation)))
  133. (and (string-match "\\`\\\\cite{" value)
  134. (substring value (match-end 0) -1)))))
  135. ;;; Filters
  136. (defun org-bibtex-process-bib-files (tree backend info)
  137. "Send each bibliography in parse tree to \"bibtex2html\" process.
  138. Return new parse tree."
  139. (when (org-export-derived-backend-p backend 'ascii 'html)
  140. ;; Initialize dynamically scoped variables. The first one
  141. ;; contain an alist between keyword objects and their HTML
  142. ;; translation. The second one will contain an alist between
  143. ;; citation keys and names in the output (according to style).
  144. (setq org-bibtex-html-entries-alist nil
  145. org-bibtex-html-keywords-alist nil)
  146. (org-element-map tree 'keyword
  147. (lambda (keyword)
  148. (when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
  149. (let ((arguments (org-bibtex-get-arguments keyword))
  150. (file (org-bibtex-get-file keyword))
  151. temp-file)
  152. ;; limit is set: collect citations throughout the document
  153. ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
  154. ;; argument.
  155. (when (plist-get arguments :limit)
  156. (let ((citations
  157. (org-element-map tree '(latex-fragment link)
  158. (lambda (object)
  159. (and (org-bibtex-citation-p object)
  160. (org-bibtex-get-citation-key object))))))
  161. (with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
  162. (insert (mapconcat 'identity citations "\n")))
  163. (setq arguments
  164. (plist-put arguments
  165. :options
  166. (append (plist-get arguments :options)
  167. (list "-citefile" temp-file))))))
  168. ;; Call "bibtex2html" on specified file.
  169. (unless (eq 0 (apply 'call-process
  170. (append '("bibtex2html" nil nil nil)
  171. '("-a" "-nodoc" "-noheader" "-nofooter")
  172. (list "--style"
  173. (org-bibtex-get-style keyword))
  174. (plist-get arguments :options)
  175. (list (concat file ".bib")))))
  176. (error "Executing bibtex2html failed"))
  177. (and temp-file (delete-file temp-file))
  178. ;; Open produced HTML file, and collect Bibtex key names
  179. (with-temp-buffer
  180. (insert-file-contents (concat file ".html"))
  181. ;; Update `org-bibtex-html-entries-alist'.
  182. (goto-char (point-min))
  183. (while (re-search-forward
  184. "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t)
  185. (push (cons (match-string 1) (match-string 2))
  186. org-bibtex-html-entries-alist)))
  187. ;; Open produced HTML file, wrap references within a block and
  188. ;; return it.
  189. (with-temp-buffer
  190. (cond
  191. ((org-export-derived-backend-p backend 'html)
  192. (insert "<div id=\"bibliography\">\n<h2>References</h2>\n")
  193. (insert-file-contents (concat file ".html"))
  194. (insert "\n</div>"))
  195. ((org-export-derived-backend-p backend 'ascii)
  196. ;; convert HTML references to text w/pandoc
  197. (unless (eq 0 (call-process "pandoc" nil nil nil
  198. (concat file ".html")
  199. "-o"
  200. (concat file ".txt")))
  201. (error "Executing pandoc failed"))
  202. (insert "References\n==========\n\n")
  203. (insert-file-contents (concat file ".txt"))
  204. (goto-char (point-min))
  205. (while (re-search-forward
  206. "\\[ \\[bib\\][^ ]+ \\(\\]\\||[\n\r]\\)" nil t)
  207. (replace-match ""))
  208. (goto-char (point-min))
  209. (while (re-search-forward "\\( \\]\\| \\]\\| |\\)" nil t)
  210. (replace-match ""))
  211. (goto-char (point-min))
  212. (while (re-search-forward "[\n\r]\\([\n\r][\n\r]\\)" nil t)
  213. (replace-match "\\1"))))
  214. ;; Update `org-bibtex-html-keywords-alist'.
  215. (push (cons keyword (buffer-string))
  216. org-bibtex-html-keywords-alist)))))))
  217. ;; Return parse tree unchanged.
  218. tree)
  219. (defun org-bibtex-merge-contiguous-citations (tree backend info)
  220. "Merge all contiguous citation in parse tree.
  221. As a side effect, this filter will also turn all \"cite\" links
  222. into \"\\cite{...}\" LaTeX fragments."
  223. (when (org-export-derived-backend-p backend 'html 'latex 'ascii)
  224. (org-element-map tree '(link latex-fragment)
  225. (lambda (object)
  226. (when (org-bibtex-citation-p object)
  227. (let ((new-citation (list 'latex-fragment
  228. (list :value ""
  229. :post-blank (org-element-property
  230. :post-blank object)))))
  231. ;; Insert NEW-CITATION right before OBJECT.
  232. (org-element-insert-before new-citation object)
  233. ;; Remove all subsequent contiguous citations from parse
  234. ;; tree, keeping only their citation key.
  235. (let ((keys (list (org-bibtex-get-citation-key object)))
  236. next)
  237. (while (and (setq next (org-export-get-next-element object info))
  238. (or (and (stringp next)
  239. (not (org-string-match-p "\\S-" next)))
  240. (org-bibtex-citation-p next)))
  241. (unless (stringp next)
  242. (push (org-bibtex-get-citation-key next) keys))
  243. (org-element-extract-element object)
  244. (setq object next))
  245. (org-element-extract-element object)
  246. ;; Eventually merge all keys within NEW-CITATION. Also
  247. ;; ensure NEW-CITATION has the same :post-blank property
  248. ;; as the last citation removed.
  249. (org-element-put-property
  250. new-citation
  251. :post-blank (org-element-property :post-blank object))
  252. (org-element-put-property
  253. new-citation
  254. :value (format "\\cite{%s}"
  255. (mapconcat 'identity (nreverse keys) ",")))))))))
  256. tree)
  257. (eval-after-load 'ox
  258. '(progn (add-to-list 'org-export-filter-parse-tree-functions
  259. 'org-bibtex-process-bib-files)
  260. (add-to-list 'org-export-filter-parse-tree-functions
  261. 'org-bibtex-merge-contiguous-citations)))
  262. ;;; LaTeX Part
  263. (defadvice org-latex-keyword (around bibtex-keyword)
  264. "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
  265. Fallback to `latex' back-end for other keywords."
  266. (let ((keyword (ad-get-arg 0)))
  267. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  268. ad-do-it
  269. (let ((file (org-bibtex-get-file keyword))
  270. (style (org-bibtex-get-style keyword)))
  271. (setq ad-return-value
  272. (when file
  273. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  274. (format "\\bibliography{%s}" file))))))))
  275. (ad-activate 'org-latex-keyword)
  276. ;;; HTML Part
  277. (defvar org-bibtex-html-entries-alist nil) ; Dynamically scoped.
  278. (defvar org-bibtex-html-keywords-alist nil) ; Dynamically scoped.
  279. ;;;; Advices
  280. (defadvice org-html-keyword (around bibtex-keyword)
  281. "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
  282. Fallback to `html' back-end for other keywords."
  283. (let ((keyword (ad-get-arg 0)))
  284. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  285. ad-do-it
  286. (setq ad-return-value
  287. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  288. (defadvice org-html-latex-fragment (around bibtex-citation)
  289. "Translate \"\\cite\" LaTeX fragments into HTML syntax.
  290. Fallback to `html' back-end for other keywords."
  291. (let ((fragment (ad-get-arg 0)))
  292. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  293. (setq ad-return-value
  294. (format "[%s]"
  295. (mapconcat
  296. (lambda (key)
  297. (format "<a href=\"#%s\">%s</a>"
  298. key
  299. (or (cdr (assoc key org-bibtex-html-entries-alist))
  300. key)))
  301. (org-split-string
  302. (org-bibtex-get-citation-key fragment) ",") ","))))))
  303. (ad-activate 'org-html-keyword)
  304. (ad-activate 'org-html-latex-fragment)
  305. ;;; Ascii Part
  306. (defadvice org-ascii-keyword (around bibtex-keyword)
  307. "Translate \"BIBLIOGRAPHY\" keywords into ascii syntax.
  308. Fallback to `ascii' back-end for other keywords."
  309. (let ((keyword (ad-get-arg 0)))
  310. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  311. ad-do-it
  312. (setq ad-return-value
  313. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  314. (defadvice org-ascii-latex-fragment (around bibtex-citation)
  315. "Translate \"\\cite\" LaTeX fragments into ascii syntax.
  316. Fallback to `ascii' back-end for other keywords."
  317. (let ((fragment (ad-get-arg 0)))
  318. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  319. (setq ad-return-value
  320. (format "[%s]"
  321. (mapconcat
  322. (lambda (key)
  323. (or (cdr (assoc key org-bibtex-html-entries-alist))
  324. key))
  325. (org-split-string
  326. (org-bibtex-get-citation-key fragment) ",") ","))))))
  327. (ad-activate 'org-ascii-keyword)
  328. (ad-activate 'org-ascii-latex-fragment)
  329. (provide 'ox-bibtex)
  330. ;;; ox-bibtex.el ends here