From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: Bibliography Date: Sun, 12 May 2013 14:57:57 +0200 Message-ID: <874ne85p9m.fsf@gmail.com> References: <20130506145017.GF20172@kuru.dyndns-at-home.com> <87wqrcdjs4.fsf@gmail.com> <87li7sdjdj.fsf@gmail.com> <20130507053752.GA14940@panahar> <87ehdid9do.fsf@gmail.com> <20130509092858.GA24865@panahar> <87a9o4nheg.fsf@gmail.com> <20130510025641.GA26504@panahar> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([208.118.235.92]:44065) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UbVqh-0002mw-Rp for emacs-orgmode@gnu.org; Sun, 12 May 2013 08:58:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UbVqg-0007x1-J7 for emacs-orgmode@gnu.org; Sun, 12 May 2013 08:57:59 -0400 Received: from mail-wi0-x230.google.com ([2a00:1450:400c:c05::230]:38682) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UbVqg-0007wx-9M for emacs-orgmode@gnu.org; Sun, 12 May 2013 08:57:58 -0400 Received: by mail-wi0-f176.google.com with SMTP id hr14so2033464wib.3 for ; Sun, 12 May 2013 05:57:57 -0700 (PDT) In-Reply-To: <20130510025641.GA26504@panahar> (Vikas Rawal's message of "Fri, 10 May 2013 08:26:41 +0530") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hello, Vikas Rawal writes: >> Actually, ox-html.el also tries to do something with bibliographies, >> which introduces the duplication. >> >> We should decide if the code handling them should be in contrib/ or in >> the various export back-ends. Since HTML handling requires external >> programs, I lean towards the former. > > I agree. Better to keep it separately in contrib/ > >> If we agree, I'll remove the faulty code from ox-html.el and prepare >> a new version for ox-bibtex.el. > > +1 from me. > >> By the way, where should the bibliography be inserted: at the keyword or >> at the end of the document (in both HTML and LaTeX)? > > at the keyword please. Here's a new take on the problem. You need to use latest maint (or master) revision. What do you think? I'm Cc'ing Taru Karttunen, the "org-exp-bibtex.el" author, to know if he agrees with the changes. Regards, -- Nicolas Goaziou --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=ox-bibtex.el Content-Transfer-Encoding: quoted-printable ;;; ox-bibtex.el --- Export bibtex fragments ;; Copyright (C) 2009-2013 Taru Karttunen ;; Author: Taru Karttunen ;; This file is not currently part of GNU Emacs. ;; This program is free software; you can redistribute it and/or ;; modify it under the terms of the GNU General Public License as ;; published by the Free Software Foundation; either version 2, or (at ;; your option) any later version. ;; This program is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program ; see the file COPYING. If not, write to ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. ;;; Commentary: ;; ;; This is an utility to handle BibTeX export to both LaTeX and html ;; exports. It uses the bibtex2html software from: ;; ;; http://www.lri.fr/~filliatr/bibtex2html/ ;; ;; The usage is as follows: ;; ;; #+BIBLIOGRAPHY: bibfilebasename stylename optional-options ;; ;; e.g. given foo.bib and using style plain: ;; ;; #+BIBLIOGRAPHY: foo plain option:-d ;; ;; Optional options are of the form: ;; ;; option:-foobar pass '-foobar' to bibtex2html ;; ;; e.g., ;; ;; option:-d sort by date ;; option:-a sort as BibTeX (usually by author) *default* ;; option:-u unsorted i.e. same order as in .bib file ;; option:-r reverse the sort ;; ;; See the bibtex2html man page for more. Multiple options can be ;; combined like: ;; ;; option:-d option:-r ;; ;; Limiting to only the entries cited in the document: ;; ;; limit:t ;; ;; For LaTeX export this simply inserts the lines ;; ;; \bibliographystyle{plain} ;; \bibliography{foo} ;; ;; into the TeX file when exporting. ;; ;; For HTML export it: ;; 1) converts all \cite{foo} to links to the bibliography, ;; 2) creates a foo.html and foo_bib.html, ;; 3) includes the contents of foo.html in the exported HTML file. (declare-function org-export-extend-backend "org-export" (backend)) (declare-function org-export-with-backend "org-export" (back-end data &optional contents info)) ;;; Internal Functions (defun org-bibtex-get-file (keyword) "Return bibliography file as a string. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no file is found, return nil instead." (let ((value (org-element-property :value keyword))) (and value (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value) (match-string 1 value)))) (defun org-bibtex-get-style (keyword) "Return bibliography style as a string. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no style is found, return nil instead." (let ((value (org-element-property :value keyword))) (and value (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value) (match-string 2 value)))) (defun org-bibtex-get-arguments (keyword) "Return \"bibtex2html\" arguments specified by the user. KEYWORD is a \"BIBLIOGRAPHY\" keyword. Return value is a plist containing `:options' and `:limit' properties. The former contains a list of strings to be passed as options ot \"bibtex2html\" process. The latter contains a boolean." (let ((value (org-element-property :value keyword))) (and value (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value) (let (options limit) (dolist (arg (org-split-string (match-string 3 value)) ;; Return value. (list :options (nreverse options) :limit limit)) (let* ((s (split-string arg ":")) (key (car s)) (value (nth 1 s))) (cond ((equal "limit" key) (setq limit (not (equal "nil" value)))) ((equal "option" key) (push value options))))))))) (defun org-bibtex-citation-p (fragment) "Non-nil when a LaTeX macro is a citation. FRAGMENT is a `latex-fragment' type object." (string-match "\\`\\\\cite{" (org-element-property :value fragment))) (defun org-bibtex-get-citation-key (citation) "Return key for a given citation, as a string. CITATION is a `latex-fragment' type object satisfying to `org-bibtex-citation-p' predicate." (let ((value (org-element-property :value citation))) (and (string-match "\\`\\\\cite{" value) (substring value (match-end 0) -1)))) ;;; LaTeX Part (defadvice org-latex-keyword (around bibtex-keyword) "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax. Fallback to `latex' back-end for other keywords." (let ((keyword (ad-get-arg 0))) (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY")) ad-do-it (let ((file (org-bibtex-get-file keyword)) (style (org-bibtex-get-style keyword))) (setq ad-return-value (when file (concat (and style (format "\\bibliographystyle{%s}\n" styl= e)) (format "\\bibliography{%s}" file)))))))) (ad-activate 'org-latex-keyword) ;;; HTML Part (defvar org-bibtex-html-entries-alist) ; Dynamically scoped. (defvar org-bibtex-html-keywords-alist) ; Dynamically scoped. ;;;; Advices (defadvice org-html-keyword (around bibtex-keyword) "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax. Fallback to `html' back-end for other keywords." (let ((keyword (ad-get-arg 0))) (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY")) ad-do-it (setq ad-return-value (cdr (assq keyword org-bibtex-html-keywords-alist)))))) (defadvice org-html-latex-fragment (around bibtex-citation) "Translate \"\\cite\" LaTeX fragments into HTML syntax. Fallback to `html' back-end for other keywords." (let ((fragment (ad-get-arg 0))) (if (not (org-bibtex-citation-p fragment)) ad-do-it (setq ad-return-value (mapconcat (lambda (key) (format "[%s]" key (or (cdr (assoc key org-bibtex-html-entries-alist)) key))) (org-split-string (org-bibtex-get-citation-key fragment) ",") ""))))) (ad-activate 'org-html-keyword) (ad-activate 'org-html-latex-fragment) ;;;; 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 (lambda (fragment) (and (org-bibtex-citation-p fragment) (org-bibtex-get-citation-key fragment)))))) (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" "-nofoot= er") (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=3D\"\\([-_a-zA-Z0-9]\\)\">\\(\\w+\\)" nil t) (push (cons (match-string 1) (match-string 2)) org-bibtex-html-keywords-alist))))))) ;; Return parse tree unchanged. tree) (eval-after-load 'ox '(add-to-list 'org-export-filter-parse-tree-functions 'org-bibtex-process-bib-files)) (provide 'ox-bibtex) ;;; ox-bibtex.el ends here --=-=-=--