diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-05-05 07:01:28 -0700 |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-05-05 07:01:28 -0700 |
commit | 06e290786eec127290be976dcfce08081b01c08d (patch) | |
tree | 7aecf3dc5e096e634d7e0941e26921bd470d5bd8 /pystache | |
parent | 2de83f87b16edad7fa028e56adf08548fa41a777 (diff) | |
download | pystache-06e290786eec127290be976dcfce08081b01c08d.tar.gz |
Work with ParsedTemplate instance instead of parse_tree list.
Diffstat (limited to 'pystache')
-rw-r--r-- | pystache/parsed.py | 7 | ||||
-rw-r--r-- | pystache/parser.py | 14 |
2 files changed, 12 insertions, 9 deletions
diff --git a/pystache/parsed.py b/pystache/parsed.py index a37565b..d791be4 100644 --- a/pystache/parsed.py +++ b/pystache/parsed.py @@ -10,7 +10,7 @@ This module is meant only for internal use. class ParsedTemplate(object): - def __init__(self, parse_tree): + def __init__(self): """ Arguments: @@ -30,11 +30,14 @@ class ParsedTemplate(object): * RenderEngine._make_get_section() """ - self._parse_tree = parse_tree + self._parse_tree = [] def __repr__(self): return "[%s]" % (", ".join([repr(part) for part in self._parse_tree])) + def add(self, node): + self._parse_tree.append(node) + def render(self, context): """ Returns: a string of type unicode. diff --git a/pystache/parser.py b/pystache/parser.py index b573138..1202c66 100644 --- a/pystache/parser.py +++ b/pystache/parser.py @@ -96,7 +96,7 @@ class Parser(object): a ParsedTemplate instance. """ - parse_tree = [] + parsed_template = ParsedTemplate() content_end_index, parsed_section = None, None @@ -111,7 +111,7 @@ class Parser(object): before_tag = template[start_index : match_index] - parse_tree.append(before_tag) + parsed_template.add(before_tag) matches = match.groupdict() @@ -137,7 +137,7 @@ class Parser(object): if end_index < len(template): end_index += template[end_index] == '\n' and 1 or 0 elif leading_whitespace: - parse_tree.append(leading_whitespace) + parsed_template.add(leading_whitespace) match_index += len(leading_whitespace) leading_whitespace = '' @@ -145,7 +145,7 @@ class Parser(object): if tag_key != section_key: raise ParsingError("Section end tag mismatch: %s != %s" % (tag_key, section_key)) - return end_index, match_index, ParsedTemplate(parse_tree) + return end_index, match_index, parsed_template if tag_type in ('#', '^'): start_index, content_end_index, parsed_section = self.parse(template, end_index, tag_key) @@ -155,12 +155,12 @@ class Parser(object): node = self._make_node(template, tag_type, tag_key, leading_whitespace, end_index, content_end_index, parsed_section) - parse_tree.append(node) + parsed_template.add(node) # Save the rest of the template. - parse_tree.append(template[start_index:]) + parsed_template.add(template[start_index:]) - return ParsedTemplate(parse_tree) + return parsed_template def _make_node(self, template, tag_type, tag_key, leading_whitespace, section_start_index, section_end_index, parsed_section): |