From d163092b5c159ddd1813550d1f9bab72adbdf80a Mon Sep 17 00:00:00 2001 From: altaf-creator Date: Fri, 31 Jul 2026 20:57:25 +0800 Subject: index and blog is now ssr --- main.py | 14 ++++++++++++ pages.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ www/about/index.html | 2 +- www/projects/index.html | 2 +- www/scripts/onload.js | 3 --- 5 files changed, 76 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 23f201f..78074cc 100644 --- a/main.py +++ b/main.py @@ -138,4 +138,18 @@ def event_page_redirect(event_name: str): ) +@app.get("/") +def index_page(): + status, html = pages.render_index() + + return fastapi.responses.Response(content=html, media_type="text/html", status_code=status) + + +@app.get("/blog/") +def bloglist_page(): + status, html = pages.render_blog_list() + + return fastapi.responses.Response(content=html, media_type="text/html", status_code=status) + + app.mount("/", fastapi.staticfiles.StaticFiles(directory="./www/", html=True), name="static") diff --git a/pages.py b/pages.py index e204a3e..04c1671 100644 --- a/pages.py +++ b/pages.py @@ -13,11 +13,15 @@ PATH_HTML_EVENT_TEMPLATE = "./templates/eventdetails.html" PATH_EVENT_INDEX = "./data/events.json" PATH_HTML_VIDEO_TEMPLATE = "./templates/video.html" PATH_VIDEO_INDEX = "./data/videos.json" +PATH_INDEX = "./www/index.html" +PATH_BLOGLIST = "./www/blog/index.html" HTML_PROJECT_TEMPLATE = """""" HTML_BLOGPOST_TEMPLATE = """""" HTML_EVENT_TEMPLATE = """""" HTML_VIDEO_TEMPLATE = """""" +HTML_INDEX = """""" +HTML_BLOGLIST = """""" PROJECT_INDEX = {} BLOGPOST_INDEX = {} @@ -34,6 +38,10 @@ with open(PATH_HTML_EVENT_TEMPLATE) as f: HTML_EVENT_TEMPLATE = f.read() with open(PATH_HTML_VIDEO_TEMPLATE) as f: HTML_VIDEO_TEMPLATE = f.read() +with open(PATH_INDEX) as f: + HTML_INDEX = f.read() +with open(PATH_BLOGLIST) as f: + HTML_BLOGLIST = f.read() with open(PATH_PROJECT_INDEX) as f: PROJECT_INDEX = json.load(f) @@ -234,3 +242,55 @@ def render_event(event_name: str) -> tuple: soup.title.string = soup.title.string.replace("{[{title}]}", metadata["title"]) return (200, str(soup)) + + +def render_index() -> tuple: + html = HTML_INDEX + + soup = BeautifulSoup(html, "html.parser") + + soup.select_one("#blog-container").append(render_blogpost_card(len(BLOGPOST_INDEX["posts"]) - 1)) + soup.select_one("#blog-container").append(render_blogpost_card(len(BLOGPOST_INDEX["posts"]) - 2)) + + return (200, soup) + + +def render_blog_list() -> tuple: + html = HTML_BLOGLIST + + soup = BeautifulSoup(html, "html.parser") + + for i in range(len(BLOGPOST_INDEX["posts"]) - 1, -1, -1): + soup.select_one("#blog-container").append(render_blogpost_card(i)) + + return (200, soup) + + +def render_blogpost_card(idx: int) -> BeautifulSoup: + if idx >= len(BLOGPOST_INDEX["posts"]): + print(f"blog of index {idx} doesn't exist") + return None + + post = BLOGPOST_INDEX["posts"][idx] + + evaluatedTags = "" + + for tag in post["tags"]: + evaluatedTags += f"{tag}" + + snippet = f""" +
+ +
+
+ {evaluatedTags} +
+

{post["title"]}

+

{post["date"]}

+

{post["description"]}

+
+
+ """ + + return BeautifulSoup(snippet, "html.parser") diff --git a/www/about/index.html b/www/about/index.html index bc49f24..46572ee 100644 --- a/www/about/index.html +++ b/www/about/index.html @@ -130,7 +130,7 @@
Project -

Tank Shooter

+

Tank Shooter

Created and published my first major game project. A 2D pixel art arcade shooter game.

/ Tank Shooter →

diff --git a/www/projects/index.html b/www/projects/index.html index cfd3ba2..fd053a2 100644 --- a/www/projects/index.html +++ b/www/projects/index.html @@ -123,7 +123,7 @@