ox-bibtex.el 15 KB

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