Exporting citations in html

| categories: org-mode | tags:

Now that org-ref works well for LaTeX, I want to explore a simple approach to exporting citations with a bibliography in html. This will enable me to put citations in blogposts, like this one armiento-2014-high and these ones daza-2014-carbon-dioxid,mehta-2014-ident-poten,suntivich-2014-estim-hybrid. We should be able to have the same citation in more than one place like this armiento-2014-high, but only have one entry in the bibliography. The bibliography should be sorted if we ask for it alesi-2012-evaluat-primar. I am curious to see this book citation: day-1995-scien-englis.

The first thing we need is a list of bibtex keys cited in this buffer.

(defun org-ref-get-bibtex-keys ()
  "return a list of unique keys in the buffer"
  (interactive)
  (let ((keys '()))
    (org-element-map (org-element-parse-buffer) 'link
      (lambda (link)
        (let ((plist (nth 1 link)))
          (when (-contains? org-ref-cite-types (plist-get plist ':type))
            (dolist
                (key
                 (org-ref-split-and-strip-string (plist-get plist ':path)))
              (when (not (-contains? keys key))
                (setq keys (append keys (list key)))))))))
    keys))

(org-ref-get-bibtex-keys)
armiento-2014-high daza-2014-carbon-dioxid mehta-2014-ident-poten suntivich-2014-estim-hybrid alesi-2012-evaluat-primar day-1995-scien-englis

Good. Now, we need to create an HTML string for the bibliography. For each key, we will create an unordered list of simple citations. Each citation will be a named anchor in html. Let us start with a function that takes a key, and generates the HTML for that entry.

(defun org-ref-get-bibtex-entry-html (key)
(interactive)

 (let ((org-ref-bibliography-files (org-ref-find-bibliography))
       (cb (current-buffer))
       (file) (entry))

   (setq file (catch 'result
                (loop for file in org-ref-bibliography-files do
                      (message "looking for %s in %s" key file)
                      (if (org-ref-key-in-file-p key (file-truename file))
                          (throw 'result file)
                        (message "%s not found in %s" key (file-truename file))))))
   (set-buffer (find-file-noselect file))
   (prog1
       (bibtex-search-entry key nil 0)
     (setq entry (org-ref-bib-citation))
     (set-buffer cb))

   (format "<li><a name=\"#%s\">[%s] %s<\\a><li>" key key entry)))

(org-ref-get-bibtex-entry-html "mehta-2014-ident-poten")
<li><a name="#mehta-2014-ident-poten">[mehta-2014-ident-poten] Mehta, Prateek and Salvador, Paul A. and Kitchin,  John R., "Identifying Potential \ce{BO_2} Oxide Polymorphs for  Epitaxial Growth Candidates", ACS Applied Materials \& Interfaces, 0:null (2014)<\a><li>

That looks excellent. Now we simply map that function over the list of keys.

(defun org-ref-get-html-bibliography ()
(interactive)
(let ((keys (org-ref-get-bibtex-keys)))
(when keys
(concat "<h1>Bibliography</h1>
<ul>"
(mapconcat (lambda (x) (org-ref-get-bibtex-entry-html x)) keys "\n")
"\n</ul>"))))

(org-ref-get-html-bibliography)

<h1>Bibliography</h1> <ul><li><a name="#armiento-2014-high">[armiento-2014-high] Armiento, R. and Kozinsky, B. and Hautier, G. and Fornari, M. and Ceder, G., "High-throughput screening of perovskite alloys for piezoelectric performance and thermodynamic stability", Phys. Rev. B, 89:134103 (2014)<\a><li> <li><a name="#daza-2014-carbon-dioxid">[daza-2014-carbon-dioxid] Daza, Yolanda A. and Kent, Ryan A. and Yung, Matthew M. and Kuhn, John N., "Carbon Dioxide Conversion by Reverse Water-Gas Shift Chemical Looping on Perovskite-Type Oxides", Industrial \& Engineering Chemistry Research, 53:5828-5837 (2014)<\a><li> <li><a name="#mehta-2014-ident-poten">[mehta-2014-ident-poten] Mehta, Prateek and Salvador, Paul A. and Kitchin, John R., "Identifying Potential \ce{BO_2} Oxide Polymorphs for Epitaxial Growth Candidates", ACS Applied Materials \& Interfaces, 0:null (2014)<\a><li> <li><a name="#suntivich-2014-estim-hybrid">[suntivich-2014-estim-hybrid] Suntivich, Jin and Hong, Wesley T. and Lee, Yueh-Lin and Rondinelli, James M. and Yang, Wanli and Goodenough, John B. and Dabrowski, Bogdan and Freeland, John W. and Shao-Horn, Yang, "Estimating Hybridization of Transition Metal and Oxygen States in Perovskites from O K-edge X-ray Absorption Spectroscopy", The Journal of Physical Chemistry C, 118:1856-1863 (2014)<\a><li> <li><a name="#alesi-2012-evaluat-primar">[alesi-2012-evaluat-primar] Alesi, W. Richard and Kitchin, John R., "Evaluation of a Primary Amine-Functionalized Ion-Exchange Resin for \ce{CO_2} Capture", Industrial \& Engineering Chemistry Research, 51:6907-6915 (2012)<\a><li> <li><a name="#day-1995-scien-englis">[day-1995-scien-englis] Robert A. Day, "Scientific English: A Guide for Scientists and Other Profesionals", , : (1995)<\a><li> </ul> <h1>Bibliography</h1> <ul><li><a name="#armiento-2014-high">[armiento-2014-high] Armiento, R. and Kozinsky, B. and Hautier, G. and Fornari, M. and Ceder, G., "High-throughput screening of perovskite alloys for piezoelectric performance and thermodynamic stability", Phys. Rev. B, 89:134103 (2014)<\a><li> <li><a name="#daza-2014-carbon-dioxid">[daza-2014-carbon-dioxid] Daza, Yolanda A. and Kent, Ryan A. and Yung, Matthew M. and Kuhn, John N., "Carbon Dioxide Conversion by Reverse Water-Gas Shift Chemical Looping on Perovskite-Type Oxides", Industrial \& Engineering Chemistry Research, 53:5828-5837 (2014)<\a><li> <li><a name="#mehta-2014-ident-poten">[mehta-2014-ident-poten] Mehta, Prateek and Salvador, Paul A. and Kitchin, John R., "Identifying Potential \ce{BO_2} Oxide Polymorphs for Epitaxial Growth Candidates", ACS Applied Materials \& Interfaces, 0:null (2014)<\a><li> <li><a name="#suntivich-2014-estim-hybrid">[suntivich-2014-estim-hybrid] Suntivich, Jin and Hong, Wesley T. and Lee, Yueh-Lin and Rondinelli, James M. and Yang, Wanli and Goodenough, John B. and Dabrowski, Bogdan and Freeland, John W. and Shao-Horn, Yang, "Estimating Hybridization of Transition Metal and Oxygen States in Perovskites from O K-edge X-ray Absorption Spectroscopy", The Journal of Physical Chemistry C, 118:1856-1863 (2014)<\a><li> </ul>

That is basically all we need. The citation links will export as hrefs to these named targets, so they should work fine. All we need to do is modify the blogofile code a bit to use this, and add those functions to org-ref, and we should get a bibliography in our blogposts.

Bibliography

Copyright (C) 2014 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.6

Discuss on Twitter