-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpage-generator.py
371 lines (310 loc) · 13.5 KB
/
webpage-generator.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
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
364
365
366
367
368
369
370
371
import json
import time
import math
import dominate
from dominate.tags import *
from abc import ABCMeta, abstractmethod
class HtmlWidget:
__metaclass__ = ABCMeta
@abstractmethod
def get_html(self):
pass
class WebpageGenerator:
headings = []
def __init__(self):
self.generate_website()
def generate_website(self):
with html() as page_html:
self.get_headers("data/headers.json")
self.get_body()
write_to_file(page_html.render())
print(page_html)
def get_headers(self, file):
header_data = read_json_file(file)
with head() as page_header:
title(header_data['title'])
meta(charset=header_data['charset'])
for content_meta in header_data['content-metas']:
meta(name=content_meta['name'], content=content_meta['content'])
for import_link in header_data['links']:
if import_link['type'] == 'css':
link(href=import_link['source'], rel='stylesheet', type='text/css')
elif import_link['type'] == 'js':
script(src=import_link['source'], type='text/javascript')
elif import_link['type'] == 'font':
link(href=import_link['source'], rel='stylesheet')
elif import_link['type'] == 'icon':
link(href=import_link['source'], rel='icon', type='image/x-icon')
link(href=import_link['source'], rel='shortcut icon', type='image/x-icon')
return page_header
def get_body(self):
container = div(_class="container")
with container.add(div(_class="row")):
self.get_body_content()
self.get_table_of_contents()
return body(container)
def get_body_content(self):
with div(_class="col m12 l10") as content_div:
self.get_about_me("data/about-me.json")
self.get_education("data/education.json")
self.get_experience("data/experience.json")
self.get_skills("data/skills.json")
self.get_projects_section("data/projects.json")
self.get_timeline("data/timeline.json")
return content_div
def get_table_of_contents(self):
toc_div = div(_class="col hide-on-med-and-down l2")
toc_div.add(self.get_table_of_contents_wrapper())
return toc_div
def get_about_me(self, file):
about_me_data = read_json_file(file)
with div(id="about-me", _class="section scrollspy center-align") as about_me_div:
h1("Mohammad Ali Khan")
img(_class="responsive-img circle", src="img/" + about_me_data['picture'], alt="Picture of Ali")
br()
self.get_social_media_icons(about_me_data['social-icons'])
p(about_me_data['description'], _class="flow-text")
self.headings.append("About Me")
return about_me_div
def get_social_media_icons(self, icons):
with div(_id="social-network-icons") as social_media_icons:
for icon in icons:
social_media_icons.add(self.get_social_icon(icon))
return social_media_icons
def get_social_icon(self, icon):
return a(img(_class="responsive-img icon", src="img/" + icon['image'], alt=icon['name']), href=icon['link'])
def get_education(self, file):
data = read_json_file(file)
education_div = self.get_div_and_heading(data['title'], data['icon'])
education_div.add(self.get_educations_div(data['educations']))
return education_div
def get_div_and_heading(self, section_title, icon):
section_div = div(id=section_title.lower(), _class="section scrollspy")
section_div.add(h2(section_title,i(icon, _class="material-icons heading-icon"), _class="section-heading"))
self.headings.append(section_title)
return section_div
def get_educations_div(self, educations):
educations_div = div(_class="educations")
for education in educations:
educations_div.add(self.get_education_instance_div(education))
return educations_div
def get_education_instance_div(self, education):
with div(_class="education-instance") as education_instance_div:
h3(education['education'])
h4(education['degree'] + " - " + education['grade'])
Footnotes(education['footnotes']).get_html()
return education_instance_div
def get_experience(self, file):
data = read_json_file(file)
experience_div = self.get_div_and_heading(data['title'], data['icon'])
experience_div.add(self.get_experiences_div(data['experiences']))
return experience_div
def get_experiences_div(self, experiences):
experiences_div = div(_class="experiences")
count = 0
for experience in experiences:
count += 1
experiences_div.add(self.get_experience_instance_div(experience))
if(count != len(experiences)):
experiences_div.add(div(_class="divider"))
return experiences_div
def get_experience_instance_div(self, experience):
with div(_class="experience-instance") as experience_instance_div:
h3(experience['company'])
h4(experience['position'])
Footnotes(experience['footnotes']).get_html()
return experience_instance_div
def get_skills(self, file):
data = read_json_file(file)
skills_div = self.get_div_and_heading(data['title'], data['icon'])
skills_div.add(self.get_skills_div(data['sections']))
return skills_div
def get_skills_div(self, sections):
skills_div = div(_class="skills")
count = 0
skills_row = ""
skills_column = ""
for skills_section in sections:
if(count % 2 == 0):
skills_row = div(_class="row")
if(count == 2):
skills_column = div(_class="col m12 l8 offset-l2")
else:
skills_column = div(_class="col m12 l6")
skills_column.add(self.get_skills_section_instance(skills_section))
skills_row.add(skills_column)
if(count % 2 == 0):
skills_div.add(skills_row)
count += 1
return skills_div
def get_skills_section_instance(self, skill_section):
with div(_class="skill-section") as skill_section_instance_div:
h4(skill_section['title'])
self.get_skills_section_skill_list(skill_section['ratings'])
return skill_section_instance_div
def get_skills_section_skill_list(self, ratings):
with ul(_class="skill-list") as skill_list:
for rating in ratings:
with li(_class="skill-item"):
self.get_skill_item(rating)
return skill_list
def get_skill_item(self, rating):
with div(_class="row valign-wrapper") as skill_item_div:
with div(_class="col s6 skills left-align"):
h5(rating['skill'])
with div(_class="col s6 ratings centre-align"):
self.get_rating(float(rating['rating']))
return skill_item_div
def get_rating(self, rating):
full_stars = math.floor(rating / 1)
half_stars = math.ceil(rating % 1)
empty_stars = 5 - full_stars - half_stars
with div(_class="rating") as rating_div:
self.get_stars(full_stars, "star")
self.get_stars(half_stars, "star_half")
self.get_stars(empty_stars, "star_border")
return rating_div
def get_stars(self, number, icon):
number = int(number)
stars_div = div(_class="stars")
for index in range(number):
stars_div += i(icon, _class="small material-icons")
return stars_div
def get_projects_section(self, file):
data = read_json_file(file)
project_div = self.get_div_and_heading(data['title'], data['icon'])
project_div.add(self.get_projects(data['projects']))
return project_div
def get_projects(self, projects):
projects_div = div(_class="projects")
count = 0
project_row = ""
for project in projects:
if(count % 2 == 0):
project_row = div(_class="row")
project_column = div(_class="col m12 l6")
project_column.add(ProjectCard(project).get_html())
project_row.add(project_column)
if(count % 2 == 0):
projects_div.add(project_row)
count += 1
return projects_div
def get_timeline(self, file):
data = read_json_file(file)
timeline_div = self.get_div_and_heading(data['title'], data['icon'])
timeline_div.add(p(data['description']))
timeline_div.add(div(id="chart"))
return timeline_div
def get_table_of_contents_wrapper(self):
toc_div = div(_class="toc-wrapper")
toc_div.add(self.get_toc_list(self.headings))
return toc_div
def get_toc_list(self, headings):
toc_list = ul(_class="section table-of-contents")
for heading in headings:
toc_list.add(self.get_toc_item(heading))
return toc_list
def get_toc_item(self, heading):
heading_id = self.get_heading_item_id(heading)
return li(a(heading, href="#"+heading_id))
def get_heading_item_id(self, heading):
return heading.replace(' ', '-').lower()
def write_to_file(text):
file = open("index.html", 'w+')
file.write(text)
def get_meta_tag(name, content):
return "<meta name\"" + name + "\" content=\"" + content + "\" />"
def read_json_file(filename):
return json.loads(open(filename).read())
class ProjectCard(HtmlWidget):
def __init__(self, project):
self.project = project
def get_html(self):
return self.__get_project_card(self.project)
def __get_project_card(self, project):
with div(_class="card hoverable") as project_card:
self.__get_card_activator(project['image'])
self.__get_card_content(project['name'], project['tags'])
self.__get_card_action(project['footnotes'])
self.__get_card_reveal(project['name'], project['description'])
return project_card
def __get_card_activator(self, image):
activator_div = div(_class="card-image waves-effect waves-block waves-light")
activator_div.add(img(src=image, _class="activator"))
return activator_div
def __get_card_content(self, name, tags):
with div(_class="card-content") as card_content_div:
self.__get_card_title_span(name, "more_vert")
self.__get_project_tags(tags)
return card_content_div
def __get_card_title_span(self, name, icon):
with span(_class="card-title activator grey-text text-darken-4") as card_title_span:
b(name)
i(icon, _class="material-icons right")
return card_title_span
def __get_project_tags(self, tags):
with div(_class="project-tags") as project_tags_divs:
for tag in tags:
self.__get_project_tag(tag)
return project_tags_divs
def __get_project_tag(self, tag):
return div(tag['tag'], _class="chip " + tag['type'].lower())
def __get_card_action(self, footnotes):
card_action_div = div(_class="card-action")
card_action_div.add(Footnotes(footnotes).get_html())
return card_action_div
def __get_card_reveal(self, name, description):
with div(_class="card-reveal") as card_reveal_div:
self.__get_card_title_span(name, "close")
p(description)
return card_reveal_div
class Footnotes(HtmlWidget):
def __init__(self, footnotes):
self.footnotes = footnotes
def get_html(self):
return self.__get_footnotes(self.footnotes)
def __get_footnotes(self, footnotes):
with div(_class="flex-list") as footnotes_list_div:
with ul():
for footnote in footnotes:
Footnote(footnote).get_html()
return footnotes_list_div
class Footnote(HtmlWidget):
def __init__(self, footnote):
self.footnote = footnote
def get_html(self):
return self.__get_footnote(self.footnote)
def __get_footnote(self, footnote):
footnote_item = li()
footnote_item.add(self.__get_footnote_icon(footnote['type']))
footnote_item.add(self.__get_footnote_text(footnote))
return footnote_item
def __get_footnote_icon(self, footnote_type):
icons = {
'time' : "date_range",
'location' : "place",
'link' : "link",
'code' : "code",
'documentation' : "insert_drive_file",
'video' : "play_circle_filled"
}
return i(icons[footnote_type], _class="material-icons")
def __get_footnote_text(self, footnote):
if footnote['type'] == 'time':
return self.__get_date(footnote['time'])
elif footnote['type'] == 'location':
return self.__get_location(footnote['location'])
else:
return self.__get_link(footnote[footnote['type']])
def __get_date(self, date):
start_date = time.strptime(date['start'], "%Y-%m-%d")
text = time.strftime("%B %Y", start_date)
if 'end' in date:
end_date = time.strptime(date['end'], "%Y-%m-%d")
text += " to " + time.strftime("%B %Y", end_date)
return text
def __get_location(self, location):
return location['city'] + ", " + location['country']
def __get_link(self, link):
return a(link['title'], href=link['source'])
WebpageGenerator()