forked from cmu-vision/cmu-vision.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_course_page.py
82 lines (64 loc) · 2.71 KB
/
gen_course_page.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
import os
import glob
import random
MAX_PROJECTS = 24
CURRENT_YEAR = 2022
def read_file(fpath):
with open(fpath, 'r') as f:
content = f.read()
return content
def get_txtfile_ids(folder):
txtfiles = glob.glob('{}/*.txt'.format(folder))
ids = [os.path.basename(txtfile)[:-4] for txtfile in txtfiles]
return ids
# <img class="paper static" title="paper" src="src.png" />
def parse_course_info(course_id, course_folder):
elems = {}
keys = []
fpath = os.path.join(course_folder, course_id + '.txt')
with open(fpath, 'r') as f:
for ln in f.readlines():
ln_items = ln.split('::')
if len(ln_items) != 2:
continue
key, val = ln_items[0], ln_items[1]
key = key.rstrip().lstrip()
val = val.rstrip().lstrip()
elems[key] = val
keys.append(key)
if 'img' not in keys:
elems['img'] = 'courses/16385.jpeg'
return elems, keys
def get_course_html_string(id, elems, keys):
comment_str = '<!--------------------------------------------------------------------------->'
course_str = ''
course_str += '\n' + comment_str + '\n'
course_str += '<table class="course" id="{}">\n'.format(id)
course_str += '<tr>\n<td>\n'
course_str += '<div class="cropTeaser" style="background-image: url(\'{}\');">\n</div>\n'.format(elems['img'])
course_str += '</td>\n<td class="courseDescription">\n'
course_str += '<h2 class="courseTitle"> {} : {} </h2> \n {} \n'.format(elems['number'], elems['title'], elems['description'])
if 'url' in keys:
course_str += '<p class="offeringList"><a href="{}">Course Website</a></p>\n'.format(elems['url'])
course_str += '</td>\n</tr>\n</table>\n'
## Meta fields
# add some javascript here if needed using the id for the added element
course_str += comment_str + '\n'
return course_str
if __name__ == '__main__':
page_string = read_file('./courses_base.html')
###########################################################################
###########################################################################
folder = './courses'
courses_string = ''
ids = get_txtfile_ids(folder)
ids.sort()
for id in ids:
elems, keys = parse_course_info(id, folder)
courses_string += get_course_html_string(id, elems, keys)
page_string = page_string.replace('<!-- autogen courses -->', '<!-- autogen courses -->\n' + courses_string)
###########################################################################
###########################################################################
fpath = './courses.html'
with open(fpath, 'w') as f:
f.write(page_string)