From 79eac5c7c5bc57c36aaa32bb9a8b025222d4eb96 Mon Sep 17 00:00:00 2001 From: Stefan Bethke Date: Fri, 16 Oct 2026 18:58:29 +0200 Subject: [PATCH] Make sure missing data is handled gracefully --- README.md | 4 +++- layouts/_shortcodes/schedule.html | 29 ++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7947ee0..fbc1417 100644 --- a/README.md +++ b/README.md @@ -43,4 +43,6 @@ days: Wide screens get one column per day on an hour grid, with tours that start at the same time side by side. The first column shows the hour wherever the first day has no tour in it. On narrow screens a day picker shows one day at a time, starting with today's day during the event. -For a different event than the page's section, pass its name: `{{< schedule 39c3 >}}`. \ No newline at end of file +For a different event than the page's section, pass its name: `{{< schedule 39c3 >}}`. + +Without a data file, or without any tours in it, the shortcode renders nothing and the build logs a warning. To silence it, for example for an upcoming event, add `warning-schedule-empty` to `ignoreLogs` in `hugo.yaml`. \ No newline at end of file diff --git a/layouts/_shortcodes/schedule.html b/layouts/_shortcodes/schedule.html index 5bd1606..1a238ed 100644 --- a/layouts/_shortcodes/schedule.html +++ b/layouts/_shortcodes/schedule.html @@ -6,10 +6,20 @@ Usage: {{< schedule >}} on a section page (the event is the section name), or {{< schedule 39c3 >}}. */ -}} {{- $event := .Get 0 | default .Page.Section -}} -{{- $schedule := index hugo.Data.schedule $event -}} -{{- if not $schedule -}} - {{- errorf "schedule: no data file data/schedule/%s.yaml: %s" $event .Position -}} +{{- /* A missing or empty data file, or one without tours, renders nothing but a warning. */ -}} +{{- $file := false -}} +{{- $days := slice -}} +{{- with hugo.Data.schedule -}} + {{- if isset . $event }}{{ $file = true }}{{ end -}} + {{- with index . $event }}{{ with .days }}{{ $days = . }}{{ end }}{{ end -}} {{- end -}} +{{- $tourCount := 0 -}} +{{- range $days }}{{ with .tours }}{{ $tourCount = add $tourCount (len .) }}{{ end }}{{ end -}} +{{- if not $file -}} + {{- warnidf "warning-schedule-empty" "schedule: no data file data/schedule/%s.yaml: %s" $event .Position -}} +{{- else if not $tourCount -}} + {{- warnidf "warning-schedule-empty" "schedule: no tours in data/schedule/%s.yaml: %s" $event .Position -}} +{{- else -}} {{- $id := printf "schedule-%s" $event | anchorize -}} {{- /* Tour page titles start with the event name ("39C3 Rote Flora"); the cards don't need it. */ -}} {{- $prefix := printf `(?i)^%s\s+` $event -}} @@ -20,7 +30,7 @@ Every hour from the first to the last tour starts a new row ($hours) with a line so an hour without tours still gets a row. */ -}} {{- $slotCount := dict -}} -{{- range $schedule.days -}} +{{- range $days -}} {{- $count := dict -}} {{- range .tours -}} {{- $count = merge $count (dict .time (add (index $count .time | default 0) 1)) -}} @@ -51,17 +61,17 @@ so an hour without tours still gets a row. {{- end -}}
-
-{{- range $column, $day := $schedule.days }} +
+{{- range $column, $day := $days }}

{{ i18n "schedule_day" (dict "Day" .day) }} {{ time.AsTime .date | time.Format "Mon 2.1." }}

    {{- $seen := dict }} -{{- range sort .tours "time" }} +{{- range sort (.tours | default slice) "time" }} {{- $nth := index $seen .time | default 0 }} {{- $seen = merge $seen (dict .time (add $nth 1)) }} {{- $page := $.Page.GetPage .tour }} @@ -84,7 +94,7 @@ so an hour without tours still gets a row. {{- end }} {{- /* Hour lines; where the first day has no tour in the hour's first row, its column shows the hour. They sit in .schedule__days, whose first row holds the day titles, hence the + 1. */}} -{{- $firstDay := index $schedule.days 0 }} +{{- $firstDay := index $days 0 }} {{- range $hours }} {{- $row := .row }}
+{{- end }}