From 3afb29306a889d096ecfcf5c588919792e8f5fbc Mon Sep 17 00:00:00 2001
From: trinkey <97406176+trinkey@users.noreply.github.com>
Date: Tue, 19 Mar 2024 18:42:28 -0400
Subject: [PATCH] templating on `/u/...` pages
---
README.md | 1 -
_server.py | 45 ++++++++++++++++++++++++++++++++++++++++++++-
public/user.html | 4 ++--
3 files changed, 46 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 6a9f8a4..700f0fd 100644
--- a/README.md
+++ b/README.md
@@ -5,4 +5,3 @@ it's like pronouns.page but i made it
### todo
* social links
-* templating and embeds on /u/... pages
diff --git a/_server.py b/_server.py
index 32f3587..ec74860 100644
--- a/_server.py
+++ b/_server.py
@@ -10,6 +10,7 @@ import flask
import json
import os
+from DotIndex import DotIndex
from typing import Union, Callable
from flask import request, redirect
from werkzeug.middleware.proxy_fix import ProxyFix
@@ -120,6 +121,48 @@ def create_folder_serve(directory) -> Callable:
x.__name__ = directory
return x
+def get_template(json, username):
+ def add_to_output(starting, json, key, title):
+ starting += f'
{title}
'
+ for i in json[key]:
+ starting += f"""
{icons[i[1]]} {escape_html(i[0])}
"""
+ return starting + "
"
+
+ json = DotIndex(json)
+
+ icons = {
+ "1": '',
+ "2": '',
+ "3": '',
+ "4": ''
+ }
+
+ styles = f"--background: {json.colors.background}; --background-low-opacity: {json.colors.background}33; --accent: {json.colors.accent}; --accent-low-opacity: {json.colors.accent}66; --text: {json.colors.text}; --text-low-opacity: {json.colors.text}88;" # type: ignore
+ title = f"{escape_html(json.display_name)} (@{username})".replace("{{TEMPLATE}}", "HA" * 50) # type: ignore
+ embed = f'' # type: ignore
+ inner = f'{escape_html(json.display_name)}
{escape_html(json.description)}
' # type: ignore
+
+ inner = add_to_output(inner, json, "names", "Names");
+ inner = add_to_output(inner, json, "pronouns", "Pronouns");
+ inner = add_to_output(inner, json, "honorifics", "Honorifics");
+ inner = add_to_output(inner, json, "compliments", "Compliments");
+ inner = add_to_output(inner, json, "relationship", "Relationship
Descriptions");
+
+ inner += "
"
+
+ return title, inner, styles, embed
+
+def get_user_page(user):
+ x = open(f"{CONTENT_DIRECTORY}user.html", "r").read()
+ try:
+ user_json = json.loads(open(f"{SAVING_DIRECTORY}{user}.json", "r").read())
+ except FileNotFoundError:
+ return x.replace("{{TEMPLATE}}", '
User not found!
Sign up -
Log in').replace("{{TITLE}}", "User not found - InfoPage")
+
+ title, inner, styles, embed = get_template(user_json, user)
+
+ return x.replace("")(create_file_serve("user.html"))
+app.route("/u/
")(get_user_page)
app.route("/home")(home)
app.route("/js/")(create_folder_serve("js"))
diff --git a/public/user.html b/public/user.html
index 68275af..b32d4af 100644
--- a/public/user.html
+++ b/public/user.html
@@ -2,7 +2,7 @@
- Loading user...
+ {{TITLE}}
@@ -11,6 +11,6 @@
-
+ {{TEMPLATE}}