-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_publications_resources.py
112 lines (99 loc) · 4.03 KB
/
update_publications_resources.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
#!/usr/bin/env python
import sys
from datetime import datetime
from pathlib import Path
import typing as tp
import pandas as pd
PUB_FORMAT = """<li>
<p>
{authors}.
<br><strong>{title}</strong>.
<br>
{journal} ({year}).
<a rel="datacite:doi" href="http://dx.doi.org/{doi}">
doi:{doi}</a>
<a style="float:right">
<span
data-badge-type="2"
data-doi="{doi}"
data-hide-no-mentions="true"
class="altmetric-embed"></span>
<span
class="__dimensions_badge_embed__"
data-doi="{doi}"
data-legend="hover-right"
data-style="small_rectangle"
style='display: inline-block;'></span>
</a>
<br>{resources}
</p>
</li>"""
RESOURCE_FORMAT = """<a href="{resource_link}">
<button type="button" class="btn btn-default btn-sm">
<span
class="{resource_glypt}"
aria-hidden="true"></span> {resource_type} </button></a>"""
# columns: authors,title,journal,year,doi,publication_type
PUBS_CSV = Path("publications.csv")
# columns: doi,resource_type,resource_link
RESOURCES_CSV = Path("publication_resources.csv")
INPUT_DIR = Path(".")
input_file = "_index.md"
OUTPUT_DIR = Path(".")
output_file = "index.md"
DATE = datetime.now().isoformat().split("T")[0]
INDENT = " "
AUTHOR_NAME = "André F. Rendeiro"
# A mapping between resource types and the HTML class with its glypt:
glypts = {
"PDF": "glyphicon glyphicon-file",
"Preprint": "glyphicon glyphicon-file",
"Data": "glyphicon glyphicon-hdd",
"Code": "fab fa-github",
"Notebook": "fab book-open",
"Press": "glyphicon glyphicon-text-color",
}
def main() -> int:
pubs = pd.read_csv(PUBS_CSV, comment="#").query("publication_type != 'unpublished'")
resources = pd.read_csv(RESOURCES_CSV, index_col=0, comment="#")
pub_list: tp.Dict[str, tp.List[str]] = dict()
for pub_type in ["journal", "opinion", "review", "preprint"]:
pub_list[pub_type] = list()
for _, pub in pubs.query("publication_type == @pub_type").iterrows():
if pub['doi'] in resources.index:
res = resources.loc[[pub["doi"]]]
_res = list()
for i, (_, r) in enumerate(res.iterrows()):
if r.isnull().all():
continue
r["resource_glypt"] = glypts[r["resource_type"]]
_res.append(
("\n" if i == 0 else "")
+ " "
+ RESOURCE_FORMAT.format(**r.to_dict())
)
pub["resources"] = "\n".join(_res)
else:
pub["resources"] = ""
p = PUB_FORMAT.format(**pub.to_dict())
pub_list[pub_type].append(" " + p)
with open(INPUT_DIR / input_file, "r") as handle:
content = (
handle.read()
.replace("{{preprints_go_here}}", "\n".join(pub_list["preprint"]))
.replace("{{opinions_go_here}}", "\n".join(pub_list["opinion"]))
.replace("{{reviews_go_here}}", "\n".join(pub_list["review"]))
.replace("{{publications_go_here}}", "\n".join(pub_list["journal"]))
.replace("{{current_date}}", DATE)
)
content = content.replace(AUTHOR_NAME, "<u>" + AUTHOR_NAME + "</u>").replace(
r"$^\Omega$", "<sup>Ω</sup>"
)
with open(OUTPUT_DIR / output_file, "w") as handle:
handle.write(content)
return 0
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
sys.exit(1)