{{/* The public agenda: one grid per month, all of them built here. Every month between the first event and the last is rendered, plus the current one, and assets/js/agenda.js shows one at a time. Two things fall out of building them all rather than fetching a month at a time: * **With JavaScript off, every month is on the page**, stacked, each under its own heading. Nothing is hidden from a reader who has no script, and nothing is hidden from a crawler either. * Moving between months is instant and makes no request. The weekday arithmetic is the awkward part, because Hugo exposes no AddDate: the length of a month comes from a table plus a leap-year check, and the weekday of the first falls out of formatting that date. It looks like something simpler was missed, and it is not. */}} {{ define "main" }} {{ $lang := .Site.Language.Lang }} {{ $events := where (where .Site.RegularPages "Section" "post") "Params.event_date" "!=" nil }} {{/* The span to render: from the earliest event to the latest, and always including the month the site was built in, so an empty agenda still shows the reader where they are. */}} {{ $months := slice }} {{ range $events }}{{ $months = $months | append (substr .Params.event_date 0 7) }}{{ end }} {{ $months = $months | append (now.Format "2006-01") }} {{ $months = sort (uniq $months) }} {{ $from := index $months 0 }} {{ $to := index $months (sub (len $months) 1) }} {{/* Expanded so a month with nothing in it can still be visited. */}} {{ $span := slice }} {{ range $y := seq (int (substr $from 0 4)) (int (substr $to 0 4)) }} {{ range $m := seq 1 12 }} {{ $key := printf "%04d-%02d" $y $m }} {{ if and (ge $key $from) (le $key $to) }}{{ $span = $span | append $key }}{{ end }} {{ end }} {{ end }} {{ $script := resources.Get "js/agenda.js" | minify | fingerprint }}

{{ .Title }}

{{ with .Content }}
{{ . }}
{{ end }} {{ range $key := $span }} {{ $year := int (substr $key 0 4) }} {{ $month := int (substr $key 5 2) }} {{ $firstDay := printf "%s-01" $key }} {{/* Days in the month: a table, with February decided by the leap rule — every four years, not every hundred, except every four hundred. */}} {{ $lengths := slice 31 28 31 30 31 30 31 31 30 31 30 31 }} {{ $days := index $lengths (sub $month 1) }} {{ if eq $month 2 }} {{ if or (and (eq (mod $year 4) 0) (ne (mod $year 100) 0)) (eq (mod $year 400) 0) }} {{ $days = 29 }} {{ end }} {{ end }} {{/* Monday first, as in Austria and most of Latin America. Go names the weekday; this turns the name into a column offset. */}} {{ $offsets := dict "Mon" 0 "Tue" 1 "Wed" 2 "Thu" 3 "Fri" 4 "Sat" 5 "Sun" 6 }} {{ $lead := index $offsets (($firstDay | time.AsTime).Format "Mon") }}

{{ partial "month-name.html" (dict "month" $month "lang" $lang) }} {{ $year }}

    {{ if gt $lead 0 }}{{ range seq $lead }}{{ end }}{{ end }} {{ range $day := seq 1 $days }} {{ $date := printf "%s-%02d" $key $day }} {{/* By the hour, so two things on one day read in the order they happen rather than in the order they were announced. The private calendar has sorted this way since it was written (apps/board/events.py: `ORDER BY starts_on, COALESCE(starts_at, '99:99')`), and the two show the same events to the same people. An event with no time sorts first here, which is right: all day begins before the evening does. */}} {{ $onThisDay := sort (where $events "Params.event_date" $date) "Params.event_time" }} {{/* The column a day falls in is its weekday, Monday first — the same arithmetic the padding above is made of. On a phone the grid becomes one day per row, where a column position says nothing and the name has to be written out. */}} {{ $weekday := mod (add (sub $day 1) $lead) 7 }}
  1. {{ partial "weekday-name.html" (dict "weekday" $weekday "lang" $lang) }} {{ $day }} {{ range $onThisDay }} {{ with .Params.event_time }} {{ end }}{{ .Title }} {{ end }}
  2. {{ end }}
{{ $inMonth := where (where $events "Params.event_date" "ge" (printf "%s-01" $key)) "Params.event_date" "le" (printf "%s-31" $key) }} {{ if not $inMonth }}

{{ partial "t.html" (dict "key" "noevents" "lang" $lang) }}

{{ end }}
{{ end }}
{{ end }}