Mercurial > piecrust2
comparison piecrust/templating/jinjaengine.py @ 326:1ecc0c16ba64 2.0.0a6
data: Better error message for old date formats, add `emaildate` filter.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Mon, 30 Mar 2015 14:43:08 -0700 |
| parents | b7ab1b503510 |
| children | 938be93215cb |
comparison
equal
deleted
inserted
replaced
| 325:c750dc21d55d | 326:1ecc0c16ba64 |
|---|---|
| 1 import re | 1 import re |
| 2 import time | 2 import time |
| 3 import os.path | 3 import os.path |
| 4 import logging | 4 import logging |
| 5 import threading | 5 import threading |
| 6 import email.utils | |
| 6 import strict_rfc3339 | 7 import strict_rfc3339 |
| 7 from jinja2 import Environment, FileSystemLoader, TemplateNotFound | 8 from jinja2 import Environment, FileSystemLoader, TemplateNotFound |
| 8 from jinja2.exceptions import TemplateSyntaxError | 9 from jinja2.exceptions import TemplateSyntaxError |
| 9 from jinja2.ext import Extension, Markup | 10 from jinja2.ext import Extension, Markup |
| 10 from jinja2.lexer import Token, describe_token | 11 from jinja2.lexer import Token, describe_token |
| 147 'wordcount': get_word_count, | 148 'wordcount': get_word_count, |
| 148 'stripoutertag': strip_outer_tag, | 149 'stripoutertag': strip_outer_tag, |
| 149 'stripslash': strip_slash, | 150 'stripslash': strip_slash, |
| 150 'titlecase': title_case, | 151 'titlecase': title_case, |
| 151 'atomdate': get_atom_date, | 152 'atomdate': get_atom_date, |
| 153 'emaildate': get_email_date, | |
| 152 'date': get_date}) | 154 'date': get_date}) |
| 153 | 155 |
| 154 # Backwards compatibility with Twig. | 156 # Backwards compatibility with Twig. |
| 155 if twig_compatibility_mode is True: | 157 if twig_compatibility_mode is True: |
| 156 self.filters['raw'] = self.filters['safe'] | 158 self.filters['raw'] = self.filters['safe'] |
| 237 | 239 |
| 238 def get_atom_date(value): | 240 def get_atom_date(value): |
| 239 return strict_rfc3339.timestamp_to_rfc3339_localoffset(int(value)) | 241 return strict_rfc3339.timestamp_to_rfc3339_localoffset(int(value)) |
| 240 | 242 |
| 241 | 243 |
| 244 def get_email_date(value, localtime=False): | |
| 245 return email.utils.formatdate(value, localtime=localtime) | |
| 246 | |
| 247 | |
| 242 def get_date(value, fmt): | 248 def get_date(value, fmt): |
| 243 if value == 'now': | 249 if value == 'now': |
| 244 value = time.time() | 250 value = time.time() |
| 245 if '%' not in fmt: | 251 if '%' not in fmt: |
| 246 suggest = php_format_to_strftime_format(fmt) | 252 suggest = php_format_to_strftime_format(fmt) |
| 253 if suggest != fmt: | |
| 254 suggest_message = ("You probably want a format that looks " | |
| 255 "like: '%s'." % suggest) | |
| 256 else: | |
| 257 suggest_message = ("We can't suggest a proper date format " | |
| 258 "for you right now, though.") | |
| 247 raise Exception("PieCrust 1 date formats won't work in PieCrust 2. " | 259 raise Exception("PieCrust 1 date formats won't work in PieCrust 2. " |
| 248 "You probably want a format that look like '%s'. " | 260 "%s\n" |
| 249 "Please check the `strftime` formatting page here: " | 261 "Please check the `strftime` formatting page here: " |
| 250 "https://docs.python.org/3/library/datetime.html" | 262 "https://docs.python.org/3/library/datetime.html" |
| 251 "#strftime-and-strptime-behavior" % | 263 "#strftime-and-strptime-behavior" % |
| 252 suggest) | 264 suggest_message) |
| 253 return time.strftime(fmt, time.localtime(value)) | 265 return time.strftime(fmt, time.localtime(value)) |
| 254 | 266 |
| 255 | 267 |
| 256 class PieCrustFormatExtension(Extension): | 268 class PieCrustFormatExtension(Extension): |
| 257 tags = set(['pcformat']) | 269 tags = set(['pcformat']) |
