-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_pages.py
78 lines (69 loc) · 2.36 KB
/
generate_pages.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
import dateparser
import json
from mako.template import Template
from pathlib import Path
from tqdm import tqdm
import re
templates = {}
templates["base"] = Template(filename="templates/base.html")
def build_webpage(template_name, output_path, title, obj):
if template_name not in templates:
templates[template_name] = Template(filename=f"templates/{template_name}.html")
data = templates["base"].render(
title=title, contents=templates[template_name].render(**obj)
)
output_path = Path(output_path)
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "w") as fp:
fp.write(data)
tqdm.write("Loading...")
data = {}
with open("output.json", "r") as fp:
data = json.load(fp)
tqdm.write("Generating index page...")
data["category_ids_sorted"] = list(
sorted(data["categories"].keys(), key=lambda x: data["categories"][x]["name"])
)
build_webpage("index", "site/index.html", "gbadev.org forum archive", data)
threads_to_category = {}
tqdm.write("Generating category pages...")
for category_id in tqdm(data["category_ids_sorted"]):
data["category_id"] = category_id
data["category"] = data["categories"][category_id]
data["thread_ids_sorted"] = list(
sorted(
filter(
lambda x: data["threads"][x]["category_id"] == int(category_id),
data["threads"].keys(),
),
key=lambda x: -int(x),
)
)
for i in data["thread_ids_sorted"]:
threads_to_category[i] = category_id
build_webpage(
"category",
f"site/category/{category_id}.html",
f"{data['categories'][category_id]['name']} - gbadev.org forum archive",
data,
)
tqdm.write("Generating thread pages...")
for thread_id in tqdm(data["threads"].keys()):
thread = data["threads"][thread_id]
category_id = threads_to_category[thread_id]
data["category_id"] = category_id
data["category"] = data["categories"][data["category_id"]]
data["thread_id"] = thread_id
data["thread"] = thread
data["post_ids_sorted"] = list(
map(
lambda x: str(x[1]),
sorted(thread["posts"].items(), key=lambda x: int(x[0])),
)
)
build_webpage(
"thread",
f"site/thread/{category_id}/{thread_id}.html",
f"{thread['name']} - gbadev.org forum archive",
data,
)