summaryrefslogtreecommitdiff
path: root/pystache
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-05-05 05:29:51 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-05-05 05:29:51 -0700
commitce1b81b1a5951c0ea34c4f8251ada3573af9c691 (patch)
treed4c51c86572d68cfb4ac28b2656afd4db6b7ff70 /pystache
parente167059a23e4a3ed1b6942e1df916950828a57e0 (diff)
downloadpystache-ce1b81b1a5951c0ea34c4f8251ada3573af9c691.tar.gz
More parser refactoring: further simplified _handle_tag_type().
Diffstat (limited to 'pystache')
-rw-r--r--pystache/parser.py49
1 files changed, 22 insertions, 27 deletions
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))