From c4eb6a34e1f8d833a4b385207aa1d4cb9d08604c Mon Sep 17 00:00:00 2001 From: Daniel Holbach Date: Wed, 7 Dec 2022 13:59:38 +0100 Subject: [PATCH] calendar: use data template instead Signed-off-by: Daniel Holbach --- content/en/.gitignore | 4 -- data/.gitignore | 2 + hack/import-calendar.py | 75 +++++++---------------- layouts/shortcodes/blocks/calendar.html | 31 +++++++++- layouts/shortcodes/blocks/next_event.html | 4 +- 5 files changed, 55 insertions(+), 61 deletions(-) create mode 100644 data/.gitignore diff --git a/content/en/.gitignore b/content/en/.gitignore index 35682013..93c8019a 100644 --- a/content/en/.gitignore +++ b/content/en/.gitignore @@ -1,7 +1,3 @@ -# Generated -calendar_include.html -next_event_include.html - # Imported community.md contributing.md diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 00000000..f4fd6d4d --- /dev/null +++ b/data/.gitignore @@ -0,0 +1,2 @@ +calendar.yaml + diff --git a/hack/import-calendar.py b/hack/import-calendar.py index f6912485..2eb1bf75 100755 --- a/hack/import-calendar.py +++ b/hack/import-calendar.py @@ -25,14 +25,13 @@ from icalendar import Calendar import pytz import recurring_ical_events import urllib3 +import yaml CAL_URL = 'https://lists.cncf.io/g/cncf-flux-dev/ics/4130481/1290943905/feed.ics' TOP_LEVEL_DIR = os.path.realpath( os.path.join(os.path.dirname(__file__), '..')) -CONTENT_DIR = os.path.join(TOP_LEVEL_DIR, 'content/en') -CALENDAR_INCLUDE_HTML = os.path.join(CONTENT_DIR, 'calendar_include.html') -NEXT_EVENT_INCLUDE_HTML = os.path.join(CONTENT_DIR, 'next_event_include.html') +CALENDAR_YAML = os.path.join(TOP_LEVEL_DIR, 'data/calendar.yaml') URL_RE = re.compile(r"((https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", re.MULTILINE|re.UNICODE) @@ -54,7 +53,6 @@ def fix_double_url(text): return DOUBLE_URL_RE.sub(r"\1", text) - def download_calendar(): http = urllib3.PoolManager() r = http.request('GET', CAL_URL) @@ -73,6 +71,7 @@ def read_organizer(event): return {"name": name, "email": email} + def read_calendar(cal): events = [] gcal = Calendar.from_ical(cal) @@ -112,66 +111,36 @@ def format_location_html(event): return html -def write_events_html(events): - if os.path.exists(CALENDAR_INCLUDE_HTML): - os.remove(CALENDAR_INCLUDE_HTML) +def write_events_yaml(ical): + if os.path.exists(CALENDAR_YAML): + os.remove(CALENDAR_YAML) - if not events: + if not ical: return - html = """ - """ - - f = open(CALENDAR_INCLUDE_HTML, 'w') - f.write(html) - f.close() - - with open(NEXT_EVENT_INCLUDE_HTML, 'w') as f: - event = events[0] - if not event['location'].startswith('http'): - event['location'] = '/#calendar' - f.write('📆 Next event: {date} {time} UTC: {title}'.format( - where=event['location'], - date=event['time'].strftime('%F'), - time=event['time'].strftime('%H:%M'), - title=event['title'])) - f.close() def main(): cal = download_calendar() if not cal: sys.exit(1) events = read_calendar(cal) - write_events_html(events) + write_events_yaml(events) if __name__ == '__main__': try: diff --git a/layouts/shortcodes/blocks/calendar.html b/layouts/shortcodes/blocks/calendar.html index 3cb72b42..49911e5c 100644 --- a/layouts/shortcodes/blocks/calendar.html +++ b/layouts/shortcodes/blocks/calendar.html @@ -1,10 +1,35 @@ {{ $_hugo_config := `{ "version": 1 }` }}

Our Team Calendar

- {{ if (fileExists "/content/en/calendar_include.html") -}}

The upcoming meetings, talks and community events in the next month are listed below. (All times are UTC.)

- {{ readFile "/content/en/calendar_include.html" | safeHTML }} - {{ end }} + +
    + {{ range $.Site.Data.calendar }} +
  • +
    +
    {{ .date }}
    +
    {{ .time }}
    +
    {{ .label }}
    +
    +
    + + + {{ .description | safeHTML }} +
    +
  • + {{ end }} +
+

See this page for more detail and subscription options.

diff --git a/layouts/shortcodes/blocks/next_event.html b/layouts/shortcodes/blocks/next_event.html index b6cbb647..d30a76f8 100644 --- a/layouts/shortcodes/blocks/next_event.html +++ b/layouts/shortcodes/blocks/next_event.html @@ -2,5 +2,7 @@ {{ $blockID := printf "td-cover-block-%d" .Ordinal }}

-{{- readFile "/content/en/next_event_include.html" | safeHTML -}} +{{ range first 1 $.Site.Data.calendar }} +📆 Next event: {{ .date }} {{ .time }} UTC: {{ .label }} +{{ end }}