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 #} - - - - - - - Embeddable calendar - - - -
- {% for start_date, events in grouped_events[:3] %} -
-
-
- {{ start_date.strftime("%b") }} -
-
- {{ start_date.strftime("%d") }} -
-
- {{ start_date.strftime("%a") }} -
-
-
- {% for event in events %} -
-
-
{{ event.start.strftime("%H:%M") }}
-
-
{{ event.end.strftime("%H:%M") }}
-
-
- {% if event.description or event.location %} -
- {{ event.summary }} -
- {% if event.description %} -
- {#- description doesn't need an icon, just wastes space #} -
{{ event.description }}
-
- {% endif %} - {% if event.location %} -
-
-
{{ event.location }}
-
- {% endif %} -
-
- {% else %} - {{ event.summary }} - {% endif %} -
-
-
- {% endfor %} -
-
- {% endfor %} -
- - diff --git a/templates/sidebar/base.html b/templates/sidebar/base.html new file mode 100644 index 0000000..b12549b --- /dev/null +++ b/templates/sidebar/base.html @@ -0,0 +1,21 @@ + + + + {% block header %} + {#- TODO: replace with locally served files #} + + + + + + + + Embeddable calendar + {% endblock %} + + + + {% block body %} + {% endblock %} + + diff --git a/templates/sidebar/embed.html b/templates/sidebar/embed.html new file mode 100644 index 0000000..afdc142 --- /dev/null +++ b/templates/sidebar/embed.html @@ -0,0 +1,21 @@ + + + + {#- TODO: replace with locally served files #} + + + + + + + Embeddable calendar + + + +
+ {% include "sidebar/includes/header.html" %} + {% include "sidebar/includes/events-list.html" %} + {% include "sidebar/includes/footer.html" %} +
+ + diff --git a/templates/sidebar/includes/events-list.html b/templates/sidebar/includes/events-list.html new file mode 100644 index 0000000..03ed66d --- /dev/null +++ b/templates/sidebar/includes/events-list.html @@ -0,0 +1,57 @@ +{% for start_date, events in grouped_events %} +
+
+
+ {{ start_date.strftime("%b") }} +
+
+ {{ start_date.strftime("%d") }} +
+
+ {{ start_date.strftime("%a") }} +
+
+
+ {% for event in events %} +
+
+
{{ event.start.strftime("%H:%M") }}
+
+
{{ event.end.strftime("%H:%M") }}
+
+
+ {% if event.description or event.location %} +
+ {{ event.summary | urlize }} +
+ {% if event.description %} +
+ {#- description doesn't need an icon, just wastes space #} +
{{ event.description | urlize | nl2br }}
+
+ {% endif %} + {% if event.location %} +
+
+
{{ event.location | urlize | nl2br }}
+
+ {% endif %} +
+
+ {% else %} + {{ event.summary }} + {% endif %} +
+ + {%- if "AusbauLab" in event.categories %} +
+ {%- elif event.color %} +
+ {%- else %} +
+ {%- endif %} +
+ {% endfor %} +
+
+{% endfor %} diff --git a/templates/sidebar/includes/footer.html b/templates/sidebar/includes/footer.html new file mode 100644 index 0000000..a50762f --- /dev/null +++ b/templates/sidebar/includes/footer.html @@ -0,0 +1,3 @@ +
+ Kalender abonnieren +
diff --git a/templates/sidebar/includes/header.html b/templates/sidebar/includes/header.html new file mode 100644 index 0000000..12f9c30 --- /dev/null +++ b/templates/sidebar/includes/header.html @@ -0,0 +1 @@ +

Veranstaltungen