-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript.py
219 lines (193 loc) · 6.71 KB
/
script.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
import tomllib
from dataclasses import dataclass, field
from typing import List, Optional
from tinyhtml import html, h, frag, raw
@dataclass
class Item:
title: str
description: Optional[str] = None
url: Optional[str] = None
icon: Optional[str] = None
embed_url: Optional[str] = None
embed_height: str = "315"
@dataclass
class Section:
title: str
description: Optional[str] = None
icon: Optional[str] = None
icon_size: str = "24px"
direction: str = "column"
item_style: str = "outline"
items: List[Item] = field(default_factory=list)
@dataclass
class Data:
name: str
base_url: str
sections: List[Section] = field(default_factory=list)
description: Optional[str] = None
keywords: Optional[str] = None
image: Optional[str] = None
theme: str = "dark"
primary_color: str = "#546e7a"
text_align: str = "center"
gtag_id: Optional[str] = None
def load_data(file_path):
with open(file_path, "rb") as f:
raw_data = tomllib.load(f)
return Data(
name=raw_data.get("name", ""),
description=raw_data.get("description"),
keywords=raw_data.get("keywords"),
base_url=raw_data.get("base_url", ""),
image=raw_data.get("image"),
theme=raw_data.get("theme", "dark"),
primary_color=raw_data.get("primary_color", "#546e7a"),
text_align=raw_data.get("text_align", "center"),
gtag_id=raw_data.get("gtag_id"),
sections=[
Section(
title=section.get("title"),
description=section.get("description"),
icon=section.get("icon"),
icon_size=section.get("icon_size", "24px"),
direction=section.get("direction", "column"),
item_style=section.get("item_style", "outline"),
items=[
Item(
title=item.get("title"),
description=item.get("description"),
url=item.get("url"),
icon=item.get("icon"),
embed_url=item.get("embed_url"),
embed_height=item.get("embed_height", "315"),
)
for item in section.get("items", [])
],
)
for section in raw_data.get("sections", [])
],
)
def create_section(section: Section):
items = frag(
h(
"div",
klass="item",
style=f"width: {'100%' if section.direction == 'column' else 'unset'}",
)(
h(
"a",
role="button",
klass=f"{'outline' if section.item_style == 'outline' else ''}",
href=item.url,
target="_blank",
)(
h(
"img",
klass="icon",
src=item.icon,
alt=item.title,
style=f"width: {section.icon_size};",
)
if item.icon
else None,
h("hgroup")(h("h4")(item.title), h("h5")(item.description)),
)
if item.url
else raw(f"""
<iframe
src="{item.embed_url}"
height="{item.embed_height}"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen="true"
>
</iframe>
""")
if item.embed_url
else None
)
for item in section.items
)
return h("div", klass="section")(
h("hgroup")(
h("h3")(section.title),
h("p")(section.description),
),
h("div", klass="items", style=f"flex-direction: {section.direction}")(items),
)
def create_meta_tags(data: Data):
return frag(
h("title")(data.name),
h("meta", name="description", content=data.description),
h("meta", name="keywords", content=data.keywords),
h("meta", name="viewport", content="width=device-width, initial-scale=1"),
h("meta", charset="utf-8"),
h("meta", property="og:title", content=data.name),
h("meta", property="og:description", content=data.description),
h("meta", property="og:image", content=f"{data.base_url}/img/{data.image}"),
h("meta", name="twitter:title", content=data.name),
h("meta", name="twitter:description", content=data.description),
h("meta", name="twitter:image", content=f"{data.base_url}/img/{data.image}"),
h("meta", name="twitter:card", content="summary_large_image"),
)
def create_head(data: Data):
return h("head")(
create_meta_tags(data),
h("link", rel="stylesheet", href="css/pico.min.css"),
h("link", rel="stylesheet", href="css/style.css"),
h("style", rel="stylesheet")(
f"""
[data-theme="dark"], [data-theme="light"] {{
--primary: {data.primary_color} !important;
}}
* {{
text-align: {data.text_align};
}}
"""
),
raw(
f"""
<script async src="https://www.googletagmanager.com/gtag/js?id={data.gtag_id}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){{dataLayer.push(arguments);}}
gtag('js', new Date());
gtag('config', '{data.gtag_id}');
</script>
"""
)
if data.gtag_id
else None,
)
def create_header(data: Data):
return h("header", klass="container")(
h("hgroup")(
h("img", klass="avatar", src=f"img/{data.image}", alt="avatar"),
h("h1")(data.name),
h("p")(data.description) if data.description else None,
)
)
def create_footer():
return h("footer", klass="container")(
h("small")("Generated with "),
h("a", klass="", href="https://github.com/thevahidal/jake/", target="_blank")(
"Jake"
),
)
def generate_html(data: Data):
sections = frag(create_section(section) for section in data.sections)
return html(lang="en", data_theme=data.theme)(
create_head(data),
h("body")(
create_header(data),
h("main", klass="container")(sections),
create_footer(),
),
).render()
def main():
data = load_data("data.toml")
output = generate_html(data)
with open("dist/index.html", "w") as f:
f.write(output)
if __name__ == "__main__":
main()