Used the decorator syntax consistently for stringfilter. Fixed linebreaks that was decorated twice.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17050 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2011-10-29 14:39:02 +00:00
parent 24f3c06e4a
commit 69eadc7f15
1 changed files with 27 additions and 28 deletions

View File

@ -53,6 +53,7 @@ def stringfilter(func):
# STRINGS # # STRINGS #
################### ###################
@stringfilter
def addslashes(value): def addslashes(value):
""" """
Adds slashes before quotes. Useful for escaping strings in CSV, for Adds slashes before quotes. Useful for escaping strings in CSV, for
@ -61,13 +62,12 @@ def addslashes(value):
""" """
return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'") return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
addslashes.is_safe = True addslashes.is_safe = True
addslashes = stringfilter(addslashes)
@stringfilter
def capfirst(value): def capfirst(value):
"""Capitalizes the first character of the value.""" """Capitalizes the first character of the value."""
return value and value[0].upper() + value[1:] return value and value[0].upper() + value[1:]
capfirst.is_safe = True capfirst.is_safe = True
capfirst = stringfilter(capfirst)
@register.filter("escapejs") @register.filter("escapejs")
@stringfilter @stringfilter
@ -171,12 +171,13 @@ def floatformat(text, arg=-1):
return input_val return input_val
floatformat.is_safe = True floatformat.is_safe = True
@stringfilter
def iriencode(value): def iriencode(value):
"""Escapes an IRI value for use in a URL.""" """Escapes an IRI value for use in a URL."""
return force_unicode(iri_to_uri(value)) return force_unicode(iri_to_uri(value))
iriencode.is_safe = True iriencode.is_safe = True
iriencode = stringfilter(iriencode)
@stringfilter
def linenumbers(value, autoescape=None): def linenumbers(value, autoescape=None):
"""Displays text with line numbers.""" """Displays text with line numbers."""
lines = value.split(u'\n') lines = value.split(u'\n')
@ -192,14 +193,14 @@ def linenumbers(value, autoescape=None):
return mark_safe(u'\n'.join(lines)) return mark_safe(u'\n'.join(lines))
linenumbers.is_safe = True linenumbers.is_safe = True
linenumbers.needs_autoescape = True linenumbers.needs_autoescape = True
linenumbers = stringfilter(linenumbers)
@stringfilter
def lower(value): def lower(value):
"""Converts a string into all lowercase.""" """Converts a string into all lowercase."""
return value.lower() return value.lower()
lower.is_safe = True lower.is_safe = True
lower = stringfilter(lower)
@stringfilter
def make_list(value): def make_list(value):
""" """
Returns the value turned into a list. Returns the value turned into a list.
@ -209,8 +210,8 @@ def make_list(value):
""" """
return list(value) return list(value)
make_list.is_safe = False make_list.is_safe = False
make_list = stringfilter(make_list)
@stringfilter
def slugify(value): def slugify(value):
""" """
Normalizes string, converts to lowercase, removes non-alpha characters, Normalizes string, converts to lowercase, removes non-alpha characters,
@ -220,7 +221,6 @@ def slugify(value):
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower()) value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
return mark_safe(re.sub('[-\s]+', '-', value)) return mark_safe(re.sub('[-\s]+', '-', value))
slugify.is_safe = True slugify.is_safe = True
slugify = stringfilter(slugify)
def stringformat(value, arg): def stringformat(value, arg):
""" """
@ -238,13 +238,14 @@ def stringformat(value, arg):
return u"" return u""
stringformat.is_safe = True stringformat.is_safe = True
@stringfilter
def title(value): def title(value):
"""Converts a string into titlecase.""" """Converts a string into titlecase."""
t = re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title()) t = re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title())
return re.sub("\d([A-Z])", lambda m: m.group(0).lower(), t) return re.sub("\d([A-Z])", lambda m: m.group(0).lower(), t)
title.is_safe = True title.is_safe = True
title = stringfilter(title)
@stringfilter
def truncatechars(value, arg): def truncatechars(value, arg):
""" """
Truncates a string after a certain number of characters. Truncates a string after a certain number of characters.
@ -257,8 +258,8 @@ def truncatechars(value, arg):
return value # Fail silently. return value # Fail silently.
return Truncator(value).chars(length) return Truncator(value).chars(length)
truncatechars.is_safe = True truncatechars.is_safe = True
truncatechars = stringfilter(truncatechars)
@stringfilter
def truncatewords(value, arg): def truncatewords(value, arg):
""" """
Truncates a string after a certain number of words. Truncates a string after a certain number of words.
@ -273,8 +274,8 @@ def truncatewords(value, arg):
return value # Fail silently. return value # Fail silently.
return Truncator(value).words(length, truncate=' ...') return Truncator(value).words(length, truncate=' ...')
truncatewords.is_safe = True truncatewords.is_safe = True
truncatewords = stringfilter(truncatewords)
@stringfilter
def truncatewords_html(value, arg): def truncatewords_html(value, arg):
""" """
Truncates HTML after a certain number of words. Truncates HTML after a certain number of words.
@ -289,14 +290,14 @@ def truncatewords_html(value, arg):
return value # Fail silently. return value # Fail silently.
return Truncator(value).words(length, html=True, truncate=' ...') return Truncator(value).words(length, html=True, truncate=' ...')
truncatewords_html.is_safe = True truncatewords_html.is_safe = True
truncatewords_html = stringfilter(truncatewords_html)
@stringfilter
def upper(value): def upper(value):
"""Converts a string into all uppercase.""" """Converts a string into all uppercase."""
return value.upper() return value.upper()
upper.is_safe = False upper.is_safe = False
upper = stringfilter(upper)
@stringfilter
def urlencode(value, safe=None): def urlencode(value, safe=None):
""" """
Escapes a value for use in a URL. Escapes a value for use in a URL.
@ -311,7 +312,6 @@ def urlencode(value, safe=None):
kwargs['safe'] = safe kwargs['safe'] = safe
return urlquote(value, **kwargs) return urlquote(value, **kwargs)
urlencode.is_safe = False urlencode.is_safe = False
urlencode = stringfilter(urlencode)
@register.filter @register.filter
@stringfilter @stringfilter
@ -321,6 +321,7 @@ def urlize(value, autoescape=None):
urlize.is_safe = True urlize.is_safe = True
urlize.needs_autoescape = True urlize.needs_autoescape = True
@stringfilter
def urlizetrunc(value, limit, autoescape=None): def urlizetrunc(value, limit, autoescape=None):
""" """
Converts URLs into clickable links, truncating URLs to the given character Converts URLs into clickable links, truncating URLs to the given character
@ -332,14 +333,14 @@ def urlizetrunc(value, limit, autoescape=None):
autoescape=autoescape)) autoescape=autoescape))
urlizetrunc.is_safe = True urlizetrunc.is_safe = True
urlizetrunc.needs_autoescape = True urlizetrunc.needs_autoescape = True
urlizetrunc = stringfilter(urlizetrunc)
@stringfilter
def wordcount(value): def wordcount(value):
"""Returns the number of words.""" """Returns the number of words."""
return len(value.split()) return len(value.split())
wordcount.is_safe = False wordcount.is_safe = False
wordcount = stringfilter(wordcount)
@stringfilter
def wordwrap(value, arg): def wordwrap(value, arg):
""" """
Wraps words at specified line length. Wraps words at specified line length.
@ -348,8 +349,8 @@ def wordwrap(value, arg):
""" """
return wrap(value, int(arg)) return wrap(value, int(arg))
wordwrap.is_safe = True wordwrap.is_safe = True
wordwrap = stringfilter(wordwrap)
@stringfilter
def ljust(value, arg): def ljust(value, arg):
""" """
Left-aligns the value in a field of a given width. Left-aligns the value in a field of a given width.
@ -358,8 +359,8 @@ def ljust(value, arg):
""" """
return value.ljust(int(arg)) return value.ljust(int(arg))
ljust.is_safe = True ljust.is_safe = True
ljust = stringfilter(ljust)
@stringfilter
def rjust(value, arg): def rjust(value, arg):
""" """
Right-aligns the value in a field of a given width. Right-aligns the value in a field of a given width.
@ -368,14 +369,14 @@ def rjust(value, arg):
""" """
return value.rjust(int(arg)) return value.rjust(int(arg))
rjust.is_safe = True rjust.is_safe = True
rjust = stringfilter(rjust)
@stringfilter
def center(value, arg): def center(value, arg):
"""Centers the value in a field of a given width.""" """Centers the value in a field of a given width."""
return value.center(int(arg)) return value.center(int(arg))
center.is_safe = True center.is_safe = True
center = stringfilter(center)
@stringfilter
def cut(value, arg): def cut(value, arg):
""" """
Removes all values of arg from the given string. Removes all values of arg from the given string.
@ -385,7 +386,6 @@ def cut(value, arg):
if safe and arg != ';': if safe and arg != ';':
return mark_safe(value) return mark_safe(value)
return value return value
cut = stringfilter(cut)
################### ###################
# HTML STRINGS # # HTML STRINGS #
@ -400,6 +400,7 @@ def escape_filter(value):
return mark_for_escaping(value) return mark_for_escaping(value)
escape_filter.is_safe = True escape_filter.is_safe = True
@stringfilter
def force_escape(value): def force_escape(value):
""" """
Escapes a string's HTML. This returns a new string containing the escaped Escapes a string's HTML. This returns a new string containing the escaped
@ -407,7 +408,6 @@ def force_escape(value):
possible escaping). possible escaping).
""" """
return mark_safe(escape(value)) return mark_safe(escape(value))
force_escape = stringfilter(force_escape)
force_escape.is_safe = True force_escape.is_safe = True
@register.filter("linebreaks") @register.filter("linebreaks")
@ -422,8 +422,8 @@ def linebreaks_filter(value, autoescape=None):
return mark_safe(linebreaks(value, autoescape)) return mark_safe(linebreaks(value, autoescape))
linebreaks_filter.is_safe = True linebreaks_filter.is_safe = True
linebreaks_filter.needs_autoescape = True linebreaks_filter.needs_autoescape = True
linebreaks = stringfilter(linebreaks)
@stringfilter
def linebreaksbr(value, autoescape=None): def linebreaksbr(value, autoescape=None):
""" """
Converts all newlines in a piece of plain text to HTML line breaks Converts all newlines in a piece of plain text to HTML line breaks
@ -436,15 +436,14 @@ def linebreaksbr(value, autoescape=None):
return mark_safe(value.replace('\n', '<br />')) return mark_safe(value.replace('\n', '<br />'))
linebreaksbr.is_safe = True linebreaksbr.is_safe = True
linebreaksbr.needs_autoescape = True linebreaksbr.needs_autoescape = True
linebreaksbr = stringfilter(linebreaksbr)
@stringfilter
def safe(value): def safe(value):
""" """
Marks the value as a string that should not be auto-escaped. Marks the value as a string that should not be auto-escaped.
""" """
return mark_safe(value) return mark_safe(value)
safe.is_safe = True safe.is_safe = True
safe = stringfilter(safe)
def safeseq(value): def safeseq(value):
""" """
@ -455,6 +454,7 @@ def safeseq(value):
return [mark_safe(force_unicode(obj)) for obj in value] return [mark_safe(force_unicode(obj)) for obj in value]
safeseq.is_safe = True safeseq.is_safe = True
@stringfilter
def removetags(value, tags): def removetags(value, tags):
"""Removes a space separated list of [X]HTML tags from the output.""" """Removes a space separated list of [X]HTML tags from the output."""
tags = [re.escape(tag) for tag in tags.split()] tags = [re.escape(tag) for tag in tags.split()]
@ -465,13 +465,12 @@ def removetags(value, tags):
value = endtag_re.sub(u'', value) value = endtag_re.sub(u'', value)
return value return value
removetags.is_safe = True removetags.is_safe = True
removetags = stringfilter(removetags)
@stringfilter
def striptags(value): def striptags(value):
"""Strips all [X]HTML tags.""" """Strips all [X]HTML tags."""
return strip_tags(value) return strip_tags(value)
striptags.is_safe = True striptags.is_safe = True
striptags = stringfilter(striptags)
################### ###################
# LISTS # # LISTS #