"""Jinja2-ports of many of Django's default filters. TODO: Most of the filters in here need to be updated for autoescaping. """ from coffin.template import Library from jinja2.runtime import Undefined from jinja2 import filters register = Library() def url(view_name, *args, **kwargs): from coffin.template.defaulttags import url return url._reverse(view_name, args, kwargs) register.filter(url, jinja2_only=True) register.object(url) @register.filter(jinja2_only=True) def timesince(value, *arg): if value is None or isinstance(value, Undefined): return u'' from django.utils.timesince import timesince return timesince(value, *arg) @register.filter(jinja2_only=True) def timeuntil(value, *args): if value is None or isinstance(value, Undefined): return u'' from django.utils.timesince import timeuntil return timeuntil(value, *args)
def jinja_safe_output(value): return Markup(value) def django_safe_output(value): return mark_safe(value) def unsafe_output(value): return unicode(value) def django_raw_output(value): return value def django_escape_output(value): # Make sure the value is converted to unicode first, because otherwise, # if it is already SafeData (for example, when coming from the template # code), then mark_for_escaping would do nothing. We want to guarantee # a EscapeData return value in this filter though. return mark_for_escaping(unicode(value)) from coffin.template import Library register = Library() register.filter('needing_autoescape', needing_autoescape) register.filter('jinja_safe_output', jinja_safe_output) register.filter('django_safe_output', django_safe_output) register.filter('django_raw_output', django_raw_output) register.filter('unsafe_output', unsafe_output) register.filter('django_escape_output', django_escape_output)
return Markup(value) def django_safe_output(value): return mark_safe(value) def unsafe_output(value): return unicode(value) def django_raw_output(value): return value def django_escape_output(value): # Make sure the value is converted to unicode first, because otherwise, # if it is already SafeData (for example, when coming from the template # code), then mark_for_escaping would do nothing. We want to guarantee # a EscapeData return value in this filter though. return mark_for_escaping(unicode(value)) from coffin.template import Library register = Library() register.filter('needing_autoescape', needing_autoescape) register.filter('jinja_safe_output', jinja_safe_output) register.filter('django_safe_output', django_safe_output) register.filter('django_raw_output', django_raw_output) register.filter('unsafe_output', unsafe_output) register.filter('django_escape_output', django_escape_output)
"""Register a portable filter with a Coffin library object. """ def foo(value): return "{foo}" from coffin.template import Library register = Library() register.filter('foo', foo)
from jinja2 import environmentfilter, contextfilter @environmentfilter def environment(environment, value): return "" @contextfilter def context(context, value): return "" def multiarg(value, arg1, arg2): return "" def jinja_forced(value): return "" def django_jinja_forced(value): # a django filter that returns a django-safestring. It will *only* # be added to jinja, and coffin will hopefully ensure the string # stays safe. from django.utils.safestring import mark_safe return mark_safe(value) from coffin.template import Library register = Library() register.filter('environment', environment) register.filter('context', context) register.filter('multiarg', multiarg) register.filter('jinja_forced', jinja_forced, jinja2_only=True) register.filter('django_jinja_forced', django_jinja_forced, jinja2_only=True)
return "" @contextfilter def context(context, value): return "" def multiarg(value, arg1, arg2): return "" def jinja_forced(value): return "" def django_jinja_forced(value): # a django filter that returns a django-safestring. It will *only* # be added to jinja, and coffin will hopefully ensure the string # stays safe. from django.utils.safestring import mark_safe return mark_safe(value) from coffin.template import Library register = Library() register.filter('environment', environment) register.filter('context', context) register.filter('multiarg', multiarg) register.filter('jinja_forced', jinja_forced, jinja2_only=True) register.filter('django_jinja_forced', django_jinja_forced, jinja2_only=True)