diff --git a/fabcal/routers/__init__.py b/fabcal/routers/__init__.py
index 0b59582..75ad936 100644
--- a/fabcal/routers/__init__.py
+++ b/fabcal/routers/__init__.py
@@ -1,4 +1,31 @@
+import re
+
+from jinja2 import pass_eval_context
+from markupsafe import Markup, escape
from starlette.templating import Jinja2Templates
templates = Jinja2Templates(directory="templates")
+
+
+@pass_eval_context
+def nl2br(eval_ctx, value):
+ br = "
\n"
+
+ if eval_ctx.autoescape:
+ value = escape(value)
+ br = Markup(br)
+
+ result = "\n\n".join(
+ # need to convert p to Markup again after applying re.split(...)
+ f"
{br.join(Markup(p).splitlines())}
" + for p in re.split(r"(?:\r\n|\r(?!\n)|\n){2,}", value) + ) + + if eval_ctx.autoescape: + result = Markup(result) + + return result + + +templates.env.filters["nl2br"] = nl2br diff --git a/fabcal/routers/api_v1.py b/fabcal/routers/api_v1.py index f8404b3..e3e621e 100644 --- a/fabcal/routers/api_v1.py +++ b/fabcal/routers/api_v1.py @@ -8,7 +8,7 @@ router = APIRouter() @router.get("/events.ics") -async def events(): +async def events_ics(): return Response( await get_data(), headers={ diff --git a/fabcal/routers/frontend.py b/fabcal/routers/frontend.py index 268f3fe..92b6ab4 100644 --- a/fabcal/routers/frontend.py +++ b/fabcal/routers/frontend.py @@ -15,8 +15,7 @@ from fabcal.routers import templates router = APIRouter() -@router.get("/embed-sidebar.html", response_class=HTMLResponse) -async def embed(request: Request, max_width: str = None): +async def generate_response(request: Request, template_name: str, **additional_context): events = await get_future_events() grouped_events = list(group_by_date(events).items()) @@ -32,8 +31,19 @@ async def embed(request: Request, max_width: str = None): def base64_encode(s: str): return base64.b64encode(s.encode()).decode() + context = { + "request": request, + "grouped_events": grouped_events, + "dir": dir, + "localized_abbreviated_month": localized_abbreviated_month, + "localized_abbreviated_weekday": localized_abbreviated_weekday, + "base64_encode": base64_encode, + } + + context.update(additional_context) + return templates.TemplateResponse( - "embed-sidebar.html", + template_name, context={ "request": request, "grouped_events": grouped_events, @@ -43,3 +53,8 @@ async def embed(request: Request, max_width: str = None): "base64_encode": base64_encode, }, ) + + +@router.get("/sidebar/embed.html", response_class=HTMLResponse) +async def embed_sidebar(request: Request): + return await generate_response(request, "sidebar/embed.html") diff --git a/static/style.css b/static/style.css index d2af298..5e27b13 100644 --- a/static/style.css +++ b/static/style.css @@ -26,7 +26,6 @@ body { display: flex; align-items: stretch; flex-direction: row; - background-color: white; } .calendar-date-date { @@ -36,6 +35,7 @@ body { margin-right: 8px; flex: 35px 0 0; height: 100%; + background-color: white; } .calendar-date-month { background-color: var(--calendar-fablab-red); @@ -96,6 +96,12 @@ body { flex-direction: column; justify-content: center; } +.calendar-event-description p { + margin-bottom: 0.5em; +} +.calendar-event-description p:last-child { + margin-bottom: 0; +} .calendar-event-type { flex: 0 0 12px; margin: -5px -5px -5px 0; diff --git a/templates/embed-sidebar.html b/templates/embed-sidebar.html deleted file mode 100644 index d4128ba..0000000 --- a/templates/embed-sidebar.html +++ /dev/null @@ -1,68 +0,0 @@ - - - - {#- TODO: replace with locally served files #} - - - - - - -