From 482437c9b71bf5948f33a925f71d7da4cd40ea6b Mon Sep 17 00:00:00 2001 From: webrian Date: Sat, 14 Oct 2023 19:29:51 +0200 Subject: [PATCH] Add Python script to write dynamically courses page --- .gitignore | 5 +- _data/kurse-cours.csv | 2 + _includes/navigation-fr.html | 105 ++++++++++++++++++----------------- de/kurse/index.md | 3 +- fr/cours/index.md | 11 ++++ scripts/template-de.md | 12 ++++ scripts/template-fr.md | 12 ++++ scripts/write-courses.py | 37 ++++++++++++ 8 files changed, 134 insertions(+), 53 deletions(-) create mode 100644 _data/kurse-cours.csv create mode 100644 fr/cours/index.md create mode 100644 scripts/template-de.md create mode 100644 scripts/template-fr.md create mode 100755 scripts/write-courses.py diff --git a/.gitignore b/.gitignore index 2e018be..db68958 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ _site/ +# Ignore dynamically created includes +_includes/courstable-* .sass-cache/ .jekyll-cache/ .jekyll-metadata @@ -7,4 +9,5 @@ _site/ vendor/bundle Gemfile Gemfile.lock -scripts/* +# Ignore temporary files from vim +*.swp diff --git a/_data/kurse-cours.csv b/_data/kurse-cours.csv new file mode 100644 index 0000000..8e8f4ac --- /dev/null +++ b/_data/kurse-cours.csv @@ -0,0 +1,2 @@ +date;title_de;title_fr;url +15.11.2023;QGIS Kurs OST;Cours QGIS OST;https://ost.ch diff --git a/_includes/navigation-fr.html b/_includes/navigation-fr.html index eb12fc1..192671d 100644 --- a/_includes/navigation-fr.html +++ b/_includes/navigation-fr.html @@ -1,54 +1,57 @@ diff --git a/de/kurse/index.md b/de/kurse/index.md index 1711c8f..e4920ac 100644 --- a/de/kurse/index.md +++ b/de/kurse/index.md @@ -7,4 +7,5 @@ lang: de # Aktuelle Kurse -# Vergangene Kurse \ No newline at end of file + +{% include courstable-de.html %} diff --git a/fr/cours/index.md b/fr/cours/index.md new file mode 100644 index 0000000..9aefbc7 --- /dev/null +++ b/fr/cours/index.md @@ -0,0 +1,11 @@ +--- + +layout: plain-fr +lang: fr + +--- + +# Cours Actuels + + +{% include courstable-fr.html %} diff --git a/scripts/template-de.md b/scripts/template-de.md new file mode 100644 index 0000000..5728caa --- /dev/null +++ b/scripts/template-de.md @@ -0,0 +1,12 @@ + + + + + + {% for d in data %} + + + + {% endfor %} + +
DatumKurzbeschriebLink
{{ d[0] }}{{ d[1] }}{{ d[2] }}
diff --git a/scripts/template-fr.md b/scripts/template-fr.md new file mode 100644 index 0000000..4c30abe --- /dev/null +++ b/scripts/template-fr.md @@ -0,0 +1,12 @@ + + + + + + {% for d in data %} + + + + {% endfor %} + +
DateBrève descriptionLink
{{ d[0] }}{{ d[1] }}{{ d[2] }}
diff --git a/scripts/write-courses.py b/scripts/write-courses.py new file mode 100755 index 0000000..3aa1080 --- /dev/null +++ b/scripts/write-courses.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + + +import csv +import sys +from jinja2 import Environment, FileSystemLoader + +def run(): + + environment = Environment(loader=FileSystemLoader("./")) + + for lang in ( {"lang": "de", "dir": "kurse"}, {"lang": "fr", "dir": "cours"} ): + data = [] + + template = environment.get_template("template-%s.md" % lang['lang']) + + filename = "../_includes/courstable-%s.html" % (lang['lang']) + + with open("../_data/kurse-cours.csv", newline='') as csvfile: + reader = csv.reader(csvfile, delimiter=";") + + # Skip header row + next(reader, None) + + for row in reader: + data.append((row[0], row[1], row[2])) + + content = template.render( + data=data + ) + + with open(filename, mode="w", encoding="utf-8") as message: + message.write(content) + print(f"... wrote {filename}") + +if __name__ == "__main__": + sys.exit(run())