]> git.donarmstrong.com Git - org-ref.git/blob - x2bib.el
whitespace changes
[org-ref.git] / x2bib.el
1 ;;; x2bib.el --- Bibliography conversion to Bibtex
2
3 ;;; Header:
4
5 ;;; Commentary:
6
7 ;; This module is more for my convenience to convert bibliography files to bibtex.  This can be done at the command line, for example, but I want to do it in Emacs.  There are a few scenarios where this happens.
8 ;; 1. Someone sends me a non-Bibtex file (Endnote, etc...)
9 ;; 2. From some online search I select many references and there is no export to Bibtex option, e.g. from Web of Science.
10
11 ;; This code is mostly wrappers around the command line utilities at http://sourceforge.net/p/bibutils/home/Bibutils.
12
13 ;; Here are the commands that are available.
14
15 ;; bib2xml      convert BibTeX to MODS XML intermediate
16 ;; biblatex2xml convert BibLaTeX to MODS XML intermediate
17 ;; copac2xml    convert COPAC format references to MODS XML intermediate
18 ;; end2xml      convert EndNote (Refer format) to MODS XML intermediate
19 ;; endx2xml     convert EndNote XML to MODS XML intermediate
20 ;; isi2xml      convert ISI web of science to MODS XML intermediate
21 ;; med2xml      convert Pubmed XML references to MODS XML intermediate
22 ;; modsclean    a MODS to MODS converter for testing puposes mostly
23 ;; ris2xml      convert RIS format to MODS XML intermediate
24 ;; xml2ads      convert MODS XML intermediate into Smithsonian Astrophysical Observatory (SAO)/National Aeronautics and Space Administration (NASA) Astrophyics Data System or ADS reference format (converter submitted by Richard Mathar)
25 ;; xml2bib      convert MODS XML intermediate into BibTeX
26 ;; xml2end      convert MODS XML intermediate into format for EndNote
27 ;; xml2isi      convert MODS XML intermediate to ISI format
28 ;; xml2ris      convert MODS XML intermediate into RIS format
29 ;; xml2wordbib  convert MODS XML intermediate into Word 2007 bibliography format
30
31
32 ;;; Code:
33
34 ;; ** RIS to bibtex
35 ;; RIS can be pretty easily exported from Endnote. Here is a function to read an RIS file and convert it to bibtex which is inserted at point. Note that there is often other output from the commands. We try to comment them out here, but you should probably inspect the entries, and do other bibtex file compliance checks.
36
37 (defun ris2bib (risfile &optional verbose)
38   "Convert RISFILE to bibtex and insert at point.
39 Without a prefix arg, stderr is diverted."
40  (interactive
41   (list (read-file-name "RIS file:")
42         (prefix-numeric-value current-prefix-arg)))
43  (let ((result (shell-command-to-string
44                 (concat
45                  (format
46                   "ris2xml %s | xml2bib -w"
47                   risfile)
48                  (unless verbose " 2> /dev/null")))))
49    ;; make some lines into comments.
50    (setq result (replace-regexp-in-string
51                  "^xml2bib:"
52                  "% xml2bib:"
53                  result))
54    (setq result (replace-regexp-in-string
55                  "^ris2xml:"
56                  "% ris2xml"
57                  result))
58    (setq result (replace-regexp-in-string
59                  "^     Defaulting"
60                  "%     Defaulting"
61                  result))
62    (insert result)))
63
64 ;; ** Pubmed XML to bibtex
65 ;; In http://www.ncbi.nlm.nih.gov/pubmed/ you can select entries, and then send them to a file. If you choose Pubmed XML as the format, then you can use this function to convert it to bibtex.
66
67 (defun medxml2bib (medfile &optional verbose)
68  "Convert MEDFILE (in Pubmed xml) to bibtex and insert at point.
69 Without a prefix arg, stderr is diverted."
70  (interactive
71   (list (read-file-name "MED file:")
72         (prefix-numeric-value current-prefix-arg)))
73  (let ((result (shell-command-to-string
74                 (concat
75                  (format
76                   "med2xml %s | xml2bib -w"
77                   medfile)
78                  (unless verbose " 2> /dev/null")))))
79    ;; make some lines into comments.
80    (setq result (replace-regexp-in-string
81                  "^xml2bib:"
82                  "% xml2bib:"
83                  result))
84    (setq result (replace-regexp-in-string
85                  "^med2xml:"
86                  "% med2xml"
87                  result))
88    (setq result (replace-regexp-in-string
89                  "^     Defaulting"
90                  "%     Defaulting"
91                  result))
92    (insert result)))
93
94 ;; ** Clean up all the entries
95
96 ;; Finally, after you put the new entries in, you probably need to do some clean up actions. This little function does that.
97
98 (defun clean-entries ()
99  "Map over bibtex entries and clean them."
100  (interactive)
101  (bibtex-map-entries
102   (lambda (a b c)
103    (ignore-errors
104    (org-ref-clean-bibtex-entry)))))
105
106 (provide 'x2bib)
107
108 ;;; x2bib.el ends here