diff --git a/handlers.py b/handlers.py
index 6e67f47..841fe0f 100644
--- a/handlers.py
+++ b/handlers.py
@@ -2,7 +2,7 @@ import json
from datetime import datetime
from db import get_db, get_setting, set_setting, get_site_name, index_url, clean_url
-from templates import esc, snippet, wrap_page
+from templates import esc, snippet, wrap_page, DEFAULT_TEMPLATE
from rns_client import fetch_remote_sites
@@ -186,19 +186,13 @@ def handle_search(query):
if q and remote_rows:
sub_count = f" + {len(remote_rows)} from subscriptions"
return _respond(
- f'
'
f''
- f'{count} page(s) indexed.'
- f' + add url'
- f' | browse'
- f' | tags'
- f' | subscriptions'
- f' | customize'
- f' | about
'
- f'
{result_html}{trusted_html}{remote_html}'
+ f'{count} pages indexed'
+ f' ยท + add url
'
+ f'{result_html}{trusted_html}{remote_html}'
)
@@ -366,8 +360,11 @@ def handle_import_submit(body):
return handle_import_form(f"Imported {imported} page(s). {errors} error(s).")
-def handle_style_form(msg=""):
- css = get_setting("custom_css")
+def handle_style_form(msg="", query=None):
+ if query and "reset" in query:
+ set_setting("custom_template", "")
+ msg = "Template reset to default."
+ template = get_setting("custom_template") or DEFAULT_TEMPLATE
name = get_site_name()
sharing = get_setting("sharing_enabled", "0")
checked = " checked" if sharing == "1" else ""
@@ -379,20 +376,10 @@ def handle_style_form(msg=""):
f"sharing
"
f'
"
- f"custom css
"
- f"Some classes you can target:
"
- f""
- f"body - page background, font\n"
- f"h1 - page titles\n"
- f"input, button - search bar\n"
- f"a - links\n"
- f".result - each search result\n"
- f".note - your notes on results\n"
- f".trusted - trusted sites dropdown\n"
- f"small - url text\n"
- f"ul, li - browse page list"
- f"
"
- f'
'
+ f"custom html
"
+ f"Edit the full page template. Use {esc('{{content}}')} "
+ f"where page content should appear.
"
+ f'
'
f''
f""
f"bookmarklet
"
@@ -404,10 +391,10 @@ def handle_style_form(msg=""):
def handle_style_submit(body):
- css = body.get("css", [""])[0]
+ template = body.get("template", [""])[0]
name = body.get("site_name", ["tinyweb"])[0].strip()
sharing = "1" if body.get("sharing_enabled") else "0"
- set_setting("custom_css", css)
+ set_setting("custom_template", template if template.strip() != DEFAULT_TEMPLATE.strip() else "")
set_setting("site_name", name or "tinyweb")
set_setting("sharing_enabled", sharing)
return handle_style_form("Saved.")
@@ -755,6 +742,7 @@ def handle_subscription_autosync(sub_id):
def handle_subscription_delete(sub_id):
db = get_db()
+ db.execute("DELETE FROM remote_pages WHERE subscription_id = ?", (sub_id,))
db.execute("DELETE FROM subscriptions WHERE id = ?", (sub_id,))
db.commit()
db.close()
@@ -826,7 +814,7 @@ def dispatch_request(data):
elif path == "/bookmark":
return handle_bookmark(query)
elif path == "/style":
- return handle_style_form()
+ return handle_style_form(query=query)
elif path == "/about":
return handle_about()
elif path == "/export":
diff --git a/templates.py b/templates.py
index 735a38e..870add5 100644
--- a/templates.py
+++ b/templates.py
@@ -15,7 +15,23 @@ def snippet(text, query, ctx=80):
return ("..." if start > 0 else "") + text[start:end] + ("..." if end < len(text) else "")
+DEFAULT_TEMPLATE = "\n\n\n\n{{content}}\n\n"
+
+
+def _default_template():
+ name = esc(get_setting("site_name", "tinyweb"))
+ return (
+ "\n\n\n\n"
+ f'{name}'
+ ' | search | browse'
+ ' | tags | subscriptions'
+ ' | customize | about
\n'
+ "
\n{{content}}\n\n"
+ )
+
+
def wrap_page(body_html):
- css = get_setting("custom_css")
- style = f"" if css else ""
- return f"{style}{body_html}"
+ template = get_setting("custom_template") or _default_template()
+ if "{{content}}" not in template:
+ template = _default_template()
+ return template.replace("{{content}}", body_html)