ox-bibtex.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 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. ;; "stylename" can also be "nil", in which case no style will be used.
  40. ;;
  41. ;; Optional options are of the form:
  42. ;;
  43. ;; option:-foobar pass '-foobar' to bibtex2html
  44. ;;
  45. ;; e.g.,
  46. ;;
  47. ;; option:-d sort by date
  48. ;; option:-a sort as BibTeX (usually by author) *default*
  49. ;; option:-u unsorted i.e. same order as in .bib file
  50. ;; option:-r reverse the sort
  51. ;;
  52. ;; See the bibtex2html man page for more. Multiple options can be
  53. ;; combined like:
  54. ;;
  55. ;; option:-d option:-r
  56. ;;
  57. ;; Limiting to only the entries cited in the document:
  58. ;;
  59. ;; limit:t
  60. ;;
  61. ;; For LaTeX export this simply inserts the lines
  62. ;;
  63. ;; \bibliographystyle{plain}
  64. ;; \bibliography{foo}
  65. ;;
  66. ;; into the TeX file when exporting.
  67. ;;
  68. ;; For HTML export it:
  69. ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
  70. ;; bibliography,
  71. ;; 2) creates a foo.html and foo_bib.html,
  72. ;; 3) includes the contents of foo.html in the exported HTML file.
  73. ;;
  74. ;; For ascii export it:
  75. ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
  76. ;; bibliography,
  77. ;; 2) creates a foo.txt and foo_bib.html,
  78. ;; 3) includes the contents of foo.txt in the exported ascii file.
  79. ;;
  80. ;; For LaTeX export it:
  81. ;; 1) converts all [[cite:foo]] to \cite{foo}.
  82. ;; Initialization
  83. (eval-when-compile (require 'cl))
  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. ;;; Follow cite: links
  136. (defun org-bibtex-file nil "Org-mode file of bibtex entries.")
  137. (defun org-bibtex-goto-citation (&optional citation)
  138. "Visit a citation given its ID."
  139. (interactive)
  140. (let ((citation (or citation
  141. (org-icompleting-read "Citation: "
  142. (obe-citations)))))
  143. (find-file (or org-bibtex-file
  144. (error "`org-bibtex-file' has not been configured")))
  145. (goto-char (point-min))
  146. (when (re-search-forward (format " :CUSTOM_ID: %s" citation) nil t)
  147. (outline-previous-visible-heading 1)
  148. t)))
  149. (let ((jump-fn (car (org-remove-if-not #'fboundp '(ebib org-bibtex-goto-citation)))))
  150. (org-add-link-type "cite" jump-fn))
  151. ;;; Filters
  152. (defun org-bibtex-process-bib-files (tree backend info)
  153. "Send each bibliography in parse tree to \"bibtex2html\" process.
  154. Return new parse tree."
  155. (when (org-export-derived-backend-p backend 'ascii 'html)
  156. ;; Initialize dynamically scoped variables. The first one
  157. ;; contain an alist between keyword objects and their HTML
  158. ;; translation. The second one will contain an alist between
  159. ;; citation keys and names in the output (according to style).
  160. (setq org-bibtex-html-entries-alist nil
  161. org-bibtex-html-keywords-alist nil)
  162. (org-element-map tree 'keyword
  163. (lambda (keyword)
  164. (when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
  165. (let ((arguments (org-bibtex-get-arguments keyword))
  166. (file (org-bibtex-get-file keyword))
  167. temp-file)
  168. ;; limit is set: collect citations throughout the document
  169. ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
  170. ;; argument.
  171. (when (plist-get arguments :limit)
  172. (let ((citations
  173. (org-element-map tree '(latex-fragment link)
  174. (lambda (object)
  175. (and (org-bibtex-citation-p object)
  176. (org-bibtex-get-citation-key object))))))
  177. (with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
  178. (insert (mapconcat 'identity citations "\n")))
  179. (setq arguments
  180. (plist-put arguments
  181. :options
  182. (append (plist-get arguments :options)
  183. (list "-citefile" temp-file))))))
  184. ;; Call "bibtex2html" on specified file.
  185. (unless (eq 0 (apply
  186. 'call-process
  187. (append '("bibtex2html" nil nil nil)
  188. '("-a" "-nodoc" "-noheader" "-nofooter")
  189. (let ((style
  190. (org-not-nil
  191. (org-bibtex-get-style keyword))))
  192. (and style (list "--style" style)))
  193. (plist-get arguments :options)
  194. (list (concat file ".bib")))))
  195. (error "Executing bibtex2html failed"))
  196. (and temp-file (delete-file temp-file))
  197. ;; Open produced HTML file, and collect Bibtex key names
  198. (with-temp-buffer
  199. (insert-file-contents (concat file ".html"))
  200. ;; Update `org-bibtex-html-entries-alist'.
  201. (goto-char (point-min))
  202. (while (re-search-forward
  203. "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t)
  204. (push (cons (match-string 1) (match-string 2))
  205. org-bibtex-html-entries-alist)))
  206. ;; Open produced HTML file, wrap references within a block and
  207. ;; return it.
  208. (with-temp-buffer
  209. (cond
  210. ((org-export-derived-backend-p backend 'html)
  211. (insert (format "<div id=\"bibliography\">\n<h2>%s</h2>\n"
  212. (org-export-translate "References" :html info)))
  213. (insert-file-contents (concat file ".html"))
  214. (insert "\n</div>"))
  215. ((org-export-derived-backend-p backend 'ascii)
  216. ;; convert HTML references to text w/pandoc
  217. (unless (eq 0 (call-process "pandoc" nil nil nil
  218. (concat file ".html")
  219. "-o"
  220. (concat file ".txt")))
  221. (error "Executing pandoc failed"))
  222. (insert
  223. (format
  224. "%s\n==========\n\n"
  225. (org-export-translate
  226. "References"
  227. (intern (format ":%s" (plist-get info :ascii-charset)))
  228. info)))
  229. (insert-file-contents (concat file ".txt"))
  230. (goto-char (point-min))
  231. (while (re-search-forward
  232. "\\[ \\[bib\\][^ ]+ \\(\\]\\||[\n\r]\\)" nil t)
  233. (replace-match ""))
  234. (goto-char (point-min))
  235. (while (re-search-forward "\\( \\]\\| \\]\\| |\\)" nil t)
  236. (replace-match ""))
  237. (goto-char (point-min))
  238. (while (re-search-forward "[\n\r]\\([\n\r][\n\r]\\)" nil t)
  239. (replace-match "\\1"))))
  240. ;; Update `org-bibtex-html-keywords-alist'.
  241. (push (cons keyword (buffer-string))
  242. org-bibtex-html-keywords-alist)))))))
  243. ;; Return parse tree unchanged.
  244. tree)
  245. (defun org-bibtex-merge-contiguous-citations (tree backend info)
  246. "Merge all contiguous citation in parse tree.
  247. As a side effect, this filter will also turn all \"cite\" links
  248. into \"\\cite{...}\" LaTeX fragments and will extract options.
  249. Cite options are placed into square brackets at the beginning of
  250. the \"\\cite\" command for the LaTeX backend, and are removed for
  251. the HTML and ASCII backends."
  252. (when (org-export-derived-backend-p backend 'html 'latex 'ascii)
  253. (org-element-map tree '(link latex-fragment)
  254. (lambda (object)
  255. (when (org-bibtex-citation-p object)
  256. (let ((new-citation (list 'latex-fragment
  257. (list :value ""
  258. :post-blank (org-element-property
  259. :post-blank object))))
  260. option)
  261. ;; Insert NEW-CITATION right before OBJECT.
  262. (org-element-insert-before new-citation object)
  263. ;; Remove all subsequent contiguous citations from parse
  264. ;; tree, keeping only their citation key.
  265. (let ((keys (list (org-bibtex-get-citation-key object)))
  266. next)
  267. (while (and (setq next (org-export-get-next-element object info))
  268. (or (and (stringp next)
  269. (not (org-string-match-p "\\S-" next)))
  270. (org-bibtex-citation-p next)))
  271. (unless (stringp next)
  272. (push (org-bibtex-get-citation-key next) keys))
  273. (org-element-extract-element object)
  274. (setq object next))
  275. ;; Find any options in keys, e.g., "(Chapter 2)key" has
  276. ;; the option "Chapter 2".
  277. (setq keys
  278. (mapcar
  279. (lambda (k)
  280. (if (string-match "^(\\([^)]\+\\))\\(.*\\)" k)
  281. (progn
  282. (when (org-export-derived-backend-p backend 'latex)
  283. (setq option (format "[%s]" (match-string 1 k))))
  284. (match-string 2 k))
  285. k))
  286. keys))
  287. (org-element-extract-element object)
  288. ;; Eventually merge all keys within NEW-CITATION. Also
  289. ;; ensure NEW-CITATION has the same :post-blank property
  290. ;; as the last citation removed.
  291. (org-element-put-property
  292. new-citation
  293. :post-blank (org-element-property :post-blank object))
  294. (org-element-put-property
  295. new-citation
  296. :value (format "\\cite%s{%s}"
  297. (or option "")
  298. (mapconcat 'identity (nreverse keys) ",")))))))))
  299. tree)
  300. (eval-after-load 'ox
  301. '(progn (add-to-list 'org-export-filter-parse-tree-functions
  302. 'org-bibtex-process-bib-files)
  303. (add-to-list 'org-export-filter-parse-tree-functions
  304. 'org-bibtex-merge-contiguous-citations)))
  305. ;;; LaTeX Part
  306. (defadvice org-latex-keyword (around bibtex-keyword)
  307. "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
  308. Fallback to `latex' 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. (let ((file (org-bibtex-get-file keyword))
  313. (style (org-not-nil (org-bibtex-get-style keyword))))
  314. (setq ad-return-value
  315. (when file
  316. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  317. (format "\\bibliography{%s}" file))))))))
  318. (ad-activate 'org-latex-keyword)
  319. ;;; HTML Part
  320. (defvar org-bibtex-html-entries-alist nil) ; Dynamically scoped.
  321. (defvar org-bibtex-html-keywords-alist nil) ; Dynamically scoped.
  322. ;;;; Advices
  323. (defadvice org-html-keyword (around bibtex-keyword)
  324. "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
  325. Fallback to `html' back-end for other keywords."
  326. (let ((keyword (ad-get-arg 0)))
  327. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  328. ad-do-it
  329. (setq ad-return-value
  330. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  331. (defadvice org-html-latex-fragment (around bibtex-citation)
  332. "Translate \"\\cite\" LaTeX fragments into HTML syntax.
  333. Fallback to `html' back-end for other keywords."
  334. (let ((fragment (ad-get-arg 0)))
  335. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  336. (setq ad-return-value
  337. (format "[%s]"
  338. (mapconcat
  339. (lambda (key)
  340. (format "<a href=\"#%s\">%s</a>"
  341. key
  342. (or (cdr (assoc key org-bibtex-html-entries-alist))
  343. key)))
  344. (org-split-string
  345. (org-bibtex-get-citation-key fragment) ",") ","))))))
  346. (ad-activate 'org-html-keyword)
  347. (ad-activate 'org-html-latex-fragment)
  348. ;;; Ascii Part
  349. (defadvice org-ascii-keyword (around bibtex-keyword)
  350. "Translate \"BIBLIOGRAPHY\" keywords into ascii syntax.
  351. Fallback to `ascii' back-end for other keywords."
  352. (let ((keyword (ad-get-arg 0)))
  353. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  354. ad-do-it
  355. (setq ad-return-value
  356. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  357. (defadvice org-ascii-latex-fragment (around bibtex-citation)
  358. "Translate \"\\cite\" LaTeX fragments into ascii syntax.
  359. Fallback to `ascii' back-end for other keywords."
  360. (let ((fragment (ad-get-arg 0)))
  361. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  362. (setq ad-return-value
  363. (format "[%s]"
  364. (mapconcat
  365. (lambda (key)
  366. (or (cdr (assoc key org-bibtex-html-entries-alist))
  367. key))
  368. (org-split-string
  369. (org-bibtex-get-citation-key fragment) ",") ","))))))
  370. (ad-activate 'org-ascii-keyword)
  371. (ad-activate 'org-ascii-latex-fragment)
  372. (provide 'ox-bibtex)
  373. ;;; ox-bibtex.el ends here