changeset 662:cbd5cdec0695

jinja: Add `md5` filter.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 01 Mar 2016 22:27:04 -0800
parents 2f780b191541
children 3ceeca7bb71c
files piecrust/templating/jinjaengine.py
diffstat 1 files changed, 10 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/templating/jinjaengine.py	Tue Mar 01 22:26:09 2016 -0800
+++ b/piecrust/templating/jinjaengine.py	Tue Mar 01 22:27:04 2016 -0800
@@ -1,6 +1,7 @@
 import re
 import time
 import os.path
+import hashlib
 import logging
 import threading
 import email.utils
@@ -221,6 +222,7 @@
                 'stripoutertag': strip_outer_tag,
                 'stripslash': strip_slash,
                 'titlecase': title_case,
+                'md5': make_md5,
                 'atomdate': get_xml_date,
                 'xmldate': get_xml_date,
                 'emaildate': get_email_date,
@@ -310,13 +312,21 @@
     return value.title()
 
 
+def make_md5(value):
+    return hashlib.md5(value.lower().encode('utf8')).hexdigest()
+
+
 def get_xml_date(value):
+    """ Formats timestamps like 1985-04-12T23:20:50.52Z
+    """
     if value == 'now':
         value = time.time()
     return strict_rfc3339.timestamp_to_rfc3339_localoffset(int(value))
 
 
 def get_email_date(value, localtime=False):
+    """ Formats timestamps like Fri, 09 Nov 2001 01:08:47 -0000
+    """
     if value == 'now':
         value = time.time()
     return email.utils.formatdate(value, localtime=localtime)