diff --git a/pystache/parser.py b/pystache/parser.py index 30fd1756..a2d3fc42 100644 --- a/pystache/parser.py +++ b/pystache/parser.py @@ -17,15 +17,14 @@ # TODO: add some unit tests for this. # TODO: add a test case that checks for spurious spaces. # TODO: add test cases for delimiters. -def parse(template, delimiters=None): +def parse(template, delimiters=None, raise_on_mismatch=False): """ Parse a unicode template string and return a ParsedTemplate instance. Arguments: - template: a unicode template string. - delimiters: a 2-tuple of delimiters. Defaults to the package default. + raise_on_mismatch: a boolean indicating whether to raise an exception when parsing fails. Examples: @@ -36,7 +35,7 @@ def parse(template, delimiters=None): """ if type(template) is not str: raise Exception('Template is not unicode: %s' % type(template)) - parser = _Parser(delimiters) + parser = _Parser(delimiters, raise_on_mismatch=raise_on_mismatch) return parser.parse(template) @@ -220,12 +219,14 @@ def render(self, engine, context): class _Parser(object): _delimiters = None _template_re = None + _raise_on_mismatch = False - def __init__(self, delimiters=None): + def __init__(self, delimiters=None, raise_on_mismatch=False): if delimiters is None: delimiters = defaults.DELIMITERS self._delimiters = delimiters + self._raise_on_mismatch = raise_on_mismatch def _compile_delimiters(self): self._template_re = _compile_template_re(self._delimiters) @@ -237,17 +238,12 @@ def _change_delimiters(self, delimiters): def parse(self, template): """ Parse a template string starting at some index. - This method uses the current tag delimiter. Arguments: - template: a unicode string that is the template to parse. - index: the index at which to start parsing. - Returns: - a ParsedTemplate instance. """ @@ -337,6 +333,10 @@ def parse(self, template): parsed_template.add(node) + # Some open/close tags were mismatched. + if self._raise_on_mismatch and states: + raise ParsingError('Tag mismatch.') + # Avoid adding spurious empty strings to the parse tree. if start_index != len(template): parsed_template.add(template[start_index:]) diff --git a/pystache/tests/test_parser.py b/pystache/tests/test_parser.py index 1e3c9012..e376d785 100644 --- a/pystache/tests/test_parser.py +++ b/pystache/tests/test_parser.py @@ -8,19 +8,50 @@ import unittest from pystache.defaults import DELIMITERS -from pystache.parser import _compile_template_re as make_re +from pystache.parser import _compile_template_re as make_re, parse, ParsingError class RegularExpressionTestCase(unittest.TestCase): - """Tests the regular expression returned by _compile_template_re().""" def test_re(self): """ Test getting a key from a dictionary. - """ re = make_re(DELIMITERS) match = re.search("b {{test}}") self.assertEqual(match.start(), 1) + + +class ParseTestCase(unittest.TestCase): + """Tests the parse() function.""" + + def test_parse_okay(self): + """ + Test parsing templates in the cases there are no errors. + """ + ts = [ + '