ox-bibtex.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. (require 'cl-lib)
  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 (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. (cl-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 (completing-read "Citation: " (obe-citations)))))
  145. (find-file (or org-bibtex-file
  146. (error "`org-bibtex-file' has not been configured")))
  147. (goto-char (point-min))
  148. (when (re-search-forward (format " :CUSTOM_ID: %s" citation) nil t)
  149. (outline-previous-visible-heading 1)
  150. t)))
  151. (let ((jump-fn (car (cl-remove-if-not #'fboundp '(ebib org-bibtex-goto-citation)))))
  152. (org-add-link-type "cite" jump-fn))
  153. ;;; Filters
  154. (defun org-bibtex-process-bib-files (tree backend info)
  155. "Send each bibliography in parse tree to \"bibtex2html\" process.
  156. Return new parse tree."
  157. (when (org-export-derived-backend-p backend 'ascii 'html)
  158. ;; Initialize dynamically scoped variables. The first one
  159. ;; contain an alist between keyword objects and their HTML
  160. ;; translation. The second one will contain an alist between
  161. ;; citation keys and names in the output (according to style).
  162. (setq org-bibtex-html-entries-alist nil
  163. org-bibtex-html-keywords-alist nil)
  164. (org-element-map tree 'keyword
  165. (lambda (keyword)
  166. (when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
  167. (let ((arguments (org-bibtex-get-arguments keyword))
  168. (file (org-bibtex-get-file keyword))
  169. temp-file
  170. out-file)
  171. ;; Test if filename is given with .bib-extension and strip
  172. ;; it off. Filenames with another extensions will be
  173. ;; untouched and will finally rise an error in bibtex2html.
  174. (setq file (if (equal (file-name-extension file) "bib")
  175. (file-name-sans-extension file) file))
  176. ;; Outpufiles of bibtex2html will be put into current working directory
  177. ;; so define a variable for this.
  178. (setq out-file (file-name-sans-extension
  179. (file-name-nondirectory file)))
  180. ;; limit is set: collect citations throughout the document
  181. ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
  182. ;; argument.
  183. (when (plist-get arguments :limit)
  184. (let ((citations
  185. (org-element-map tree '(latex-fragment link)
  186. (lambda (object)
  187. (and (org-bibtex-citation-p object)
  188. (org-bibtex-get-citation-key object))))))
  189. (with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
  190. (insert (mapconcat 'identity citations "\n")))
  191. (setq arguments
  192. (plist-put arguments
  193. :options
  194. (append (plist-get arguments :options)
  195. (list "-citefile" temp-file))))))
  196. ;; Call "bibtex2html" on specified file.
  197. (unless (eq 0 (apply
  198. 'call-process
  199. (append '("bibtex2html" nil nil nil)
  200. '("-a" "-nodoc" "-noheader" "-nofooter")
  201. (let ((style
  202. (org-not-nil
  203. (org-bibtex-get-style keyword))))
  204. (and style (list "--style" style)))
  205. (plist-get arguments :options)
  206. (list (concat file ".bib")))))
  207. (error "Executing bibtex2html failed"))
  208. (and temp-file (delete-file temp-file))
  209. ;; Open produced HTML file, and collect Bibtex key names
  210. (with-temp-buffer
  211. (insert-file-contents (concat out-file ".html"))
  212. ;; Update `org-bibtex-html-entries-alist'.
  213. (goto-char (point-min))
  214. (while (re-search-forward
  215. "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\([^<]+\\)" nil t)
  216. (push (cons (match-string 1) (match-string 2))
  217. org-bibtex-html-entries-alist)))
  218. ;; Open produced HTML file, wrap references within a block and
  219. ;; return it.
  220. (with-temp-buffer
  221. (cond
  222. ((org-export-derived-backend-p backend 'html)
  223. (insert (format "<div id=\"bibliography\">\n<h2>%s</h2>\n"
  224. (org-export-translate "References" :html info)))
  225. (insert-file-contents (concat out-file ".html"))
  226. (goto-char (point-max))
  227. (insert "\n</div>"))
  228. ((org-export-derived-backend-p backend 'ascii)
  229. ;; convert HTML references to text w/pandoc
  230. (unless (eq 0 (call-process "pandoc" nil nil nil
  231. (concat out-file ".html")
  232. "-o"
  233. (concat out-file ".txt")))
  234. (error "Executing pandoc failed"))
  235. (insert
  236. (format
  237. "%s\n==========\n\n"
  238. (org-export-translate
  239. "References"
  240. (intern (format ":%s" (plist-get info :ascii-charset)))
  241. info)))
  242. (insert-file-contents (concat out-file ".txt"))
  243. (goto-char (point-min))
  244. (while (re-search-forward
  245. "\\[ \\[bib\\][^ ]+ \\(\\]\\||[\n\r]\\)" nil t)
  246. (replace-match ""))
  247. (goto-char (point-min))
  248. (while (re-search-forward "\\( \\]\\| \\]\\| |\\)" nil t)
  249. (replace-match ""))
  250. (goto-char (point-min))
  251. (while (re-search-forward "[\n\r]\\([\n\r][\n\r]\\)" nil t)
  252. (replace-match "\\1"))))
  253. ;; Update `org-bibtex-html-keywords-alist'.
  254. (push (cons keyword (buffer-string))
  255. org-bibtex-html-keywords-alist)))))))
  256. ;; Return parse tree unchanged.
  257. tree)
  258. (defun org-bibtex-merge-contiguous-citations (tree backend info)
  259. "Merge all contiguous citation in parse tree.
  260. As a side effect, this filter will also turn all \"cite\" links
  261. into \"\\cite{...}\" LaTeX fragments and will extract options.
  262. Cite options are placed into square brackets at the beginning of
  263. the \"\\cite\" command for the LaTeX backend, and are removed for
  264. the HTML and ASCII backends."
  265. (when (org-export-derived-backend-p backend 'html 'latex 'ascii)
  266. (org-element-map tree '(link latex-fragment)
  267. (lambda (object)
  268. (when (org-bibtex-citation-p object)
  269. (let ((new-citation (list 'latex-fragment
  270. (list :value ""
  271. :post-blank (org-element-property
  272. :post-blank object))))
  273. option)
  274. ;; Insert NEW-CITATION right before OBJECT.
  275. (org-element-insert-before new-citation object)
  276. ;; Remove all subsequent contiguous citations from parse
  277. ;; tree, keeping only their citation key.
  278. (let ((keys (list (org-bibtex-get-citation-key object)))
  279. next)
  280. (while (and (setq next (org-export-get-next-element object info))
  281. (or (and (stringp next)
  282. (not (string-match-p "\\S-" next)))
  283. (org-bibtex-citation-p next)))
  284. (unless (stringp next)
  285. (push (org-bibtex-get-citation-key next) keys))
  286. (org-element-extract-element object)
  287. (setq object next))
  288. ;; Find any options in keys, e.g., "(Chapter 2)key" has
  289. ;; the option "Chapter 2".
  290. (setq keys
  291. (mapcar
  292. (lambda (k)
  293. (if (string-match "^(\\([^)]\+\\))\\(.*\\)" k)
  294. (progn
  295. (when (org-export-derived-backend-p backend 'latex)
  296. (setq option (format "[%s]" (match-string 1 k))))
  297. (match-string 2 k))
  298. k))
  299. keys))
  300. (org-element-extract-element object)
  301. ;; Eventually merge all keys within NEW-CITATION. Also
  302. ;; ensure NEW-CITATION has the same :post-blank property
  303. ;; as the last citation removed.
  304. (org-element-put-property
  305. new-citation
  306. :post-blank (org-element-property :post-blank object))
  307. (org-element-put-property
  308. new-citation
  309. :value (format "\\cite%s{%s}"
  310. (or option "")
  311. (mapconcat 'identity (nreverse keys) ",")))))))))
  312. tree)
  313. (eval-after-load 'ox
  314. '(progn (add-to-list 'org-export-filter-parse-tree-functions
  315. 'org-bibtex-process-bib-files)
  316. (add-to-list 'org-export-filter-parse-tree-functions
  317. 'org-bibtex-merge-contiguous-citations)))
  318. ;;; LaTeX Part
  319. (defadvice org-latex-keyword (around bibtex-keyword)
  320. "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
  321. Fallback to `latex' back-end for other keywords."
  322. (let ((keyword (ad-get-arg 0)))
  323. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  324. ad-do-it
  325. (let ((file (org-bibtex-get-file keyword))
  326. (style (org-not-nil (org-bibtex-get-style keyword))))
  327. (setq ad-return-value
  328. (when file
  329. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  330. (format "\\bibliography{%s}" file))))))))
  331. (ad-activate 'org-latex-keyword)
  332. ;;; HTML Part
  333. (defvar org-bibtex-html-entries-alist nil) ; Dynamically scoped.
  334. (defvar org-bibtex-html-keywords-alist nil) ; Dynamically scoped.
  335. ;;;; Advices
  336. (defadvice org-html-keyword (around bibtex-keyword)
  337. "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
  338. Fallback to `html' back-end for other keywords."
  339. (let ((keyword (ad-get-arg 0)))
  340. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  341. ad-do-it
  342. (setq ad-return-value
  343. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  344. (defadvice org-html-latex-fragment (around bibtex-citation)
  345. "Translate \"\\cite\" LaTeX fragments into HTML syntax.
  346. Fallback to `html' back-end for other keywords."
  347. (let ((fragment (ad-get-arg 0)))
  348. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  349. (setq ad-return-value
  350. (format "[%s]"
  351. (mapconcat
  352. (lambda (key)
  353. (format "<a href=\"#%s\">%s</a>"
  354. key
  355. (or (cdr (assoc key org-bibtex-html-entries-alist))
  356. key)))
  357. (org-split-string
  358. (org-bibtex-get-citation-key fragment) ",") ","))))))
  359. (ad-activate 'org-html-keyword)
  360. (ad-activate 'org-html-latex-fragment)
  361. ;;; Ascii Part
  362. (defadvice org-ascii-keyword (around bibtex-keyword)
  363. "Translate \"BIBLIOGRAPHY\" keywords into ascii syntax.
  364. Fallback to `ascii' back-end for other keywords."
  365. (let ((keyword (ad-get-arg 0)))
  366. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  367. ad-do-it
  368. (setq ad-return-value
  369. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  370. (defadvice org-ascii-latex-fragment (around bibtex-citation)
  371. "Translate \"\\cite\" LaTeX fragments into ascii syntax.
  372. Fallback to `ascii' back-end for other keywords."
  373. (let ((fragment (ad-get-arg 0)))
  374. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  375. (setq ad-return-value
  376. (format "[%s]"
  377. (mapconcat
  378. (lambda (key)
  379. (or (cdr (assoc key org-bibtex-html-entries-alist))
  380. key))
  381. (org-split-string
  382. (org-bibtex-get-citation-key fragment) ",") ","))))))
  383. (ad-activate 'org-ascii-keyword)
  384. (ad-activate 'org-ascii-latex-fragment)
  385. (provide 'ox-bibtex)
  386. ;;; ox-bibtex.el ends here