1
0
mirror of https://github.com/django/django.git synced 2025-07-05 10:19:20 +00:00

new-admin: Negligible syntax changes in django.core.template in preparation for merge to trunk

git-svn-id: http://code.djangoproject.com/svn/django/branches/new-admin@1372 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-23 19:05:55 +00:00
parent 855d9909c6
commit c18d99735e
5 changed files with 54 additions and 53 deletions

View File

@ -74,7 +74,8 @@ VARIABLE_TAG_END = '}}'
ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.' ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.'
#What to report as the origin of templates that come from non loader sources (ie strings) # what to report as the origin for templates that come from non-loader sources
# (e.g. strings)
UNKNOWN_SOURCE="<unknown source>" UNKNOWN_SOURCE="<unknown source>"
# match a variable or block tag and capture the entire tag, including start/end delimiters # match a variable or block tag and capture the entire tag, including start/end delimiters
@ -127,7 +128,8 @@ class Template:
"Compilation stage" "Compilation stage"
if TEMPLATE_DEBUG and origin == None: if TEMPLATE_DEBUG and origin == None:
origin = StringOrigin(template_string) origin = StringOrigin(template_string)
#Could do some crazy stack frame stuff to record where this string came from... # Could do some crazy stack-frame stuff to record where this string
# came from...
self.nodelist = compile_string(template_string, origin) self.nodelist = compile_string(template_string, origin)
def __iter__(self): def __iter__(self):
@ -204,13 +206,13 @@ class Token:
def __str__(self): def __str__(self):
return '<%s token: "%s...">' % ( return '<%s token: "%s...">' % (
{TOKEN_TEXT:'Text', TOKEN_VAR:'Var', TOKEN_BLOCK:'Block'}[self.token_type], {TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type],
self.contents[:20].replace('\n', '') self.contents[:20].replace('\n', '')
) )
def __repr__(self): def __repr__(self):
return '<%s token: "%s">' % ( return '<%s token: "%s">' % (
{TOKEN_TEXT:'Text', TOKEN_VAR:'Var', TOKEN_BLOCK:'Block'}[self.token_type], {TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type],
self.contents[:].replace('\n', '') self.contents[:].replace('\n', '')
) )
@ -237,7 +239,7 @@ class Lexer(object):
class DebugLexer(Lexer): class DebugLexer(Lexer):
def __init__(self, template_string, origin): def __init__(self, template_string, origin):
super(DebugLexer,self).__init__(template_string, origin) super(DebugLexer, self).__init__(template_string, origin)
def tokenize(self): def tokenize(self):
"Return a list of tokens from a given template_string" "Return a list of tokens from a given template_string"
@ -245,14 +247,14 @@ class DebugLexer(Lexer):
for match in tag_re.finditer(self.template_string): for match in tag_re.finditer(self.template_string):
start, end = match.span() start, end = match.span()
if start > upto: if start > upto:
token_tups.append( (self.template_string[upto:start], (upto, start) ) ) token_tups.append( (self.template_string[upto:start], (upto, start)) )
upto = start upto = start
token_tups.append( (self.template_string[start:end], (start,end) ) ) token_tups.append( (self.template_string[start:end], (start,end)) )
upto = end upto = end
last_bit = self.template_string[upto:] last_bit = self.template_string[upto:]
if last_bit: if last_bit:
token_tups.append( (last_bit, (upto, upto + len(last_bit) ) ) ) token_tups.append( (last_bit, (upto, upto + len(last_bit))) )
return [ self.create_token(tok, (self.origin, loc)) for tok, loc in token_tups] return [self.create_token(tok, (self.origin, loc)) for tok, loc in token_tups]
def create_token(self, token_string, source): def create_token(self, token_string, source):
token = super(DebugLexer, self).create_token(token_string) token = super(DebugLexer, self).create_token(token_string)
@ -373,8 +375,7 @@ class DebugParser(Parser):
def unclosed_block_tag(self, parse_until): def unclosed_block_tag(self, parse_until):
(command, source) = self.command_stack.pop() (command, source) = self.command_stack.pop()
msg = "Unclosed tag '%s'. Looking for one of: %s " % \ msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until))
(command, ', '.join(parse_until) )
raise self.source_error( source, msg) raise self.source_error( source, msg)
def compile_function_error(self, token, e): def compile_function_error(self, token, e):
@ -668,8 +669,7 @@ filter_raw_string = filter_raw_string.replace("\n", "").replace(" ", "")
filter_re = re.compile(filter_raw_string) filter_re = re.compile(filter_raw_string)
class RegexFilterParser(object): class RegexFilterParser(object):
""" Not used yet because of i18n""" "Not used yet because of i18n"
def __init__(self, token): def __init__(self, token):
matches = filter_re.finditer(token) matches = filter_re.finditer(token)
var = None var = None
@ -827,7 +827,7 @@ class DebugNodeList(NodeList):
raise raise
except Exception: except Exception:
from sys import exc_info from sys import exc_info
wrapped = TemplateSyntaxError( 'Caught exception whilst rendering' ) wrapped = TemplateSyntaxError('Caught an exception while rendering.')
wrapped.source = node.source wrapped.source = node.source
wrapped.exc_info = exc_info() wrapped.exc_info = exc_info()
raise wrapped raise wrapped

