forked from codewithsadee/vcard-personal-portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
453 lines (381 loc) · 16 KB
/
generate.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import datetime
import time
import ruamel.yaml
import minify_html
import markdown
import requests
print("Minifying JS...")
INFO_TEMPLATE = """
<li class="contact-item">
<div class="icon-box">
<ion-icon name="ICON"></ion-icon>
</div>
<div class="contact-info">
<p class="contact-title">NAME</p>
<address>VALUE</address>
</div>
</li>
"""
INFO_TEMPLATE_VARIABLES = ["ICON", "NAME", "VALUE"]
SOCIAL_TEMPLATE = """
<li class="social-item">
<a href="URL" class="social-link">
<ion-icon name="ICON"></ion-icon>
</a>
</li>
"""
SOCIAL_TEMPLATE_VARIABLES = ["URL", "ICON"]
PAGE_TEMPLATE_NAVBAR_HOMEPAGE = """
<li class="navbar-item">
<button class="navbar-link active" data-nav-link>NAME</button>
</li>
"""
PAGE_VARIABLES = ["NAME"]
PAGE_TEMPLATE_NAVBAR = """<li class="navbar-item">
<button class="navbar-link" data-nav-link>NAME</button>
</li>
"""
ARTICLE_TEMPLATE = """<article class="LOWER_NAME" data-page="LOWER_NAME">
<header>
<h2 class="h2 article-title">NAME</h2>
</header>
SECTIONS
</article>
"""
ARTICLE_VARIABLES = ["NAME"]
TIMELINE_TEMPLATE = """<section class="timeline">
header_goes_here
<ol class="timeline-list">
TIMELINE
</ol>
</section>
"""
TIMELINE_TEMPLATE_VARIABLES = ["TIMELINE_NAME", "TIMELINE"]
TIMELINE_ITEM_TEMPLATE = """<li class="timeline-item">
<h4 class="h4 timeline-item-title">NAME</h4>
<span>TIME_PERIOD</span>
<p class="timeline-text">DESCRIPTION</p>
</li>
"""
TIMELINE_ITEM_VARIABLES = ["NAME", "TIME_PERIOD", "DESCRIPTION"]
OPTIONAL_HEADER_TEMPLATE = """<div class="title-wrapper-default">
<div class="icon-box">
<ion-icon name="ICON"></ion-icon>
</div>
<h3 class="h3">NAME</h3>
</div>
"""
SKILLS_TEMPLATE = """<section class="skill">
header_goes_here
<ul class="skills-list content-card">SKILLS</ul>
</section>"""
SKILLS_VARIABLES = ["NAME", "SKILLS"]
SKILLS_ITEM_TEMPLATE = """<li class="skills-item">
<div class="title-wrapper">
<h5 class="h5">NAME</h5>
<data value="AMOUNT">AMOUNT%</data>
</div>
<div class="skill-progress-bg">
<div class="skill-progress-fill" style="width: AMOUNT%;"></div>
</div>
</li>
"""
SKILLS_ITEM_VARIABLES = ["NAME", "AMOUNT"]
CONTACT_FORM_TEMPLATE = """<section class="contact-form">
header_goes_here
<form action="FORMSPREE_URL" class="form" method="POST" data-form>
<div class="input-wrapper">
<input type="text" name="fullname" class="form-input" placeholder="Full Name" required data-form-input>
<input type="email" name="email" class="form-input" placeholder="Email Address" required data-form-input>
</div>
<textarea name="message" class="form-input" placeholder="Your Message" required data-form-input></textarea>
<button class="form-btn" type="submit" disabled data-form-btn>
<ion-icon name="paper-plane"></ion-icon>
<span>Send Message</span>
</button>
</form>
</section>"""
CONTACT_FORM_TEMPLATE_VARIABLES = ["FORMSPREE_URL"]
PROJECT_ITEM_TEMPLATE = """<li class="project-item active" data-filter-item data-category="CATEGORY">
<a href="LINK">
<figure class="project-img">
<div class="project-item-icon-box">
<ion-icon name="eye-outline"></ion-icon>
</div>
<img src="PROJECT_IMAGE" alt="PROJECT_NAME" loading="lazy">
</figure>
<h3 class="project-title">PROJECT_NAME</h3>
<p class="project-category">DESCRIPTION</p>
</a>
</li>
"""
PROJECT_TEMPLATE = """
<section class="projects">
header_goes_here
<ul class="filter-list">
<li class="filter-item">
<button class="active" data-filter-btn>All</button>
</li>
FILTER_ITEMS
</ul>
<div class="filter-select-box">
<button class="filter-select" data-select>
<div class="select-value" data-select-value>Select category</div>
<div class="select-icon">
<ion-icon name="chevron-down"></ion-icon>
</div>
</button>
<ul class="select-list">
<li class="select-item">
<button class="active" data-filter-btn>All</button>
</li>
SELECT_ITEMS
</ul>
</div>
<ul class="project-list">
PROJECT_ITEMS
</ul>
</section>
</ul>
"""
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<script>if (location.href !== document.URL.toLowerCase()) {
location.href = document.URL.toLowerCase();
}</script>
<!-- Google tag (gtag.js) -->
<script defer src="https://www.googletagmanager.com/gtag/js?id=GTAG_IDENTIFICATION"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GTAG_IDENTIFICATION');
</script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="configuration.description">
<title>configuration.name - @configuration.username</title>
<link rel="shortcut icon" href="configuration.favicon" type="image/x-icon">
<link rel="stylesheet" href="/assets/css/style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preload" href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" as="style" onload="this.onload=null;this.rel='stylesheet'">
<meta name="google-site-verification" content="gLWjWyzJC6D2D_6Pe9HWCRkaWL_rKtxRf05_Dhiu2is" />
</head>
<body>
<main>
<aside class="sidebar" data-sidebar>
<div class="sidebar-info">
put_back_to_home_link_here
<figure class="avatar-box">
<img src="configuration.sidebar.image" alt="configuration.name" width="80">
</figure>
<div class="info-content">
<h1 class="name" title="configuration.name"><a class="colorlink" href="https://github.com/configuration.username"> configuration.name</a></h1>
<p class="title">configuration.sidebar.title</p>
</div>
<button class="info_more-btn" data-sidebar-btn>
<span>More Information</span>
<ion-icon name="chevron-down"></ion-icon>
</button>
</div>
<div class="sidebar-info_more">
<div class="separator"></div>
<ul class="contacts-list">
generated_info_panel
</ul>
<div class="separator"></div>
<ul class="social-list">
generated_social_panel
</ul>
</div>
</aside>
<div class="main-content">
<nav class="navbar">
<ul class="navbar-list">
generated_navbar
</ul>
</nav>
generated_articles
</div>
</main>
<script defer src="/assets/js/minified_script.js"></script>
<script defer type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js"></script>
<script defer nomodule src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"></script>
</body>
</html>"""
TEXT_TEMPLATE = """
<section class="about-text">
TEXT
</section>
"""
FILTER_ITEM_TEMPLATE = """
<li class="filter-item">
<button data-filter-btn>CATEGORY</button>
</li>
"""
SELECT_ITEM_TEMPLATE = """
<li class="select-item">
<button data-filter-btn>CATEGORY</button>
</li>
"""
tz = datetime.timezone.utc
ft = "%Y-%m-%dT%H:%M:%SZ"
t = datetime.datetime.now(tz=tz).strftime(ft)
SITEMAP_ITEM_TEMPLATE = f"""<url>
<loc>PAGE</loc>
<lastmod>{t}</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
"""
SITEMAP_TEMPLATE = """<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
SITEMAP
</urlset>"""
def markdown_text_parsing(txt):
o = markdown.markdown(txt)
# print(o)
return o.replace('href', "style=\"display: inline\" class=\"colorlink\" href")
def generate_timeline_item(variables):
# for item in variables:
# if item != "TIME_PERIOD":
variables["description"] = markdown_text_parsing(variables["description"]).replace("<p>", "").replace("</p>", "")
return TIMELINE_ITEM_TEMPLATE.replace("NAME", variables['name']).replace("TIME_PERIOD",
variables['time_period']).replace(
"DESCRIPTION", variables['description'])
def generate_skills_item(variables):
return SKILLS_ITEM_TEMPLATE.replace("NAME", variables['name']).replace("AMOUNT", variables['level'])
def generate_text(variables):
ret = TEXT_TEMPLATE.replace("TEXT", markdown_text_parsing(variables["text"]["text"]).replace("\n", ""))
# print(ret)
return ret
def generate_header(variables):
return OPTIONAL_HEADER_TEMPLATE.replace("NAME", markdown_text_parsing(variables["name"]).replace("<p>", "")).replace("ICON",
variables["icon"])
def generate_contact(variables):
return CONTACT_FORM_TEMPLATE.replace("FORMSPREE_URL", variables["contact"]["formspree_url"])
def generate_navbar_item(variables, is_first=True):
if is_first:
return PAGE_TEMPLATE_NAVBAR_HOMEPAGE.replace("NAME", variables["name"])
return PAGE_TEMPLATE_NAVBAR.replace("NAME", variables['name'])
def generate_timeline(variables):
variables = variables['timeline']
timeline_internal = ''
for timeline_item_raw in variables['timeline']:
timeline_internal += generate_timeline_item(timeline_item_raw)
return TIMELINE_TEMPLATE.replace("TIMELINE", timeline_internal)
def generate_skills(variables):
variables = variables['skills']
skills_internal = ''
for skills_item_raw in variables['skills']:
skills_internal += generate_skills_item(skills_item_raw)
return SKILLS_TEMPLATE.replace("SKILLS", skills_internal)
def generate_project_item(variables):
return (PROJECT_ITEM_TEMPLATE.replace("PROJECT_NAME", variables["name"])
.replace("PROJECT_IMAGE", variables["image"])
.replace("DESCRIPTION", variables["description"])
.replace("LINK", variables["link"])
.replace("CATEGORY", variables["tag"].lower()))
def generate_projects(variables):
variables = variables['projects']
projects_internal = ''
for project_item_raw in variables['projects']:
projects_internal += generate_project_item(project_item_raw)
p = PROJECT_TEMPLATE.replace("PROJECT_ITEMS", projects_internal)
select_items = ''
filter_items = ''
for tag in variables['tags']:
select_items += SELECT_ITEM_TEMPLATE.replace("CATEGORY", tag)
filter_items += FILTER_ITEM_TEMPLATE.replace("CATEGORY", tag)
p = p.replace("FILTER_ITEMS", filter_items).replace("SELECT_ITEMS", select_items)
return p
def generate_article(variables, is_first=True):
generator_map = {"timeline": generate_timeline, "skills": generate_skills, "text": generate_text,
"projects": generate_projects, "contact": generate_contact}
article = ARTICLE_TEMPLATE.replace("LOWER_NAME", variables['name'].lower()).replace("NAME", variables['name'])
sections = ""
for section in variables["elements"]:
section_data = generator_map[list(section)[0]](section)
section = section[list(section)[0]]
header = ""
if "header" in section.keys():
header = generate_header(section['header'])
section_data = section_data.replace("header_goes_here", header)
sections += section_data
if is_first:
article = article.replace("\" data-page=", " active\" data-page=")
return article.replace("SECTIONS", sections)
def generate_html(variables):
html = (HTML_TEMPLATE.replace("configuration.favicon", variables['favicon']).replace("configuration.name",
variables["name"]).replace(
"configuration.username", variables["username"]).replace("configuration.sidebar.image",
variables["sidebar"]["image"]).replace(
"configuration.sidebar.title", variables["sidebar"]["title"])
.replace("configuration.description", variables["meta_description"]))
articles = ""
navbar = ""
for i, article in enumerate(variables["pages"]):
articles += generate_article(variables["pages"][article], i == variables["starting_page_index"])
navbar += generate_navbar_item(variables["pages"][article], i == variables["starting_page_index"])
info_panels = ""
for info in variables["sidebar"]["info_panels"]:
info_panels += (INFO_TEMPLATE.replace("ICON", info["icon"])
.replace("NAME", info["title"])
.replace("VALUE", markdown_text_parsing(info["value"])))
social_panels = ""
for social in variables["sidebar"]["social_panels"]:
social_panels += (SOCIAL_TEMPLATE.replace("ICON", social["icon"])
.replace("URL", social["url"]))
back_to_home = ""
if variables["sidebar"]["back_to_home"]:
back_to_home = (f'<a class="colorlink" style="text-align: center"'
f' href="https://{variables["username"]}.github.io"><ion-icon name="home" size="large"></ion-icon></a>')
html = (html.replace("generated_articles", articles).replace("generated_navbar", navbar)
.replace("generated_info_panel", info_panels).replace("generated_social_panel", social_panels)
.replace("put_back_to_home_link_here", back_to_home).replace("GTAG_IDENTIFICATION",
variables["analytics"]["tag_manager_id"]))
return html
def generate_sitemap(links):
sitemap = ''
for link in links:
sitemap += SITEMAP_ITEM_TEMPLATE.replace("PAGE", link)
return SITEMAP_TEMPLATE.replace("SITEMAP", sitemap)
js = open("assets/js/script.js")
resp = requests.post("https://www.toptal.com/developers/javascript-minifier/api/raw", data={"input": js.read()})
minified = open("assets/js/minified_script.js", "w")
minified.truncate(0)
minified.write(resp.text)
minified.close()
js.close()
print("Starting...")
start_time = time.time()
y = ruamel.yaml.YAML()
jobs = y.load(open("generate_execute.yaml"))["jobs"]
links = []
for i, job_key in enumerate(jobs.keys()):
print("executing job", job_key, f"({i+1}/{len(jobs)})")
job = jobs[job_key]
data = y.load(open(job["in"]))
if data["use_sitemap"]:
for lk in data["pages"]:
if data['path']:
url = f"https://{data['username']}.github.io/{data['path']}#{lk}"
else:
url = f"https://{data['username']}.github.io#{lk}"
url = url.replace("//", "/")
url = url.replace("https:/", "https://")
links.append(url)
f = open(job["out"], "w")
f.truncate(0)
f.write(minify_html.minify(generate_html(data), minify_js=True, remove_processing_instructions=True))
f.close()
print(f"Completed {len(jobs)} jobs in {round((time.time() - start_time) * 100, 2)}ms.")
print("Writing sitemap...")
f = open("sitemap.xml", "w")
f.truncate(0)
f.write(generate_sitemap(links))
f.close()
print("All tasks complete. :)")