commit dcbff38c016c824c5927b1f57385d19aad4157f3
Author: Bastian Kleineidam <calvin@debian.org>
Date: Wed Feb 6 11:34:56 2008 +0100
Allow comments and whitespace before an {% extends %}
diff --git a/django/template/__init__.py b/django/template/__init__.py
index 1772b89..c85f9fa 100644
a
|
b
|
class Parser(object):
|
303 | 303 | return NodeList() |
304 | 304 | |
305 | 305 | def extend_nodelist(self, nodelist, node, token): |
306 | | if node.must_be_first and nodelist: |
307 | | try: |
308 | | if nodelist.contains_nontext: |
309 | | raise AttributeError |
310 | | except AttributeError: |
311 | | raise TemplateSyntaxError("%r must be the first tag in the template." % node) |
| 306 | if node.must_be_first and nodelist_extends_filter(nodelist): |
| 307 | raise TemplateSyntaxError("%r must be the first tag in the template (preceding nodes: %s)" % (node, nodelist)) |
312 | 308 | if isinstance(nodelist, NodeList) and not isinstance(node, TextNode): |
313 | 309 | nodelist.contains_nontext = True |
314 | 310 | nodelist.append(node) |
… |
… |
class Parser(object):
|
360 | 356 | else: |
361 | 357 | raise TemplateSyntaxError("Invalid filter: '%s'" % filter_name) |
362 | 358 | |
| 359 | |
| 360 | is_whitespace = re.compile(r"^\s+$").search |
| 361 | def nodelist_extends_filter (nodelist): |
| 362 | """Filter ignorable nodes preceding a "extends" tag.""" |
| 363 | return filter(lambda x: not node_extends_ignore(x), nodelist) |
| 364 | |
| 365 | |
| 366 | def node_extends_ignore (node): |
| 367 | """Determine if the contents of the node can be ignored preceding |
| 368 | a "extends" tag.""" |
| 369 | from defaulttags import CommentNode |
| 370 | return (isinstance(node, TextNode) and is_whitespace(node.s)) or \ |
| 371 | isinstance(node, CommentNode) |
| 372 | |
| 373 | |
363 | 374 | class TokenParser(object): |
364 | 375 | """ |
365 | 376 | Subclass this and implement the top() method to parse a template line. When |