-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathcreate_hugo_yaml.py
executable file
·289 lines (253 loc) · 10.6 KB
/
create_hugo_yaml.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2019 Marcel Bollmann <marcel@bollmann.me>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Usage: create_hugo_yaml.py [--importdir=DIR] [--exportdir=DIR] [-c] [--debug] [--dry-run]
Creates YAML files containing all necessary Anthology data for the static website generator.
Options:
--importdir=DIR Directory to import XML files from. [default: {scriptdir}/../data/]
--exportdir=DIR Directory to write YAML files to. [default: {scriptdir}/../build/data/]
--debug Output debug-level log messages.
-c, --clean Delete existing files in target directory before generation.
-n, --dry-run Do not write YAML files (useful for debugging).
-h, --help Display this helpful text.
"""
from docopt import docopt
from collections import defaultdict
from tqdm import tqdm
import logging as log
import os
import yaml
try:
from yaml import CSafeDumper as Dumper
except ImportError:
from yaml import SafeDumper as Dumper
from anthology import Anthology
from anthology.utils import SeverityTracker, deconstruct_anthology_id
from create_hugo_pages import check_directory
SCRIPTDIR = os.path.dirname(os.path.realpath(__file__))
def export_anthology(anthology, outdir, clean=False, dryrun=False):
"""
Dumps files in build/yaml/*.yaml. These files are used in conjunction with the hugo
page stubs created by create_hugo_pages.py to instantiate Hugo templates.
"""
# Prepare paper index
papers = defaultdict(dict)
citation_styles = {
"acl": f"{SCRIPTDIR}/acl.csl",
# "apa": "apa-6th-edition",
# "mla": "modern-language-association-7th-edition",
}
for id_, paper in anthology.papers.items():
log.debug("export_anthology: processing paper '{}'".format(id_))
data = paper.as_dict()
if paper.parent_volume.ingest_date:
data["ingest_date"] = paper.parent_volume.ingest_date
data["title_html"] = paper.get_title("html")
data["title_raw"] = paper.get_title("xml")
if "xml_title" in data:
del data["xml_title"]
if "xml_booktitle" in data:
data["booktitle_html"] = paper.get_booktitle("html")
del data["xml_booktitle"]
if "xml_abstract" in data:
data["abstract_html"] = paper.get_abstract("html")
data["abstract_raw"] = paper.get_abstract("xml")
del data["xml_abstract"]
if "xml_url" in data:
del data["xml_url"]
if "author" in data:
data["author"] = [
anthology.people.resolve_name(name, id_) for name, id_ in data["author"]
]
if "editor" in data:
data["editor"] = [
anthology.people.resolve_name(name, id_) for name, id_ in data["editor"]
]
for abbrev, style in citation_styles.items():
data[f"citation_{abbrev}"] = paper.as_citation_html(style)
papers[paper.collection_id][paper.full_id] = data
# Prepare people index
people = defaultdict(dict)
for id_ in anthology.people.personids():
name = anthology.people.get_canonical_name(id_)
log.debug("export_anthology: processing person '{}'".format(repr(name)))
data = name.as_dict()
data["slug"] = id_
if id_ in anthology.people.comments:
data["comment"] = anthology.people.comments[id_]
if id_ in anthology.people.similar:
data["similar"] = sorted(anthology.people.similar[id_])
papers_for_id = anthology.people.get_papers(id_, role="author") + [
paper
for paper in anthology.people.get_papers(id_, role="editor")
if anthology.papers.get(paper).is_volume
]
data["papers"] = sorted(
papers_for_id,
key=lambda p: anthology.papers.get(p).get("year"),
reverse=True,
)
data["coauthors"] = sorted(
[[co_id, count] for (co_id, count) in anthology.people.get_coauthors(id_)],
key=lambda p: p[1],
reverse=True,
)
data["venues"] = sorted(
[
[venue, count]
for (venue, count) in anthology.people.get_venues(id_).items()
],
key=lambda p: p[1],
reverse=True,
)
variants = [
n
for n in anthology.people.get_used_names(id_)
if n.first != name.first or n.last != name.last
]
if len(variants) > 0:
data["variant_entries"] = [name.as_dict() for name in sorted(variants)]
people[id_[0]][id_] = data
# Prepare volume index
volumes = {}
for id_, volume in anthology.volumes.items():
log.debug("export_anthology: processing volume '{}'".format(id_))
data = volume.as_dict()
data["title_html"] = volume.get_title("html")
data["title_raw"] = volume.get_title("xml")
del data["xml_booktitle"]
if "xml_abstract" in data:
del data["xml_abstract"]
if "xml_url" in data:
del data["xml_url"]
data["has_abstracts"] = volume.has_abstracts
data["papers"] = volume.paper_ids
data["venues"] = volume.get_venues()
if "author" in data:
data["author"] = [
anthology.people.resolve_name(name, id_) for name, id_ in data["author"]
]
if "editor" in data:
data["editor"] = [
anthology.people.resolve_name(name, id_) for name, id_ in data["editor"]
]
volumes[volume.full_id] = data
# Prepare venue index
venues = {}
for main_venue, data in anthology.venues.items():
data.get("oldstyle_letter", "W")
data = data.copy()
data["volumes_by_year"] = {}
for year in sorted(data["years"]):
# Grab just the volumes that match the current year
filtered_volumes = list(
filter(lambda k: volumes[k]["year"] == year, data["volumes"])
)
data["volumes_by_year"][year] = filtered_volumes
if not data["volumes_by_year"]:
log.warning(f"Venue '{main_venue}' has no volumes associated with it")
data["years"] = sorted(list(data["years"]))
# The export uses volumes_by_year, deleting this saves space
del data["volumes"]
venues[main_venue] = data
# Prepare events index
events = {}
for event_name, event_data in anthology.eventindex.items():
main_venue = event_data["venue"]
event_data = event_data.copy()
def volume_sorter(volume):
"""
Puts all main volumes before satellite ones.
Main volumes are sorted in a stabile manner as
found in the XML. Colocated ones are sorted
alphabetically.
:param volume: The Anthology volume
"""
if main_venue in volumes[volume]["venues"]:
# sort volumes in main venue first
return "_"
elif deconstruct_anthology_id(volume)[1] == main_venue:
# this puts Findings at the top (e.g., 2022-findings.emnlp will match emnlp)
return "__"
else:
# sort colocated volumes alphabetically, using
# the alphabetically-earliest volume
return min(volumes[volume]["venues"])
event_data["volumes"] = sorted(event_data["volumes"], key=volume_sorter)
events[event_name] = event_data
# Prepare SIG index
sigs = {}
for main_venue, sig in anthology.sigs.items():
data = {
"name": sig.name,
"slug": sig.slug,
"url": sig.url,
"volumes_by_year": sig.volumes_by_year,
"years": sorted([str(year) for year in sig.years]),
}
sigs[main_venue] = data
# Dump all
if not dryrun:
# Create directories
for subdir in ("", "papers", "people"):
target_dir = "{}/{}".format(outdir, subdir)
if not check_directory(target_dir, clean=clean):
return
progress = tqdm(total=len(papers) + len(people) + 7)
for collection_id, paper_list in papers.items():
with open("{}/papers/{}.yaml".format(outdir, collection_id), "w") as f:
yaml.dump(paper_list, Dumper=Dumper, stream=f)
progress.update()
with open("{}/volumes.yaml".format(outdir), "w") as f:
yaml.dump(volumes, Dumper=Dumper, stream=f)
progress.update(5)
with open("{}/venues.yaml".format(outdir), "w") as f:
yaml.dump(venues, Dumper=Dumper, stream=f)
progress.update()
with open(f"{outdir}/events.yaml", "w") as f:
yaml.dump(events, Dumper=Dumper, stream=f)
progress.update()
with open("{}/sigs.yaml".format(outdir), "w") as f:
yaml.dump(sigs, Dumper=Dumper, stream=f)
progress.update()
for first_letter, people_list in people.items():
with open("{}/people/{}.yaml".format(outdir, first_letter), "w") as f:
yaml.dump(people_list, Dumper=Dumper, stream=f)
progress.update()
progress.close()
if __name__ == "__main__":
args = docopt(__doc__)
scriptdir = os.path.dirname(os.path.abspath(__file__))
if "{scriptdir}" in args["--importdir"]:
args["--importdir"] = os.path.abspath(
args["--importdir"].format(scriptdir=scriptdir)
)
if "{scriptdir}" in args["--exportdir"]:
args["--exportdir"] = os.path.abspath(
args["--exportdir"].format(scriptdir=scriptdir)
)
log_level = log.DEBUG if args["--debug"] else log.INFO
log.basicConfig(format="%(levelname)-8s %(message)s", level=log_level)
tracker = SeverityTracker()
log.getLogger().addHandler(tracker)
log.info("Reading the Anthology data...")
anthology = Anthology(importdir=args["--importdir"])
log.info("Exporting to YAML...")
export_anthology(
anthology, args["--exportdir"], clean=args["--clean"], dryrun=args["--dry-run"]
)
if tracker.highest >= log.ERROR:
exit(1)