bbcovert/render.py

171 lines
6.6 KiB
Python

import jinja2
import os
import shutil
import json
import sys
import time
import tomllib
import math
print("Load config")
with open("config.toml", 'rb') as file:
cfg = tomllib.load(file)["render"]
show_st = cfg["show_st"]
clean = cfg["clean"]
page_split = cfg["page_split"]
print("Init build")
if (clean):
if os.path.exists("build"):
shutil.rmtree("build")
os.mkdir("build")
if not os.path.exists("theme"):
print("Connot find any themes")
sys.exit(1)
def render_func(filepath, extpath, **kwargs):
if (show_st):
print(f' render "{extpath}"')
with open(filepath, 'r') as file:
result = jinja2.Template(file.read()).render(**kwargs)
with open(extpath, 'w') as file:
file.write(result)
if not (os.path.exists("build/home.html")):
print("Render home page")
with open("dump/forums.json", 'r') as file:
json_res = json.load(file)
render_args = {
"forums": [
{
"url": "/topics/"+str(i["id"])+"/1.html",
"name": i["name"],
"description": i["description"]
} for i in json_res
]
}
render_func("theme/home.html", "build/index.html", **render_args)
if not (os.path.exists("build/users")):
print("Render users")
os.mkdir("build/users")
with open("dump/users.json", 'r') as file:
user_json_res = json.load(file)
count = 0
print(f"Users count: {len(user_json_res)}")
for ids, context in user_json_res.items():
count += 1
render_func("theme/user.html", "build/users/"+str(ids)+".html", user={
"name": context["name"],
"description": context["description"],
"reg_time": time.strftime("%Y-%m-%d", time.localtime(context["time"]))
})
if not (show_st):
if (count % 50 == 0):
print(f" render {count}", end='\r')
print(f" render {count}")
if not (os.path.exists("build/topics")):
print("Render topics")
os.mkdir("build/topics")
with open("dump/forums.json", 'r') as file:
json_res = json.load(file)
count = 0
for r in os.listdir("dump/forums"):
count += 1
u_cfg = []
with open("dump/forums/"+r, 'r') as file:
strs = file.readlines()
n_count = 0
for i in strs:
n_count += 1
line_st = i.rstrip("\r\n").lstrip("\r\n")
if (line_st == ""):
continue
line_json = json.loads(line_st)
u_cfg.append(
{
"id": n_count,
"name": line_json["name"],
"time": time.strftime("%Y-%m-%d", time.localtime(line_json["time"])),
"url": "/posts/"+str(line_json["id"])+"/1.html"
}
)
ids = int(r.split(".")[0])
os.mkdir("build/topics/" + str(ids))
for page_counts in range(1, math.ceil(len(strs)/page_split)+1):
render_func("theme/topics.html", "build/topics/" +
str(ids)+"/"+str(page_counts)+".html", topics={
"topics": u_cfg[page_split*(page_counts-1):page_split*page_counts],
"from_forum": list(filter(lambda d: d.get('id') == ids, json_res))[0]["name"],
"from_url": "/index.html"
}, page={
"now": page_counts,
"count": math.ceil(len(strs)/page_split),
"first_url": "/topics/"+str(ids)+"/"+str(1)+".html",
"prev_url": ("/topics/"+str(ids)+"/"+str(page_counts-1)+".html") if (page_counts > 1) else "",
"next_url": ("/topics/"+str(ids)+"/"+str(page_counts+1)+".html") if ((page_counts) < math.ceil(len(strs)/page_split)) else "",
"end_url": "/topics/"+str(ids)+"/"+str(math.ceil(len(strs)/page_split))+".html"
})
if not (show_st):
print(f" render {count}", end='\r')
print(f" render {count}")
if not (os.path.exists("build/posts")):
print("Render posts")
os.mkdir("build/posts")
n_count = 0
for r in os.listdir("dump/forums"):
with open("dump/forums/"+r, 'r') as file:
strs = file.readlines()
page_ncount = 0
for i in strs:
page_ncount += 1
line_st = i.rstrip("\r\n").lstrip("\r\n")
if (line_st == ""):
continue
n_json = json.loads(line_st)
n_id = n_json["id"]
if not (os.path.exists("dump/topics/"+str(n_id)+".manifest")):
continue
with open("dump/topics/"+str(n_id)+".manifest", 'r') as file:
strs_m = file.readlines()
u_cfg = []
t_count = 0
for j in strs_m:
n_count += 1
t_count += 1
m_st = j.rstrip("\r\n").lstrip("\r\n")
if (m_st == ""):
continue
m_json = json.loads(m_st)
ids = n_id
u_cfg.append({
"id": t_count,
"text": m_json["text"],
"postername": m_json["poster_name"],
"posterurl": "/users/"+str(m_json["poster_id"])+".html"
})
if (n_count % 10 == 0):
print(f" render {n_count}", end='\r')
os.mkdir("build/posts/" + str(ids))
for page_counts in range(1, math.ceil(len(u_cfg)/page_split)+1):
render_func("theme/posts.html", "build/posts/" +
str(ids)+"/"+str(page_counts)+".html", posts={
"posts": u_cfg[page_split*(page_counts-1):page_split*page_counts],
"from_topic": n_json["name"],
"from_url": "/topics/"+str(r.split(".")[0])+"/"+str((page_ncount-1)//page_split+1)+".html"
}, page={
"now": page_counts,
"count": math.ceil(len(u_cfg)/page_split),
"first_url": "/posts/"+str(ids)+"/"+str(1)+".html",
"prev_url": ("/posts/"+str(ids)+"/"+str(page_counts-1)+".html") if (page_counts > 1) else "",
"next_url": ("/posts/"+str(ids)+"/"+str(page_counts+1)+".html") if ((page_counts) < math.ceil(len(u_cfg)/page_split)) else "",
"end_url": "/posts/"+str(ids)+"/"+str(math.ceil(len(u_cfg)/page_split))+".html"
})
print(f" render {n_count}")