forked from github/pelican
adds a 'strftime' jinja fiter that uses LOCALE
This commit is contained in:
parent
70a72545ce
commit
2790446906
3 changed files with 43 additions and 2 deletions
|
|
@ -78,7 +78,7 @@ Sorting
|
|||
-------
|
||||
|
||||
URL wrappers (currently categories, tags, and authors), have
|
||||
comparison methods that allow them to be easily sorted by name:
|
||||
comparison methods that allow them to be easily sorted by name::
|
||||
|
||||
{% for tag, articles in tags|sort %}
|
||||
|
||||
|
|
@ -87,6 +87,24 @@ command`__ has a number of options.
|
|||
|
||||
__ http://jinja.pocoo.org/docs/templates/#sort
|
||||
|
||||
|
||||
Date Formatting
|
||||
---------------
|
||||
|
||||
Pelican formats the date with according to your settings and locale
|
||||
(``DATE_FORMATS``/``DEFAULT_DATE_FORMAT``) and provides a
|
||||
``locale_date`` attribute. On the other hand, ``date`` attribute will
|
||||
be a `datetime`_ object. If you need custom formatting for a date
|
||||
different than your settings, use the Jinja filter ``strftime``
|
||||
that comes with Pelican. Usage is same as Python `strftime`_ format,
|
||||
but the filter will do the right thing and format your date according
|
||||
to the locale given in your settings::
|
||||
|
||||
{{ article.date|strftime('%d %B %Y') }}
|
||||
|
||||
.. _datetime: http://docs.python.org/2/library/datetime.html#datetime-objects
|
||||
.. _strftime: http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
|
||||
|
||||
index.html
|
||||
----------
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ from pelican.contents import (
|
|||
Article, Page, Category, Static, is_valid_content
|
||||
)
|
||||
from pelican.readers import read_file
|
||||
from pelican.utils import copy, process_translations, mkdir_p
|
||||
from pelican.utils import copy, process_translations, mkdir_p, DateFormatter
|
||||
from pelican import signals
|
||||
import pelican.utils
|
||||
|
||||
|
|
@ -65,6 +65,9 @@ class Generator(object):
|
|||
|
||||
logger.debug('template list: {0}'.format(self.env.list_templates()))
|
||||
|
||||
# provide utils.strftime as a jinja filter
|
||||
self.env.filters.update({'strftime': DateFormatter()})
|
||||
|
||||
# get custom Jinja filters from user settings
|
||||
custom_filters = self.settings.get('JINJA_FILTERS', {})
|
||||
self.env.filters.update(custom_filters)
|
||||
|
|
|
|||
|
|
@ -65,6 +65,26 @@ def strftime(date, date_format):
|
|||
return template % tuple(formatted_candidates)
|
||||
|
||||
|
||||
class DateFormatter(object):
|
||||
'''A date formatter object used as a jinja filter
|
||||
|
||||
Uses the `strftime` implementation and makes sure jinja uses the locale
|
||||
defined in LOCALE setting
|
||||
'''
|
||||
|
||||
def __init__(self):
|
||||
self.locale = locale.setlocale(locale.LC_TIME)
|
||||
|
||||
def __call__(self, date, date_format):
|
||||
old_locale = locale.setlocale(locale.LC_TIME)
|
||||
locale.setlocale(locale.LC_TIME, self.locale)
|
||||
|
||||
formatted = strftime(date, date_format)
|
||||
|
||||
locale.setlocale(locale.LC_TIME, old_locale)
|
||||
return formatted
|
||||
|
||||
|
||||
def python_2_unicode_compatible(klass):
|
||||
"""
|
||||
A decorator that defines __unicode__ and __str__ methods under Python 2.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue