# HG changeset patch # User Ludovic Chabant # Date 1456900024 28800 # Node ID cbd5cdec0695e4ac7945768e8b31aeee53c99e14 # Parent 2f780b1915416fa9e70eaefc5a9bfeccf9fdeff3 jinja: Add `md5` filter. diff -r 2f780b191541 -r cbd5cdec0695 piecrust/templating/jinjaengine.py --- 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)