123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- (require 'org)
- (require 'org-exp)
- (defvar org-export-current-backend)
- (defun org-export-bibtex-preprocess ()
- "Export all BibTeX."
- (interactive)
- (save-window-excursion
- (setq oebp-cite-plist '())
-
- (goto-char (point-min))
- (while (re-search-forward "^#\\+BIBLIOGRAPHY:[ \t]+\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\([^\r\n]*\\)" nil t)
- (let ((file (match-string 1))
- (style (match-string 2))
- (opt (org-exp-bibtex-options-to-plist (match-string 3))))
- (replace-match
- (cond
- ((eq org-export-current-backend 'html)
- (let (extra-args cite-list end-hook tmp-files)
- (dolist (elt opt)
- (when (equal "option" (car elt))
- (setq extra-args (cons (cdr elt) extra-args))))
- (when (assoc "limit" opt)
- (org-exp-bibtex-docites (lambda ()
- (dolist (c (org-split-string (match-string 1) ","))
- (add-to-list 'cite-list c))))
- (let ((tmp (make-temp-file "org-exp-bibtex")))
- (with-temp-file tmp (dolist (i cite-list) (insert (concat i "\n"))))
- (setq tmp-files (cons tmp tmp-files))
- (setq extra-args (append extra-args `("-citefile" ,tmp)))))
- (when (not (eq 0 (apply 'call-process (append '("bibtex2html" nil nil nil)
- `("-a" "--nodoc" "--style" ,style "--no-header")
- extra-args
- (list (concat file ".bib"))))))
- (error "Executing bibtex2html failed"))
- (dolist (f tmp-files) (delete-file f)))
- (with-temp-buffer
- (save-match-data
- (insert-file-contents (concat file ".html"))
- (goto-char (point-min))
- (while (re-search-forward (org-re "a name=\"\\([-_[:word:]]+\\)\">\\([[:word:]]+\\)") nil t)
- (setq oebp-cite-plist (cons (cons (match-string 1) (match-string 2)) oebp-cite-plist)))
- (goto-char (point-min))
- (while (re-search-forward "<hr>" nil t)
- (replace-match "<hr/>" t t))
- (concat "\n#+BEGIN_HTML\n<div id=\"bibliography\">\n<h2>References</h2>\n" (buffer-string) "\n</div>\n#+END_HTML\n"))))
- ((eq org-export-current-backend 'latex)
- (concat "\n#+LATEX: \\bibliographystyle{" style "}"
- "\n#+LATEX: \\bibliography{" file "}\n"))) t t)))
-
- (when (eq org-export-current-backend 'html)
-
- (org-exp-bibtex-docites
- (lambda ()
- (let ((keys (save-match-data (org-split-string (match-string 1) ","))))
- (when (> (length keys) 1)
- (replace-match (mapconcat (lambda (k) (format "\\cite{%s}" k)) keys "")
- t t)))))
-
- (org-exp-bibtex-docites
- (lambda () (let* ((cn (match-string 1))
- (cv (assoc cn oebp-cite-plist)))
- (replace-match (concat "\[_{}[[#" cn "][" (if cv (cdr cv) cn) "]]\]")) t t))))))
- (defun org-exp-bibtex-docites (fun)
- (save-excursion
- (save-match-data
- (goto-char (point-min))
- (when (eq org-export-current-backend 'html)
- (while (re-search-forward "\\\\cite{\\([^}\n]+\\)}" nil t)
- (apply fun nil))))))
- (defun org-exp-bibtex-options-to-plist (options)
- (save-match-data
- (flet ((f (o) (let ((s (split-string o ":"))) (cons (nth 0 s) (nth 1 s)))))
- (mapcar 'f (split-string options nil t)))))
- (add-hook 'org-export-preprocess-hook 'org-export-bibtex-preprocess)
- (provide 'org-exp-bibtex)
|