diff --git a/pelicanconf.py b/pelicanconf.py index 40b578b..424f4cb 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -41,9 +41,12 @@ PLUGIN_PATHS = ["plugins"] PLUGINS = [ 'i18n_subsites', "pelican-page-hierarchy", + "always_modified", # 'pdf-img', ] +ALWAYS_MODIFIED = True + # Mirror source structure PATH_METADATA = '(?P.*)\..*' ARTICLE_URL = ARTICLE_SAVE_AS = PAGE_URL = PAGE_SAVE_AS = '{path_no_ext}.html' diff --git a/plugins/__init__.py b/plugins/__init__.py new file mode 100644 index 0000000..1fd5e14 --- /dev/null +++ b/plugins/__init__.py @@ -0,0 +1 @@ +from .always_modified import * diff --git a/plugins/always_modified.py b/plugins/always_modified.py new file mode 100644 index 0000000..0f2578b --- /dev/null +++ b/plugins/always_modified.py @@ -0,0 +1,20 @@ +""" +If "modified" date/time is not defined in article metadata, fall back to the "created" date. +""" + +from pelican import signals +from pelican.contents import Content, Article + +def add_modified(content): + if not isinstance(content, Article): + return + + if not content.settings.get('ALWAYS_MODIFIED', False): + return + + if hasattr(content, 'date') and not hasattr(content, 'modified'): + content.modified = content.date + content.locale_modified = content.locale_date + +def register(): + signals.content_object_init.connect(add_modified) diff --git a/plugins/always_modified/README.md b/plugins/always_modified/README.md new file mode 100644 index 0000000..585e67b --- /dev/null +++ b/plugins/always_modified/README.md @@ -0,0 +1,14 @@ +# Always Modified + +Say you want to sort by modified date/time in a theme template, but not all +your articles have modified date/timestamps explicitly defined in article +metadata. This plugin facilitates that sorting by assuming the modified date +(if undefined) is equal to the created date. + +## Usage + +1. Add `ALWAYS_MODIFIED = True` to your settings file. +2. Now you can sort by modified date in your templates: + + {% for article in articles|sort(reverse=True,attribute='modified') %} + diff --git a/theme/templates/index.html b/theme/templates/index.html index 82152b7..4f4e424 100644 --- a/theme/templates/index.html +++ b/theme/templates/index.html @@ -2,7 +2,7 @@ {% block content %} -{% for article in articles_page.object_list %} +{% for article in articles_page.object_list | sort(reverse=True,attribute='modified') %}

{{ article.title }}