diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index 60fa59e04d..7829327ec3 100644
--- a/django/template/defaultfilters.py
+++ b/django/template/defaultfilters.py
@@ -2,15 +2,22 @@
import re
import random as random_module
+import unicodedata
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
from functools import wraps
+from pprint import pformat
from django.template.base import Variable, Library
from django.conf import settings
from django.utils import formats
+from django.utils.dateformat import format
from django.utils.encoding import force_unicode, iri_to_uri
-from django.utils.html import conditional_escape
-from django.utils.safestring import mark_safe, SafeData
+from django.utils.html import (conditional_escape, escapejs, fix_ampersands,
+ escape, urlize, linebreaks, strip_tags)
+from django.utils.http import urlquote
+from django.utils.text import truncate_words, truncate_html_words, wrap, phone2numeric
+from django.utils.safestring import mark_safe, SafeData, mark_for_escaping
+from django.utils.timesince import timesince, timeuntil
from django.utils.translation import ugettext, ungettext
register = Library()
@@ -61,18 +68,18 @@ def capfirst(value):
capfirst.is_safe=True
capfirst = stringfilter(capfirst)
-def escapejs(value):
+@register.filter("escapejs")
+@stringfilter
+def escapejs_filter(value):
"""Hex encodes characters for use in JavaScript strings."""
- from django.utils.html import escapejs
return escapejs(value)
-escapejs = stringfilter(escapejs)
-def fix_ampersands(value):
+@register.filter("fix_ampersands")
+@stringfilter
+def fix_ampersands_filter(value):
"""Replaces ampersands with ``&`` entities."""
- from django.utils.html import fix_ampersands
return fix_ampersands(value)
-fix_ampersands.is_safe=True
-fix_ampersands = stringfilter(fix_ampersands)
+fix_ampersands_filter.is_safe=True
# Values for testing floatformat input against infinity and NaN representations,
# which differ across platforms and Python versions. Some (i.e. old Windows
@@ -171,7 +178,6 @@ iriencode = stringfilter(iriencode)
def linenumbers(value, autoescape=None):
"""Displays text with line numbers."""
- from django.utils.html import escape
lines = value.split(u'\n')
# Find the maximum width of the line count, for use with zero padding
# string format command
@@ -209,7 +215,6 @@ def slugify(value):
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
- import unicodedata
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
return mark_safe(re.sub('[-\s]+', '-', value))
@@ -247,7 +252,6 @@ def truncatewords(value, arg):
Newlines within the string are removed.
"""
- from django.utils.text import truncate_words
try:
length = int(arg)
except ValueError: # Invalid literal for int().
@@ -264,7 +268,6 @@ def truncatewords_html(value, arg):
Newlines in the HTML are preserved.
"""
- from django.utils.text import truncate_html_words
try:
length = int(arg)
except ValueError: # invalid literal for int()
@@ -288,7 +291,6 @@ def urlencode(value, safe=None):
default safe characters will be used (but an empty string can be provided
when *all* characters should be escaped).
"""
- from django.utils.http import urlquote
kwargs = {}
if safe is not None:
kwargs['safe'] = safe
@@ -296,13 +298,13 @@ def urlencode(value, safe=None):
urlencode.is_safe = False
urlencode = stringfilter(urlencode)
-def urlize(value, autoescape=None):
+@register.filter("urlize")
+@stringfilter
+def urlize_filter(value, autoescape=None):
"""Converts URLs in plain text into clickable links."""
- from django.utils.html import urlize
return mark_safe(urlize(value, nofollow=True, autoescape=autoescape))
-urlize.is_safe=True
-urlize.needs_autoescape = True
-urlize = stringfilter(urlize)
+urlize_filter.is_safe = True
+urlize_filter.needs_autoescape = True
def urlizetrunc(value, limit, autoescape=None):
"""
@@ -311,7 +313,6 @@ def urlizetrunc(value, limit, autoescape=None):
Argument: Length to truncate URLs to.
"""
- from django.utils.html import urlize
return mark_safe(urlize(value, trim_url_limit=int(limit), nofollow=True,
autoescape=autoescape))
urlizetrunc.is_safe = True
@@ -330,7 +331,6 @@ def wordwrap(value, arg):
Argument: number of characters to wrap the text at.
"""
- from django.utils.text import wrap
return wrap(value, int(arg))
wordwrap.is_safe = True
wordwrap = stringfilter(wordwrap)
@@ -376,14 +376,14 @@ cut = stringfilter(cut)
# HTML STRINGS #
###################
-def escape(value):
+@register.filter("escape")
+@stringfilter
+def escape_filter(value):
"""
Marks the value as a string that should not be auto-escaped.
"""
- from django.utils.safestring import mark_for_escaping
return mark_for_escaping(value)
-escape.is_safe = True
-escape = stringfilter(escape)
+escape_filter.is_safe = True
def force_escape(value):
"""
@@ -391,23 +391,22 @@ def force_escape(value):
characters (as opposed to "escape", which marks the content for later
possible escaping).
"""
- from django.utils.html import escape
return mark_safe(escape(value))
force_escape = stringfilter(force_escape)
force_escape.is_safe = True
-def linebreaks(value, autoescape=None):
+@register.filter("linebreaks")
+@stringfilter
+def linebreaks_filter(value, autoescape=None):
"""
Replaces line breaks in plain text with appropriate HTML; a single
newline becomes an HTML line break (``
``) and a new line
followed by a blank line becomes a paragraph break (``