-
Notifications
You must be signed in to change notification settings - Fork 15
/
biblio-hal.el
106 lines (90 loc) · 4.08 KB
/
biblio-hal.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
;;; biblio-hal.el --- Lookup and import bibliographic entries from HAL (archives ouvertes) -*- lexical-binding: t -*-
;; Copyright (C) 2016 Clément Pit-Claudel
;; Author: Clément Pit-Claudel <clement.pitclaudel@live.com>
;; URL: https://github.com/cpitclaudel/biblio.el
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Lookup and download bibliographic records from HAL using `hal-lookup'.
;;
;; This file implements a backend for the for the `biblio' package (which see for more
;; documentation).
;;; Code:
(require 'biblio-core)
(defun biblio-hal--forward-bibtex (metadata forward-to)
"Forward BibTeX for HAL entry METADATA to FORWARD-TO."
(funcall forward-to (biblio-alist-get 'bibtex metadata)))
;; (defun biblio-hal--format-author (author)
;; "Format AUTHOR for HAL search results."
;; (pcase author
;; (`(,author . ,affiliation)
;; (biblio-join " " author (biblio-parenthesize affiliation)))))
(defun biblio-hal--extract-interesting-fields (item)
"Prepare a HAL search result ITEM for display."
(let-alist item
(list (cons 'doi .doiId_s)
(cons 'year (aref (timezone-parse-date .producedDate_tdate) 0))
(cons 'bibtex .label_bibtex)
(cons 'title (biblio-join " "
(biblio-join-1 ", " .title_s)
(biblio-parenthesize
(biblio-join-1 ", " .subtitle_s))))
(cons 'authors .authFullName_s)
;; Too many institutions? (biblio-parenthesize (biblio-join-1 ", " .structName_s))
(cons 'publisher .journalPublisher_s)
(cons 'container .journalTitle_s)
(cons 'references (biblio-remove-empty
(list .doiId_s .halId_s .arxivId_s)))
(cons 'type .submitType_s)
(cons 'url .uri_s)
(cons 'direct-url (car (append .files_s nil))))))
(defun biblio-hal--parse-search-results ()
"Extract search results from HAL response."
(biblio-decode-url-buffer 'utf-8)
(let-alist (json-read)
(unless .response
(display-warning 'biblio-hal "HAL query failed"))
(seq-map #'biblio-hal--extract-interesting-fields .response.docs)))
(defun biblio-hal--url (query)
"Create a HAL url to look up QUERY."
(format "https://api.archives-ouvertes.fr/search/?q=%s&wt=%s&fl=%s"
(url-encode-url query) "json"
(biblio-join "," ;; Use ‘*’ to show all fields
"arxivId_s" "halId_s" "doiId_s" ;; "journalIssn_s"
"title_s" "subtitle_s" "authFullName_s" "structName_s"
"journalPublisher_s" "submitType_s" ;; "abstract_s"
;; "journalTitle_s" "volume_s" "issue_s" "page_s" "writingDate_s"
"label_bibtex" "files_s" "uri_s" "producedDate_tdate")))
;;;###autoload
(defun biblio-hal-backend (command &optional arg &rest more)
"A HAL backend for biblio.el.
COMMAND, ARG, MORE: See `biblio-backends'."
(pcase command
(`name "HAL")
(`prompt "HAL (archives ouvertes) query: ")
(`url (biblio-hal--url arg))
(`parse-buffer (biblio-hal--parse-search-results))
(`forward-bibtex (biblio-hal--forward-bibtex arg (car more)))
(`register (add-to-list 'biblio-backends #'biblio-hal-backend))))
;;;###autoload
(add-hook 'biblio-init-hook #'biblio-hal-backend)
;;;###autoload
(defun biblio-hal-lookup (&optional query)
"Start a HAL search for QUERY, prompting if needed."
(interactive)
(biblio-lookup #'biblio-hal-backend query))
;;;###autoload
(defalias 'hal-lookup 'biblio-hal-lookup)
(provide 'biblio-hal)
;;; biblio-hal.el ends here