From f71f8546283dbdf698c7578f8f9154045c84f9e7 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Fri, 14 Oct 2005 20:10:13 +0000 Subject: Fixed #626 -- Moved template modules to django.core.template package. django.core.template_loader is deprecated, in favor of django.core.template.loader. git-svn-id: http://code.djangoproject.com/svn/django/trunk@867 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/defaultfilters.py | 465 ----------------- django/core/defaulttags.py | 770 ---------------------------- django/core/extensions.py | 5 +- django/core/rss.py | 13 +- django/core/template.py | 494 ------------------ django/core/template/__init__.py | 494 ++++++++++++++++++ django/core/template/defaultfilters.py | 465 +++++++++++++++++ django/core/template/defaulttags.py | 771 +++++++++++++++++++++++++++++ django/core/template/loader.py | 163 ++++++ django/core/template/loaders/__init__.py | 0 django/core/template/loaders/filesystem.py | 21 + django/core/template_file.py | 21 - django/core/template_loader.py | 167 +------ django/middleware/admin.py | 10 +- django/views/admin/doc.py | 3 +- django/views/admin/template.py | 9 +- django/views/auth/login.py | 2 +- django/views/defaults.py | 7 +- django/views/registration/passwords.py | 5 +- docs/forms.txt | 36 +- docs/sessions.txt | 4 +- docs/templates_python.txt | 10 +- docs/tutorial03.txt | 9 +- docs/tutorial04.txt | 4 +- tests/othertests/templates.py | 11 +- 25 files changed, 1970 insertions(+), 1989 deletions(-) delete mode 100644 django/core/defaultfilters.py delete mode 100644 django/core/defaulttags.py delete mode 100644 django/core/template.py create mode 100644 django/core/template/__init__.py create mode 100644 django/core/template/defaultfilters.py create mode 100644 django/core/template/defaulttags.py create mode 100644 django/core/template/loader.py create mode 100644 django/core/template/loaders/__init__.py create mode 100644 django/core/template/loaders/filesystem.py delete mode 100644 django/core/template_file.py diff --git a/django/core/defaultfilters.py b/django/core/defaultfilters.py deleted file mode 100644 index b9e0e67745..0000000000 --- a/django/core/defaultfilters.py +++ /dev/null @@ -1,465 +0,0 @@ -"Default variable filters" - -import template, re -import random as random_module - -################### -# STRINGS # -################### - -def addslashes(value, _): - "Adds slashes - useful for passing strings to JavaScript, for example." - return value.replace('"', '\\"').replace("'", "\\'") - -def capfirst(value, _): - "Capitalizes the first character of the value" - value = str(value) - return value and value[0].upper() + value[1:] - -def fix_ampersands(value, _): - "Replaces ampersands with ``&`` entities" - from django.utils.html import fix_ampersands - return fix_ampersands(value) - -def floatformat(text, _): - """ - Displays a floating point number as 34.2 (with one decimal place) - but - only if there's a point to be displayed - """ - from math import modf - if not text: - return '' - if modf(float(text))[0] < 0.1: - return text - return "%.1f" % float(text) - -def linenumbers(value, _): - "Displays text with line numbers" - from django.utils.html import escape - lines = value.split('\n') - # Find the maximum width of the line count, for use with zero padding string format command - width = str(len(str(len(lines)))) - for i, line in enumerate(lines): - lines[i] = ("%0" + width + "d. %s") % (i + 1, escape(line)) - return '\n'.join(lines) - -def lower(value, _): - "Converts a string into all lowercase" - return value.lower() - -def make_list(value, _): - """ - 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. - """ - return list(str(value)) - -def slugify(value, _): - "Converts to lowercase, removes non-alpha chars and converts spaces to hyphens" - value = re.sub('[^\w\s-]', '', value).strip().lower() - return re.sub('\s+', '-', value) - -def stringformat(value, arg): - """ - Formats the variable according to the argument, a string formatting specifier. - This specifier uses Python string formating syntax, with the exception that - the leading "%" is dropped. - - See http://docs.python.org/lib/typesseq-strings.html for documentation - of Python string formatting - """ - try: - return ("%" + arg) % value - except (ValueError, TypeError): - return "" - -def title(value, _): - "Converts a string into titlecase" - return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title()) - -def truncatewords(value, arg): - """ - Truncates a string after a certain number of words - - Argument: Number of words to truncate after - """ - from django.utils.text import truncate_words - try: - length = int(arg) - except ValueError: # invalid literal for int() - return value # Fail silently. - if not isinstance(value, basestring): - value = str(value) - return truncate_words(value, length) - -def upper(value, _): - "Converts a string into all uppercase" - return value.upper() - -def urlencode(value, _): - "Escapes a value for use in a URL" - import urllib - return urllib.quote(value) - -def urlize(value, _): - "Converts URLs in plain text into clickable links" - from django.utils.html import urlize - return urlize(value, nofollow=True) - -def urlizetrunc(value, limit): - """ - Converts URLs into clickable links, truncating URLs to the given character limit - - Argument: Length to truncate URLs to. - """ - from django.utils.html import urlize - return urlize(value, trim_url_limit=int(limit), nofollow=True) - -def wordcount(value, _): - "Returns the number of words" - return len(value.split()) - -def wordwrap(value, arg): - """ - Wraps words at specified line length - - Argument: number of words to wrap the text at. - """ - from django.utils.text import wrap - return wrap(value, int(arg)) - -def ljust(value, arg): - """ - Left-aligns the value in a field of a given width - - Argument: field size - """ - return str(value).ljust(int(arg)) - -def rjust(value, arg): - """ - Right-aligns the value in a field of a given width - - Argument: field size - """ - return str(value).rjust(int(arg)) - -def center(value, arg): - "Centers the value in a field of a given width" - return str(value).center(int(arg)) - -def cut(value, arg): - "Removes all values of arg from the given string" - return value.replace(arg, '') - -################### -# HTML STRINGS # -################### - -def escape(value, _): - "Escapes a string's HTML" - from django.utils.html import escape - return escape(value) - -def linebreaks(value, _): - "Converts newlines into

