tinyweb/templates.py

74 lines
3.1 KiB
Python

import html
from db import get_setting
FORUM_ENABLED = False
def esc(s):
return html.escape(str(s))
FORUM_CSS = """
<style>
.forum-form { max-width: 500px; }
.forum-form input:not([type=checkbox]):not([type=radio]), .forum-form textarea { width: 100%; box-sizing: border-box; }
.forum-form textarea { resize: vertical; }
.forum-form input, .forum-form textarea, .forum-form button { margin-bottom: 8px; }
.forum-form small { display: block; margin-bottom: 6px; }
.forum-form label { display: block; margin-bottom: 6px; }
.forum-form + .forum-form { margin-top: 1rem; }
.forum-form + .section-title { margin-top: 1rem; }
.section-desc + .forum-form { margin-top: 0.8rem; }
ul + .forum-form { margin-top: 1rem; }
.checkbox-label { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }
.forum-toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.forum-toolbar form { flex: 1; min-width: 160px; margin: 0; }
.forum-toolbar input[name=q] { width: 100%; box-sizing: border-box; }
.forum-toolbar-actions { display: flex; flex-wrap: wrap; gap: 4px; }
.forum-list { list-style: none; padding: 0; }
.forum-status { margin: 0 0 0.8rem 0; }
.forum-status span { margin-right: 0.5rem; }
.forum-nav { margin: 0.5rem 0; }
.section { margin: 1.5rem 0; }
.section-title { font-weight: 600; margin-bottom: 0.3rem; }
.section-desc { font-size: 0.85rem; margin-bottom: 0.5rem; }
.section ul { margin: 0.3rem 0; }
</style>"""
def _nav_html():
name = esc(get_setting("site_name", "tinyweb"))
forum_link = ' | <a href="/forum">forum</a>' if FORUM_ENABLED else ""
return (
f'<p><b><a href="/">{name}</a></b>'
' | <a href="/">search</a> | <a href="/pages">browse</a>'
' | <a href="/tags">tags</a> | <a href="/subscriptions">subscriptions</a>'
f'{forum_link}'
' | <a href="/style">customize</a> | <a href="/about">about</a></p>\n'
"<hr>\n"
)
DEFAULT_TEMPLATE = "<html>\n<head>\n<meta name=\"referrer\" content=\"no-referrer\">\n<meta http-equiv=\"x-dns-prefetch-control\" content=\"off\">\n</head>\n<body>\n{{nav}}{{content}}\n</body>\n</html>"
def _default_template():
return (
'<html>\n<head>\n<meta name="referrer" content="no-referrer">\n<meta http-equiv="x-dns-prefetch-control" content="off">\n'
'</head>\n<body>\n{{nav}}{{content}}\n</body>\n</html>'
)
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()
forum_link = ' <a href="/forum">forum</a>' if FORUM_ENABLED else ""
template = template.replace("{{forum_link}}", forum_link)
template = template.replace("{{site_name}}", esc(get_setting("site_name", "tinyweb")))
template = template.replace("{{nav}}", _nav_html())
# Inject forum layout CSS into <head> for any template
head_end = "</head>"
if head_end in template and FORUM_CSS not in template:
template = template.replace(head_end, FORUM_CSS + head_end)
return template.replace("{{content}}", body_html)