-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_index.py
53 lines (35 loc) · 1.32 KB
/
create_index.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
"""This script creates the list of animations urls and embeds it into the index page."""
from glob import glob
def create_urls() -> list[tuple[str, str]]:
"""Create urls for each animation."""
folders = []
for folder in glob("animations/*"):
name = folder.split("/")[-1].replace("-", " ").lower()
folders.append((name, folder))
return sorted(folders, key=lambda x: x[0])
def indent(n: int) -> str:
"""Return n tabs."""
return "\t" * n
def base_indent(content: str) -> int:
"""Return the number of tabs before the first non-empty line."""
return (len(content) - len(content.lstrip())) // 3
def embed_urls(urls: list[str]) -> None:
"""Embed the urls in the page."""
with open("index_template.html") as f:
content = f.read()
links_line = [f for f in content.split("\n") if "{{ LINKS }}" in f][0]
base = base_indent(links_line)
new_content = ""
for name, url in urls:
new_content += "\n"
new_content += f'{indent(base + 1)}<a href="{url}">{name}</a>'
new_content += f"\n{indent(base)}"
content = content.replace("{{ LINKS }}", new_content)
with open("index.html", "w") as f:
f.write(content)
def main():
"""Script entry point."""
urls = create_urls()
embed_urls(urls)
if __name__ == "__main__":
main()