Fix #1198, enable custom locale in template rendering, fixes links

reverts getpelican/pelican@ddcccfeaa9

If one used a locale that made use of unicode characters (like fr_FR.UTF-8)
the files on disk would be in correct locale while links would be to C.

Uses a SafeDatetime class that works with unicode format strigns
by using custom strftime to prevent ascii decoding errors with Python2.

Also added unicode decoding for the calendar module to fix period
archives.
This commit is contained in:
Ondrej Grover 2014-04-27 10:25:57 +02:00 committed by Deniz Turgut
commit 3f6b130d6e
91 changed files with 5704 additions and 77 deletions

View file

@ -14,6 +14,7 @@ import shutil
import traceback
import pickle
import hashlib
import datetime
from collections import Hashable
from contextlib import contextmanager
@ -56,7 +57,10 @@ def strftime(date, date_format):
for candidate in candidates:
# test for valid C89 directives only
if candidate[1] in 'aAbBcdfHIjmMpSUwWxXyYzZ%':
formatted = date.strftime(candidate)
if isinstance(date, SafeDatetime):
formatted = date.strftime(candidate, safe=False)
else:
formatted = date.strftime(candidate)
# convert Py2 result to unicode
if not six.PY3 and enc is not None:
formatted = formatted.decode(enc)
@ -68,6 +72,17 @@ def strftime(date, date_format):
return template % tuple(formatted_candidates)
class SafeDatetime(datetime.datetime):
'''Subclass of datetime that works with utf-8 format strings on PY2'''
def strftime(self, fmt, safe=True):
'''Uses our custom strftime if supposed to be *safe*'''
if safe:
return strftime(self, fmt)
else:
return super(SafeDatetime, self).strftime(fmt)
class DateFormatter(object):
'''A date formatter object used as a jinja filter
@ -183,8 +198,10 @@ def get_date(string):
If no format matches the given date, raise a ValueError.
"""
string = re.sub(' +', ' ', string)
default = SafeDatetime.now().replace(hour=0, minute=0,
second=0, microsecond=0)
try:
return dateutil.parser.parse(string)
return dateutil.parser.parse(string, default=default)
except (TypeError, ValueError):
raise ValueError('{0!r} is not a valid date'.format(string))