org-exp-bibtex.el 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ;;; org-exp-bibtex.el --- Export bibtex fragments
  2. ;; Copyright (C) 2009 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
  25. ;; e.g. given foo.bib and using style plain:
  26. ;; #+BIBLIOGRAPHY: foo plain
  27. ;; For LaTeX export this simply inserts the lines
  28. ;; \bibliographystyle{plain}
  29. ;; \bibliography{foo}
  30. ;; into the tex-file when exporting.
  31. ;; For Html export it:
  32. ;; 1) converts all \cite{foo} to links to the bibliography
  33. ;; 2) creates a foo.html and foo_bib.html
  34. ;; 3) includes the contents of foo.html in the exported html file
  35. (defun org-export-bibtex-preprocess ()
  36. "Export all BibTeX."
  37. (interactive)
  38. (save-window-excursion
  39. (setq oebp-cite-plist '())
  40. ;; Convert #+BIBLIOGRAPHY: name style
  41. (goto-char (point-min))
  42. (while (re-search-forward "^#\\+BIBLIOGRAPHY:\\s-+\\(\\w+\\)\\s-+\\(\\w+\\)" nil t)
  43. (let ((file (match-string 1))
  44. (style (match-string 2)))
  45. (replace-match
  46. (cond
  47. (htmlp ;; We are exporting to HTML
  48. (call-process "bibtex2html" nil nil nil "--nodoc" "--style" style "--no-header" (concat file ".bib"))
  49. (with-temp-buffer
  50. (save-match-data
  51. (insert-file-contents (concat file ".html"))
  52. (goto-char (point-min))
  53. (while (re-search-forward "a name=\"\\(\\w+\\)\">\\(\\w+\\)" nil t)
  54. (setq oebp-cite-plist (cons (cons (match-string 1) (match-string 2)) oebp-cite-plist)))
  55. (goto-char (point-min))
  56. (while (re-search-forward "<hr>" nil t)
  57. (replace-match "<hr/>"))
  58. (concat "\n#+BEGIN_HTML\n<div class=\"bibliography\">\n" (buffer-string) "\n</div>\n#+END_HTML\n"))))
  59. (latexp ;; Latex export
  60. (concat "\n#+LATEX: \\\\bibliographystyle{" style "}"
  61. "\n#+LATEX: \\\\bibliography{" file "}\n"))))))
  62. ;; Convert cites to links in html
  63. (goto-char (point-min))
  64. (when htmlp
  65. (while (re-search-forward "\\\\cite{\\(\\w+\\)}" nil t)
  66. (let* ((cn (match-string 1))
  67. (cv (assoc cn oebp-cite-plist)))
  68. (replace-match
  69. (concat "\[_{}[[" cn "][" (if cv (cdr cv) cn) "]]\]")))))))
  70. (add-hook 'org-export-preprocess-hook 'org-export-bibtex-preprocess)
  71. (provide 'org-exp-bibtex)
  72. ;;; org-exp-bibtex.el ends here