From: Armin Ronacher Date: Sat, 28 Apr 2007 13:56:27 +0000 (+0200) Subject: [svn] checkpoint X-Git-Tag: 2.0rc1~335 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=c7ddd1d55b6562c3874c9691b58ad540cdb5237a;p=jinja2.git [svn] checkpoint --HG-- branch : trunk --- diff --git a/docs/src/designerdoc.txt b/docs/src/designerdoc.txt index e826d8f..7179292 100644 --- a/docs/src/designerdoc.txt +++ b/docs/src/designerdoc.txt @@ -611,6 +611,7 @@ value from the parent template is used instead. two similarly-named ``{% block %}`` tags in a template, that template's parent wouldn't know which one of the blocks' content to use. + Super Blocks ============ diff --git a/jinja/filters.py b/jinja/filters.py index 08208ca..fd5c1d4 100644 --- a/jinja/filters.py +++ b/jinja/filters.py @@ -122,7 +122,7 @@ def do_escape(attribute=False): return wrapped -def do_xmlattr(): +def do_xmlattr(autospace=False): """ Create an SGML/XML attribute string based on the items in a dict. All values that are neither `none` nor `undefined` are automatically @@ -130,7 +130,7 @@ def do_xmlattr(): .. sourcecode:: html+jinja - @@ -143,6 +143,10 @@ def do_xmlattr(): ... + As you can see it automatically appends a space in front of the item + if the filter returned something. You can disable this by passing + `false` as only argument to the filter. + *New in Jinja 1.1* """ e = escape @@ -156,7 +160,10 @@ def do_xmlattr(): e(env.to_unicode(key)), e(env.to_unicode(value), True) )) - return u' '.join(result) + rv = u' '.join(result) + if autospace: + rv = ' ' + rv + return rv return wrapped @@ -315,7 +322,7 @@ def do_first(): try: return iter(seq).next() except StopIteration: - return env.undefined_singleton[0] + return env.undefined_singleton return wrapped @@ -327,7 +334,7 @@ def do_last(): try: return iter(_reversed(seq)).next() except StopIteration: - return env.undefined_singleton[-1] + return env.undefined_singleton return wrapped @@ -339,7 +346,7 @@ def do_random(): try: return choice(seq) except IndexError: - return env.undefined_singleton[0] + return env.undefined_singleton return wrapped diff --git a/jinja/nodes.py b/jinja/nodes.py index f3af166..bd60ca5 100644 --- a/jinja/nodes.py +++ b/jinja/nodes.py @@ -30,11 +30,15 @@ def inc_lineno(offset, tree): todo.extend(node.getChildNodes()) -def get_nodes(nodetype, tree): +def get_nodes(nodetype, tree, exclude_root=True): """ - Get all nodes from nodetype in the tree. + Get all nodes from nodetype in the tree excluding the + node passed if `exclude_root` is `True` (default). """ - todo = [tree] + if exclude_root: + todo = tree.getChildNodes() + else: + todo = [tree] while todo: node = todo.pop() if node.__class__ is nodetype: @@ -42,6 +46,26 @@ def get_nodes(nodetype, tree): todo.extend(node.getChildNodes()) +def get_nodes_parentinfo(nodetype, tree): + """ + Like `get_nodes` but it yields tuples in the form ``(parent, node)``. + If a node is a direct ancestor of `tree` parent will be `None`. + + Always excludes the root node. + """ + todo = [tree] + while todo: + node = todo.pop() + if node is tree: + parent = None + else: + parent = node + for child in node.getChildNodes(): + if child.__class__ is nodetype: + yield parent, child + todo.append(child) + + class Node(ast.Node): """ Jinja node. @@ -90,7 +114,7 @@ class DynamicText(Node): def __repr__(self): return 'DynamicText(%r, %r)' % ( - self.text, + self.format_string, self.variables ) diff --git a/jinja/parser.py b/jinja/parser.py index c76f990..a7e2e05 100644 --- a/jinja/parser.py +++ b/jinja/parser.py @@ -531,7 +531,6 @@ class Parser(object): return nodes.Trans(flineno, singular, plural, indicator, replacements or None) - def parse_python(self, lineno, gen, template): """ Convert the passed generator into a flat string representing diff --git a/jinja/translators/python.py b/jinja/translators/python.py index f2195fd..0e5cad8 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -231,9 +231,7 @@ class PythonTranslator(Translator): #: the value represents the feature name that appears #: in the exception. self.unsupported = { - ast.ListComp: 'list comprehensions', - ast.From: 'imports', - ast.Import: 'imports', + ast.ListComp: 'list comprehensions' } #: because of python2.3 compatibility add generator @@ -408,7 +406,7 @@ class PythonTranslator(Translator): parent = self.environment.loader.parse(node.extends.template, node.filename) # look up all block nodes in the current template and - # add them to the override dict + # add them to the override dict. for n in get_nodes(nodes.Block, node): overwrites[n.name] = n # handle direct overrides