org-exp-bibtex.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ;;; org-exp-bibtex.el --- Export bibtex fragments
  2. ;; Copyright (C) 2009-2012 Taru Karttunen
  3. ;; Author: Taru Karttunen <taruti@taruti.net>
  4. ;; This file is not currently part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation; either version 2, or (at
  8. ;; your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program ; see the file COPYING. If not, write to
  15. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. ;; Boston, MA 02111-1307, USA.
  17. ;;; Commentary:
  18. ;;
  19. ;; This is an utility to handle BibTeX export to both LaTeX and html
  20. ;; exports. It uses the bibtex2html software from
  21. ;; http://www.lri.fr/~filliatr/bibtex2html/
  22. ;;
  23. ;; The usage is as follows:
  24. ;; #+BIBLIOGRAPHY: bibfilebasename stylename optional-options
  25. ;; e.g. given foo.bib and using style plain:
  26. ;; #+BIBLIOGRAPHY: foo plain option:-d
  27. ;;
  28. ;; Optional options are of the form:
  29. ;;
  30. ;; option:-foobar pass '-foobar' to bibtex2html
  31. ;; e.g.
  32. ;; option:-d sort by date.
  33. ;; option:-a sort as BibTeX (usually by author) *default*
  34. ;; option:-u unsorted i.e. same order as in .bib file
  35. ;; option:-r reverse the sort.
  36. ;; see the bibtex2html man page for more. Multiple options can be combined like:
  37. ;; option:-d option:-r
  38. ;;
  39. ;; Limiting to only the entries cited in the document:
  40. ;; limit:t
  41. ;; For LaTeX export this simply inserts the lines
  42. ;; \bibliographystyle{plain}
  43. ;; \bibliography{foo}
  44. ;; into the tex-file when exporting.
  45. ;; For Html export it:
  46. ;; 1) converts all \cite{foo} to links to the bibliography
  47. ;; 2) creates a foo.html and foo_bib.html
  48. ;; 3) includes the contents of foo.html in the exported html file
  49. (require 'org)
  50. (require 'org-exp)
  51. (defvar org-export-current-backend) ; dynamically bound in org-exp.el
  52. (defun org-export-bibtex-preprocess ()
  53. "Export all BibTeX."
  54. (interactive)
  55. (save-window-excursion
  56. (setq oebp-cite-plist '())
  57. ;; Convert #+BIBLIOGRAPHY: name style
  58. (goto-char (point-min))
  59. (while (re-search-forward "^#\\+BIBLIOGRAPHY:[ \t]+\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\([^\r\n]*\\)" nil t)
  60. (let ((file (match-string 1))
  61. (style (match-string 2))
  62. (opt (org-exp-bibtex-options-to-plist (match-string 3))))
  63. (replace-match
  64. (cond
  65. ((eq org-export-current-backend 'html) ;; We are exporting to HTML
  66. (let (extra-args cite-list end-hook tmp-files)
  67. (dolist (elt opt)
  68. (when (equal "option" (car elt))
  69. (setq extra-args (cons (cdr elt) extra-args))))
  70. (when (assoc "limit" opt) ;; Limit is true - collect references
  71. (org-exp-bibtex-docites (lambda ()
  72. (dolist (c (org-split-string (match-string 1) ","))
  73. (add-to-list 'cite-list c))))
  74. ;; (message "cites: %s" cite-list)
  75. (let ((tmp (make-temp-file "org-exp-bibtex")))
  76. (with-temp-file tmp (dolist (i cite-list) (insert (concat i "\n"))))
  77. (setq tmp-files (cons tmp tmp-files))
  78. (setq extra-args (append extra-args `("-citefile" ,tmp)))))
  79. (when (not (eq 0 (apply 'call-process (append '("bibtex2html" nil nil nil)
  80. `("-a" "--nodoc" "--style" ,style "--no-header")
  81. extra-args
  82. (list (concat file ".bib"))))))
  83. (error "Executing bibtex2html failed"))
  84. (dolist (f tmp-files) (delete-file f)))
  85. (with-temp-buffer
  86. (save-match-data
  87. (insert-file-contents (concat file ".html"))
  88. (goto-char (point-min))
  89. (while (re-search-forward (org-re "a name=\"\\([-_[:word:]]+\\)\">\\([[:word:]]+\\)") nil t)
  90. (setq oebp-cite-plist (cons (cons (match-string 1) (match-string 2)) oebp-cite-plist)))
  91. (goto-char (point-min))
  92. (while (re-search-forward "<hr>" nil t)
  93. (replace-match "<hr/>" t t))
  94. (concat "\n#+BEGIN_HTML\n<div id=\"bibliography\">\n<h2>References</h2>\n" (buffer-string) "\n</div>\n#+END_HTML\n"))))
  95. ((eq org-export-current-backend 'latex) ;; Latex export
  96. (concat "\n#+LATEX: \\bibliographystyle{" style "}"
  97. "\n#+LATEX: \\bibliography{" file "}\n"))) t t)))
  98. ;; Convert cites to links in html
  99. (when (eq org-export-current-backend 'html)
  100. ;; Split citation commands with multiple keys
  101. (org-exp-bibtex-docites
  102. (lambda ()
  103. (let ((keys (save-match-data (org-split-string (match-string 1) ","))))
  104. (when (> (length keys) 1)
  105. (replace-match (mapconcat (lambda (k) (format "\\cite{%s}" k)) keys "")
  106. t t)))))
  107. ;; Replace the citation commands with links
  108. (org-exp-bibtex-docites
  109. (lambda () (let* ((cn (match-string 1))
  110. (cv (assoc cn oebp-cite-plist)))
  111. ;; (message "L: %s" (concat "\[_{}[[" cn "][" (if cv (cdr cv) cn) "]]\]"))
  112. (replace-match (concat "\[_{}[[#" cn "][" (if cv (cdr cv) cn) "]]\]")) t t))))))
  113. (defun org-exp-bibtex-docites (fun)
  114. (save-excursion
  115. (save-match-data
  116. (goto-char (point-min))
  117. (when (eq org-export-current-backend 'html)
  118. (while (re-search-forward "\\\\cite{\\([^}\n]+\\)}" nil t)
  119. (apply fun nil))))))
  120. (defun org-exp-bibtex-options-to-plist (options)
  121. (save-match-data
  122. (flet ((f (o) (let ((s (split-string o ":"))) (cons (nth 0 s) (nth 1 s)))))
  123. (mapcar 'f (split-string options nil t)))))
  124. (add-hook 'org-export-preprocess-hook 'org-export-bibtex-preprocess)
  125. (provide 'org-exp-bibtex)
  126. ;;; org-exp-bibtex.el ends here