Parcourir la source

Add Dateish plugin

This plugin allows arbitrary metadata fields to be automatically
converted to datetime objects, just like the 'date' field.
Dan McGinnis il y a 10 ans
Parent
commit
cdc5ea6d43
3 fichiers modifiés avec 63 ajouts et 0 suppressions
  1. 36 0
      dateish/Readme.rst
  2. 1 0
      dateish/__init__.py
  3. 26 0
      dateish/dateish.py

+ 36 - 0
dateish/Readme.rst

@@ -0,0 +1,36 @@
+Dateish Plugin for Pelican
+==========================
+
+This plugin adds the ability to treat arbitrary metadata fields as datetime
+objects.
+
+Usage
+-----
+
+For example, if you have the following pieces of metadata in an article:
+
+.. code-block:: markdown
+
+    # my_article.markdown
+    Date: 2000-01-01
+    Created_Date: 1999-08-04
+    Idea_Date: 1993-03-04
+
+Normally, the Created_Date and Idea_Date variables will be strings, so you will
+not be able to use the strftime() Jinja filter on them.
+
+With this plugin, you define in your settings file a list of the names of
+the additional metadata fields you want to treat as dates:
+
+.. code-block:: python
+
+    # pelicanconf.py
+    DATEISH_PROPERTIES = ['created_date', 'idea_date']
+
+Then you can use them in templates just like date:
+
+.. code-block:: html+jinja
+
+    # mytemplate.html
+    <p>Created date: {{ article.created_date | strftime('%d %B %Y') }}</p>
+

+ 1 - 0
dateish/__init__.py

@@ -0,0 +1 @@
+from .dateish import *

+ 26 - 0
dateish/dateish.py

@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+"""
+Dateish Plugin for Pelican
+==========================
+
+This plugin adds the ability to treat arbitrary metadata fields as datetime
+objects.
+"""
+
+from pelican import signals
+from pelican.utils import get_date
+
+
+def dateish(generator):
+    if 'DATEISH_PROPERTIES' not in generator.settings:
+        return
+
+    for article in generator.articles:
+        for field in generator.settings['DATEISH_PROPERTIES']:
+            if hasattr(article, field):
+                text = getattr(article, field)
+                as_datetime = get_date(text)
+                setattr(article, field, as_datetime)
+
+def register():
+    signals.article_generator_finalized.connect(dateish)