org-exp-bibtex.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 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. (defun org-export-bibtex-preprocess ()
  50. "Export all BibTeX."
  51. (interactive)
  52. (save-window-excursion
  53. (setq oebp-cite-plist '())
  54. ;; Convert #+BIBLIOGRAPHY: name style
  55. (goto-char (point-min))
  56. (while (re-search-forward "^#\\+BIBLIOGRAPHY:\\s-+\\(\\w+\\)\\s-+\\(\\w+\\)\\([^\r\n]*\\)" nil t)
  57. (let ((file (match-string 1))
  58. (style (match-string 2))
  59. (opt (org-exp-bibtex-options-to-plist (match-string 3))))
  60. (replace-match
  61. (cond
  62. (htmlp ;; We are exporting to HTML
  63. (let (extra-args cite-list end-hook tmp-files)
  64. (dolist (elt opt)
  65. (when (equal "option" (car elt))
  66. (setq extra-args (cons (cdr elt) extra-args))))
  67. (when (assoc "limit" opt) ;; Limit is true - collect references
  68. (org-exp-bibtex-docites (lambda ()
  69. (dolist (c (org-split-string (match-string 1) ","))
  70. (add-to-list 'cite-list c))))
  71. ;; (message "cites: %s" cite-list)
  72. (let ((tmp (make-temp-file "org-exp-bibtex")))
  73. (with-temp-file tmp (dolist (i cite-list) (insert (concat i "\n"))))
  74. (setq tmp-files (cons tmp tmp-files))
  75. (setq extra-args (append extra-args `("-citefile" ,tmp)))))
  76. (when (not (eq 0 (apply 'call-process (append '("bibtex2html" nil nil nil)
  77. `("-a" "--nodoc" "--style" ,style "--no-header")
  78. extra-args
  79. (list (concat file ".bib"))))))
  80. (error "Executing bibtex2html failed"))
  81. (dolist (f tmp-files) (delete-file f)))
  82. (with-temp-buffer
  83. (save-match-data
  84. (insert-file-contents (concat file ".html"))
  85. (goto-char (point-min))
  86. (while (re-search-forward "a name=\"\\(\\w+\\)\">\\(\\w+\\)" nil t)
  87. (setq oebp-cite-plist (cons (cons (match-string 1) (match-string 2)) oebp-cite-plist)))
  88. (goto-char (point-min))
  89. (while (re-search-forward "<hr>" nil t)
  90. (replace-match "<hr/>" t t))
  91. (concat "\n#+BEGIN_HTML\n<div id=\"bibliography\">\n" (buffer-string) "\n</div>\n#+END_HTML\n"))))
  92. (latexp ;; Latex export
  93. (concat "\n#+LATEX: \\bibliographystyle{" style "}"
  94. "\n#+LATEX: \\bibliography{" file "}\n"))) t t)))
  95. ;; Convert cites to links in html
  96. (when htmlp
  97. ;; Split citation commands with multiple keys
  98. (org-exp-bibtex-docites
  99. (lambda ()
  100. (let ((keys (save-match-data (org-split-string (match-string 1) ","))))
  101. (when (> (length keys) 1)
  102. (replace-match (mapconcat (lambda (k) (format "\\cite{%s}" k)) keys "")
  103. t t)))))
  104. ;; Replace the citation commands with links
  105. (org-exp-bibtex-docites
  106. (lambda () (let* ((cn (match-string 1))
  107. (cv (assoc cn oebp-cite-plist)))
  108. ;; (message "L: %s" (concat "\[_{}[[" cn "][" (if cv (cdr cv) cn) "]]\]"))
  109. (replace-match (concat "\[_{}[[#" cn "][" (if cv (cdr cv) cn) "]]\]")) t t))))
  110. ))
  111. (defun org-exp-bibtex-docites (fun)
  112. (save-excursion
  113. (save-match-data
  114. (goto-char (point-min))
  115. (when htmlp
  116. (while (re-search-forward "\\\\cite{\\([^}\n]+\\)}" nil t)
  117. (apply fun nil))))))
  118. (defun org-exp-bibtex-options-to-plist (options)
  119. (save-match-data
  120. (flet ((f (o) (let ((s (split-string o ":"))) (cons (nth 0 s) (nth 1 s)))))
  121. (mapcar 'f (split-string options nil t)))))
  122. (add-hook 'org-export-preprocess-hook 'org-export-bibtex-preprocess)
  123. (provide 'org-exp-bibtex)
  124. ;;; org-exp-bibtex.el ends here