Quellcode durchsuchen

Merge pull request #517 from howthebodyworks/always_modified

Add always_modified plugin
Justin Mayer vor 9 Jahren
Ursprung
Commit
5db0c91a65
4 geänderte Dateien mit 37 neuen und 0 gelöschten Zeilen
  1. 2 0
      Readme.rst
  2. 14 0
      always_modified/README.md
  3. 1 0
      always_modified/__init__.py
  4. 20 0
      always_modified/always_modified.py

+ 2 - 0
Readme.rst

@@ -36,6 +36,8 @@ Plugin descriptions
 ========================  ===========================================================
 Plugin                    Description
 ========================  ===========================================================
+Always modified           Copy created date metadata into modified date for easy "latest updates" indexes
+
 AsciiDoc reader           Use AsciiDoc to write your posts.
 
 Asset management          Use the Webassets module to manage assets such as CSS and JS files.

+ 14 - 0
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') %}
+

+ 1 - 0
always_modified/__init__.py

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

+ 20 - 0
always_modified/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)