From 169d6925b6a246db528b69b67ef9a49b0a1ed474 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 29 Nov 2022 12:18:06 +0100 Subject: list blog posts on koszko.html --- src/koszko_org_website/app.py | 51 ++++++++++++++++++++++ .../templates/en/koszko.html.jinja | 6 +++ src/koszko_org_website/templates/koszko.html.jinja | 11 +++++ .../templates/pl/koszko.html.jinja | 6 +++ 4 files changed, 74 insertions(+) diff --git a/src/koszko_org_website/app.py b/src/koszko_org_website/app.py index e3e6339..84be22a 100644 --- a/src/koszko_org_website/app.py +++ b/src/koszko_org_website/app.py @@ -5,6 +5,7 @@ # Copyright (C) 2021, 2022 Wojtek Kosior import gettext +import re import dataclasses as dc import typing as t @@ -38,6 +39,55 @@ def raise_exception(msg) -> t.NoReturn: raise Exception(msg) +post_titles_pl = { + 'gospel-despite-no-enthusiasm': 'Dobra Nowina pomimo zniechęcenia' +} + +post_titles_en = { + 'gospel-despite-no-enthusiasm': 'Gospel despite lack of enthusiasm' +} + +post_titles = {'en': post_titles_en, 'pl': post_titles_pl} + +@dc.dataclass(frozen=True) +class PostData: + date: str + page_path: str + title: str + + def __lt__(self, other: 'PostData') -> bool: + return (other.date, self.page_path) < (self.date, other.page_path) + +post_filename_re = re.compile( + r''' + ^ + (?P20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]) + - + (?P.*) + \.html\.jinja + $ + ''', + re.VERBOSE +) + +def get_posts(lang: str) -> t.Sequence[PostData]: + lang_dir = here / 'templates' / lang + posts_data = [] + + for template_path in (lang_dir / 'posts').glob('20*'): + filename_match = post_filename_re.match(template_path.name) + assert filename_match is not None + + date = filename_match.group('date') + post_identifier = filename_match.group('post_identifier') + page_path = f'posts/{date}-{post_identifier}.html' + title = post_titles[lang][post_identifier] + + posts_data.append(PostData(date=date, page_path=page_path, title=title)) + + return sorted(posts_data) + + @dc.dataclass(init=False) class Website(flask.Flask): def __init__(self) -> None: @@ -56,6 +106,7 @@ class Website(flask.Flask): } self.jinja_env.globals['raise'] = raise_exception + self.jinja_env.globals['get_posts'] = get_posts def koszko_install_translations(self, locale: str) -> None: translations = gettext.translation( diff --git a/src/koszko_org_website/templates/en/koszko.html.jinja b/src/koszko_org_website/templates/en/koszko.html.jinja index ecf94dc..b6ce215 100644 --- a/src/koszko_org_website/templates/en/koszko.html.jinja +++ b/src/koszko_org_website/templates/en/koszko.html.jinja @@ -153,4 +153,10 @@ Copyright (C) 2021,2022 Wojtek Kosior {{ request_table() }} {% endcall %} + + {% call section('posts') %} + {{ header(2, 'Posts') }} + + {{ posts() }} + {% endcall %} {% endblock %} diff --git a/src/koszko_org_website/templates/koszko.html.jinja b/src/koszko_org_website/templates/koszko.html.jinja index 4253329..14b5bd5 100644 --- a/src/koszko_org_website/templates/koszko.html.jinja +++ b/src/koszko_org_website/templates/koszko.html.jinja @@ -64,3 +64,14 @@ Copyright (C) 2021,2022 Wojtek Kosior {% endmacro %} + +{% macro posts() %} + {% call unordered_list() %} + {% for post_data in get_posts(lang_short) %} + {% call list_entry() %} + {{ link(same_lang_url(post_data.page_path), post_data.title) }} + {{ italic('(' ~ post_data.date ~ ')') }} + {% endcall %} + {% endfor %} + {% endcall %} +{% endmacro %} diff --git a/src/koszko_org_website/templates/pl/koszko.html.jinja b/src/koszko_org_website/templates/pl/koszko.html.jinja index 7828c4a..6dae783 100644 --- a/src/koszko_org_website/templates/pl/koszko.html.jinja +++ b/src/koszko_org_website/templates/pl/koszko.html.jinja @@ -159,4 +159,10 @@ Copyright (C) 2021,2022 Wojtek Kosior {{ request_table() }} {% endcall %} + + {% call section('posts') %} + {{ header(2, 'Posty') }} + + {{ posts() }} + {% endcall %} {% endblock %} -- cgit v1.2.3