From 764745a1a48affe5a483885e6b2f73df3815c591 Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Mon, 29 Nov 2021 22:17:24 +0100 Subject: [PATCH] 1st version --- .gitignore | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ __init__.py | 16 ++++++++++++ j2filters.py | 25 ++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 __init__.py create mode 100644 j2filters.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2745309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,73 @@ +# ---> Vim +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +*.un~ +Session.vim +.netrwhist +*~ + + + +# ---> Pelican +output/ + +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..1c5a371 --- /dev/null +++ b/__init__.py @@ -0,0 +1,16 @@ +from pelican import signals + +from . import j2filters + +import datetime + +def add_filter(pelican): + """Add date filter to Pelican.""" + pelican.env.filters.update({'date': j2filters.j2filter_date}) + pelican.env.filters.update({'date_from_to': j2filters.j2filter_date_from_to}) + pelican.env.filters.update({'date_from_to_long': j2filters.j2filter_date_from_to_long}) + +def register(): + """Plugin registration.""" + signals.generator_init.connect(add_filter) + diff --git a/j2filters.py b/j2filters.py new file mode 100644 index 0000000..d68a1de --- /dev/null +++ b/j2filters.py @@ -0,0 +1,25 @@ +def j2filter_date(dt, frmt): + return dt.strftime(frmt) + + +def j2filter_date_from_to(dts, dte): + if dts.year != dte.year: + out = dts.strftime("%d.%m.%Y") + elif dts.month != dte.month: + out = dts.strftime("%d.%m.") + else: + out = dts.strftime("%d.") + + return out + " - " + dte.strftime("%d.%m.%Y") + + +def j2filter_date_from_to_long(dts, dte): + if dts.year != dte.year: + out = dts.strftime("%d. %B %Y") + elif dts.month != dte.month: + out = dts.strftime("%d. %B") + else: + out = dts.strftime("%d.") + + return out + " - " + dte.strftime("%d. %B %Y") +