-
Notifications
You must be signed in to change notification settings - Fork 0
/
md_parser.py
233 lines (208 loc) · 8.15 KB
/
md_parser.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
from datetime import datetime, timedelta, timezone
from re import compile as re_compile
from statics import Rating, RatingSVG
readme_replace_regex = re_compile(r"<!-- BEGIN INPUT -->[\s\S]*<!-- END INPUT -->")
img_relative_path_in_root = "![](./images/{})"
img_relative_path_in_reviews_sized = '<img src="../../images/{}" width="{}" height="{}">'
class ReviewParser:
"""
parse review issue body
"""
def __init__(self, md: str):
self.data = {}
self.__parse(md)
for item in self.data:
self.data[item] = self.data[item].strip()
self.data["Course Code"] = self.data["Course Code"].replace(" ", "").upper()
self.data["Contents"] = self.data["Contents"].splitlines()
self.data["Rating for Content"] = getattr(
Rating, self.data["Rating for Content"], Rating.A
).value
self.data["Rating for Teaching"] = getattr(
Rating, self.data["Rating for Teaching"], Rating.A
).value
self.data["Rating for Grade"] = getattr(
Rating, self.data["Rating for Grade"], Rating.A
).value
self.data["Rating for Workload"] = getattr(
Rating, self.data["Rating for Workload"], Rating.A
).value
self.data["Rating Overall"] = (
self.data["Rating for Content"]
+ self.data["Rating for Teaching"]
+ self.data["Rating for Grade"]
+ self.data["Rating for Workload"]
) // 4
def __parse(self, md: str):
current_section = None
for line in md.splitlines():
if line.startswith("### "):
current_section = line[4:].strip()
self.data[current_section] = ""
elif current_section == "Contents":
if line.startswith("- [X]"):
self.data[current_section] += line[5:].strip() + "\n"
elif current_section is not None and line and not line.startswith("```"):
self.data[current_section] += line + "\n"
def render(self, title: str, author: str, issue_id: str) -> str:
"""
render the review on review_template.md
:param title: title of the review
:param author: author of the review
:return: markdown string
"""
d = self.data.copy()
d["Date"] = datetime.now(tz=timezone(offset=timedelta(hours=8))).strftime(
"%Y-%m-%d %H:%M on HK Time"
)
d["Author"] = author
d["Contents"] = (
"| "
+ " | ".join(d["Contents"])
+ " |\n"
+ "|"
+ (len(d["Contents"]) * " ---------------- |")
)
d["Rating for Content"] = img_relative_path_in_reviews_sized.format(
RatingSVG[d["Rating for Content"]], 30, 30
)
d["Rating for Teaching"] = img_relative_path_in_reviews_sized.format(
RatingSVG[d["Rating for Teaching"]], 30, 30
)
d["Rating for Grade"] = img_relative_path_in_reviews_sized.format(
RatingSVG[d["Rating for Grade"]], 30, 30
)
d["Rating for Workload"] = img_relative_path_in_reviews_sized.format(
RatingSVG[d["Rating for Workload"]], 30, 30
)
d["Rating Overall"] = img_relative_path_in_reviews_sized.format(
RatingSVG[d["Rating Overall"]], 60, 60
)
d["ISSUE ID"] = issue_id
with open("./reviews/review_template.md", "r") as f:
template = f.read()
return template.format(title=title, **d)
@property
def course_code(self) -> str:
return self.data["Course Code"]
@property
def rating_content(self) -> int:
return self.data["Rating for Content"]
@property
def rating_teaching(self) -> int:
return self.data["Rating for Teaching"]
@property
def rating_grade(self) -> int:
return self.data["Rating for Grade"]
@property
def rating_workload(self) -> int:
return self.data["Rating for Workload"]
@property
def rating_overall(self) -> int:
return self.data["Rating Overall"]
class ReadmeIO:
def __init__(self):
with open("./README.md", "r") as f:
self.readme = f.read()
def write(self, all_courses: dict):
course_content = "<!-- BEGIN INPUT -->\n"
course_content += "| Course | Intro | Rating |\n"
course_content += "| --------- | --------- | --------- |\n"
for course in all_courses:
course_content += "| [{}]({}) | {} | {} |\n".format(
course,
"./reviews/" + course + "/README.md",
all_courses[course]["intro"],
img_relative_path_in_root.format(
RatingSVG[
round(
sum(all_courses[course]["overall_rating_sum"].values())
/ len(all_courses[course]["overall_rating_sum"])
)
]
),
)
course_content += "\n\n<!-- END INPUT -->"
self.readme = readme_replace_regex.sub(course_content, self.readme)
with open("./README.md", "w") as f:
f.write(self.readme)
class CourseReadmeIO:
def __init__(self, course_code: str, course_intro: str):
with open("./reviews/review_readme_template.md", "r") as template:
self.readme = template.read().format(Course=course_code, Intro=course_intro)
with open("./reviews/" + course_code + "/README.md", "w") as f:
f.write(self.readme)
def write(self, course: dict, course_code: str):
# calculate course average rating and save as svg for course->README.md
content_avg_rating = img_relative_path_in_reviews_sized.format(
RatingSVG[
round(
sum(course["content_rating_sum"].values())
/ len(course["content_rating_sum"])
)
],
30,
30,
)
teaching_avg_rating = img_relative_path_in_reviews_sized.format(
RatingSVG[
round(
sum(course["teaching_rating_sum"].values())
/ len(course["teaching_rating_sum"])
)
],
30,
30,
)
grade_avg_rating = img_relative_path_in_reviews_sized.format(
RatingSVG[
round(
sum(course["grade_rating_sum"].values())
/ len(course["grade_rating_sum"])
)
],
30,
30,
)
workload_avg_rating = img_relative_path_in_reviews_sized.format(
RatingSVG[
round(
sum(course["workload_rating_sum"].values())
/ len(course["workload_rating_sum"])
)
],
30,
30,
)
overall_avg_rating = img_relative_path_in_reviews_sized.format(
RatingSVG[
round(
sum(course["overall_rating_sum"].values())
/ len(course["overall_rating_sum"])
)
],
50,
50,
)
# construct content
course_content = "<!-- BEGIN INPUT -->\n\n"
course_content += "## Summary\n\n"
course_content += "| Content | Teaching | Grade | Workload |\n"
course_content += "| --------- | --------- | --------- | --------- |\n"
course_content += "| {} | {} | {} | {} |\n\n".format(
content_avg_rating,
teaching_avg_rating,
grade_avg_rating,
workload_avg_rating,
)
course_content += "***Overall Rating:***\n\n{}\n\n".format(overall_avg_rating)
course_content += "## Reviews\n\n"
for issue_id in course["reviews"]:
course_content += "- [{}]({})\n".format(
course["reviews"][issue_id], f"{issue_id}.md"
)
course_content += "\n<!-- END INPUT -->"
# write to file
self.readme = readme_replace_regex.sub(course_content, self.readme)
with open("./reviews/" + course_code + "/README.md", "w") as f:
f.write(self.readme)