View File

@ -192,7 +192,7 @@ class RegroupNode(Node):
for obj in obj_list: for obj in obj_list:
grouper = resolve_variable_with_filters('var.%s' % self.expression, \ grouper = resolve_variable_with_filters('var.%s' % self.expression, \
Context({'var': obj})) Context({'var': obj}))
#TODO: Is this a sensible way to determine equality? # TODO: Is this a sensible way to determine equality?
if output and repr(output[-1]['grouper']) == repr(grouper): if output and repr(output[-1]['grouper']) == repr(grouper):
output[-1]['list'].append(obj) output[-1]['list'].append(obj)
else: else:

View File

@ -7,9 +7,10 @@
# #
# name is the template name. # name is the template name.
# dirs is an optional list of directories to search instead of TEMPLATE_DIRS. # dirs is an optional list of directories to search instead of TEMPLATE_DIRS.
#
# The loader should return a tuple of (template_source, path). The path returned # The loader should return a tuple of (template_source, path). The path returned
# will be shown to the user for debugging purposes, so it should identify where the template # might be shown to the user for debugging purposes, so it should identify where
# was loaded from. # the template was loaded from.
# #
# Each loader should have an "is_usable" attribute set. This is a boolean that # Each loader should have an "is_usable" attribute set. This is a boolean that
# specifies whether the loader can be used in this Python installation. Each # specifies whether the loader can be used in this Python installation. Each
@ -185,26 +186,26 @@ class ConstantIncludeNode(Node):
def __init__(self, template_path): def __init__(self, template_path):
try: try:
t = get_template(template_path) t = get_template(template_path)
self.nodelist = t.nodelist self.template = t
except Exception, e: except Exception, e:
if TEMPLATE_DEBUG: if TEMPLATE_DEBUG:
raise raise
self.nodelist = None self.template = None
def render(self, context): def render(self, context):
if self.nodelist: if self.template:
return self.nodelist.render(context) return self.template.render(context)
else: else:
return '' return ''
class IncludeNode(Node): class IncludeNode(Node):
def __init__(self, template_path_var): def __init__(self, template_name):
self.template_path_var = template_path_var self.template_name = template_name
def render(self, context): def render(self, context):
try: try:
template_path = resolve_variable(self.template_path_var, context) template_name = resolve_variable(self.template_name, context)
t = get_template(template_path) t = get_template(template_name)
return t.render(context) return t.render(context)
except Exception, e: except Exception, e:
if TEMPLATE_DEBUG: if TEMPLATE_DEBUG: