]> git.donarmstrong.com Git - org-ref.git/blobdiff - doi-utils.org
reimplement doi-utils-doi-to-bibtex-string
[org-ref.git] / doi-utils.org
index ede56829d821f76d34c41a3ee36a86e89d742f6a..44e3317f42956f29ce8186e30538e2571152e0b3 100644 (file)
@@ -451,6 +451,92 @@ We can use that data to construct a bibtex entry. We do that by defining a templ
 
 Now we define a function that fills in that template from the metadata.
 
+As different bibtex types share common keys, it is advantageous to separate data extraction from json, and the formatting of the bibtex entry.
+
+#+BEGIN_SRC emacs-lisp :notangle doi-utils.el
+(defmacro defpar (name &optional value)
+  `(progn (defvar ,name)
+          (setf ,name ,value)))
+
+(defpar doi-utils-json-metadata-extract
+    '((type       (plist-get results :type))
+      (author     (mapconcat (lambda (x) (concat (plist-get x :given) " " (plist-get x :family)))
+                   (plist-get results :author) " and "))
+      (title      (plist-get results :title))
+      (journal    (plist-get results :container-title))
+      (volume     (plist-get results :volume))
+      (issue      (plist-get results :issue))
+      (number     (plist-get results :issue))
+      (year       (elt (elt (plist-get (plist-get results :issued) :date-parts) 0) 0))
+      (month      (elt (elt (plist-get (plist-get results :issued) :date-parts) 0) 1))
+      (pages      (plist-get results :page))
+      (doi        (plist-get results :DOI))
+      (url        (plist-get results :URL))
+      (booktitle  (plist-get results :container-title))))
+#+END_SRC
+
+Next, we need to define the different bibtex types. Each type has a bibtex type (for output) and the type as provided in the doi record. Finally, we have to declare the fields we want to output.
+
+#+BEGIN_SRC emacs-lisp :notangle doi-utils.el
+(defvar doi-utils-bibtex-type-generators nil)
+
+(defun concat-prepare (lst &optional acc)
+  "Given a list `lst' of strings and other expressions, which are
+intented to passed to `concat', concat any subsequent strings,
+minimising the number of arguments being passed to `concat'
+without changing the results."
+  (cond ((null lst) (nreverse acc))
+        ((and (stringp (car lst))
+              (stringp (car acc)))
+         (concat-prepare (cdr lst) (cons (concat (car acc) (car lst))
+                                         (cdr acc))))
+        (t (concat-prepare (cdr lst) (cons (car lst) acc)))))
+
+
+(defmacro doi-utils-def-bibtex-type (name matching-types &rest fields)
+  "Define a BibTeX type identified by (symbol) `name' with
+`fields' (given as symbols), matching to retrieval expressions in
+`doi-utils-json-metadata-extract'. This type will only be used
+when the `:type' parameter in the JSON metadata is contained in
+`matching-types' - a list of strings."
+  `(push (lambda (type results)
+           (when (or ,@(mapcar (lambda (match-type) `(string= type ,match-type)) matching-types))
+             (let ,(mapcar (lambda (field) (or (assoc field doi-utils-json-metadata-extract)
+                                          (error "unknown bibtex field type %s" field)))
+                           fields)
+               (concat
+                ,@(concat-prepare
+                   (-flatten
+                    (list (concat "@" (symbol-name name) "{,\n")
+                          ;; there seems to be some bug with mapcan,
+                          ;; so we fall back to flatten
+                          (mapcar (lambda (field)
+                                    `(,(concat "  " (symbol-name field) " = {")
+                                       ,field
+                                       "},\n"))
+                                  fields)
+                          "}\n")))))))
+         doi-utils-bibtex-type-generators))
+
+(doi-utils-def-bibtex-type article ("journal-article" "article-journal")
+                           author title journal year volume number pages doi url)
+
+(doi-utils-def-bibtex-type inproceedings ("proceedings-article")
+                           author title booktitle year month pages doi url)
+#+END_SRC
+
+With the code generating the bibtex entry in place, we can glue it to the json retrieval code.
+#+BEGIN_SRC emacs-lisp :notangle doi-utils.el
+(defun doi-utils-doi-to-bibtex-string (doi)
+  "return a bibtex entry as a string for the doi. Only articles are currently supported"
+  (let* ((results (doi-utils-get-json-metadata doi))
+         (type (plist-get results :type)))
+    (format "%s" results) ; json-data
+    (some (lambda (g) (funcall g type results)) doi-utils-bibtex-type-generators)
+    (message-box "%s not supported yet." type)))
+#+END_SRC
+
+
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
 (defun doi-utils-doi-to-bibtex-string (doi)
   "return a bibtex entry as a string for the doi. Only articles are currently supported"
@@ -570,7 +656,7 @@ prompt. Otherwise, you have to type or pste in a DOI."
                              (s-match "^10" (buffer-substring
                                              (region-beginning)
                                              (region-end))))
-                      (buffer-susbstring (region-beginning) (region-end)))
+                      (buffer-substring (region-beginning) (region-end)))
                      ;; if the first entry in the kill-ring looks
                      ;; like a DOI, let's use it.
                      ((if (s-match "^10" (car kill-ring))
@@ -904,6 +990,7 @@ error."
 
 
 * Adding a bibtex entry from a crossref query
+The idea here is to perform a query on Crossref, get a helm buffer of candidates, and select the entry(ies) you want to add to your bibtex file. You can select a region, e.g. a free form citation, or set of words, or you can type the query in by hand.
 
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
 (defun doi-utils-add-entry-from-crossref-query (query bibtex-file)
@@ -914,7 +1001,9 @@ error."
                 (cond
                  ;; If region is active assume we want it
                  ((region-active-p)
-                  (buffer-susbstring (region-beginning) (region-end)))
+                  (replace-regexp-in-string
+                   "\n" " "
+                   (buffer-substring (region-beginning) (region-end))))
                  ;; type or paste it in
                  (t
                   nil)))
@@ -934,7 +1023,9 @@ error."
       (setq json-string (buffer-substring url-http-end-of-headers (point-max)))
       (setq json-data (json-read-from-string json-string)))
 
-    (let* ((name (format "Crossref hits for %s" query))
+    (let* ((name (format "Crossref hits for %s"
+                        ;; remove carriage returns. they cause problems in helm.
+                        (replace-regexp-in-string "\n" " " query)))
           (helm-candidates (mapcar (lambda (x)
                                      (cons
                                       (concat
@@ -948,7 +1039,7 @@ error."
                     ;; just return the candidate
                     (action . (("Insert bibtex entry" . (lambda (doi)
                                                           (doi-utils-add-bibtex-entry-from-doi
-                                                           (replace-regexp-in-string "^http://dx.doi.org/" "" doi))))
+                                                           (replace-regexp-in-string "^http://dx.doi.org/" "" doi) ,bibtex-file)))
                                ("Open url" . (lambda (doi)
                                                (browse-url doi))))))))
       (helm :sources '(source)))))
@@ -1143,7 +1234,7 @@ error."
 #+END_EXAMPLE
 
 
-#+BEGIN_SRC emacs-lisp :var data=json  :results value raw
+#+BEGIN_SRC emacs-lisp :var data=json  :results value raw :tangle no
 (let ((json-object-type 'plist)
       (json (json-read-from-string data)))
 (aref json 0))
@@ -1155,14 +1246,15 @@ error."
 
 
 Here is a list of helm candidates
-#+BEGIN_SRC emacs-lisp :var data=json :results code
+#+BEGIN_SRC emacs-lisp :var data=json :results code :tangle no
 (let (;(json-object-type 'plist)
       (json (json-read-from-string data)))
   (mapcar (lambda (x) (cons (assoc 'fullCitation x) x)) json))
 #+END_SRC
 
 #+RESULTS:
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :tangle no
+
 (((fullCitation . "Ann M. Deml, Vladan Stevanovi\304\207, Christopher L. Muhich, Charles B. Musgrave, Ryan O'Hayre, 2014, 'Oxide enthalpy of formation and band gap energy as accurate descriptors of oxygen vacancy formation energetics', <i>Energy &amp; Environmental Science</i>, vol. 7, no. 6, p. 1996")
   (year . "2014")
   (coins . "ctx_ver=Z39.88-2004&amp;rft_id=info%3Adoi%2Fhttp%3A%2F%2Fdx.doi.org%2F10.1039%2Fc3ee43874k&amp;rfr_id=info%3Asid%2Fcrossref.org%3Asearch&amp;rft.atitle=Oxide+enthalpy+of+formation+and+band+gap+energy+as+accurate+descriptors+of+oxygen+vacancy+formation+energetics&amp;rft.jtitle=Energy+%26+Environmental+Science&amp;rft.date=2014&amp;rft.volume=7&amp;rft.issue=6&amp;rft.spage=1996&amp;rft.aufirst=Ann+M.&amp;rft.aulast=Deml&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&amp;rft.genre=article&amp;rft.au=Ann+M.+Deml&amp;rft.au=+Vladan+Stevanovi%C4%87&amp;rft.au=+Christopher+L.+Muhich&amp;rft.au=+Charles+B.+Musgrave&amp;rft.au=+Ryan+O%27Hayre")
@@ -1248,15 +1340,104 @@ Here is a list of helm candidates
 
 
 * ISBN utility
-This is not really a doi utility, but I am putting it here for now since it is just a single function. It looks up an ISBN and takes you to a page that has a bibtex entry. I am not crazy about that, but I have not found an isbn metadata source yet.
+These are not really doi utilities, but for now I am putting them here.
+
+I found this on the web. It can be handy, but the bibtex entry has a lot of stuff in it.
 
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
-(defun isbn-to-bibtex (isbn)
+(defun isbn-to-bibtex-lead (isbn)
  "Search lead.to for ISBN bibtex entry. You have to copy the entry if it is on the page to your bibtex file."
- (interactive "ISBN: ")
+ (interactive "sISBN: ")
 (browse-url
 (format "http://lead.to/amazon/en/?key=%s+&si=all&op=bt&bn=&so=sa&ht=us" isbn)))
 #+END_SRC
+
+Here we get isbn metadata and build a bibtex entry.
+http://xisbn.worldcat.org/xisbnadmin/doc/api.htm#getmetadata
+
+
+#+BEGIN_SRC emacs-lisp :tangle doi-utils.el
+(defun isbn-to-bibtex (isbn bibfile)
+  "Get bibtex entry for ISBN and insert it into BIBFILE unless an
+entry with the generated key already exists in the file."
+  (interactive
+   (list
+    (read-input
+     "ISBN: "
+     ;; now set initial input
+     (cond
+      ;; If region is active and it starts with a number, we use it
+      ((and  (region-active-p)
+            (s-match "^[0-9]" (buffer-substring (region-beginning) (region-end))))
+       (buffer-substring (region-beginning) (region-end)))
+      ;; if first entry in kill ring starts with a number assume it is an isbn
+      ;; and use it as the guess
+      ((if (s-match "^[0-9]" (car kill-ring))
+          (car kill-ring)))
+      ;; type or paste it in
+      (t
+       nil)))
+    (ido-completing-read
+     "Bibfile: "
+     (append (f-entries "." (lambda (f) (f-ext? f "bib")))
+            org-ref-default-bibliography))))
+
+  (let* ((results (with-current-buffer
+                     (url-retrieve-synchronously
+                      (format
+                       "http://xisbn.worldcat.org/webservices/xid/isbn/%s?method=getMetadata&format=json&fl=*"
+                       isbn))
+                   (json-read-from-string
+                    (buffer-substring url-http-end-of-headers (point-max)))))
+        (status (cdr (nth 1 results)))
+        (metadata (aref (cdar results) 0))
+        (new-entry)
+        (new-key))
+
+    ;; check if we got something
+    (unless (string= "ok" status)
+      (error "Status is %s" status))
+
+    ;; construct an alphabetically sorted bibtex entry. I assume ISBN numbers go
+    ;; with book entries.
+    (setq new-entry
+         (concat "\n@book{,\n"
+                 (mapconcat
+                  'identity
+                  (loop for field in (-sort 'string-lessp (mapcar 'car metadata))
+                        collect
+                        (format "  %s={%s}," field (cdr (assoc field metadata))))
+                  "\n")
+                 "\n}\n"))
+
+    ;; build entry in temp buffer to get the key so we can check for duplicates
+    (setq new-entry (with-temp-buffer
+                     (insert new-entry)
+                     (org-ref-clean-bibtex-entry)
+                     (setq new-key (bibtex-key-in-head))
+                     (buffer-string)))
+    (find-file bibfile)
+    (goto-char (point-min))
+    (when (search-forward new-key nil t)
+      (beep)
+      (setq new-key (read-input
+                    (format  "%s already exists. Enter new key (C-g to cancel): " new-key)
+                    new-key)))
+    (goto-char (point-max))
+    (insert new-entry)
+    ;; set key. It is simplest to just replace it, even if it is the same.
+    (bibtex-beginning-of-entry)
+    (re-search-forward bibtex-entry-maybe-empty-head)
+    (if (match-beginning bibtex-key-in-head)
+       (delete-region (match-beginning bibtex-key-in-head)
+                      (match-end bibtex-key-in-head)))
+    (insert new-key)
+    (bibtex-fill-entry)
+    (save-buffer)))
+#+END_SRC
+
+
+
 * end of file
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
 (provide 'doi-utils)
@@ -1268,25 +1449,3 @@ This is not really a doi utility, but I am putting it here for now since it is j
 
 #+RESULTS:
 : Loaded doi-utils.el
-
-
-
-
-
-#+BEGIN_SRC emacs-lisp
-(setq data '(("John" . "john@email.com")
-             ("Jim" . "jim@email.com")
-             ("Jane" . "jane@email.com")
-             ("Jill" . "jill@email.com")))
-
-(setq some-helm-source
-      `((name . "HELM at the Emacs")
-        (candidates . ,(mapcar 'car data))
-        (action . (lambda (candidate)
-                    (message-box "%s" (cdr (assoc candidate data)))))))
-
-(message-box "you chose %s" (helm :sources '(some-helm-source)))
-#+END_SRC
-
-#+RESULTS:
-: you chose jim@email.com