Commit f94aa621 by Chris Jerdonek

Started decoupling Parser from RenderEngine.

parent 7eef0a68
......@@ -38,7 +38,7 @@ class ParsedTemplate(object):
def add(self, node):
self._parse_tree.append(node)
def render(self, context):
def render(self, engine, context):
"""
Returns: a string of type unicode.
......@@ -47,9 +47,10 @@ class ParsedTemplate(object):
def get_unicode(val):
if callable(val):
return val(context)
return val
if isinstance(val, basestring):
return val
return val.render(engine, context)
parts = map(get_unicode, self._parse_tree)
s = ''.join(parts)
return unicode(s)
......@@ -52,6 +52,26 @@ class ParsingError(Exception):
pass
class VariableNode(object):
def __init__(self, key):
self.key = key
def render(self, engine, context):
s = engine._get_string_value(context, self.key)
return engine.escape(s)
class LiteralNode(object):
def __init__(self, key):
self.key = key
def render(self, engine, context):
s = engine._get_string_value(context, self.key)
return engine.literal(s)
class Parser(object):
_delimiters = None
......@@ -192,10 +212,10 @@ class Parser(object):
return u''
if tag_type == '':
return self.engine._make_get_escaped(tag_key)
return VariableNode(tag_key)
if tag_type == '&':
return self.engine._make_get_literal(tag_key)
return LiteralNode(tag_key)
if tag_type == '>':
return self.engine._make_get_partial(tag_key, leading_whitespace)
......
......@@ -106,30 +106,6 @@ class RenderEngine(object):
return val
def _make_get_literal(self, name):
def get_literal(context):
"""
Returns: a string of type unicode.
"""
s = self._get_string_value(context, name)
return self.literal(s)
return get_literal
def _make_get_escaped(self, name):
get_literal = self._make_get_literal(name)
def get_escaped(context):
"""
Returns: a string of type unicode.
"""
s = self._get_string_value(context, name)
return self.escape(s)
return get_escaped
def _make_get_partial(self, tag_key, leading_whitespace):
template = self.resolve_partial(tag_key)
......@@ -158,7 +134,7 @@ class RenderEngine(object):
# Per the spec, lambdas in inverted sections are considered truthy.
if data:
return u''
return parsed_template.render(context)
return self._render_parsed(parsed_template, context)
return get_inverse
......@@ -228,13 +204,16 @@ class RenderEngine(object):
continue
context.push(val)
parts.append(parsed_template.render(context))
parts.append(self._render_parsed(parsed_template, context))
context.pop()
return unicode(''.join(parts))
return get_section_value
def _render_parsed(self, parsed_template, context_stack):
return parsed_template.render(self, context_stack)
def _render_value(self, val, context, delimiters=None):
"""
Render an arbitrary value.
......@@ -262,4 +241,4 @@ class RenderEngine(object):
parser = Parser(self, delimiters=delimiters)
parsed_template = parser.parse(template)
return parsed_template.render(context_stack)
return self._render_parsed(parsed_template, context_stack)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment