[Jinja2] init

This commit is contained in:
coja
2026-05-02 23:18:51 +02:00
parent 9eca59f80f
commit 0724816c88
13 changed files with 350 additions and 352 deletions

252
prep.py
View File

@@ -1,7 +1,7 @@
#! /usr/bin/env python3
from jinja2 import Environment, FileSystemLoader
import csv
from datetime import datetime
import os
DAYS_SR = ["PON", "UTO", "SRE", "ČET", "PET", "SUB", "NED"]
DAYS_EN = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
@@ -18,6 +18,8 @@ TYPES_DICT = {
"party": ("zabava", "entertainment"),
}
env = Environment(loader=FileSystemLoader('template'))
def load_events(csv_path:str) -> list[dict]:
events = []
with open(csv_path, encoding='utf-8') as csv_file:
@@ -39,51 +41,11 @@ def load_events(csv_path:str) -> list[dict]:
events.append(current_event)
return events
def build_html(events: list[dict], dayNames: list[str], typesNames: dict) -> str:
events_html = []
for event in events:
title = event["title"]
location = event["location"]
date = event["date"]
date = dayNames[date.weekday()]+", "+str(date.day)+". "+str(date.month)+". "+str(date.year)+", "
time = event["time"]+"h"
event_html = []
event_html.append(f"<div class='date'>{date} {time}</div>")
if event["link"] != "":
event_html.append(f"<div class='title'><a href=\"{event['link']}\">{title}</a></div>")
else:
event_html.append(f"<div class='title'>{title}</div>")
if "https://" in location:
place,link = location.split("https://")
event_html.append(f"<div class='place'><a href=\"https://{link}\" target='_blank'>@{place.strip()}</a></div>")
else:
event_html.append(f"<div class='place'>@{location.strip()}</div>")
if len(event["types"]) != 0:
types_list = "<div class='types'>"
last_item = event["types"][-1]
for t in event["types"]:
if typesNames.get(t) is not None:
types_list += typesNames.get(t)
if t != last_item:
types_list += ', '
else:
print(f"Unknown type {t}!")
types_list += "</div>"
event_html.append(types_list)
event_html = "".join(event_html)
events_html.append(f"\n<div class='event'>{event_html}</div>")
return events_html
def build_ical(events: list[dict]) -> str:
today = datetime.today().now()
# Header
events_ical = ""
with open("template/head.ical", "r") as file:
events_ical += file.read()
# Events
for event in events:
title = event["title"]
location = event["location"]
@@ -92,85 +54,165 @@ def build_ical(events: list[dict]) -> str:
url = event["link"]
uid = str(date.month).zfill(2) + str(date.day).zfill(2) + time[:2]
date = str(date.year) + str(date.month).zfill(2) + str(date.day).zfill(2)
date_str = str(date.year) + str(date.month).zfill(2) + str(date.day).zfill(2)
created = str(today.year) + str(today.month).zfill(2) + str(today.day).zfill(2) + "T" + str(today.hour).zfill(2) + str(today.minute).zfill(2) + str(today.second).zfill(2) + "Z"
date = date + "T" + time.replace(":", "") + "00"
date_str = date_str + "T" + time.replace(":", "") + "00"
event_template = ""
with open("template/event.ical", "r") as file:
event_template += file.read()
event_template = event_template.replace("<!--UID-->", uid)
event_template = event_template.replace("<!--CREATED-->", created)
event_template = event_template.replace("<!--DATE-->", date)
event_template = event_template.replace("<!--TITLE-->", title)
event_template = event_template.replace("<!--URL-->", url)
if location.startswith("DC Krov"):
event_template = event_template.replace("<!--LOCATION-->", "DC Krov\\, Kraljice Marije 47\\, 6\\, Beograd\\, Serbia")
elif location.startswith("Matematički fakultet (Učionica 153)"):
event_template = event_template.replace("<!--LOCATION-->", "Matematički fakultet\\, Svetog Nikole 39\\, Beograd\\, Serbia")
else:
event_template = event_template.replace("<!--LOCATION-->", location)
event_template_str = env.get_template("event.ical").render(
UID=uid,
CREATED=created,
DATE=date_str,
TITLE=title,
URL=url,
LOCATION="DC Krov\\, Kraljice Marije 47\\, 6\\, Beograd\\, Serbia" if location.startswith("DC Krov") else ("Matematički fakultet\\, Svetog Nikole 39\\, Beograd\\, Serbia" if location.startswith("Matematički fakultet (Učionica 153)") else location)
)
events_ical += event_template_str
events_ical += event_template
# Footer
with open("template/end.ical", "r") as file:
events_ical += file.read()
return events_ical
events = sorted(load_events("dogadjaji.csv"), key=lambda e: e["date"])
def render_page(template_name, output_path, context):
template = env.get_template(template_name)
with open(output_path, "w") as file:
file.write(template.render(context))
# Main execution
events = sorted(load_events("dogadjaji.csv"), key=lambda e: e["date"])
today = datetime.today().date()
past_events = list(filter(lambda e: e["date"] <= today, events))
past_events.reverse()
new_events = list(filter(lambda e: e["date"] >= today, events))
past_events = sorted([e for e in events if e["date"] <= today], key=lambda e: e["date"], reverse=True)
new_events = [e for e in events if e["date"] >= today]
sr_types = {k: v[0] for k, v in TYPES_DICT.items()}
en_types = {k: v[1] for k, v in TYPES_DICT.items()}
page_template = ""
# Build Serbian Pages
render_page("events-sr.html", "pages/sr/events.html", {
"lang": "sr",
"title": "Događaji",
"sr_link": "/events_archive",
"events_html": env.from_string("""
{% for event in events %}
<div class='event'>
<div class='date'>{{ event.date.strftime('%a, %d. %b. %Y') }}, {{ event.time }}h</div>
{% if event.link %}
<div class='title'><a href="{{ event.link }}">{{ event.title }}</a></div>
{% else %}
<div class='title'>{{ event.title }}</div>
{% endif %}
{% if 'https://' in event.location %}
{% set place, link = event.location.split('https://') %}
<div class='place'><a href="https://{{ link }}" target='_blank'>@{{ place.strip() }}</a></div>
{% else %}
<div class='place'>@{{ event.location.strip() }}</div>
{% endif %}
{% if event.types %}
<div class='types'>
{% for t in event.types %}
{{ types_names.get(t, t) }}{% if not loop.last %}, {% endif %}
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
""").render(events=new_events, types_names=sr_types)
})
sr_types = {}
en_types = {}
render_page("events-en.html", "pages/en/events.html", {
"lang": "en",
"title": "Events",
"sr_link": "/events_archive",
"events_html": env.from_string("""
{% for event in events %}
<div class='event'>
<div class='date'>{{ event.date.strftime('%a, %d. %b. %Y') }}, {{ event.time }}h</div>
{% if event.link %}
<div class='title'><a href="{{ event.link }}">{{ event.title }}</a></div>
{% else %}
<div class='title'>{{ event.title }}</div>
{% endif %}
{% if 'https://' in event.location %}
{% set place, link = event.location.split('https://') %}
<div class='place'><a href="https://{{ link }}" target='_blank'>@{{ place.strip() }}</a></div>
{% else %}
<div class='place'>@{{ event.location.strip() }}</div>
{% endif %}
{% if event.types %}
<div class='types'>
{% for t in event.types %}
{{ types_names.get(t, t) }}{% if not loop.last %}, {% endif %}
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
""").render(events=new_events, types_names=en_types)
})
for key, value_pair in TYPES_DICT.items():
sr_types[key] = value_pair[0]
en_types[key] = value_pair[1]
# Build Archive Pages
render_page("events_archive-sr.html", "pages/sr/events_archive.html", {
"lang": "sr",
"title": "Arhiva događaja",
"sr_link": "/events",
"events_html": env.from_string("""
{% for event in events %}
<div class='event'>
<div class='date'>{{ event.date.strftime('%a, %d. %b. %Y') }}, {{ event.time }}h</div>
{% if event.link %}
<div class='title'><a href="{{ event.link }}">{{ event.title }}</a></div>
{% else %}
<div class='title'>{{ event.title }}</div>
{% endif %}
{% if 'https://' in event.location %}
{% set place, link = event.location.split('https://') %}
<div class='place'><a href="https://{{ link }}" target='_blank'>@{{ place.strip() }}</a></div>
{% else %}
<div class='place'>@{{ event.location.strip() }}</div>
{% endif %}
{% if event.types %}
<div class='types'>
{% for t in event.types %}
{{ types_names.get(t, t) }}{% if not loop.last %}, {% endif %}
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
""").render(events=past_events, types_names=sr_types)
})
# Build Serbian Events page
new_events_html = build_html(new_events, DAYS_SR, sr_types)
with open("template/events-sr.html", "r") as file:
page_template = ([line for line in file])
with open("pages/sr/events.html", "w") as file:
file.writelines(page_template + new_events_html)
# Build English Events page
new_events_html = build_html(new_events, DAYS_EN, en_types)
with open("template/events-en.html", "r") as file:
page_template = ([line for line in file])
with open("pages/en/events.html", "w") as file:
file.writelines(page_template + new_events_html)
# Build Serbian Archive page
past_events_html = build_html(past_events, DAYS_SR, sr_types)
with open("template/events_archive-sr.html", "r") as file:
page_template = ([line for line in file])
with open("pages/sr/events_archive.html", "w") as file:
file.writelines(page_template + past_events_html)
# Build English Archive page
past_events_html = build_html(past_events, DAYS_EN, en_types)
with open("template/events_archive-en.html", "r") as file:
page_template = ([line for line in file])
with open("pages/en/events_archive.html", "w") as file:
file.writelines(page_template + past_events_html)
new_events_ical = build_ical(new_events)
render_page("events_archive-en.html", "pages/en/events_archive.html", {
"lang": "en",
"title": "Events archive",
"sr_link": "/en/events",
"events_html": env.from_string("""
{% for event in events %}
<div class='event'>
<div class='date'>{{ event.date.strftime('%a, %d. %b. %Y') }}, {{ event.time }}h</div>
{% if event.link %}
<div class='title'><a href="{{ event.link }}">{{ event.title }}</a></div>
{% else %}
<div class='title'>{{ event.title }}</div>
{% endif %}
{% if 'https://' in event.location %}
{% set place, link = event.location.split('https://') %}
<div class='place'><a href="https://{{ link }}" target='_blank'>@{{ place.strip() }}</a></div>
{% else %}
<div class='place'>@{{ event.location.strip() }}</div>
{% endif %}
{% if event.types %}
<div class='types'>
{% for t in event.types %}
{{ types_names.get(t, t) }}{% if not loop.last %}, {% endif %}
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
""").render(events=past_events, types_names=en_types)
})
# Build ical
with open("site/events.ical", "w") as file:
file.write(build_ical(new_events))