comparison piecrust/templating/jinjaengine.py @ 41:1a30e3d7ee47

Error out if `date` filter is used with PHP date formats.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 21 Aug 2014 22:29:03 -0700
parents 19f3ac27c3d5
children 002fa58f54dc
comparison
equal deleted inserted replaced
40:4bc166d3a830 41:1a30e3d7ee47
11 from pygments.lexers import get_lexer_by_name, guess_lexer 11 from pygments.lexers import get_lexer_by_name, guess_lexer
12 from piecrust.data.paginator import Paginator 12 from piecrust.data.paginator import Paginator
13 from piecrust.rendering import format_text 13 from piecrust.rendering import format_text
14 from piecrust.routing import CompositeRouteFunction 14 from piecrust.routing import CompositeRouteFunction
15 from piecrust.templating.base import TemplateEngine, TemplateNotFoundError 15 from piecrust.templating.base import TemplateEngine, TemplateNotFoundError
16 from piecrust.uriutil import multi_replace
16 17
17 18
18 logger = logging.getLogger(__name__) 19 logger = logging.getLogger(__name__)
19 20
20 21
166 167
167 168
168 def get_date(value, fmt): 169 def get_date(value, fmt):
169 if value == 'now': 170 if value == 'now':
170 value = time.time() 171 value = time.time()
172 if '%' not in fmt:
173 suggest = php_format_to_strftime_format(fmt)
174 raise Exception("PieCrust 1 date formats won't work in PieCrust 2. "
175 "You probably want a format that look like '%s'. "
176 "Please check the `strftime` formatting page here: "
177 "https://docs.python.org/3/library/datetime.html"
178 "#strftime-and-strptime-behavior" %
179 suggest)
171 return time.strftime(fmt, time.localtime(value)) 180 return time.strftime(fmt, time.localtime(value))
172 181
173 182
174 class PieCrustHighlightExtension(Extension): 183 class PieCrustHighlightExtension(Extension):
175 tags = set(['highlight', 'geshi']) 184 tags = set(['highlight', 'geshi'])
270 return rv 279 return rv
271 rv = caller() 280 rv = caller()
272 self.environment.piecrust_cache[key] = rv 281 self.environment.piecrust_cache[key] = rv
273 return rv 282 return rv
274 283
284
285 def php_format_to_strftime_format(fmt):
286 replacements = {
287 'd': '%d',
288 'D': '%a',
289 'j': '%d',
290 'l': '%A',
291 'w': '%w',
292 'z': '%j',
293 'W': '%W',
294 'F': '%B',
295 'm': '%m',
296 'M': '%b',
297 'n': '%m',
298 'y': '%Y',
299 'Y': '%y',
300 'g': '%I',
301 'G': '%H',
302 'h': '%I',
303 'H': '%H',
304 'i': '%M',
305 's': '%S',
306 'e': '%Z',
307 'O': '%z',
308 'c': '%Y-%m-%dT%H:%M:%SZ'}
309 return multi_replace(fmt, replacements)
310