-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
org-ref-url-utils.el
310 lines (257 loc) · 10.1 KB
/
org-ref-url-utils.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
304
305
306
307
308
309
310
;;; org-ref-url-utils.el --- Utility functions to scrape DOIs from urls -*- lexical-binding: t; -*-
;; Copyright (C) 2015-2024 John Kitchin
;; Author: John Kitchin <[email protected]>
;; 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:
;; Drag a webpage onto a bibtex file to insert a bibtex entry.
;;
;; [2021-08-29 Sun] I have not found drag-and-drop reliable or convenient. I
;; don't recommend using this, but I am also not deleting it (yet).
;; This works by scraping DOIs from the content at the URL using patterns in
;; `org-ref-doi-regexps'. If one DOI is found, it is added as an entry. If
;; multiple DOIs are found, you will get a selection buffer to choose what
;; you want to add. You can add new patterns to `org-ref-doi-regexps'.
;; You can press Control to "debug" a URL, which will open a buffer of the
;; content with the current DOI patterns highlighted. If you want to get all the
;; DOIs at a URL, you can press Meta during the drag-n-drop.
;; You can also insert formatted bibtex entries using the
;; `org-ref-url-html-to-bibtex' command, which converts a web page to
;; bibtex or biblatex entry using URL. The org-cliplink package can
;; help cleanup HTML code. Installing it is recommended.
;;; Code:
(defvar org-ref-bibliography-entry-format)
(defvar org-ref-cite-types)
(defvar org-cliplink-escape-alist)
(declare-function 'org-ref-get-bibtex-key-and-file "org-ref-core.el")
(declare-function 'org-ref-find-bibliography "org-ref-core.el")
(declare-function dnd-handle-one-url "dnd")
(declare-function dnd--unescape-uri "dnd")
(require 'doi-utils)
(require 'f)
(require 's)
(eval-when-compile
(require 'cl-lib))
(defgroup org-ref-url nil
"Customization group for org-ref-url-utils"
:tag "Org Ref URL"
:group 'org-ref-url-utils)
(defcustom org-ref-doi-regexps
'("scheme=\"doi\" content=\"\\([^\"]*\\)\""
"citation_doi\" content=\"\\([^\"]*\\)\""
"data-doi=\"\\([^\"]*\\)\""
"content=\"\\([^\"]*\\)\" name=\"citation_doi"
"objectDOI\" : \"\\([^\"]*\\)\""
"doi = '\\([^']*\\)'"
"\"http://dx.doi.org/\\([^\"]*\\)\""
"/doi/\\([^\"]*\\)\">"
"doi/full/\\(.*\\)&"
"doi=\\([^&]*\\)&")
"List of regexps to match a DOI.
The doi should be in group 1 so that (match-string 1) contains
the DOI."
:type '(repeat regexp)
:group 'org-ref-url-utils)
(defvar org-ref-url-title-re
"<title.?+?>\\([[:ascii:][:nonascii:]]*?\\|.+\\)</title>"
"Regular expression for matching title.")
(defvar org-ref-url-author-re
"<meta name=\"author\" content=\"\\(.+\\)\"\s?/?>"
"Regular expression for matching author.")
(defvar org-ref-url-date-re
"<[a-z].+ class=\\(.?+date.[^>]*\\)>\\([[:ascii:][:nonascii:]]*?\\)</[a-z].+>"
"Regular expression for matching date.")
(defvar org-ref-url-bibtex-template
"@misc{key,
title = {${:title}},
author = {${:author}},
howpublished = {${:url}},
year = {${:year}},
note = {Online; accessed ${:urldate}}
}"
"BibTeX entry template for online sources.")
(defvar org-ref-url-biblatex-template
"@online{key,
title = {${:title}},
author = {${:author}},
url = {${:url}}
year = {${:year}},
urldate = {Online; accessed ${:urldate}}
}"
"Biblatex entry template for online sources.")
;;* Scrape DOIs from a URL
(defun org-ref-url-scrape-dois (url)
"Scrape all dois from a URL matching a pattern in `org-ref-doi-regexps'.
Returns a list of collected DOIs in the order found."
(let ((dois '()))
(with-current-buffer (url-retrieve-synchronously url)
(cl-loop for doi-pattern in org-ref-doi-regexps
do
(goto-char (point-min))
(while (re-search-forward doi-pattern nil t)
(cl-pushnew (match-string 1) dois :test #'equal)))
(reverse dois))))
(defun org-ref-url-dnd-protocol (url action)
"Protocol function for use in `dnd-protocol-alist'.
We scrape DOIs from the url first. If there is one, we add it. If
there is more than one, we offer a completion buffer of selections. If
no DOI is found, we create a misc entry, with a prompt for a key."
;; make sure we are on a bib-file
(if (and (buffer-file-name)
(f-ext? (buffer-file-name) "bib"))
(let ((dois (org-ref-url-scrape-dois url)))
(cond
;; One doi found. Assume it is what we want.
((= 1 (length dois))
(doi-utils-add-bibtex-entry-from-doi
(car dois)
(buffer-file-name))
action)
;; Multiple DOIs found
((> (length dois) 1)
(let ((doi (completing-read "Select a DOI: " dois)))
(doi-utils-add-bibtex-entry-from-doi doi (buffer-file-name))))
;; No DOIs found, add a misc entry.
(t
(org-ref-url-html-to-bibtex (buffer-file-name) url)
action)))
;; pass back to dnd. Copied from `org-download-dnd'. Apparently
;; returning nil does not do this.
(let ((dnd-protocol-alist
(rassq-delete-all
'org-ref-url-dnd-protocol
(copy-alist dnd-protocol-alist))))
(dnd-handle-one-url nil action url))))
(add-to-list 'dnd-protocol-alist '("^https?" . org-ref-url-dnd-protocol))
;;* Enable a DOI to be dragged onto a bibtex buffer
(defun org-ref-doi-dnd-protocol (doi action)
"Protocol for when a doi is dragged onto a bibtex file.
A doi will be either doi:10.xxx or 10.xxx."
(if (and (buffer-file-name)
(f-ext? (buffer-file-name) "bib"))
(let ((doi (dnd--unescape-uri doi)))
;; Get the actual doi now
(string-match "\\(?:DOI\\|doi\\)?:? *\\(10.*\\)" doi)
(setq doi (match-string 1 doi))
(when doi
(doi-add-bibtex-entry doi (buffer-file-name))
(save-buffer)
action))
;; not on a bib file
(let ((dnd-protocol-alist
(rassq-delete-all
'org-ref-url-dnd-protocol
(copy-alist dnd-protocol-alist))))
(dnd-handle-one-url nil action doi))))
(add-to-list 'dnd-protocol-alist '("^doi" . org-ref-doi-dnd-protocol))
(add-to-list 'dnd-protocol-alist '("^10" . org-ref-doi-dnd-protocol))
;;* Debugging DOIs from a URL
;; You can use this to see if there are any DOIs in a URL, and to use re-builder
;; to add new patterns to `org-ref-doi-regexps'.
;;;###autoload
(defun org-ref-url-debug-url (url)
"Open a buffer to URL with all doi patterns highlighted."
(interactive "sURL: ")
(switch-to-buffer
(url-retrieve-synchronously url))
(highlight-regexp
(mapconcat 'identity org-ref-doi-regexps "\\|")))
;;* HTML to BibTeX functions
(defun org-ref-url-html-replace (string)
"Replace HTML entities in STRING with their unicode equivalent."
(let (result
(case-fold-search nil))
(with-temp-buffer
(insert string)
(mapc (lambda (char)
(goto-char (point-min))
(while (re-search-forward (car char) nil t)
(replace-match (cdr char))))
org-cliplink-escape-alist)
(setq result (buffer-substring (point-min) (point-max))))
result))
(defun org-ref-url-add-nil (list)
"Add nil to all missing keys in LIST."
(let (newlist)
(mapc (lambda (key)
(unless (alist-get key list)
(push (cons key "nil") newlist)))
(list :title :author :url :urldate :year))
(append list newlist)))
(defun org-ref-url-html-read (url)
"Read URL content and return fields.
Fields include author, title, url, urldate, and year."
;; Start with fields we already know
(let ((fields `((:url . ,url)
(:urldate . ,(format-time-string "%d %B %Y")))))
(with-current-buffer
(url-retrieve-synchronously url t t)
;; find pubdate
(goto-char (point-min))
(when (re-search-forward org-ref-url-date-re nil t)
(let ((string (match-string 2)))
(when (string-match "\\([0-9]\\{4\\}\\)" string)
(push (cons :year (match-string 1 string)) fields))))
;; find author
(goto-char (point-min))
(let ((author (if (re-search-forward org-ref-url-author-re nil t)
(match-string 1)
"Unknown")))
(push (cons :author author) fields))
;; find title
(goto-char (point-min))
(when (re-search-forward org-ref-url-title-re nil t)
(push (cons :title
(s-trim (decode-coding-string (match-string 1) 'utf-8)))
fields)))
;; Finally add nil value to missing fields
(org-ref-url-add-nil fields)))
;;;###autoload
(defun org-ref-url-html-to-bibtex (bibfile &optional url)
"Convert URL to a bibtex or biblatex entry in BIBFILE.
If URL is the first in the kill ring, use it. Otherwise, prompt for
one in the minibuffer."
(interactive (if (-contains? (org-ref-find-bibliography) (buffer-file-name))
(list (buffer-file-name))
(list (completing-read "Bibtex file: " (org-ref-find-bibliography)))))
(let ((url (if url url
(if (s-match "^http" (current-kill 0 'do-not-move))
(format "%s" (current-kill 0 'do-not-move))
(read-from-minibuffer "URL: ")))))
(with-current-buffer
(find-file-noselect bibfile)
;; Maybe check dialect if set as local variable
(let* ((dialect bibtex-dialect)
(alist (org-ref-url-html-read url))
(entry (s-format
;; Check dialect and format entry accordingly
(if (eq dialect 'biblatex)
org-ref-url-biblatex-template
org-ref-url-bibtex-template)
'aget alist)))
(goto-char (point-max))
;; Place new entry one line after the last entry. Sometimes we are in a
;; new file though, in which case we don't want to do this.
(unless (bobp)
(while (not (looking-back "^}\n" 2))
(delete-char -1)))
(insert "\n")
(insert (if (require 'org-cliplink nil 'noerror)
;; Sanitize values by replacing html entities
(org-ref-url-html-replace entry)
entry))
(insert "\n")
(bibtex-beginning-of-entry)
(org-ref-clean-bibtex-entry)
(save-buffer)))))
(provide 'org-ref-url-utils)
;;; org-ref-url-utils.el ends here