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
|
from bs4 import BeautifulSoup
import json
from copy import copy
import markdown_renderer # markdown_renderer.py
PATH_HTML_PROJECT_TEMPLATE = "./templates/project.html"
PATH_PROJECT_INDEX = "./data/projects.json"
PATH_HTML_BLOGPOST_TEMPLATE = "./templates/blogpost.html"
PATH_BLOGPOST_INDEX = "./data/blogs.json"
PATH_HTML_EVENT_TEMPLATE = "./templates/eventdetails.html"
PATH_EVENT_INDEX = "./data/events.json"
PATH_HTML_VIDEO_TEMPLATE = "./templates/video.html"
PATH_VIDEO_INDEX = "./data/videos.json"
PATH_INDEX = "./www/index.html"
PATH_BLOGLIST = "./www/blog/index.html"
HTML_PROJECT_TEMPLATE = """"""
HTML_BLOGPOST_TEMPLATE = """"""
HTML_EVENT_TEMPLATE = """"""
HTML_VIDEO_TEMPLATE = """"""
HTML_INDEX = """"""
HTML_BLOGLIST = """"""
PROJECT_INDEX = {}
BLOGPOST_INDEX = {}
EVENT_INDEX = {}
VIDEO_INDEX = {}
# ## INITIALISATION ## #
with open(PATH_HTML_PROJECT_TEMPLATE) as f:
HTML_PROJECT_TEMPLATE = f.read()
with open(PATH_HTML_BLOGPOST_TEMPLATE) as f:
HTML_BLOGPOST_TEMPLATE = f.read()
with open(PATH_HTML_EVENT_TEMPLATE) as f:
HTML_EVENT_TEMPLATE = f.read()
with open(PATH_HTML_VIDEO_TEMPLATE) as f:
HTML_VIDEO_TEMPLATE = f.read()
with open(PATH_INDEX) as f:
HTML_INDEX = f.read()
with open(PATH_BLOGLIST) as f:
HTML_BLOGLIST = f.read()
with open(PATH_PROJECT_INDEX) as f:
PROJECT_INDEX = json.load(f)
with open(PATH_BLOGPOST_INDEX) as f:
BLOGPOST_INDEX = json.load(f)
with open(PATH_EVENT_INDEX) as f:
EVENT_INDEX = json.load(f)
with open(PATH_VIDEO_INDEX) as f:
VIDEO_INDEX = json.load(f)
for idx in range(len(BLOGPOST_INDEX["posts"])):
metadata_json = BLOGPOST_INDEX["posts"][idx]
with open("." + metadata_json["path"], "r") as f:
source = f.read()
metadata, _ = markdown_renderer.md_to_html(source)
BLOGPOST_INDEX["posts"][idx] = BLOGPOST_INDEX["posts"][idx] | metadata
print(BLOGPOST_INDEX)
# #### #
def render_project_details(project_name: str) -> tuple:
html = HTML_PROJECT_TEMPLATE
source = ""
if project_name not in PROJECT_INDEX:
return (404, "")
with open("." + PROJECT_INDEX[project_name]["path"], "r") as f:
source = f.read()
metadata, rendered_content = markdown_renderer.md_to_html(source)
print(metadata, rendered_content)
html = html.replace("{[{content}]}", rendered_content)
if "logo" in metadata:
html = html.replace("{[{img-src-icon-bottom-bar}]}", metadata["logo"])
soup = BeautifulSoup(html, 'html.parser')
soup.title.string = soup.title.string.replace("{[{title}]}", metadata["title"])
soup.find("section", "banner-section")["id"] = metadata["bannerId"]
for platform in ["win", "mac", "lin", "web"]:
if platform not in metadata["platforms"]:
soup.select_one(f"#compatible-{platform}").decompose()
if "trailerPath" not in metadata:
soup.select_one("#video-play-btn").decompose()
soup.select_one("#video").decompose()
else:
soup.select_one("#video")["src"] = metadata["trailerPath"]
for action in metadata["actions"]:
new_btn = soup.new_tag("a")
new_btn["class"] = "button button-inline"
new_btn["href"] = action["url"]
new_btn_icon = soup.new_tag("i")
new_btn_icon["class"] = action["icon"]
new_btn.append(new_btn_icon)
new_btn.append(f' {action["text"]}')
if action["mode"] == "mobile":
new_btn["class"] += " mobile"
if action["mode"] == "desktop":
new_btn["class"] += " desktop"
if action["bannerBar"]:
soup.select_one("#banner-actions-bar").append(new_btn)
if action["bottomBar"]:
soup.select_one("#bottom-bar").insert(0, copy(new_btn))
return (200, str(soup))
def render_blogpost(blogpost_id: int) -> tuple:
html = HTML_BLOGPOST_TEMPLATE
source = ""
if blogpost_id >= len(BLOGPOST_INDEX["posts"]):
return (404, "")
metadata = BLOGPOST_INDEX["posts"][blogpost_id]
with open("." + metadata["path"], "r") as f:
source = f.read()
_, rendered_content = markdown_renderer.md_to_html(source, True)
print(metadata, rendered_content)
html = html.replace("{[{content}]}", rendered_content)
soup = BeautifulSoup(html, "html.parser")
soup.title.string = soup.title.string.replace("{[{title}]}", metadata["title"])
soup.select_one("#banner")["src"] = metadata["banner"]
soup.select_one("#title").string = metadata["title"]
soup.select_one("#description").string = metadata["description"]
soup.select_one("#date").string = metadata["date"]
soup.select_one("#author").string = metadata["author"]
if metadata["banner"] == "":
soup.select_one("#banner-thumb")["src"] = metadata["thumbnail"]
soup.select_one("#big-banner-container").decompose()
for tag in metadata["tags"]:
new_tag = soup.new_tag("span")
new_tag["class"] = "chip"
new_tag.string = tag
soup.select_one("#tag-container").append(new_tag)
return (200, str(soup))
def blog_list() -> dict:
return BLOGPOST_INDEX
def render_video(video_name: str) -> tuple:
html = HTML_VIDEO_TEMPLATE
source = ""
if video_name not in VIDEO_INDEX:
return (404, "")
with open("." + VIDEO_INDEX[video_name]["content_path"], "r") as f:
source = f.read()
metadata, rendered_content = markdown_renderer.md_to_html(source)
metadata["path"] = VIDEO_INDEX[video_name]["path"]
if "download_path" in VIDEO_INDEX[video_name]:
metadata["download_path"] = VIDEO_INDEX[video_name]["download_path"]
print(metadata, rendered_content)
html = html.replace("{[{content}]}", rendered_content)
html = html.replace("{[{video-title}]}", metadata["title"])
html = html.replace("{[{description}]}", metadata["description"])
html = html.replace("{[{date}]}", metadata["date"])
soup = BeautifulSoup(html, "html.parser")
soup.title.string = soup.title.string.replace("{[{title}]}", metadata["title"])
if "youtube" in metadata["path"]:
pass
iframe = BeautifulSoup(f"""
<div id="iframe-youtube" class="iframe-manual-container">
<iframe data-src="{metadata["path"]}" id="iframe-yt" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen class="img"></iframe>
<div class="iframe-manual-warning">
<img class="img" src="{metadata["thumbnail"]}" alt="">
<div class="shadow-filter">
<div class="iframe-manual-notice">
<span class="desktop">The following is an external embed of YouTube. Google may be able to track you and use 3rd party cookies.</span>
<span class="mobile">Contains external resources from YouTube.</span>
<button class="button button-inline" onclick="iframeLoad('iframe-youtube')">Load External Video</button>
</div>
</div>
</div>
</div>
""", "html.parser")
soup.select_one("#video-parent").append(iframe)
soup.select_one("#video").decompose()
soup.select_one("#video-controls").decompose()
else:
soup.select_one("#video")["src"] = metadata["path"]
if metadata["default_copyright"]:
soup.select_one("#video-license-parent").string = "©️ All rights reserved"
else:
soup.select_one("#video-license-info")["href"] = metadata["license_url"]
soup.select_one("#video-license-info").insert(0, BeautifulSoup(metadata["license"], "html.parser"))
if "download_path" in metadata and not metadata["default_copyright"]:
soup.select_one("#video-download-btn")["href"] = metadata["download_path"]
else:
soup.select_one("#video-download-btn").decompose()
return (200, str(soup))
def render_event(event_name: str) -> tuple:
html = HTML_EVENT_TEMPLATE
source = ""
if event_name not in EVENT_INDEX:
return (404, "")
with open("." + EVENT_INDEX[event_name]["path"], "r") as f:
source = f.read()
metadata, rendered_content = markdown_renderer.md_to_html(source)
print(metadata, rendered_content)
html = html.replace("{[{content}]}", rendered_content)
soup = BeautifulSoup(html, "html.parser")
soup.title.string = soup.title.string.replace("{[{title}]}", metadata["title"])
return (200, str(soup))
def render_index() -> tuple:
html = HTML_INDEX
soup = BeautifulSoup(html, "html.parser")
soup.select_one("#blog-container").append(render_blogpost_card(len(BLOGPOST_INDEX["posts"]) - 1))
soup.select_one("#blog-container").append(render_blogpost_card(len(BLOGPOST_INDEX["posts"]) - 2))
return (200, soup)
def render_blog_list() -> tuple:
html = HTML_BLOGLIST
soup = BeautifulSoup(html, "html.parser")
for i in range(len(BLOGPOST_INDEX["posts"]) - 1, -1, -1):
soup.select_one("#blog-container").append(render_blogpost_card(i))
return (200, soup)
def render_blogpost_card(idx: int) -> BeautifulSoup:
if idx >= len(BLOGPOST_INDEX["posts"]):
print(f"blog of index {idx} doesn't exist")
return None
post = BLOGPOST_INDEX["posts"][idx]
evaluatedTags = ""
for tag in post["tags"]:
evaluatedTags += f"<span class=\"chip\">{tag}</span>"
snippet = f"""
<div class="card" tabindex="9999">
<img src="{post["thumbnail"]}"
alt="">
<div>
<div class="flex-container-normal chip-container">
{evaluatedTags}
</div>
<a href="/blog/{idx}" class="link"><h2>{post["title"]}</h2></a>
<p class="half-opacity-text"><i class="fa-solid fa-calendar"></i> {post["date"]}</p>
<p>{post["description"]}</p>
</div>
</div>
"""
return BeautifulSoup(snippet, "html.parser")
|