-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconvert.py
More file actions
363 lines (302 loc) · 12.1 KB
/
convert.py
File metadata and controls
363 lines (302 loc) · 12.1 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "beautifulsoup4",
# "pydantic",
# "pyyaml",
# "pybtex",
# ]
# ///
from __future__ import annotations
from bs4 import BeautifulSoup
from pathlib import Path
from pydantic import BaseModel
from typing import Iterator
import base64
import yaml
import datetime
import pybtex.database
class NewsItem(BaseModel):
title: str
date: datetime.date
content: str
def save(self, directory: Path) -> None:
directory.mkdir(parents=True, exist_ok=True)
filename = (
self.date.strftime("%Y-%m")
+ "-"
+ self.title.replace(" ", "-")[:32].lower()
)
if (directory / f"{filename}.md").exists():
raise FileExistsError(f"{directory / f'{filename}.md'} already exists")
data = self.model_dump(
exclude_none=True,
)
data.pop("content", None) # Exclude content from YAML
(directory / f"{filename}.md").write_text(f"""---
{yaml.dump(data, sort_keys=False)}---
{self.content}
""")
class News(list[NewsItem]):
"""
<div class="row news-item">
<div class="col-sm-2 text-small-caps">
<span class="badge badge-info">Aug. 2025</span>
</div>
<div class="col-sm-10">
Sangdon Park, Minjae Gwon and Minjae Lee has won <a href="https://aicyberchallenge.com/">DARPA AIxCC</a> as a part of team Atlanta!
</div>
</div>"""
@staticmethod
def from_index_html_string(html_string: str) -> Iterator[NewsItem]:
soup = BeautifulSoup(html_string, "html.parser")
divs = soup.select("div.news-item")
for div in divs:
date_str: str = div.select_one("div.col-sm-2").span.contents[0].strip()
try:
date = datetime.datetime.strptime(date_str, "%b. %Y").date()
except ValueError:
continue
content: str = div.select_one("div.col-sm-10").decode_contents().strip()
title = div.select_one("div.col-sm-10").text.strip()
yield NewsItem(
title=title,
date=date,
content=content,
)
class Profile(BaseModel):
name: str
role: str
email: str
webpage: str | None = None
category: str
image_base64_url: str
def save(self, directory: Path) -> None:
directory.mkdir(parents=True, exist_ok=True)
filename = self.category + "-" + self.name.replace(" ", "-").lower()
if (directory / f"{filename}.md").exists():
raise FileExistsError(f"{directory / f'{filename}.md'} already exists")
data = self.model_dump(
exclude_none=True,
)
data.pop("image_base64_url", None) # Exclude image data from YAML
if "jpg" in self.image_base64_url or "jpeg" in self.image_base64_url:
image_extension = "jpg"
elif "png" in self.image_base64_url:
image_extension = "png"
else:
raise ValueError("Unsupported image format in base64 URL")
image_data = self.image_base64_url.split(",")[1]
(directory / f"{filename}.{image_extension}").write_bytes(
base64.urlsafe_b64decode(image_data)
)
data["image"] = f"{filename}.{image_extension}"
(directory / f"{filename}.md").write_text(f"""---
{yaml.dump(data, sort_keys=False)}---
""")
class Professor(Profile): ...
class Professors(list[Professor]):
@staticmethod
def from_people_html_string(html_string: str) -> Iterator[Profile]:
soup = BeautifulSoup(html_string, "html.parser")
divs = soup.select("div.profile-info-prof")
for div in divs:
name: str = div.h4.contents[0].strip()
webpage: str = div.h4.a["href"] if div.h4.a else None
assert webpage is not None
role: str = div.select_one("p.sub-text").contents[0].strip()
email: str = (
div.select_one("p.sub-text").contents[2].strip().replace(" (at) ", "@")
)
img_tag = div.find_previous_sibling("div").img
assert img_tag is not None
image_base64_url = _base64_url_from_img_tag(img_tag)
yield Profile(
name=name,
role=role,
email=email,
webpage=webpage,
category="advisor",
image_base64_url=image_base64_url,
)
class Student(Profile):
enrollment_year: int
graduation_year: int | None = None
areas_of_interest: list[str]
class Students(list[Student]):
@staticmethod
def from_people_html_string(html_string: str) -> Iterator[Student]:
soup = BeautifulSoup(html_string, "html.parser")
divs = soup.select("div.profile-info-stud")
for div in divs:
course_info: str = div.select_one("p.sub-text-course").contents[0].strip()
if "Administrative Officer" in course_info:
continue
enrollment_year = int("20" + course_info.split("'")[1].split(" ")[0])
if "Combined" in course_info:
degree = "Ph.D./M.S."
elif "Ph.D." in course_info:
degree = "Ph.D."
elif "Master" in course_info:
degree = "M.S."
name: str = div.h4.contents[0].strip()
webpage: str = div.h4.a["href"] if div.h4.a else None
sub_text_contents = [
content.strip()
for content in div.select_one("p.sub-text").stripped_strings
if content.strip() and content.strip() != "<br/>"
]
email: str = sub_text_contents[0].replace(" (at) ", "@")
areas_of_interest: list[str] = (
[area.strip() for area in sub_text_contents[1].split(",")]
if len(sub_text_contents) > 1
else []
)
img_tag = div.find_previous_sibling("div").img
assert img_tag is not None
image_base64_url = _base64_url_from_img_tag(img_tag)
yield Student(
name=name,
role=degree,
email=email,
webpage=webpage,
category="student",
image_base64_url=image_base64_url,
enrollment_year=enrollment_year,
areas_of_interest=areas_of_interest,
)
class Alumni(Profile):
enrollment_year: int = None
graduation_year: int
current_position: str | None = None
class Alumnis(list[Alumni]):
"""
<div class="col-sm-2">
<div class="profile-image-alumni">
<img src="img/bckoh_profile.jpg" alt="" />
</div>
<div class="profile-info-alumni">
<p class="sub-text-course">'22 MS, Nalbi</p>
<h5 class="name-alumni">ByungChan Ko</h5>
<p class="sub-text">
kbc6723 (at) postech.ac.kr <br/>
</p>
</div>
</div>
"""
@staticmethod
def from_people_html_string(html_string: str) -> Iterator[Alumni]:
soup = BeautifulSoup(html_string, "html.parser")
divs = soup.select("div.profile-info-alumni")
for div in divs:
course_info: str = div.select_one("p.sub-text-course").contents[0].strip()
graduation_year = int("20" + course_info.split("'")[1].split(" ")[0])
if "MS" in course_info:
degree = "M.S."
elif "Ph.D." in course_info:
degree = "Ph.D."
name: str = div.h5.contents[0].strip()
webpage: str = div.h5.a["href"] if div.h5.a else None
sub_text_contents = [
content.strip()
for content in div.select_one("p.sub-text").stripped_strings
if content.strip() and content.strip() != "<br/>"
]
email: str = sub_text_contents[0].replace(" (at) ", "@")
current_position: str | None = (
sub_text_contents[1] if len(sub_text_contents) > 1 else None
)
img_tag = div.find_previous_sibling("div").img
assert img_tag is not None
image_base64_url = _base64_url_from_img_tag(img_tag)
yield Alumni(
name=name,
role=degree,
email=email,
webpage=webpage,
category="alumni",
image_base64_url=image_base64_url,
graduation_year=graduation_year,
current_position=current_position,
)
class Officer(Profile): ...
class Officers(list[Officer]):
@staticmethod
def from_people_html_string(html_string: str) -> Iterator[Officer]:
soup = BeautifulSoup(html_string, "html.parser")
divs = soup.select("div.profile-info-stud")
for div in divs:
course_info: str = div.select_one("p.sub-text-course").contents[0].strip()
if "Administrative Officer" not in course_info:
continue
role = "Administrative Officer"
name: str = div.h4.contents[0].strip()
webpage: str = div.h4.a["href"] if div.h4.a else None
sub_text_contents = [
content.strip()
for content in div.select_one("p.sub-text").stripped_strings
if content.strip() and content.strip() != "<br/>"
]
email: str = sub_text_contents[0].replace(" (at) ", "@")
img_tag = div.find_previous_sibling("div").img
assert img_tag is not None
image_base64_url = _base64_url_from_img_tag(img_tag)
yield Officer(
name=name,
role=role,
email=email,
webpage=webpage,
category="officer",
image_base64_url=image_base64_url,
)
class Publication(BaseModel):
bibtex: str
title: str
class Publications(list[Publication]):
@staticmethod
def from_bibtex_file(bibtext_string: str) -> Iterator[Publication]:
bib_data = pybtex.database.parse_string(bibtext_string, "bibtex")
for entry_key, entry in bib_data.entries.items():
title = entry.fields.get("title", "No Title")
yield Publication(bibtex=str(entry.to_string("bibtex")), title=title)
def save(self, directory: Path) -> None:
directory.mkdir(parents=True, exist_ok=True)
# escape newlines in bibtex for TSV format
(directory / "publications.tsv").write_text(
"title\tbibtex\n"
+ "\n".join([f"{pub.title}\t{pub.bibtex.replace(chr(10), chr(32))}" for pub in self])
)
def _base64_url_from_img_tag(
img_tag, base_directory: Path = Path(__file__).parent
) -> str:
img_path = base_directory / img_tag["src"]
img_data = img_path.read_bytes()
img_base64 = base64.urlsafe_b64encode(img_data).decode("utf-8")
if img_path.suffix.lower() in [".jpg", ".jpeg"]:
return f"data:image/jpeg;base64,{img_base64}"
elif img_path.suffix.lower() == ".png":
return f"data:image/png;base64,{img_base64}"
else:
raise ValueError(f"Unsupported image format: {img_path.suffix}")
def export_profiles():
people_html_string = (Path(__file__).parent / "people.html").read_text()
professors = Professors.from_people_html_string(people_html_string)
students = Students.from_people_html_string(people_html_string)
alumnis = Alumnis.from_people_html_string(people_html_string)
officers = Officers.from_people_html_string(people_html_string)
for person in list(professors) + list(students) + list(alumnis) + list(officers):
person.save(Path(__file__).parent / "people")
def export_news():
index_html_string = (Path(__file__).parent / "home.html").read_text()
news_items = News.from_index_html_string(index_html_string)
news = News(news_items)
for item in news:
item.save(Path(__file__).parent / "news")
def export_publications():
bibtex_string = (Path(__file__).parent / "pub.bib").read_text()
publications = Publications(Publications.from_bibtex_file(bibtex_string))
publications.save(Path(__file__).parent)
if __name__ == "__main__":
export_publications()
export_news()
export_profiles()