ox-bibtex: Small refactoring

* contrib/lisp/ox-bibtex.el (org-bibtex-process-bib-files): Execute
  function only when back-end is HTML.
This commit is contained in:
Nicolas Goaziou 2013-11-30 13:51:58 +01:00
parent cd439bc513
commit cccac9a4ed
1 changed files with 68 additions and 68 deletions

View File

@ -138,6 +138,74 @@ to `org-bibtex-citation-p' predicate."
(substring value (match-end 0) -1)))))
;;; Filters
(defun org-bibtex-process-bib-files (tree backend info)
"Send each bibliography in parse tree to \"bibtex2html\" process.
Return new parse tree."
(when (org-export-derived-backend-p backend 'html)
;; Initialize dynamically scoped variables. The first one
;; contain an alist between keyword objects and their HTML
;; translation. The second one will contain an alist between
;; citation keys and names in the output (according to style).
(setq org-bibtex-html-entries-alist nil
org-bibtex-html-keywords-alist nil)
(org-element-map tree 'keyword
(lambda (keyword)
(when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
(let ((arguments (org-bibtex-get-arguments keyword))
(file (org-bibtex-get-file keyword))
temp-file)
;; limit is set: collect citations throughout the document
;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
;; argument.
(when (plist-get arguments :limit)
(let ((citations
(org-element-map tree '(latex-fragment link)
(lambda (object)
(and (org-bibtex-citation-p object)
(org-bibtex-get-citation-key object))))))
(with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
(insert (mapconcat 'identity citations "\n")))
(setq arguments
(plist-put arguments
:options
(append (plist-get arguments :options)
(list "-citefile" temp-file))))))
;; Call "bibtex2html" on specified file.
(unless (eq 0 (apply 'call-process
(append '("bibtex2html" nil nil nil)
'("-a" "-nodoc" "-noheader" "-nofooter")
(list "--style"
(org-bibtex-get-style keyword))
(plist-get arguments :options)
(list (concat file ".bib")))))
(error "Executing bibtex2html failed"))
(and temp-file (delete-file temp-file))
;; Open produced HTML file, wrap references within a block and
;; return it.
(with-temp-buffer
(insert "<div id=\"bibliography\">\n<h2>References</h2>\n")
(insert-file-contents (concat file ".html"))
(insert "\n</div>")
;; Update `org-bibtex-html-keywords-alist'.
(push (cons keyword (buffer-string))
org-bibtex-html-keywords-alist)
;; Update `org-bibtex-html-entries-alist'.
(goto-char (point-min))
(while (re-search-forward
"a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t)
(push (cons (match-string 1) (match-string 2))
org-bibtex-html-entries-alist))))))))
;; Return parse tree unchanged.
tree)
(eval-after-load 'ox
'(add-to-list 'org-export-filter-parse-tree-functions
'org-bibtex-process-bib-files))
;;; LaTeX Part
@ -221,74 +289,6 @@ Fallback to `html' back-end for other types."
(ad-activate 'org-html-link)
;;;; Filter
(defun org-bibtex-process-bib-files (tree backend info)
"Send each bibliography in parse tree to \"bibtex2html\" process.
Return new parse tree. This function assumes current back-end is HTML."
;; Initialize dynamically scoped variables. The first one
;; contain an alist between keyword objects and their HTML
;; translation. The second one will contain an alist between
;; citation keys and names in the output (according to style).
(setq org-bibtex-html-entries-alist nil
org-bibtex-html-keywords-alist nil)
(org-element-map tree 'keyword
(lambda (keyword)
(when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
(let ((arguments (org-bibtex-get-arguments keyword))
(file (org-bibtex-get-file keyword))
temp-file)
;; limit is set: collect citations throughout the document
;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
;; argument.
(when (plist-get arguments :limit)
(let ((citations
(org-element-map tree '(latex-fragment link)
(lambda (object)
(and (org-bibtex-citation-p object)
(org-bibtex-get-citation-key object))))))
(with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
(insert (mapconcat 'identity citations "\n")))
(setq arguments
(plist-put arguments
:options
(append (plist-get arguments :options)
(list "-citefile" temp-file))))))
;; Call "bibtex2html" on specified file.
(unless (eq 0 (apply 'call-process
(append '("bibtex2html" nil nil nil)
'("-a" "-nodoc" "-noheader" "-nofooter")
(list "--style"
(org-bibtex-get-style keyword))
(plist-get arguments :options)
(list (concat file ".bib")))))
(error "Executing bibtex2html failed"))
(and temp-file (delete-file temp-file))
;; Open produced HTML file, wrap references within a block and
;; return it.
(with-temp-buffer
(insert "<div id=\"bibliography\">\n<h2>References</h2>\n")
(insert-file-contents (concat file ".html"))
(insert "\n</div>")
;; Update `org-bibtex-html-keywords-alist'.
(push (cons keyword (buffer-string))
org-bibtex-html-keywords-alist)
;; Update `org-bibtex-html-entries-alist'.
(goto-char (point-min))
(while (re-search-forward
"a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\(\\w+\\)" nil t)
(push (cons (match-string 1) (match-string 2))
org-bibtex-html-entries-alist)))))))
;; Return parse tree unchanged.
tree)
(eval-after-load 'ox
'(add-to-list 'org-export-filter-parse-tree-functions
(lambda (e b i) (when (eql b 'html)
(org-bibtex-process-bib-files e b i)))))
(provide 'ox-bibtex)
;;; ox-bibtex.el ends here