-
Notifications
You must be signed in to change notification settings - Fork 13
/
staticweb.jl
266 lines (242 loc) · 7.34 KB
/
staticweb.jl
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
const type_to_label = Dict{String,String}([
"article" => "journal",
"book" => "book",
"booklet" => "booklet",
"eprint" => "eprint",
"inbook" => "book chapter",
"incollection" => "book section",
"inproceedings" => "conference",
"manual" => "manual",
"mastersthesis" => "master's thesis",
"misc" => "other",
"phdthesis" => "doctoral thesis",
"proceedings" => "proceedings",
"techreport" => "report",
"unpublished" => "other",
])
"""
xtitle(entry
Format the title of an `Entry` for web export.
"""
function xtitle(entry)
return isdefined(entry, :title) ? entry.title : get(entry.fields, "title", "")
end
"""
xnames(entry, editors = false; names = :full)
Format the name of an `Entry` for web export.
# Arguments:
- `entry`: an entry
- `editors`: `true` if the name describes editors
- `names`: :full (full names) or :last (last names + first name abbreviation)
"""
function xnames(
entry,
editors=false;
names=:full, # Current options: :last, :full
)
# forces the names to be editors' name if the entry are Proceedings
if !editors && entry.type ∈ ["proceedings"]
return xnames(entry, true)
end
entry_names = editors ? entry.editors : entry.authors
if names == :last
parts = map(s -> [s.particle, s.last, s.junior], entry_names)
else
parts = map(s -> [s.first, s.middle, s.particle, s.last, s.junior], entry_names)
end
entry_names = map(parts) do s
return join(filter(!isempty, s), " ")
end
str = join(entry_names, ", ")
return replace(str, r"[\n\r ]+" => " ") # TODO: make it cleaner (is replace still necessary)
end
"""
xin(entry)
Format the appears-`in` field of an `Entry` for web export.
"""
function xin(entry)
str = ""
temp_last = false
temp = []
if entry.type == "article"
temp = [
entry.in.journal,
entry.in.volume * (entry.in.number != "" ? "($(entry.in.number))" : ""),
entry.in.pages,
entry.date.year,
]
elseif entry.type == "book"
temp = [entry.in.publisher, entry.in.address, entry.date.year]
elseif entry.type == "booklet"
temp = [entry.access.howpublished, entry.date.year]
elseif entry.type == "eprint"
if isempty(entry.eprint.archive_prefix)
str *= entry.eprint.eprint
else
str *= "$(entry.eprint.archive_prefix):$(entry.eprint.eprint) [$(entry.eprint.primary_class)]"
end
elseif entry.type == "inbook"
temp = [
entry.booktitle,
isempty(entry.in.chapter) ? entry.in.pages : entry.in.chapter,
entry.in.publisher,
entry.in.address,
]
elseif entry.type == "incollection"
# TODO: check if this new or the old format is/was correct, that "editors" seems out of place (and the title was switched with the names)?
temp = [
"In $(entry.booktitle)",
"editors",
xnames(entry, true),
entry.in.pages * ". " * entry.in.publisher, # TODO: conditional ". " if one of the strings is empty?
entry.in.address,
entry.date.year,
]
elseif entry.type == "inproceedings"
temp_last = entry.in.publisher != ""
temp = [
" In " * entry.booktitle,
entry.in.series,
entry.in.pages,
entry.in.address,
entry.date.year,
entry.in.publisher,
]
elseif entry.type == "manual"
temp = [entry.in.organization, entry.in.address, entry.date.year]
elseif entry.type ∈ ["mastersthesis", "phdthesis"]
temp = [
(entry.type == "mastersthesis" ? "Master's" : "PhD") * " thesis",
entry.in.school,
entry.in.address,
entry.date.year,
]
elseif entry.type == "misc"
temp_last =
get(entry.fields, "note", "") != "" &&
entry.access.howpublished != "" &&
entry.date.year != ""
temp = [
entry.access.howpublished
entry.date.year
get(entry.fields, "note", "")
]
elseif entry.type == "proceedings"
temp_last = entry.in.publisher != ""
temp = [
(entry.in.volume != "" ? "Volume $(entry.in.volume) of " : "") *
entry.in.series,
entry.in.address,
entry.date.year,
entry.in.publisher,
]
# TODO: check if this old line here was a hidden bug
# str *= entry.in.publisher != "" ? ". $(entry.in.address)" : ""
elseif entry.type == "techreport"
temp = [
entry.in.number != "" ? "Technical Report $(entry.in.number)" : "",
entry.in.institution,
entry.in.address,
entry.date.year,
]
elseif entry.type == "unpublished"
temp = [get(entry.fields, "note", ""), entry.date.year]
end
if temp_last
str *= join(filter!(!isempty, temp), ", ", ". ")
else
str *= join(filter!(!isempty, temp), ", ")
end
if !isempty(str)
str *= "."
end
return str
end
"""
xyear(entry)
Format the year of an `Entry` for web export.
"""
xyear(entry) = entry.date.year
"""
xlink(entry)
Format the download link of an `Entry` for web export.
"""
function xlink(entry)
if entry.access.doi != ""
return "https://doi.org/" * entry.access.doi
elseif entry.access.url != ""
return entry.access.url
end
return ""
end
"""
xfile(entry)
Format the downloadable path of an `Entry` file for web export.
"""
xfile(entry) = "files/$(entry.id).pdf"
"""
xcite(entry)
Format the BibTeX cite output of an `Entry` for web export.
"""
xcite(entry) = string(entry)
"""
xlabels(entry)
Format the labels of an `Entry` for web export.
"""
function xlabels(entry)
str = get(entry.fields, "swp-labels", "")
if isempty(str)
str = get(entry.fields, "labels", "")
end
if isempty(str)
return [entry.type]
end
return split(str, r"[\n\r ]*,[\n\r ]*")
end
"""
Publication
A structure to store all the information necessary to web export.
"""
struct Publication
id::String
type::String
title::String
names::String
in::String
year::String
link::String
file::String
cite::String
abstract::String
labels::Vector{String}
end
"""
Publication(entry)
Construct a `Publication` (compatible with web export) from an `Entry`.
"""
function Publication(entry)
id = entry.id
type = entry.type
title = entry.title
names = xnames(entry)
in_ = xin(entry)
year = xyear(entry)
link = xlink(entry)
file = xfile(entry)
cite = export_bibtex(entry)
abstract = get(entry.fields, "abstract", "")
labels = xlabels(entry)
return Publication(id, type, title, names, in_, year, link, file, cite, abstract, labels)
end
"""
export_web(bibliography::DataStructures.OrderedDict{String,BibInternal.Entry})
Export a bibliography in internal format to the web format of the [StaticWebPages.jl](https://github.com/Humans-of-Julia/StaticWebPages.jl) package. Also used by [DocumenterCitations.jl](https://github.com/ali-ramadhan/DocumenterCitations.jl).
"""
function export_web(bibliography)
entries = Vector{Publication}()
for entry in values(bibliography)
p = Publication(entry)
push!(entries, p)
end
return entries
end