From: noah morrison Date: Fri, 31 Oct 2014 19:00:03 +0000 (-0400) Subject: Added section detection X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=96728364c9f75010d616d51dfe761ff77e782ea3;p=chevron.git Added section detection Unclosed sections now raise an error --- diff --git a/chevron.py b/chevron.py index 26973b9..053c510 100755 --- a/chevron.py +++ b/chevron.py @@ -7,6 +7,9 @@ def tokenize(template): class EOF(Exception): pass + class UnclosedSection(Exception): + pass + def get(amount=1): return template[current:current + amount] @@ -29,6 +32,7 @@ def tokenize(template): } current = 0 + open_sections = [] l_del = '{{' r_del = '}}' while current < len(template): @@ -66,6 +70,14 @@ def tokenize(template): l_del, r_del = tag_key[:-1].split(' ') continue + elif tag_type in ['section', 'inverted section']: + open_sections.append(tag_key) + + elif tag_type == 'end': + last_section = open_sections.pop() + if tag_key != last_section: + raise UnclosedSection() + yield (tag_type, tag_key) return