and
s" - from django.utils.html import linebreaks - return linebreaks(value) - -def linebreaksbr(value, _): - "Converts newlines into
s" - return value.replace('\n', '
') - -def removetags(value, tags): - "Removes a space separated list of [X]HTML tags from the output" - tags = [re.escape(tag) for tag in tags.split()] - tags_re = '(%s)' % '|'.join(tags) - starttag_re = re.compile('<%s(>|(\s+[^>]*>))' % tags_re) - endtag_re = re.compile('' % tags_re) - value = starttag_re.sub('', value) - value = endtag_re.sub('', value) - return value - -def striptags(value, _): - "Strips all [X]HTML tags" - from django.utils.html import strip_tags - if not isinstance(value, basestring): - value = str(value) - return strip_tags(value) - -################### -# LISTS # -################### - -def dictsort(value, arg): - """ - Takes a list of dicts, returns that list sorted by the property given in - the argument. - """ - decorated = [(template.resolve_variable('var.' + arg, {'var' : item}), item) for item in value] - decorated.sort() - return [item[1] for item in decorated] - -def dictsortreversed(value, arg): - """ - Takes a list of dicts, returns that list sorted in reverse order by the - property given in the argument. - """ - decorated = [(template.resolve_variable('var.' + arg, {'var' : item}), item) for item in value] - decorated.sort() - decorated.reverse() - return [item[1] for item in decorated] - -def first(value, _): - "Returns the first item in a list" - try: - return value[0] - except IndexError: - return '' - -def join(value, arg): - "Joins a list with a string, like Python's ``str.join(list)``" - try: - return arg.join(map(str, value)) - except AttributeError: # fail silently but nicely - return value - -def length(value, _): - "Returns the length of the value - useful for lists" - return len(value) - -def length_is(value, arg): - "Returns a boolean of whether the value's length is the argument" - return len(value) == int(arg) - -def random(value, _): - "Returns a random item from the list" - return random_module.choice(value) - -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: - return value[slice(*[x and int(x) or None for x in arg.split(':')])] - except (ValueError, TypeError): - return value # Fail silently. - -def unordered_list(value, _): - """ - Recursively takes a self-nested list and returns an HTML unordered list -- - WITHOUT opening and closing