From cccac9a4ed53853ca34b8eed89dc095662f085cb Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sat, 30 Nov 2013 13:51:58 +0100 Subject: [PATCH] ox-bibtex: Small refactoring * contrib/lisp/ox-bibtex.el (org-bibtex-process-bib-files): Execute function only when back-end is HTML. --- contrib/lisp/ox-bibtex.el | 136 +++++++++++++++++++++++----------------------- 1 file changed, 68 insertions(+), 68 deletions(-) diff --git a/contrib/lisp/ox-bibtex.el b/contrib/lisp/ox-bibtex.el index a0f823609..3e732a8f9 100644 --- a/contrib/lisp/ox-bibtex.el +++ b/contrib/lisp/ox-bibtex.el @@ -139,6 +139,74 @@ to `org-bibtex-citation-p' predicate." +;;; 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 "
\n

References

\n") + (insert-file-contents (concat file ".html")) + (insert "\n
") + ;; 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 (defadvice org-latex-keyword (around bibtex-keyword) @@ -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 "
\n

References

\n") - (insert-file-contents (concat file ".html")) - (insert "\n
") - ;; 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 -- 2.11.4.GIT