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
|
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"
HTML_PROJECT_TEMPLATE = """"""
HTML_BLOGPOST_TEMPLATE = """"""
HTML_EVENT_TEMPLATE = """"""
HTML_VIDEO_TEMPLATE = """"""
PROJECT_INDEX = {}
BLOGPOST_INDEX = {}
EVENT_INDEX = {}
VIDEO_INDEX = {}
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_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)
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)
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"]
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"]
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"])
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 metadata["downloadable"] and not metadata["default_copyright"]:
soup.select_one("#video-download-btn").href = metadata["path"]
else:
soup.select_one("#video-download-btn").decompose()
return (200, str(soup))
|