diff --git a/handlers.py b/handlers.py index 6e67f47..4ef8939 100644 --- a/handlers.py +++ b/handlers.py @@ -2,15 +2,15 @@ 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 -def _respond(body_html, status=200): +def _respond(body_html, status=200, use_default=False): return { "status": status, "content_type": "text/html; charset=utf-8", - "body": wrap_page(body_html), + "body": wrap_page(body_html, use_default=use_default), "headers": {}, } @@ -186,19 +186,13 @@ def handle_search(query): if q and remote_rows: sub_count = f" + {len(remote_rows)} from subscriptions" return _respond( - f'

{esc(name)}

' f'
' f'' 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,35 +376,26 @@ 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

" f"

Drag this link to your bookmarks bar. Click it on any page to index it instantly.

" f'

+ save to {esc(name)}

' f"

{msg}

" - f'back' + f'back', + use_default=True, ) 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 +743,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 +815,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/index.db b/index.db index eaa39d2..e9cae1f 100644 Binary files a/index.db and b/index.db differ diff --git a/templates.py b/templates.py index 735a38e..372e736 100644 --- a/templates.py +++ b/templates.py @@ -15,7 +15,26 @@ def snippet(text, query, ctx=80): return ("..." if start > 0 else "") + text[start:end] + ("..." if end < len(text) else "") -def wrap_page(body_html): - css = get_setting("custom_css") - style = f"" if css else "" - return f"{style}{body_html}" +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, use_default=False): + if use_default: + template = _default_template() + else: + template = get_setting("custom_template") or _default_template() + if "{{content}}" not in template: + template = _default_template() + return template.replace("{{content}}", body_html)