-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
102 lines (85 loc) · 2.36 KB
/
build.py
File metadata and controls
102 lines (85 loc) · 2.36 KB
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
import shutil
from pathlib import Path
from typing import Any
import yaml
BASE_DIR = Path(__file__).parent
LINKS_FILE = BASE_DIR / 'links.yml'
PUBLIC_DIR = BASE_DIR / 'public'
DIST_DIR = BASE_DIR / 'dist'
INDEX_FILE = DIST_DIR / 'index.html'
PAGE = '''
<!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="UTF-8"/>
<meta name="description" content="Useful links relating to Jonathan Leeming"/>
<meta name="viewport" content="width=device-width"/>
<title>Jonathan Leeming | Links</title>
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link href="style.css" rel="stylesheet">
</head>
<body>
<main>
<h1>Links</h1>
<nav>
<ul>
{links}
</ul>
</nav>
</main>
<footer>
<p>Icons from <a href="https://phosphoricons.com/">Phosphor Icons</a>.</p>
<p><a href="https://leeming.dev">Jonathan Leeming</a> © 2025</p>
</footer>
</body>
</html>
'''
SECTION = '''
<h{level}>{title}</h{level}>
<ul>
{content}
</ul>
'''
CARD = '''
<li>
<a href="https://{url}">
{image}
<span>{title}</span>
</a>
</li>
'''
def format_link(link: dict[str, Any], level=2):
if 'section' in link:
title = link['section']
links = link.get('links', [])
return SECTION.format(
title=title,
level=level,
content='\n'.join(format_link(l, level+1) for l in links),
).strip()
url = link['url']
title = link.get('title', url)
if icon := link.get('icon'):
image = f'<img src="icons/{icon}.svg" alt="">'
else:
image = '<span></span>'
return CARD.format(
url=url,
title=title,
image=image,
).strip()
def main():
with LINKS_FILE.open('r') as f:
links = yaml.safe_load(f)
page = PAGE.format(
links='\n'.join(format_link(l) for l in links)
).strip()
if DIST_DIR.exists():
shutil.rmtree(DIST_DIR)
shutil.copytree(PUBLIC_DIR, DIST_DIR)
INDEX_FILE.write_text(page)
if __name__ == '__main__':
exit(main())