Commit ef3ef4bf by Chris Jerdonek

Refactored some of the parsing logic: delayed template slicing.

parent 1a9ca1f7
......@@ -177,10 +177,10 @@ class Parser(object):
including any trailing newlines).
"""
parsed_section, content_end_index, end_index = \
parsed_section, section_end_index, end_index = \
self.parse(template=template, start_index=start_index, section_key=section_key)
return parsed_section, template[start_index:content_end_index], end_index
return parsed_section, section_end_index, end_index
def _handle_tag_type(self, template, parse_tree, tag_type, tag_key, leading_whitespace, end_index):
......@@ -205,12 +205,14 @@ class Parser(object):
elif tag_type == '#':
parsed_section, section_contents, end_index = self._parse_section(template, end_index, tag_key)
func = engine._make_get_section(tag_key, parsed_section, section_contents, self._delimiters)
section_start_index = end_index
parsed_section, section_end_index, end_index = self._parse_section(template, end_index, tag_key)
func = engine._make_get_section(tag_key, parsed_section, self._delimiters,
template, section_start_index, section_end_index)
elif tag_type == '^':
parsed_section, section_contents, end_index = self._parse_section(template, end_index, tag_key)
parsed_section, section_end_index, end_index = self._parse_section(template, end_index, tag_key)
func = engine._make_get_inverse(tag_key, parsed_section)
elif tag_type == '>':
......
......@@ -162,14 +162,13 @@ class RenderEngine(object):
# TODO: the template_ and parsed_template_ arguments don't both seem
# to be necessary. Can we remove one of them? For example, if
# callable(data) is True, then the initial parsed_template isn't used.
def _make_get_section(self, name, parsed_template_, template_, delims):
def _make_get_section(self, name, parsed_template, delims,
template, section_start_index, section_end_index):
def get_section(context):
"""
Returns: a string of type unicode.
"""
template = template_
parsed_template = parsed_template_
data = self.resolve_context(context, name)
# From the spec:
......@@ -220,7 +219,7 @@ class RenderEngine(object):
# https://github.com/defunkt/pystache/issues/113
#
# TODO: should we check the arity?
new_template = element(template)
new_template = element(template[section_start_index:section_end_index])
new_parsed_template = self._parse(new_template, delimiters=delims)
parts.append(new_parsed_template.render(context))
continue
......
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