When I rewrote this website using Pelican, I wanted to use three letter month names
(i.e. jan
, feb
, mar
) in the URLs. In Pelican you can customize the URL of an article by setting
the ARTICLE_URL
and ARTICLE_SAVE_AS
variables, like this:
ARTICLE_URL = '/{date:%Y}/{date:%b}/{date:%d}/{slug}'
ARTICLE_SAVE_AS = '{date:%Y}/{date:%b}/{date:%d}/{slug}/index.html'
Pelican constructs the URLs and save paths by calling these strings’ format methods, with a bunch of metadata as arguments. This allows you to use strftime format codes in the URLs as in the above example.
However, apparently strftime()
doesn’t have a format code for the month name in lowercase, and I prefer to
not use uppercase letters in URLs.
My solution to this was to write a minimal Pelican plugin
that adds the lowercase month name as a separate variable to the metadata, and then I can use that variable
in the ARTICLE_URL
and ARTICLE_SAVE_AS
settings.
Here is the plugin:
from pelican import signals
def add_lowercase_month_to_metadata(content):
if "date" in content.metadata:
content.metadata["lowercase_month"] = content.metadata["date"].strftime("%B").lower()
content.metadata["lowercase_month_short"] = content.metadata["date"].strftime("%b").lower()
def register():
signals.content_object_init.connect(add_lowercase_month_to_metadata)
And here are my new URL settings:
ARTICLE_URL = '/{date:%Y}/{lowercase_month_short}/{date:%d}/{slug}'
ARTICLE_SAVE_AS = '{date:%Y}/{lowercase_month_short}/{date:%d}/{slug}/index.html'
Note that the plugin must be activated by adding it to the Pelican PLUGINS
setting.
See the Pelican docs on plugins
for info on how to do that.
Commments