-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconsult-web-doi.el
82 lines (65 loc) · 2.52 KB
/
consult-web-doi.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
;;; consult-web-doi.el --- Consulting DOI.org -*- lexical-binding: t -*-
;; Copyright (C) 2024 Armin Darvish
;; Author: Armin Darvish
;; Maintainer: Armin Darvish
;; Created: 2024
;; Version: 0.1
;; Package-Requires: ((emacs "28.1") (consult "1.1"))
;; Homepage: https://github.com/armindarvish/consult-web
;; Keywords: convenience
;;; Commentary:
;;; Code:
(require 'consult-web)
(defvar consult-web-doiorg-api-url "https://doi.org/api/handles/")
(defvar consult-web-doiorg-search-url "https://doi.org/")
(defun consult-web--doi-to-url (doi)
"Converts DOI value to target url"
(let* ((doi (if doi (format "%s" doi)))
(url (concat consult-web-doiorg-api-url doi)))
(funcall consult-web-retrieve-backend
url
:parser
(lambda ()
(goto-char (point-min))
(let* ((content (json-parse-buffer))
(items (gethash "values" content)))
(car (mapcar (lambda (item)
(if-let* ((type (gethash "type" item))
(url (if (equal type "URL") (gethash "value" (gethash "data" item)))))
url
nil)) items)))))))
(cl-defun consult-web--doiorg-fetch-results (doi &rest args)
"Fetch target url of DOI.
"
(let* ((table (make-hash-table :test 'equal))
(url (consult-web--doi-to-url doi)))
(if url
(progn
(puthash :url url
table)
(puthash :title doi
table)
(puthash :source "doiorg"
table)
(puthash :query doi
table)
))
(list table)))
(defvar consult-web--doi-search-history (list)
"History variables for search terms when using
`consult-web-doi' commands.")
(defvar consult-web--doi-selection-history (list)
"History variables for selected items when using
`consult-web-doi' commands.")
(consult-web-define-source "doiorg"
:narrow-char ?d
:face 'link
:request #'consult-web--doiorg-fetch-results
:search-history 'consult-web--doi-search-history
:selection-history 'consult-web--doi-selection-history
:dynamic t
)
;;; provide `consult-web-doi' module
(provide 'consult-web-doi)
(add-to-list 'consult-web-sources-modules-to-load 'consult-web-doi)
;;; consult-web-doi.el ends here