Skip to content

Commit

Permalink
Backported a couple of templating core changes from the advanced-i18n…
Browse files Browse the repository at this point in the history
… branch, in particular considering the determination of directive ordering../set
  • Loading branch information
cmlenz committed Oct 13, 2009
1 parent 140c613 commit d48d93c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
28 changes: 16 additions & 12 deletions genshi/template/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2006-2008 Edgewall Software
# Copyright (C) 2006-2009 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
Expand Down Expand Up @@ -321,16 +321,6 @@ class DirectiveFactory(object):
provided by this factory.
"""

def compare_directives(self):
"""Return a function that takes two directive classes and compares
them to determine their relative ordering.
"""
def _get_index(cls):
if cls in self._dir_order:
return self._dir_order.index(cls)
return 0
return lambda a, b: cmp(_get_index(a[0]), _get_index(b[0]))

def get_directive(self, name):
"""Return the directive class for the given name.
Expand All @@ -340,6 +330,20 @@ def get_directive(self, name):
"""
return self._dir_by_name.get(name)

def get_directive_index(self, dir_cls):
"""Return a key for the given directive class that should be used to
sort it among other directives on the same `SUB` event.
The default implementation simply returns the index of the directive in
the `directives` list.
:param dir_cls: the directive class
:return: the sort key
"""
if dir_cls in self._dir_order:
return self._dir_order.index(dir_cls)
return len(self._dir_order)


class Template(DirectiveFactory):
"""Abstract template base class.
Expand Down Expand Up @@ -451,7 +455,7 @@ def _prepare(self, stream):
if kind is SUB:
directives = []
substream = data[1]
for cls, value, namespaces, pos in data[0]:
for _, cls, value, namespaces, pos in sorted(data[0]):
directive, substream = cls.attach(self, substream, value,
namespaces, pos)
if directive:
Expand Down
11 changes: 2 additions & 9 deletions genshi/template/directives.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2006-2008 Edgewall Software
# Copyright (C) 2006-2009 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
Expand Down Expand Up @@ -527,7 +527,7 @@ class StripDirective(Directive):

def __call__(self, stream, directives, ctxt, **vars):
def _generate():
if _eval_expr(self.expr, ctxt, vars):
if not self.expr or _eval_expr(self.expr, ctxt, vars):
stream.next() # skip start tag
previous = stream.next()
for event in stream:
Expand All @@ -538,13 +538,6 @@ def _generate():
yield event
return _apply_directives(_generate(), directives, ctxt, vars)

@classmethod
def attach(cls, template, stream, value, namespaces, pos):
if not value:
return None, stream[1:-1]
return super(StripDirective, cls).attach(template, stream, value,
namespaces, pos)


class ChooseDirective(Directive):
"""Implementation of the ``py:choose`` directive for conditionally selecting
Expand Down
11 changes: 6 additions & 5 deletions genshi/template/markup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2006-2008 Edgewall Software
# Copyright (C) 2006-2009 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
Expand Down Expand Up @@ -131,7 +131,8 @@ def _extract_directives(self, stream, namespace, factory):
self.filepath, pos[1])
args = dict([(name.localname, value) for name, value
in attrs if not name.namespace])
directives.append((cls, args, ns_prefix.copy(), pos))
directives.append((factory.get_directive_index(cls), cls,
args, ns_prefix.copy(), pos))
strip = True

new_attrs = []
Expand All @@ -143,14 +144,14 @@ def _extract_directives(self, stream, namespace, factory):
self.filepath, pos[1])
if type(value) is list and len(value) == 1:
value = value[0][1]
directives.append((cls, value, ns_prefix.copy(),
pos))
directives.append((factory.get_directive_index(cls),
cls, value, ns_prefix.copy(), pos))
else:
new_attrs.append((name, value))
new_attrs = Attrs(new_attrs)

if directives:
directives.sort(self.compare_directives())
directives.sort()
dirmap[(depth, tag)] = (directives, len(new_stream),
strip)

Expand Down
6 changes: 3 additions & 3 deletions genshi/template/text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2006-2008 Edgewall Software
# Copyright (C) 2006-2009 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
Expand Down Expand Up @@ -219,7 +219,7 @@ def _escape_repl(mo):
cls = self.get_directive(command)
if cls is None:
raise BadDirectiveError(command)
directive = cls, value, None, (self.filepath, lineno, 0)
directive = 0, cls, value, None, (self.filepath, lineno, 0)
dirmap[depth] = (directive, len(stream))
depth += 1

Expand Down Expand Up @@ -315,7 +315,7 @@ def _parse(self, source, encoding):
cls = self.get_directive(command)
if cls is None:
raise BadDirectiveError(command)
directive = cls, value, None, (self.filepath, lineno, 0)
directive = 0, cls, value, None, (self.filepath, lineno, 0)
dirmap[depth] = (directive, len(stream))
depth += 1

Expand Down

0 comments on commit d48d93c

Please sign in to comment.