ox-bibtex.el 13 KB

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