]> git.donarmstrong.com Git - org-ref.git/blob - x2bib.el
rm pubmed.org from ORG_SOURCES and add to EL_SOURCES
[org-ref.git] / x2bib.el
1 ;;   ti
2 ;;; Header:
3
4 ;;; Commentary:
5
6 ;; ;;; Reminders about lentic:
7 ;; Lentic key binding reminders
8 ;; C-c , h  Move here in other view
9 ;; C-c , s  Swap windows
10 ;; [[elisp:lentic-mode-split-window-right]]  C-c o to open this link and make the view
11
12 ;; 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.
13 ;; 1. Someone sends me a non-Bibtex file (Endnote, etc...)
14 ;; 2. From some online search I select many references and there is no export to Bibtex option, e.g. from Web of Science.
15
16 ;; This code is mostly wrappers around the command line utilities at http://sourceforge.net/p/bibutils/home/Bibutils.
17
18 ;; Here are the commands that are available.
19
20 ;; bib2xml      convert BibTeX to MODS XML intermediate
21 ;; biblatex2xml convert BibLaTeX to MODS XML intermediate
22 ;; copac2xml    convert COPAC format references to MODS XML intermediate
23 ;; end2xml      convert EndNote (Refer format) to MODS XML intermediate
24 ;; endx2xml     convert EndNote XML to MODS XML intermediate
25 ;; isi2xml      convert ISI web of science to MODS XML intermediate
26 ;; med2xml      convert Pubmed XML references to MODS XML intermediate
27 ;; modsclean    a MODS to MODS converter for testing puposes mostly
28 ;; ris2xml      convert RIS format to MODS XML intermediate
29 ;; 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)
30 ;; xml2bib      convert MODS XML intermediate into BibTeX
31 ;; xml2end      convert MODS XML intermediate into format for EndNote
32 ;; xml2isi      convert MODS XML intermediate to ISI format
33 ;; xml2ris      convert MODS XML intermediate into RIS format
34 ;; xml2wordbib  convert MODS XML intermediate into Word 2007 bibliography format
35
36
37 ;;; Code:
38
39 ;; ** RIS to bibtex
40 ;; 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.
41
42 ;; #+BEGIN_SRC emacs-lisp
43 (defun ris2bib (risfile &optional verbose)
44   "Convert RISFILE to bibtex and insert at point.
45 Without a prefix arg, stderr is diverted."
46  (interactive
47   (list (read-file-name "RIS file:")
48         (prefix-numeric-value current-prefix-arg)))
49  (let ((result (shell-command-to-string
50                 (concat
51                  (format
52                   "ris2xml %s | xml2bib -w"
53                   risfile)
54                  (unless verbose " 2> /dev/null")))))
55    ;; make some lines into comments.
56    (setq result (replace-regexp-in-string
57                  "^xml2bib:"
58                  "% xml2bib:"
59                  result))
60    (setq result (replace-regexp-in-string
61                  "^ris2xml:"
62                  "% ris2xml"
63                  result))
64    (setq result (replace-regexp-in-string
65                  "^     Defaulting"
66                  "%     Defaulting"
67                  result))
68    (insert result)))
69 ;; #+END_SRC
70
71 ;; #+RESULTS:
72 ;; : ris2bib
73
74 ;; ** Pubmed XML to bibtex
75 ;; 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.
76
77 ;; #+BEGIN_SRC emacs-lisp
78 (defun medxml2bib (medfile &optional verbose)
79  "Convert MEDFILE (in Pubmed xml) to bibtex and insert at point.
80 Without a prefix arg, stderr is diverted."
81  (interactive
82   (list (read-file-name "MED file:")
83         (prefix-numeric-value current-prefix-arg)))
84  (let ((result (shell-command-to-string
85                 (concat
86                  (format
87                   "med2xml %s | xml2bib -w"
88                   medfile)
89                  (unless verbose " 2> /dev/null")))))
90    ;; make some lines into comments.
91    (setq result (replace-regexp-in-string
92                  "^xml2bib:"
93                  "% xml2bib:"
94                  result))
95    (setq result (replace-regexp-in-string
96                  "^med2xml:"
97                  "% med2xml"
98                  result))
99    (setq result (replace-regexp-in-string
100                  "^     Defaulting"
101                  "%     Defaulting"
102                  result))
103    (insert result)))
104 ;; #+END_SRC
105
106 ;; #+RESULTS:
107 ;; : medxml2bib
108
109 ;; ** Clean up all the entries
110
111 ;; Finally, after you put the new entries in, you probably need to do some clean up actions. This little function does that.
112
113 ;; #+BEGIN_SRC emacs-lisp
114 (defun clean-entries ()
115  "Map over bibtex entries and clean them."
116  (interactive)
117  (bibtex-map-entries
118   (lambda (a b c)
119    (ignore-errors
120    (org-ref-clean-bibtex-entry)))))
121 ;; #+END_SRC
122
123 ;; #+RESULTS:
124 ;; : clean-entries
125
126
127
128 ;; ;;; x2bib.el ends here
129
130 ;; # Local Variables:
131 ;; # lentic-init: lentic-orgel-org-init
132 ;; # End: