-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_update_publication.py
154 lines (112 loc) · 3.74 KB
/
add_update_publication.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
import argparse
import json
import os
from io import StringIO
from textwrap import dedent
from ruamel.yaml import YAML
from . import save_url_image, parse_issue_body, write_content_to_file
def front_matters_from_dict(d):
# Convert to dict
d = json.loads(json.dumps(d))
file_object = StringIO()
yaml = YAML()
yaml.preserve_quotes = True
yaml.dump(d, file_object)
front_matter = file_object.getvalue()
return front_matter
def front_matters_to_dict(front_matter):
file_object = StringIO(front_matter)
yaml = YAML()
yaml.preserve_quotes = True
d = yaml.load(file_object)
return d
def get_filename(parsed):
return "-".join([str(parsed[k]) for k in ["year", "month", "day", "shorthand"]]) + ".md"
def preprocess_parsed(parsed, keys_removed):
"""
Removes any key with a value of "_No response_" or in `keys_removed`.
"""
# First need to add some keys
parsed["categories"] = "Publications"
# Sanitize some keys
parsed["shorthand"] = parsed["shorthand"].replace("/", "-")
# Then, modify some keys
if parsed["tags"] != "_No response_":
parsed["tags"] = [x.strip() for x in parsed["tags"].split(",")]
if parsed["abstract"] == "_No response_":
parsed["abstract"] = "_Unavailable_"
# Then, remove keys
return {
k: v
for k, v in parsed.items()
if v != "_No response_" and k not in keys_removed
}
def generate_publication_post(parsed):
"""
Format the parsed content into a string.
"""
d = preprocess_parsed(
parsed, keys_removed=["year", "month", "day", "shorthand", "abstract", "action"]
)
front_matter = front_matters_from_dict(d)
top = dedent(f"---\n{front_matter}\n---\n")
bottom = dedent(
"""
*{{ page.names }}*
**{{ page.venue }}**
{% include display-publication-links.html pub=page %}
## Abstract
"""
)
try:
content = top + bottom + str(parsed.get("abstract"))
except TypeError as e:
raise Exception(f"{e}\n{top=}\n{bottom=}\n{parsed=}")
return {
"filename": get_filename(parsed),
"content": content,
}
def update_publication_post(parsed, load_dir="_posts/papers"):
new_data = preprocess_parsed(
parsed, keys_removed=["year", "month", "day", "shorthand", "abstract", "action"]
)
filename = get_filename(parsed)
with open(os.path.join(load_dir, filename), "r") as f:
lines = f.read()
_, front_matter, bottom = lines.split("---", 2)
old_data = front_matters_to_dict(front_matter)
old_data.update(new_data)
front_matter = front_matters_from_dict(old_data)
top = f"---\n{front_matter}\n---\n"
return {
"filename": filename,
"content": top + bottom,
}
def main(parsed, save_dir="_posts/papers", image_dir="assets/images/papers"):
img_path = save_url_image(
fname=parsed["shorthand"],
profile=parsed,
key="thumbnail",
image_dir=image_dir,
size=(600, 600),
crop_center=False
)
if img_path is not None:
parsed["thumbnail"] = img_path
if parsed["action"] == "Add publication":
formatted = generate_publication_post(parsed)
else:
formatted = update_publication_post(parsed, load_dir=save_dir)
write_content_to_file(formatted, save_dir)
return formatted
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--issue_body_file", type=str)
args = parser.parse_args()
if args.issue_body_file is not None:
with open(args.issue_body_file, "r") as f:
issue_body = f.read()
else:
issue_body = os.environ["ISSUE_BODY"]
parsed = parse_issue_body(issue_body)
main(parsed)