From ce1b81b1a5951c0ea34c4f8251ada3573af9c691 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sat, 5 May 2012 05:29:51 -0700 Subject: More parser refactoring: further simplified _handle_tag_type(). --- pystache/parser.py | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) (limited to 'pystache') diff --git a/pystache/parser.py b/pystache/parser.py index bd6706d..9066c54 100644 --- a/pystache/parser.py +++ b/pystache/parser.py @@ -146,57 +146,52 @@ class Parser(object): if tag_key != section_key: raise ParsingError("Section end tag mismatch: %s != %s" % (tag_key, section_key)) - return end_index, ParsedTemplate(parse_tree), match_index + return end_index, match_index, ParsedTemplate(parse_tree) if tag_type in ('#', '^'): - index, parsed_section, content_end_index = self.parse(template, end_index, tag_key) + index, content_end_index, parsed_section = self.parse(template, end_index, tag_key) else: index = end_index # Variable index is now the next character to process. - self._handle_tag_type(template, parse_tree, tag_type, tag_key, leading_whitespace, - end_index, content_end_index, parsed_section) + node = self._make_node(template, tag_type, tag_key, leading_whitespace, + end_index, content_end_index, parsed_section) + parse_tree.append(node) # Save the rest of the template. parse_tree.append(template[index:]) return ParsedTemplate(parse_tree) - def _handle_tag_type(self, template, parse_tree, tag_type, tag_key, leading_whitespace, - section_start_index, section_end_index, parsed_section): + def _make_node(self, template, tag_type, tag_key, leading_whitespace, + section_start_index, section_end_index, parsed_section): + """ + Create and return a node of the parse tree. + """ # TODO: switch to using a dictionary instead of a bunch of ifs and elifs. if tag_type == '!': - return + return u'' if tag_type == '=': delimiters = tag_key.split() self._change_delimiters(delimiters) - return + return u'' if tag_type == '': + return self.engine._make_get_escaped(tag_key) - func = self.engine._make_get_escaped(tag_key) - - elif tag_type == '&': - - func = self.engine._make_get_literal(tag_key) - - elif tag_type == '#': + if tag_type == '&': + return self.engine._make_get_literal(tag_key) - func = self.engine._make_get_section(tag_key, parsed_section, self._delimiters, + if tag_type == '#': + return self.engine._make_get_section(tag_key, parsed_section, self._delimiters, template, section_start_index, section_end_index) - elif tag_type == '^': - - func = self.engine._make_get_inverse(tag_key, parsed_section) - - elif tag_type == '>': - - func = self.engine._make_get_partial(tag_key, leading_whitespace) - - else: + if tag_type == '^': + return self.engine._make_get_inverse(tag_key, parsed_section) - raise Exception("Unrecognized tag type: %s" % repr(tag_type)) + if tag_type == '>': + return self.engine._make_get_partial(tag_key, leading_whitespace) - parse_tree.append(func) + raise Exception("Unrecognized tag type: %s" % repr(tag_type)) -- cgit v1.2.1