-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibdoc
executable file
·190 lines (142 loc) · 5.34 KB
/
bibdoc
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
#!/usr/bin/env bb
;; This file is part of Dirk Kutscher's datatracker-publications
;; project and is released under the GPL-v3 License:
;; http://www.gnu.org/licenses/gpl-3.0.html
(require '[clojure.string :as string])
(require '[babashka.curl :as curl])
(require '[babashka.cli :as cli])
(require '[cheshire.core :as json])
(defn extract-digits [s]
(->> s
(re-seq #"\d") ; Finds all occurrences of digits
(apply str))) ; Joins them into a single string
(def months ["January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December"])
(defn date-month
"returns month in text form"
[month]
(nth months (dec (Integer/parseInt month))))
(defn fetch-metadata [draft-name]
(let [url (str "https://datatracker.ietf.org/doc/" (string/trim draft-name) "/doc.json")
response (curl/get url {:accept :json})
data (json/parse-string (:body response) true)]
data))
(defn author-string [metadata]
(let [authors (get-in metadata [:authors])]
(if (empty? authors)
""
(clojure.string/join ", " (map :name authors)))))
(defn title-string [metadata]
(let [title (get-in metadata [:title])]
(if (empty? title)
""
title)))
(defn draft-string [metadata]
(let [draft (get-in metadata [:name])]
(if (empty? draft)
""
(str "Internet Draft " draft " (Work in Progress)"))))
(defn rfc-string [metadata]
(let [rfc (get-in metadata [:name])]
(if (empty? rfc)
""
(clojure.string/upper-case rfc))))
(defn date-data
"return [year month day]"
[date]
(clojure.string/split (first (clojure.string/split date #" ")) #"-"))
(defn date-string [metadata]
(let [date (get-in metadata [:time])]
(if (empty? date)
""
(let [[year month _] (date-data date)]
(str (date-month month) " " year)))))
(defn id-latest [metadata]
(let [draft (last (get-in metadata [:rev_history]))]
(if (empty? draft)
""
(get-in draft [:url]))))
(defn id-latest-name [metadata]
(let [name (id-latest metadata)]
(string/replace
(string/replace (id-latest metadata) #"/doc/" "")
#"/$" "")))
(defn uri-string [metadata]
(let [uri (id-latest metadata)]
(str "https://datatracker.ietf.org" uri)))
(defn doi [metadata]
(let [rfc (rfc-string metadata)]
(str "10.17487/" rfc)))
(defn doi-string [metadata]
(str "DOI: " (doi metadata)))
(defn doc-type [metadata]
(let [name (get-in metadata [:name])]
(if (clojure.string/starts-with? name "rfc")
:rfc
:draft)))
(defmulti format-rfc (fn [output-type metadata] output-type))
(defmethod format-rfc :bibtex [_ metadata]
(let [rfc-number (extract-digits (:name metadata))
[year month _] (date-data (:time metadata))
]
(str "@techreport{RFC" rfc-number ",\n"
" author = \"" (author-string metadata) "\",\n"
" title = \"" (title-string metadata) "\",\n"
" howpublished = \"Internet Requests for Comments\",\n"
" type = \"RFC\",\n"
" number = \"" rfc-number "\",\n"
" year = \"" year "\",\n"
" month = \"" (date-month month) "\",\n"
" issn = \"" "2070-1721" "\",\n"
" publisher = \"RFC Editor\",\n"
" institution = \"RFC Editor\",\n"
" doi = \"" (doi metadata) "\",\n"
"}")))
(defmethod format-rfc :text [_ metadata]
(->> [author-string title-string rfc-string date-string uri-string doi-string]
(map #(% metadata))
(clojure.string/join "; ")))
(defmulti format-id (fn [output-type metadata] output-type))
(defmethod format-id :bibtex [_ metadata]
(let [
[year month _] (date-data (:time metadata))
]
(str "@techreport{I-D." (:name metadata) ",\n"
" author = \"" (author-string metadata) "\",\n"
" title = \"" (title-string metadata) "\",\n"
" howpublished = \"Work In Progress\",\n"
" type = \"Internet Draft\",\n"
" number = \"" (id-latest-name metadata) "\",\n"
" howpublished = \"\\url{" (uri-string metadata) "}\",\n"
" year = \"" year "\",\n"
" month = \"" (date-month month) "\",\n"
" institution = \"IETF Secretariat\",\n"
"}")))
(defmethod format-id :text [_ metadata]
(->> [author-string title-string draft-string date-string uri-string]
(map #(% metadata))
(clojure.string/join "; ")))
(defn format-doc [output-type metadata]
(if (= (doc-type metadata) :rfc)
(format-rfc output-type metadata)
(format-id output-type metadata)))
(defn read-stdin []
(doall (line-seq (java.io.BufferedReader. *in*))))
(defn generate-output [args output-type]
(let [doc-strings (map (partial format-doc output-type) (map fetch-metadata args))]
(println (clojure.string/join "\n" doc-strings))))
(defn selected? [opt options]
(get (:opts options) opt))
(defn -main [& args]
(let [options (cli/parse-args (first args)
{:coerce {:stdin :boolean
:bibtex :boolean}})]
(let [
input (if (selected? :stdin options)
(string/split (string/join " " (read-stdin)) #" ")
(:args options))
output-type (if (selected? :bibtex options)
:bibtex
:text)
]
(generate-output input output-type))))
(-main *command-line-args*)