Commit 3de74535 by Chris Jerdonek

Simplified calls to render_parse_tree() (removed the third argument).

parent 69490716
...@@ -17,7 +17,7 @@ DEFAULT_TAG_CLOSING = '}}' ...@@ -17,7 +17,7 @@ DEFAULT_TAG_CLOSING = '}}'
END_OF_LINE_CHARACTERS = ['\r', '\n'] END_OF_LINE_CHARACTERS = ['\r', '\n']
def call(val, view, template=None): def call(val, view):
""" """
Arguments: Arguments:
...@@ -26,11 +26,16 @@ def call(val, view, template=None): ...@@ -26,11 +26,16 @@ def call(val, view, template=None):
* a unicode string * a unicode string
* the return value of a call to any of the following: * the return value of a call to any of the following:
* RenderEngine.partial_tag_function() * RenderEngine._make_get_literal():
* RenderEngine.section_tag_function() Args: context
* inverseTag() * RenderEngine._make_get_escaped():
* RenderEngine.literal_tag_function() Args: context
* RenderEngine.escape_tag_function() * RenderEngine._make_get_partial()
Args: context
* RenderEngine._make_get_section()
Args: context
* _make_get_inverse()
Args: context
""" """
if callable(val): if callable(val):
...@@ -62,23 +67,24 @@ def call(val, view, template=None): ...@@ -62,23 +67,24 @@ def call(val, view, template=None):
return unicode(val) return unicode(val)
def render_parse_tree(parse_tree, context, template): def render_parse_tree(parse_tree, context):
""" """
Convert a parse-tree into a string. Convert a parse-tree into a string.
""" """
get_string = lambda val: call(val, context, template) get_string = lambda val: call(val, context)
parts = map(get_string, parse_tree) parts = map(get_string, parse_tree)
return ''.join(parts) return ''.join(parts)
def inverseTag(name, parsed, template, delims): def _make_get_inverse(name, parsed, template, delims):
def func(self): def get_inverse(context):
data = self.get(name) data = context.get(name)
if data: if data:
return '' return ''
return render_parse_tree(parsed, self, delims) return render_parse_tree(parsed, context)
return func
return get_inverse
class EndOfSection(Exception): class EndOfSection(Exception):
...@@ -177,7 +183,7 @@ class RenderEngine(object): ...@@ -177,7 +183,7 @@ class RenderEngine(object):
raise Exception("Argument 'template' not unicode: %s: %s" % (type(template), repr(template))) raise Exception("Argument 'template' not unicode: %s: %s" % (type(template), repr(template)))
parse_tree = self.parse_string_to_tree(template_string=template) parse_tree = self.parse_string_to_tree(template_string=template)
return render_parse_tree(parse_tree, context, template) return render_parse_tree(parse_tree, context)
def parse_string_to_tree(self, template_string, delims=None): def parse_string_to_tree(self, template_string, delims=None):
...@@ -255,33 +261,36 @@ class RenderEngine(object): ...@@ -255,33 +261,36 @@ class RenderEngine(object):
return val return val
def escape_tag_function(self, name): def _make_get_literal(self, name):
get_literal = self.literal_tag_function(name) def get_literal(context):
def func(context):
s = self._get_string_value(context, name) s = self._get_string_value(context, name)
s = self.escape(s) s = self.literal(s)
return s return s
return func
def literal_tag_function(self, name): return get_literal
def func(context):
def _make_get_escaped(self, name):
get_literal = self._make_get_literal(name)
def get_escaped(context):
s = self._get_string_value(context, name) s = self._get_string_value(context, name)
s = self.literal(s) s = self.escape(s)
return s return s
return func return get_escaped
def partial_tag_function(self, name, indentation=''): def _make_get_partial(self, name, indentation=''):
def func(context): def get_partial(context):
nonblank = re.compile(r'^(.)', re.M) nonblank = re.compile(r'^(.)', re.M)
template = self.load_partial(name) template = self.load_partial(name)
# Indent before rendering. # Indent before rendering.
template = re.sub(nonblank, indentation + r'\1', template) template = re.sub(nonblank, indentation + r'\1', template)
return self._render_template(template, context) return self._render_template(template, context)
return func
def section_tag_function(self, name, parse_tree_, template_, delims): return get_partial
def func(context):
def _make_get_section(self, name, parse_tree_, template_, delims):
def get_section(context):
template = template_ template = template_
parse_tree = parse_tree_ parse_tree = parse_tree_
data = context.get(name) data = context.get(name)
...@@ -298,11 +307,12 @@ class RenderEngine(object): ...@@ -298,11 +307,12 @@ class RenderEngine(object):
parts = [] parts = []
for element in data: for element in data:
context.push(element) context.push(element)
parts.append(render_parse_tree(parse_tree, context, delims)) parts.append(render_parse_tree(parse_tree, context))
context.pop() context.pop()
return ''.join(parts) return ''.join(parts)
return func
return get_section
def parse_to_tree(self, template, index=0): def parse_to_tree(self, template, index=0):
""" """
...@@ -366,7 +376,7 @@ class RenderEngine(object): ...@@ -366,7 +376,7 @@ class RenderEngine(object):
return end_index return end_index
if captures['tag'] == '>': if captures['tag'] == '>':
func = self.partial_tag_function(name, captures['whitespace']) func = self._make_get_partial(name, captures['whitespace'])
elif captures['tag'] in ['#', '^']: elif captures['tag'] in ['#', '^']:
try: try:
...@@ -376,16 +386,16 @@ class RenderEngine(object): ...@@ -376,16 +386,16 @@ class RenderEngine(object):
tmpl = e.template tmpl = e.template
end_index = e.position end_index = e.position
tag = self.section_tag_function if captures['tag'] == '#' else inverseTag tag = self._make_get_section if captures['tag'] == '#' else _make_get_inverse
func = tag(name, bufr, tmpl, (self.otag, self.ctag)) func = tag(name, bufr, tmpl, (self.otag, self.ctag))
elif captures['tag'] in ['{', '&']: elif captures['tag'] in ['{', '&']:
func = self.literal_tag_function(name) func = self._make_get_literal(name)
elif captures['tag'] == '': elif captures['tag'] == '':
func = self.escape_tag_function(name) func = self._make_get_escaped(name)
elif captures['tag'] == '/': elif captures['tag'] == '/':
......
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