forked from jkitchin/org-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-ref-arxiv.el
303 lines (268 loc) · 12.1 KB
/
org-ref-arxiv.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
;;; org-ref-arxiv.el --- arxiv utilities for org-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2015-2021 John Kitchin
;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
;; Keywords:
;; 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; this library creates a new org-link for Arxiv (http://arxiv.org/) entries,
;; and provides functions to retrieve bibtex entries from an Arxiv number.
;;
;; An Arxiv number might look like: cond-mat/0410285 or 1503.01742
;;; Code:
(require 'bibtex)
(require 'dash)
(require 'f)
(require 'org)
(require 's)
(require 'org-ref-utils)
(require 'parsebib)
(require 'xml)
;; This is a local variable defined in `url-http'. We need it to avoid
;; byte-compiler errors.
(defvar url-http-end-of-headers)
(declare-function parsebib-find-bibtex-dialect "parsebib")
(declare-function org-ref-clean-bibtex-entry "org-ref-core")
;; this is a C function
(declare-function libxml-parse-xml-region "xml")
;;* The org-mode link
;; this just makes a clickable link that opens the entry.
;; example: arxiv:cond-mat/0410285
(org-link-set-parameters "arxiv"
:follow (lambda (link-string)
(browse-url (format "http://arxiv.org/abs/%s" link-string)))
:export (lambda (keyword desc format)
(cond
((eq format 'html)
(format "<a href=\"http://arxiv.org/abs/%s\">arxiv:%s</a>"
keyword (or desc keyword)))
((eq format 'latex)
;; write out the latex command
(format "\\url{http://arxiv.org/abs/%s}{%s}" keyword (or desc keyword))))))
;;* Getting a bibtex entry for an arXiv article using remote service:
;; For an arxiv article, there is a link to a NASA ADS page like this:
;; http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:1503.01742
;; On that page, there is a link to a bibtex entry:
;; http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode=2015arXiv150301742H&data_type=BIBTEX&db_key=PRE&nocookieset=1
;;
;; It looks like you need to get a Bibliographic code from the arxiv number to
;; then get the bibtex entry.
(defun arxiv-get-bibliographic-code (arxiv-number)
"Get Bibliographic code for ARXIV-NUMBER."
(with-current-buffer
(url-retrieve-synchronously
(concat
"http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:"
arxiv-number))
(search-forward-regexp "<link rel=\"canonical\" href=\"http://ui.adsabs.harvard.edu/abs/\\(.*\\)/abstract\"/>")
(match-string 1)))
(defun arxiv-get-bibtex-entry (arxiv-bibliographic-code)
"Get bibtex entry for ARXIV-BIBLIOGRAPHIC-CODE."
(with-current-buffer
(url-retrieve-synchronously (format "https://ui.adsabs.harvard.edu/abs/%s/exportcitation" arxiv-bibliographic-code))
(when (re-search-forward
"<textarea.*>\\(.*\\(?:\n.*\\)*?\\(?:\n\\s-*\n\\|\\'\\)\\)</textarea>"
nil t)
(xml-substitute-special (match-string 1)))))
;;* Getting a bibtex entry for an arXiv article using arXiv API:
;; Retrieves the meta data of an article view arXiv's http API,
;; extracts the necessary information, and formats a new BibTeX entry.
(defvar arxiv-entry-format-string "@article{%s,
journal = {CoRR},
title = {%s},
author = {%s},
archivePrefix = {arXiv},
year = {%s},
eprint = {%s},
primaryClass = {%s},
abstract = {%s},
url = {%s},
}"
"Template for BibTeX entries of arXiv articles.")
(declare-function doi-utils-doi-to-bibtex-string "doi-utils")
(declare-function org-ref-replace-nonascii "org-ref-bibtex")
(defun arxiv-get-bibtex-entry-via-arxiv-api (arxiv-number)
"Retrieve meta data for ARXIV-NUMBER.
Returns a formatted BibTeX entry."
(with-current-buffer
(url-retrieve-synchronously (format "http://export.arxiv.org/api/query?id_list=%s" arxiv-number) t)
(let* ((parse-tree (libxml-parse-xml-region
(progn (goto-char 0)
(search-forward "<?xml ")
(match-beginning 0))
(point-max)))
(entry (assq 'entry parse-tree))
(authors (--map (nth 2 (nth 2 it))
(--filter (and (listp it) (eq (car it) 'author)) entry)))
(year (format-time-string "%Y" (date-to-time (nth 2 (assq 'published entry)))))
(title (nth 2 (assq 'title entry)))
(names (arxiv-bibtexify-authors authors))
(category (cdar (nth 1 (assq 'primary_category entry))))
(abstract (s-trim (nth 2 (assq 'summary entry))))
(url (nth 2 (assq 'id entry)))
(temp-bibtex (format arxiv-entry-format-string "" title names year arxiv-number category abstract url))
(key (with-temp-buffer
(insert temp-bibtex)
(bibtex-mode)
(bibtex-set-dialect (parsebib-find-bibtex-dialect) t)
(org-ref-replace-nonascii)
(bibtex-generate-autokey)))
(doi (assq 'doi entry)))
(unless (and doi
(ignore-errors (doi-utils-doi-to-bibtex-string (nth 2 doi))))
;; no doi or inactive doi, so we fall back to the simple template
(format arxiv-entry-format-string key title names year arxiv-number category abstract url)))))
(defun arxiv-bibtexify-authors (authors)
"Return names in 'SURNAME, FIRST NAME' format from AUTHORS list."
(s-join " and "
(--map (concat (-last-item it) ", " (s-join " " (-remove-last 'stringp it)))
(--map (s-split " +" it) authors))))
(defun arxiv-maybe-arxiv-id-from-current-kill ()
"Try to get an arxiv ID from the current kill."
(let* ((the-current-kill (ignore-errors (current-kill 0 t))) ;; nil if empty kill ring
(arxiv-url-prefix-regexp "^https?://arxiv\\.org/\\(pdf\\|abs\\|format\\)/")
(arxiv-cite-prefix-regexp "^\\(arXiv\\|arxiv\\):")
(arxiv-id-old-regexp "[a-z-]+\\(\\.[A-Z]\\{2\\}\\)?/[0-9]\\{5,7\\}") ; Ex: math.GT/0309136
(arxiv-id-new-regexp "[0-9]\\{4\\}[.][0-9]\\{4,5\\}\\(v[0-9]+\\)?") ; Ex: 1304.4404v2
(arxiv-id-regexp (concat "\\(" arxiv-id-old-regexp "\\|" arxiv-id-new-regexp "\\)")))
(cond
(;; make sure current-kill has something in it
;; if current-kill is not a string, return nil
(not (stringp the-current-kill))
nil)
(;; check if current-kill looks like an arxiv ID
;; if so, return it
;; Ex: 1304.4404v2
(s-match (concat "^" arxiv-id-regexp) the-current-kill)
the-current-kill)
(;; check if current-kill looks like an arxiv cite
;; if so, remove the prefix and return
;; Ex: arXiv:1304.4404v2 --> 1304.4404v2
(s-match (concat arxiv-cite-prefix-regexp arxiv-id-regexp "$") the-current-kill)
(replace-regexp-in-string arxiv-cite-prefix-regexp "" the-current-kill))
(;; check if current-kill looks like an arxiv url
;; if so, remove the url prefix and return
;; Ex: https://arxiv.org/abs/1304.4404 --> 1304.4404
(s-match (concat arxiv-url-prefix-regexp arxiv-id-regexp "$") the-current-kill)
(replace-regexp-in-string arxiv-url-prefix-regexp "" the-current-kill))
(;; check if current-kill looks like an arxiv PDF url
;; if so, remove the url prefix, the .pdf suffix, and return
;; Ex: https://arxiv.org/pdf/1304.4404.pdf --> 1304.4404
(s-match (concat arxiv-url-prefix-regexp arxiv-id-regexp "\\.pdf$") the-current-kill)
(replace-regexp-in-string arxiv-url-prefix-regexp "" (substring the-current-kill 0 (- (length the-current-kill) 4))))
;; otherwise, return nil
(t
nil))))
(defvar bibtex-completion-bibliography)
;;;###autoload
(defun arxiv-add-bibtex-entry (arxiv-number bibfile)
"Add bibtex entry for ARXIV-NUMBER to BIBFILE."
(interactive
(list (read-string
"arxiv: "
(arxiv-maybe-arxiv-id-from-current-kill))
;; now get the bibfile to add it to
(completing-read
"Bibfile: "
(append (f-entries "." (lambda (f) (f-ext? f "bib")))
(if (stringp bibtex-completion-bibliography)
(list bibtex-completion-bibliography)
bibtex-completion-bibliography)))))
(save-window-excursion
(find-file bibfile)
(goto-char (point-max))
(when (not (looking-at "^")) (insert "\n"))
(insert (arxiv-get-bibtex-entry-via-arxiv-api arxiv-number))
(org-ref-clean-bibtex-entry)
(goto-char (point-max))
(when (not (looking-at "^")) (insert "\n"))
(save-buffer)))
;;;###autoload
(defun arxiv-get-pdf (arxiv-number pdf)
"Retrieve a pdf for ARXIV-NUMBER and save it to PDF."
(interactive
(list (read-string
"arxiv: "
(arxiv-maybe-arxiv-id-from-current-kill))
(read-string
"PDF: ")))
(let ((pdf-url (with-current-buffer
(url-retrieve-synchronously
(concat
"http://arxiv.org/abs/" arxiv-number))
;; <meta name="citation_pdf_url" content="http://arxiv.org/pdf/0801.1144" />
(goto-char (point-min))
(search-forward-regexp
"name=\\\"citation_pdf_url\\\" content=\\\"\\(.*\\)\\\"")
(match-string 1))))
(url-copy-file pdf-url pdf)
;; now check if we got a pdf
(unless (org-ref-pdf-p pdf)
(delete-file pdf)
(message "Error downloading arxiv pdf %s" pdf-url))))
(defvar bibtex-completion-library-path)
;;;###autoload
(defun arxiv-get-pdf-add-bibtex-entry (arxiv-number bibfile pdfdir)
"Add bibtex entry for ARXIV-NUMBER to BIBFILE.
Remove troublesome chars from the bibtex key, retrieve a pdf
for ARXIV-NUMBER and save it to PDFDIR with the same name of the
key."
(interactive
(list (read-string
"arxiv: "
(arxiv-maybe-arxiv-id-from-current-kill))
;; now get the bibfile to add it to
(completing-read
"Bibfile: "
(append (f-entries "." (lambda (f) (f-ext? f "bib")))
bibtex-completion-bibliography))
(cond
((stringp bibtex-completion-library-path)
bibtex-completion-library-path)
((= 1 (length bibtex-completion-library-path))
(car bibtex-completion-library-path))
(t
(completing-read "PDF dir: " bibtex-completion-library-path)))))
(arxiv-add-bibtex-entry arxiv-number bibfile)
(save-window-excursion
(let ((key ""))
(find-file bibfile)
(goto-char (point-max))
(bibtex-beginning-of-entry)
(re-search-forward bibtex-entry-maybe-empty-head)
(if (match-beginning bibtex-key-in-head)
(progn
(setq key (delete-and-extract-region
(match-beginning bibtex-key-in-head)
(match-end bibtex-key-in-head)))
;; remove potentially troublesome characters from key
;; as it will be used as a filename
(setq key (replace-regexp-in-string "\"\\|\\*\\|/\\|:\\|<\\|>\\|\\?\\|\\\\\\||\\|\\+\\|,\\|\\.\\|;\\|=\\|\\[\\|]\\|!\\|@"
"" key))
;; check if the key is in the buffer
(when (save-excursion
(bibtex-search-entry key))
(save-excursion
(bibtex-search-entry key)
(bibtex-copy-entry-as-kill)
(switch-to-buffer-other-window "*duplicate entry*")
(bibtex-yank))
(setq key (bibtex-read-key "Duplicate Key found, edit: " key))))
(setq key (bibtex-read-key "Key not found, insert: ")))
(insert key)
(arxiv-get-pdf arxiv-number (concat pdfdir key ".pdf"))
;; Check that it worked, and insert a field for it.
(when (file-exists-p (concat pdfdir key ".pdf"))
(bibtex-end-of-entry)
(backward-char)
(insert (format " file = {%s}\n " (concat pdfdir key ".pdf")))))))
(provide 'org-ref-arxiv)
;;; org-ref-arxiv.el ends here