self.visit(node.step, frame)
def visit_Filter(self, node, frame):
- for filter in node.filters:
+ value = node.node
+ flen = len(node.filters)
+ if isinstance(value, nodes.Const):
+ # try to optimize filters on constant values
+ for filter in reversed(node.filters):
+ value = nodes.Const(self.environment.filters \
+ .get(filter.name)(self.environment, value.value))
+ print value
+ flen -= 1
+ for filter in node.filters[:flen]:
if filter.name in frame.identifiers.declared_filter:
self.write('f_%s(' % filter.name)
else:
self.write('context.filter[%r](' % filter.name)
- self.visit(node.node, frame)
- for filter in reversed(node.filters):
+ self.visit(value, frame)
+ for filter in reversed(node.filters[:flen]):
self.signature(filter, frame)
self.write(')')
:copyright: 2007 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
-from jinja.filters import FILTERS as DEFAULT_FILTERS
+from jinja2.filters import FILTERS as DEFAULT_FILTERS
from jinja.tests import TESTS as DEFAULT_TESTS
DEFAULT_NAMESPACE = {}
except ImportError:
itemgetter = lambda a: lambda b: b[a]
from urllib import urlencode, quote
+from jinja.utils import escape
_striptags_re = re.compile(r'(<!--.*?-->|<[^>]*>)')
Convert a value to uppercase.
"""
return s.upper()
-do_upper = stringfilter(do_upper)
-def do_lower(s):
+def do_lower(env, s):
"""
Convert a value to lowercase.
"""
return s.lower()
-do_lower = stringfilter(do_lower)
-def do_escape(attribute=False):
+def do_escape(env, s, attribute=False):
"""
XML escape ``&``, ``<``, and ``>`` in a string of data. If the
optional parameter is `true` this filter will also convert
This method will have no effect it the value is already escaped.
"""
- #: because filters are cached we can make a local alias to
- #: speed things up a bit
- e = escape
- def wrapped(env, context, s):
- if isinstance(s, TemplateData):
- return s
- elif hasattr(s, '__html__'):
- return s.__html__()
- #: small speedup, do not convert to unicode if we already
- #: have an unicode object.
- if s.__class__ is not unicode:
- s = env.to_unicode(s)
- return e(s, attribute)
- return wrapped
+ # XXX: Does this still exists?
+ #if isinstance(s, TemplateData):
+ # return s
+ if hasattr(s, '__html__'):
+ return s.__html__()
+ return escape(unicode(s), attribute)
def do_xmlattr(autospace=False):