2007-11-04 10:05:56 +08:00
|
|
|
"""Default variable filters."""
|
|
|
|
|
|
|
|
import re
|
|
|
|
import random as random_module
|
2008-02-25 14:02:35 +08:00
|
|
|
try:
|
|
|
|
from functools import wraps
|
|
|
|
except ImportError:
|
|
|
|
from django.utils.functional import wraps # Python 2.3, 2.4 fallback.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-09-21 12:00:32 +08:00
|
|
|
from django.template import Variable, Library
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext, ungettext
|
2007-11-04 10:05:56 +08:00
|
|
|
from django.utils.encoding import force_unicode, iri_to_uri
|
2007-11-14 20:58:53 +08:00
|
|
|
from django.utils.safestring import mark_safe, SafeData
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
register = Library()
|
|
|
|
|
2007-02-24 02:02:51 +08:00
|
|
|
#######################
|
|
|
|
# STRING DECORATOR #
|
|
|
|
#######################
|
|
|
|
|
|
|
|
def stringfilter(func):
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Decorator for filters which should only receive unicode objects. The object
|
|
|
|
passed as the first positional argument will be converted to a unicode
|
|
|
|
object.
|
2007-02-24 02:02:51 +08:00
|
|
|
"""
|
|
|
|
def _dec(*args, **kwargs):
|
|
|
|
if args:
|
|
|
|
args = list(args)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
args[0] = force_unicode(args[0])
|
2007-11-29 09:44:30 +08:00
|
|
|
if isinstance(args[0], SafeData) and getattr(func, 'is_safe', False):
|
|
|
|
return mark_safe(func(*args, **kwargs))
|
2007-02-24 02:02:51 +08:00
|
|
|
return func(*args, **kwargs)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-02-24 02:02:51 +08:00
|
|
|
# Include a reference to the real function (used to check original
|
|
|
|
# arguments by the template parser).
|
|
|
|
_dec._decorated_function = getattr(func, '_decorated_function', func)
|
2007-11-14 20:58:53 +08:00
|
|
|
for attr in ('is_safe', 'needs_autoescape'):
|
|
|
|
if hasattr(func, attr):
|
|
|
|
setattr(_dec, attr, getattr(func, attr))
|
2008-02-25 14:02:35 +08:00
|
|
|
return wraps(func)(_dec)
|
2007-02-24 02:02:51 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
###################
|
|
|
|
# STRINGS #
|
|
|
|
###################
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
|
|
|
|
def addslashes(value):
|
2007-12-05 05:08:29 +08:00
|
|
|
"""
|
|
|
|
Adds slashes before quotes. Useful for escaping strings in CSV, for
|
|
|
|
example. Less useful for escaping JavaScript; use the ``escapejs``
|
|
|
|
filter instead.
|
|
|
|
"""
|
2006-09-23 16:41:09 +08:00
|
|
|
return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
|
2007-11-14 20:58:53 +08:00
|
|
|
addslashes.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
addslashes = stringfilter(addslashes)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def capfirst(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Capitalizes the first character of the value."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return value and value[0].upper() + value[1:]
|
2007-11-14 20:58:53 +08:00
|
|
|
capfirst.is_safe=True
|
2007-02-24 02:02:51 +08:00
|
|
|
capfirst = stringfilter(capfirst)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-12-05 05:08:29 +08:00
|
|
|
_js_escapes = (
|
|
|
|
('\\', '\\\\'),
|
|
|
|
('"', '\\"'),
|
|
|
|
("'", "\\'"),
|
|
|
|
('\n', '\\n'),
|
|
|
|
('\r', '\\r'),
|
|
|
|
('\b', '\\b'),
|
|
|
|
('\f', '\\f'),
|
|
|
|
('\t', '\\t'),
|
|
|
|
('\v', '\\v'),
|
|
|
|
('</', '<\\/'),
|
|
|
|
)
|
|
|
|
def escapejs(value):
|
|
|
|
"""Backslash-escapes characters for use in JavaScript strings."""
|
|
|
|
for bad, good in _js_escapes:
|
|
|
|
value = value.replace(bad, good)
|
|
|
|
return value
|
|
|
|
escapejs = stringfilter(escapejs)
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def fix_ampersands(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Replaces ampersands with ``&`` entities."""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.html import fix_ampersands
|
|
|
|
return fix_ampersands(value)
|
2007-11-14 20:58:53 +08:00
|
|
|
fix_ampersands.is_safe=True
|
2007-02-24 02:02:51 +08:00
|
|
|
fix_ampersands = stringfilter(fix_ampersands)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-01-03 13:29:34 +08:00
|
|
|
def floatformat(text, arg=-1):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-11-04 10:27:17 +08:00
|
|
|
Displays a float to a specified number of decimal places.
|
|
|
|
|
|
|
|
If called without an argument, it displays the floating point number with
|
|
|
|
one decimal place -- but only if there's a decimal place to be displayed:
|
2007-03-10 16:11:28 +08:00
|
|
|
|
|
|
|
* num1 = 34.23234
|
|
|
|
* num2 = 34.00000
|
2007-11-04 10:27:17 +08:00
|
|
|
* num3 = 34.26000
|
|
|
|
* {{ num1|floatformat }} displays "34.2"
|
|
|
|
* {{ num2|floatformat }} displays "34"
|
|
|
|
* {{ num3|floatformat }} displays "34.3"
|
|
|
|
|
|
|
|
If arg is positive, it will always display exactly arg number of decimal
|
|
|
|
places:
|
|
|
|
|
|
|
|
* {{ num1|floatformat:3 }} displays "34.232"
|
|
|
|
* {{ num2|floatformat:3 }} displays "34.000"
|
|
|
|
* {{ num3|floatformat:3 }} displays "34.260"
|
|
|
|
|
|
|
|
If arg is negative, it will display arg number of decimal places -- but
|
|
|
|
only if there are places to be displayed:
|
|
|
|
|
|
|
|
* {{ num1|floatformat:"-3" }} displays "34.232"
|
|
|
|
* {{ num2|floatformat:"-3" }} displays "34"
|
|
|
|
* {{ num3|floatformat:"-3" }} displays "34.260"
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2005-11-03 03:01:27 +08:00
|
|
|
try:
|
|
|
|
f = float(text)
|
2007-11-30 04:10:00 +08:00
|
|
|
except (ValueError, TypeError):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2007-01-03 13:29:34 +08:00
|
|
|
try:
|
|
|
|
d = int(arg)
|
|
|
|
except ValueError:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return force_unicode(f)
|
2008-03-20 15:36:33 +08:00
|
|
|
try:
|
|
|
|
m = f - int(f)
|
|
|
|
except OverflowError:
|
|
|
|
return force_unicode(f)
|
2007-01-03 13:29:34 +08:00
|
|
|
if not m and d < 0:
|
2007-11-14 20:58:53 +08:00
|
|
|
return mark_safe(u'%d' % int(f))
|
2007-01-03 13:29:34 +08:00
|
|
|
else:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
formatstr = u'%%.%df' % abs(d)
|
2007-11-14 20:58:53 +08:00
|
|
|
return mark_safe(formatstr % f)
|
|
|
|
floatformat.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def iriencode(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Escapes an IRI value for use in a URL."""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return force_unicode(iri_to_uri(value))
|
2007-11-20 09:37:57 +08:00
|
|
|
iriencode.is_safe = True
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
iriencode = stringfilter(iriencode)
|
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
def linenumbers(value, autoescape=None):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Displays text with line numbers."""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.html import escape
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
lines = value.split(u'\n')
|
2007-11-04 10:05:56 +08:00
|
|
|
# Find the maximum width of the line count, for use with zero padding
|
2007-11-14 20:58:53 +08:00
|
|
|
# string format command
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
width = unicode(len(unicode(len(lines))))
|
2007-11-14 20:58:53 +08:00
|
|
|
if not autoescape or isinstance(value, SafeData):
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
lines[i] = (u"%0" + width + u"d. %s") % (i + 1, line)
|
|
|
|
else:
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
lines[i] = (u"%0" + width + u"d. %s") % (i + 1, escape(line))
|
|
|
|
return mark_safe(u'\n'.join(lines))
|
|
|
|
linenumbers.is_safe = True
|
|
|
|
linenumbers.needs_autoescape = True
|
2007-02-24 02:02:51 +08:00
|
|
|
linenumbers = stringfilter(linenumbers)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def lower(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Converts a string into all lowercase."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return value.lower()
|
2007-11-14 20:58:53 +08:00
|
|
|
lower.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
lower = stringfilter(lower)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def make_list(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Returns the value turned into a list.
|
|
|
|
|
|
|
|
For an integer, it's a list of digits.
|
|
|
|
For a string, it's a list of characters.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-02-24 02:02:51 +08:00
|
|
|
return list(value)
|
2007-11-14 20:58:53 +08:00
|
|
|
make_list.is_safe = False
|
2007-02-24 02:02:51 +08:00
|
|
|
make_list = stringfilter(make_list)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def slugify(value):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Normalizes string, converts to lowercase, removes non-alpha characters,
|
|
|
|
and converts spaces to hyphens.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
"""
|
|
|
|
import unicodedata
|
|
|
|
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
|
|
|
|
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
|
2007-11-14 20:58:53 +08:00
|
|
|
return mark_safe(re.sub('[-\s]+', '-', value))
|
|
|
|
slugify.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
slugify = stringfilter(slugify)
|
2005-08-10 23:40:14 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def stringformat(value, arg):
|
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Formats the variable according to the arg, a string formatting specifier.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
This specifier uses Python string formating syntax, with the exception that
|
|
|
|
the leading "%" is dropped.
|
2005-08-10 23:40:14 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
See http://docs.python.org/lib/typesseq-strings.html for documentation
|
|
|
|
of Python string formatting
|
|
|
|
"""
|
|
|
|
try:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return (u"%" + unicode(arg)) % value
|
2005-07-13 09:25:57 +08:00
|
|
|
except (ValueError, TypeError):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u""
|
2007-11-14 20:58:53 +08:00
|
|
|
stringformat.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def title(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Converts a string into titlecase."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title())
|
2007-11-14 20:58:53 +08:00
|
|
|
title.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
title = stringfilter(title)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def truncatewords(value, arg):
|
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Truncates a string after a certain number of words.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-11-04 10:05:56 +08:00
|
|
|
Argument: Number of words to truncate after.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
from django.utils.text import truncate_words
|
|
|
|
try:
|
|
|
|
length = int(arg)
|
2007-11-04 10:05:56 +08:00
|
|
|
except ValueError: # Invalid literal for int().
|
2005-07-13 09:25:57 +08:00
|
|
|
return value # Fail silently.
|
|
|
|
return truncate_words(value, length)
|
2007-11-14 20:58:53 +08:00
|
|
|
truncatewords.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
truncatewords = stringfilter(truncatewords)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-02-10 10:51:27 +08:00
|
|
|
def truncatewords_html(value, arg):
|
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Truncates HTML after a certain number of words.
|
2007-02-10 10:51:27 +08:00
|
|
|
|
2007-11-04 10:05:56 +08:00
|
|
|
Argument: Number of words to truncate after.
|
2007-02-10 10:51:27 +08:00
|
|
|
"""
|
|
|
|
from django.utils.text import truncate_html_words
|
|
|
|
try:
|
|
|
|
length = int(arg)
|
|
|
|
except ValueError: # invalid literal for int()
|
|
|
|
return value # Fail silently.
|
|
|
|
return truncate_html_words(value, length)
|
2007-11-14 20:58:53 +08:00
|
|
|
truncatewords_html.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
truncatewords_html = stringfilter(truncatewords_html)
|
2007-02-10 10:51:27 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def upper(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Converts a string into all uppercase."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return value.upper()
|
2007-11-14 20:58:53 +08:00
|
|
|
upper.is_safe = False
|
2007-02-24 02:02:51 +08:00
|
|
|
upper = stringfilter(upper)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def urlencode(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Escapes a value for use in a URL."""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.http import urlquote
|
|
|
|
return urlquote(value)
|
2007-11-14 20:58:53 +08:00
|
|
|
urlencode.is_safe = False
|
2007-02-24 02:02:51 +08:00
|
|
|
urlencode = stringfilter(urlencode)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
def urlize(value, autoescape=None):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Converts URLs in plain text into clickable links."""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.html import urlize
|
2007-11-14 20:58:53 +08:00
|
|
|
return mark_safe(urlize(value, nofollow=True, autoescape=autoescape))
|
|
|
|
urlize.is_safe=True
|
|
|
|
urlize.needs_autoescape = True
|
2007-02-24 02:02:51 +08:00
|
|
|
urlize = stringfilter(urlize)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-12-19 12:20:02 +08:00
|
|
|
def urlizetrunc(value, limit, autoescape=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Converts URLs into clickable links, truncating URLs to the given character
|
|
|
|
limit, and adding 'rel=nofollow' attribute to discourage spamming.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
Argument: Length to truncate URLs to.
|
|
|
|
"""
|
|
|
|
from django.utils.html import urlize
|
2007-12-19 12:20:02 +08:00
|
|
|
return mark_safe(urlize(value, trim_url_limit=int(limit), nofollow=True,
|
|
|
|
autoescape=autoescape))
|
2007-11-14 20:58:53 +08:00
|
|
|
urlizetrunc.is_safe = True
|
2007-12-19 12:20:02 +08:00
|
|
|
urlizetrunc.needs_autoescape = True
|
2007-02-24 02:02:51 +08:00
|
|
|
urlizetrunc = stringfilter(urlizetrunc)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def wordcount(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Returns the number of words."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return len(value.split())
|
2007-11-14 20:58:53 +08:00
|
|
|
wordcount.is_safe = False
|
2007-02-24 02:02:51 +08:00
|
|
|
wordcount = stringfilter(wordcount)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def wordwrap(value, arg):
|
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Wraps words at specified line length.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-07-15 19:47:09 +08:00
|
|
|
Argument: number of characters to wrap the text at.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
from django.utils.text import wrap
|
2007-02-24 02:02:51 +08:00
|
|
|
return wrap(value, int(arg))
|
2007-11-14 20:58:53 +08:00
|
|
|
wordwrap.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
wordwrap = stringfilter(wordwrap)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def ljust(value, arg):
|
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Left-aligns the value in a field of a given width.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-11-04 10:05:56 +08:00
|
|
|
Argument: field size.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-02-24 02:02:51 +08:00
|
|
|
return value.ljust(int(arg))
|
2007-11-14 20:58:53 +08:00
|
|
|
ljust.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
ljust = stringfilter(ljust)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def rjust(value, arg):
|
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Right-aligns the value in a field of a given width.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-11-04 10:05:56 +08:00
|
|
|
Argument: field size.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-02-24 02:02:51 +08:00
|
|
|
return value.rjust(int(arg))
|
2007-11-14 20:58:53 +08:00
|
|
|
rjust.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
rjust = stringfilter(rjust)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def center(value, arg):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Centers the value in a field of a given width."""
|
2007-02-24 02:02:51 +08:00
|
|
|
return value.center(int(arg))
|
2007-11-14 20:58:53 +08:00
|
|
|
center.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
center = stringfilter(center)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def cut(value, arg):
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
|
|
|
Removes all values of arg from the given string.
|
|
|
|
"""
|
|
|
|
safe = isinstance(value, SafeData)
|
|
|
|
value = value.replace(arg, u'')
|
|
|
|
if safe and arg != ';':
|
|
|
|
return mark_safe(value)
|
|
|
|
return value
|
2007-02-24 02:02:51 +08:00
|
|
|
cut = stringfilter(cut)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
###################
|
|
|
|
# HTML STRINGS #
|
|
|
|
###################
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def escape(value):
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
|
|
|
def force_escape(value):
|
|
|
|
"""
|
|
|
|
Escapes a string's HTML. This returns a new string containing the escaped
|
|
|
|
characters (as opposed to "escape", which marks the content for later
|
|
|
|
possible escaping).
|
|
|
|
"""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.html import escape
|
2007-11-14 20:58:53 +08:00
|
|
|
return mark_safe(escape(value))
|
2008-01-28 10:30:53 +08:00
|
|
|
force_escape = stringfilter(force_escape)
|
2007-11-14 20:58:53 +08:00
|
|
|
force_escape.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
def linebreaks(value, autoescape=None):
|
2007-09-15 05:46:38 +08:00
|
|
|
"""
|
|
|
|
Replaces line breaks in plain text with appropriate HTML; a single
|
|
|
|
newline becomes an HTML line break (``<br />``) and a new line
|
|
|
|
followed by a blank line becomes a paragraph break (``</p>``).
|
|
|
|
"""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.html import linebreaks
|
2007-11-14 20:58:53 +08:00
|
|
|
autoescape = autoescape and not isinstance(value, SafeData)
|
|
|
|
return mark_safe(linebreaks(value, autoescape))
|
|
|
|
linebreaks.is_safe = True
|
|
|
|
linebreaks.needs_autoescape = True
|
2007-02-24 02:02:51 +08:00
|
|
|
linebreaks = stringfilter(linebreaks)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
def linebreaksbr(value, autoescape=None):
|
2007-09-15 05:46:38 +08:00
|
|
|
"""
|
|
|
|
Converts all newlines in a piece of plain text to HTML line breaks
|
|
|
|
(``<br />``).
|
|
|
|
"""
|
2007-11-14 20:58:53 +08:00
|
|
|
if autoescape and not isinstance(value, SafeData):
|
|
|
|
from django.utils.html import escape
|
|
|
|
value = escape(value)
|
|
|
|
return mark_safe(value.replace('\n', '<br />'))
|
|
|
|
linebreaksbr.is_safe = True
|
|
|
|
linebreaksbr.needs_autoescape = True
|
2007-02-24 02:02:51 +08:00
|
|
|
linebreaksbr = stringfilter(linebreaksbr)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
def safe(value):
|
|
|
|
"""
|
|
|
|
Marks the value as a string that should not be auto-escaped.
|
|
|
|
"""
|
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
return mark_safe(value)
|
|
|
|
safe.is_safe = True
|
|
|
|
safe = stringfilter(safe)
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def removetags(value, tags):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Removes a space separated list of [X]HTML tags from the output."""
|
2005-07-13 09:25:57 +08:00
|
|
|
tags = [re.escape(tag) for tag in tags.split()]
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
tags_re = u'(%s)' % u'|'.join(tags)
|
|
|
|
starttag_re = re.compile(ur'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U)
|
|
|
|
endtag_re = re.compile(u'</%s>' % tags_re)
|
|
|
|
value = starttag_re.sub(u'', value)
|
|
|
|
value = endtag_re.sub(u'', value)
|
2005-07-13 09:25:57 +08:00
|
|
|
return value
|
2007-11-14 20:58:53 +08:00
|
|
|
removetags.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
removetags = stringfilter(removetags)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def striptags(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Strips all [X]HTML tags."""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.html import strip_tags
|
|
|
|
return strip_tags(value)
|
2007-11-14 20:58:53 +08:00
|
|
|
striptags.is_safe = True
|
2007-02-24 02:02:51 +08:00
|
|
|
striptags = stringfilter(striptags)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
###################
|
|
|
|
# LISTS #
|
|
|
|
###################
|
|
|
|
|
|
|
|
def dictsort(value, arg):
|
|
|
|
"""
|
|
|
|
Takes a list of dicts, returns that list sorted by the property given in
|
|
|
|
the argument.
|
|
|
|
"""
|
2007-09-21 12:00:32 +08:00
|
|
|
var_resolve = Variable(arg).resolve
|
|
|
|
decorated = [(var_resolve(item), item) for item in value]
|
2005-07-13 09:25:57 +08:00
|
|
|
decorated.sort()
|
|
|
|
return [item[1] for item in decorated]
|
2007-11-14 20:58:53 +08:00
|
|
|
dictsort.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def dictsortreversed(value, arg):
|
|
|
|
"""
|
|
|
|
Takes a list of dicts, returns that list sorted in reverse order by the
|
|
|
|
property given in the argument.
|
|
|
|
"""
|
2007-09-21 12:00:32 +08:00
|
|
|
var_resolve = Variable(arg).resolve
|
|
|
|
decorated = [(var_resolve(item), item) for item in value]
|
2005-07-13 09:25:57 +08:00
|
|
|
decorated.sort()
|
|
|
|
decorated.reverse()
|
|
|
|
return [item[1] for item in decorated]
|
2007-11-14 20:58:53 +08:00
|
|
|
dictsortreversed.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def first(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Returns the first item in a list."""
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
return value[0]
|
|
|
|
except IndexError:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2008-01-06 11:53:04 +08:00
|
|
|
first.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def join(value, arg):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Joins a list with a string, like Python's ``str.join(list)``."""
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2007-11-14 20:58:53 +08:00
|
|
|
data = arg.join(map(force_unicode, value))
|
2005-07-13 09:25:57 +08:00
|
|
|
except AttributeError: # fail silently but nicely
|
|
|
|
return value
|
2007-11-14 20:58:53 +08:00
|
|
|
safe_args = reduce(lambda lhs, rhs: lhs and isinstance(rhs, SafeData),
|
|
|
|
value, True)
|
|
|
|
if safe_args:
|
|
|
|
return mark_safe(data)
|
|
|
|
else:
|
|
|
|
return data
|
|
|
|
join.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2008-01-06 11:53:33 +08:00
|
|
|
def last(value):
|
|
|
|
"Returns the last item in a list"
|
|
|
|
try:
|
|
|
|
return value[-1]
|
|
|
|
except IndexError:
|
|
|
|
return u''
|
|
|
|
last.is_safe = True
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def length(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Returns the length of the value - useful for lists."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return len(value)
|
2007-11-14 20:58:53 +08:00
|
|
|
length.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def length_is(value, arg):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Returns a boolean of whether the value's length is the argument."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return len(value) == int(arg)
|
2007-11-14 20:58:53 +08:00
|
|
|
length_is.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def random(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Returns a random item from the list."""
|
2005-08-19 22:59:09 +08:00
|
|
|
return random_module.choice(value)
|
2007-11-14 20:58:53 +08:00
|
|
|
random.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def slice_(value, arg):
|
|
|
|
"""
|
|
|
|
Returns a slice of the list.
|
|
|
|
|
|
|
|
Uses the same syntax as Python's list slicing; see
|
|
|
|
http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
|
|
|
|
for an introduction.
|
|
|
|
"""
|
|
|
|
try:
|
2006-01-02 02:37:33 +08:00
|
|
|
bits = []
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
for x in arg.split(u':'):
|
2006-01-02 02:37:33 +08:00
|
|
|
if len(x) == 0:
|
|
|
|
bits.append(None)
|
|
|
|
else:
|
|
|
|
bits.append(int(x))
|
|
|
|
return value[slice(*bits)]
|
|
|
|
|
2005-09-23 08:12:24 +08:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
return value # Fail silently.
|
2007-11-14 20:58:53 +08:00
|
|
|
slice_.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
def unordered_list(value, autoescape=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Recursively takes a self-nested list and returns an HTML unordered list --
|
|
|
|
WITHOUT opening and closing <ul> tags.
|
|
|
|
|
2007-08-26 09:11:20 +08:00
|
|
|
The list is assumed to be in the proper format. For example, if ``var``
|
|
|
|
contains: ``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``,
|
2005-07-13 09:25:57 +08:00
|
|
|
then ``{{ var|unordered_list }}`` would return::
|
|
|
|
|
|
|
|
<li>States
|
|
|
|
<ul>
|
|
|
|
<li>Kansas
|
|
|
|
<ul>
|
|
|
|
<li>Lawrence</li>
|
|
|
|
<li>Topeka</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li>Illinois</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
"""
|
2007-11-14 20:58:53 +08:00
|
|
|
if autoescape:
|
|
|
|
from django.utils.html import conditional_escape
|
|
|
|
escaper = conditional_escape
|
|
|
|
else:
|
|
|
|
escaper = lambda x: x
|
2007-08-26 09:11:20 +08:00
|
|
|
def convert_old_style_list(list_):
|
|
|
|
"""
|
|
|
|
Converts old style lists to the new easier to understand format.
|
|
|
|
|
|
|
|
The old list format looked like:
|
|
|
|
['Item 1', [['Item 1.1', []], ['Item 1.2', []]]
|
|
|
|
|
|
|
|
And it is converted to:
|
|
|
|
['Item 1', ['Item 1.1', 'Item 1.2]]
|
|
|
|
"""
|
|
|
|
if not isinstance(list_, (tuple, list)) or len(list_) != 2:
|
|
|
|
return list_, False
|
|
|
|
first_item, second_item = list_
|
|
|
|
if second_item == []:
|
|
|
|
return [first_item], True
|
|
|
|
old_style_list = True
|
|
|
|
new_second_item = []
|
|
|
|
for sublist in second_item:
|
|
|
|
item, old_style_list = convert_old_style_list(sublist)
|
|
|
|
if not old_style_list:
|
|
|
|
break
|
|
|
|
new_second_item.extend(item)
|
|
|
|
if old_style_list:
|
|
|
|
second_item = new_second_item
|
|
|
|
return [first_item, second_item], old_style_list
|
|
|
|
def _helper(list_, tabs=1):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
indent = u'\t' * tabs
|
2007-08-26 09:11:20 +08:00
|
|
|
output = []
|
|
|
|
|
|
|
|
list_length = len(list_)
|
|
|
|
i = 0
|
|
|
|
while i < list_length:
|
|
|
|
title = list_[i]
|
|
|
|
sublist = ''
|
|
|
|
sublist_item = None
|
|
|
|
if isinstance(title, (list, tuple)):
|
2007-11-04 10:05:56 +08:00
|
|
|
sublist_item = title
|
2007-08-26 09:11:20 +08:00
|
|
|
title = ''
|
|
|
|
elif i < list_length - 1:
|
|
|
|
next_item = list_[i+1]
|
|
|
|
if next_item and isinstance(next_item, (list, tuple)):
|
|
|
|
# The next item is a sub-list.
|
|
|
|
sublist_item = next_item
|
|
|
|
# We've processed the next item now too.
|
2007-11-04 10:05:56 +08:00
|
|
|
i += 1
|
2007-08-26 09:11:20 +08:00
|
|
|
if sublist_item:
|
|
|
|
sublist = _helper(sublist_item, tabs+1)
|
|
|
|
sublist = '\n%s<ul>\n%s\n%s</ul>\n%s' % (indent, sublist,
|
|
|
|
indent, indent)
|
2007-11-14 20:58:53 +08:00
|
|
|
output.append('%s<li>%s%s</li>' % (indent,
|
|
|
|
escaper(force_unicode(title)), sublist))
|
2007-08-26 09:11:20 +08:00
|
|
|
i += 1
|
|
|
|
return '\n'.join(output)
|
2007-11-04 10:05:56 +08:00
|
|
|
value, converted = convert_old_style_list(value)
|
2007-11-14 20:58:53 +08:00
|
|
|
return mark_safe(_helper(value))
|
|
|
|
unordered_list.is_safe = True
|
|
|
|
unordered_list.needs_autoescape = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
###################
|
|
|
|
# INTEGERS #
|
|
|
|
###################
|
|
|
|
|
|
|
|
def add(value, arg):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Adds the arg to the value."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return int(value) + int(arg)
|
2007-11-14 20:58:53 +08:00
|
|
|
add.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def get_digit(value, arg):
|
|
|
|
"""
|
|
|
|
Given a whole number, returns the requested digit of it, where 1 is the
|
|
|
|
right-most digit, 2 is the second-right-most digit, etc. Returns the
|
|
|
|
original value for invalid input (if input or argument is not an integer,
|
|
|
|
or if argument is less than 1). Otherwise, output is always an integer.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
arg = int(arg)
|
|
|
|
value = int(value)
|
|
|
|
except ValueError:
|
|
|
|
return value # Fail silently for an invalid argument
|
|
|
|
if arg < 1:
|
|
|
|
return value
|
|
|
|
try:
|
|
|
|
return int(str(value)[-arg])
|
|
|
|
except IndexError:
|
|
|
|
return 0
|
2007-11-14 20:58:53 +08:00
|
|
|
get_digit.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
###################
|
|
|
|
# DATES #
|
|
|
|
###################
|
|
|
|
|
2006-05-17 05:28:06 +08:00
|
|
|
def date(value, arg=None):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Formats a date according to the given format."""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.dateformat import format
|
2006-06-11 08:33:44 +08:00
|
|
|
if not value:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2006-05-17 05:28:06 +08:00
|
|
|
if arg is None:
|
|
|
|
arg = settings.DATE_FORMAT
|
2005-07-13 09:25:57 +08:00
|
|
|
return format(value, arg)
|
2007-11-14 20:58:53 +08:00
|
|
|
date.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-05-17 05:28:06 +08:00
|
|
|
def time(value, arg=None):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Formats a time according to the given format."""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.dateformat import time_format
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
if value in (None, u''):
|
|
|
|
return u''
|
2006-05-17 05:28:06 +08:00
|
|
|
if arg is None:
|
|
|
|
arg = settings.TIME_FORMAT
|
2005-07-13 09:25:57 +08:00
|
|
|
return time_format(value, arg)
|
2007-11-14 20:58:53 +08:00
|
|
|
time.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-06-21 14:56:08 +08:00
|
|
|
def timesince(value, arg=None):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Formats a date as the time since that date (i.e. "4 days, 6 hours")."""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.timesince import timesince
|
2006-06-11 08:33:44 +08:00
|
|
|
if not value:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2006-06-21 14:56:08 +08:00
|
|
|
if arg:
|
|
|
|
return timesince(arg, value)
|
2005-07-13 09:25:57 +08:00
|
|
|
return timesince(value)
|
2007-11-14 20:58:53 +08:00
|
|
|
timesince.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-06-21 14:56:08 +08:00
|
|
|
def timeuntil(value, arg=None):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Formats a date as the time until that date (i.e. "4 days, 6 hours")."""
|
2006-06-21 14:56:08 +08:00
|
|
|
from django.utils.timesince import timesince
|
|
|
|
from datetime import datetime
|
|
|
|
if not value:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2006-06-21 14:56:08 +08:00
|
|
|
if arg:
|
|
|
|
return timesince(arg, value)
|
|
|
|
return timesince(datetime.now(), value)
|
2007-11-14 20:58:53 +08:00
|
|
|
timeuntil.is_safe = False
|
2006-06-21 14:56:08 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
###################
|
|
|
|
# LOGIC #
|
|
|
|
###################
|
|
|
|
|
|
|
|
def default(value, arg):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""If value is unavailable, use given default."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return value or arg
|
2007-11-14 20:58:53 +08:00
|
|
|
default.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-10-14 11:48:27 +08:00
|
|
|
def default_if_none(value, arg):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""If value is None, use given default."""
|
2005-10-14 11:48:27 +08:00
|
|
|
if value is None:
|
|
|
|
return arg
|
|
|
|
return value
|
2007-11-14 20:58:53 +08:00
|
|
|
default_if_none.is_safe = False
|
2005-10-14 11:48:27 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def divisibleby(value, arg):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Returns True if the value is devisible by the argument."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return int(value) % int(arg) == 0
|
2007-11-14 20:58:53 +08:00
|
|
|
divisibleby.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-12-15 23:56:26 +08:00
|
|
|
def yesno(value, arg=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Given a string mapping values for true, false and (optionally) None,
|
|
|
|
returns one of those strings accoding to the value:
|
|
|
|
|
|
|
|
========== ====================== ==================================
|
|
|
|
Value Argument Outputs
|
|
|
|
========== ====================== ==================================
|
|
|
|
``True`` ``"yeah,no,maybe"`` ``yeah``
|
|
|
|
``False`` ``"yeah,no,maybe"`` ``no``
|
|
|
|
``None`` ``"yeah,no,maybe"`` ``maybe``
|
|
|
|
``None`` ``"yeah,no"`` ``"no"`` (converts None to False
|
|
|
|
if no mapping for None is given.
|
|
|
|
========== ====================== ==================================
|
2005-12-15 23:56:26 +08:00
|
|
|
"""
|
|
|
|
if arg is None:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
arg = ugettext('yes,no,maybe')
|
|
|
|
bits = arg.split(u',')
|
2005-07-13 09:25:57 +08:00
|
|
|
if len(bits) < 2:
|
|
|
|
return value # Invalid arg.
|
|
|
|
try:
|
|
|
|
yes, no, maybe = bits
|
2007-11-04 10:05:56 +08:00
|
|
|
except ValueError:
|
|
|
|
# Unpack list of wrong size (no "maybe" value provided).
|
2005-09-01 10:22:46 +08:00
|
|
|
yes, no, maybe = bits[0], bits[1], bits[1]
|
2005-07-13 09:25:57 +08:00
|
|
|
if value is None:
|
|
|
|
return maybe
|
|
|
|
if value:
|
|
|
|
return yes
|
|
|
|
return no
|
2007-11-14 20:58:53 +08:00
|
|
|
yesno.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
###################
|
|
|
|
# MISC #
|
|
|
|
###################
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def filesizeformat(bytes):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-11-04 10:05:56 +08:00
|
|
|
Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB,
|
|
|
|
102 bytes, etc).
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2006-11-07 12:58:10 +08:00
|
|
|
try:
|
|
|
|
bytes = float(bytes)
|
|
|
|
except TypeError:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u"0 bytes"
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
if bytes < 1024:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
|
2005-07-13 09:25:57 +08:00
|
|
|
if bytes < 1024 * 1024:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return ugettext("%.1f KB") % (bytes / 1024)
|
2005-07-13 09:25:57 +08:00
|
|
|
if bytes < 1024 * 1024 * 1024:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return ugettext("%.1f MB") % (bytes / (1024 * 1024))
|
|
|
|
return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024))
|
2007-11-14 20:58:53 +08:00
|
|
|
filesizeformat.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def pluralize(value, arg=u's'):
|
2006-07-04 14:18:39 +08:00
|
|
|
"""
|
2007-12-02 00:58:52 +08:00
|
|
|
Returns a plural suffix if the value is not 1. By default, 's' is used as
|
2007-11-04 10:08:04 +08:00
|
|
|
the suffix:
|
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
* If value is 0, vote{{ value|pluralize }} displays "0 votes".
|
|
|
|
* If value is 1, vote{{ value|pluralize }} displays "1 vote".
|
|
|
|
* If value is 2, vote{{ value|pluralize }} displays "2 votes".
|
2007-11-04 10:08:04 +08:00
|
|
|
|
|
|
|
If an argument is provided, that string is used instead:
|
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
* If value is 0, class{{ value|pluralize:"es" }} displays "0 classes".
|
|
|
|
* If value is 1, class{{ value|pluralize:"es" }} displays "1 class".
|
|
|
|
* If value is 2, class{{ value|pluralize:"es" }} displays "2 classes".
|
2007-11-04 10:08:04 +08:00
|
|
|
|
|
|
|
If the provided argument contains a comma, the text before the comma is
|
|
|
|
used for the singular case and the text after the comma is used for the
|
|
|
|
plural case:
|
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
* If value is 0, cand{{ value|pluralize:"y,ies" }} displays "0 candies".
|
|
|
|
* If value is 1, cand{{ value|pluralize:"y,ies" }} displays "1 candy".
|
|
|
|
* If value is 2, cand{{ value|pluralize:"y,ies" }} displays "2 candies".
|
2006-07-04 14:18:39 +08:00
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
if not u',' in arg:
|
|
|
|
arg = u',' + arg
|
|
|
|
bits = arg.split(u',')
|
2006-07-04 14:31:26 +08:00
|
|
|
if len(bits) > 2:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2006-07-04 14:31:26 +08:00
|
|
|
singular_suffix, plural_suffix = bits[:2]
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
if int(value) != 1:
|
2006-07-04 14:18:39 +08:00
|
|
|
return plural_suffix
|
2007-11-04 10:05:56 +08:00
|
|
|
except ValueError: # Invalid string that's not a number.
|
2005-07-13 09:25:57 +08:00
|
|
|
pass
|
2007-11-04 10:05:56 +08:00
|
|
|
except TypeError: # Value isn't a string or a number; maybe it's a list?
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
if len(value) != 1:
|
2006-07-04 14:18:39 +08:00
|
|
|
return plural_suffix
|
2007-11-04 10:05:56 +08:00
|
|
|
except TypeError: # len() of unsized object.
|
2005-07-13 09:25:57 +08:00
|
|
|
pass
|
2006-07-04 14:18:39 +08:00
|
|
|
return singular_suffix
|
2007-11-14 20:58:53 +08:00
|
|
|
pluralize.is_safe = False
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def phone2numeric(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""Takes a phone number and converts it in to its numerical equivalent."""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.text import phone2numeric
|
|
|
|
return phone2numeric(value)
|
2007-11-14 20:58:53 +08:00
|
|
|
phone2numeric.is_safe = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def pprint(value):
|
2007-11-04 10:05:56 +08:00
|
|
|
"""A wrapper around pprint.pprint -- for debugging, really."""
|
2005-07-13 09:25:57 +08:00
|
|
|
from pprint import pformat
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
|
|
|
return pformat(value)
|
|
|
|
except Exception, e:
|
2007-07-13 18:56:30 +08:00
|
|
|
return u"Error in formatting: %s" % force_unicode(e, errors="replace")
|
2007-11-14 20:58:53 +08:00
|
|
|
pprint.is_safe = True
|
2006-05-17 05:28:06 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
# Syntax: register.filter(name of filter, callback)
|
|
|
|
register.filter(add)
|
|
|
|
register.filter(addslashes)
|
|
|
|
register.filter(capfirst)
|
|
|
|
register.filter(center)
|
|
|
|
register.filter(cut)
|
|
|
|
register.filter(date)
|
|
|
|
register.filter(default)
|
|
|
|
register.filter(default_if_none)
|
|
|
|
register.filter(dictsort)
|
|
|
|
register.filter(dictsortreversed)
|
|
|
|
register.filter(divisibleby)
|
|
|
|
register.filter(escape)
|
2007-12-05 21:16:25 +08:00
|
|
|
register.filter(escapejs)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(filesizeformat)
|
|
|
|
register.filter(first)
|
|
|
|
register.filter(fix_ampersands)
|
|
|
|
register.filter(floatformat)
|
2007-11-14 20:58:53 +08:00
|
|
|
register.filter(force_escape)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(get_digit)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
register.filter(iriencode)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(join)
|
2008-01-06 11:53:33 +08:00
|
|
|
register.filter(last)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(length)
|
|
|
|
register.filter(length_is)
|
|
|
|
register.filter(linebreaks)
|
|
|
|
register.filter(linebreaksbr)
|
|
|
|
register.filter(linenumbers)
|
|
|
|
register.filter(ljust)
|
|
|
|
register.filter(lower)
|
|
|
|
register.filter(make_list)
|
|
|
|
register.filter(phone2numeric)
|
|
|
|
register.filter(pluralize)
|
|
|
|
register.filter(pprint)
|
|
|
|
register.filter(removetags)
|
|
|
|
register.filter(random)
|
|
|
|
register.filter(rjust)
|
2007-11-14 20:58:53 +08:00
|
|
|
register.filter(safe)
|
2005-11-27 09:55:55 +08:00
|
|
|
register.filter('slice', slice_)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(slugify)
|
|
|
|
register.filter(stringformat)
|
|
|
|
register.filter(striptags)
|
|
|
|
register.filter(time)
|
|
|
|
register.filter(timesince)
|
2006-06-21 14:56:08 +08:00
|
|
|
register.filter(timeuntil)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(title)
|
|
|
|
register.filter(truncatewords)
|
2007-02-10 10:51:27 +08:00
|
|
|
register.filter(truncatewords_html)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(unordered_list)
|
|
|
|
register.filter(upper)
|
|
|
|
register.filter(urlencode)
|
|
|
|
register.filter(urlize)
|
|
|
|
register.filter(urlizetrunc)
|
|
|
|
register.filter(wordcount)
|
|
|
|
register.filter(wordwrap)
|
2005-12-15 23:56:26 +08:00
|
|
|
register.filter(yesno)
|