From 96728364c9f75010d616d51dfe761ff77e782ea3 Mon Sep 17 00:00:00 2001 From: noah morrison Date: Fri, 31 Oct 2014 15:00:03 -0400 Subject: [PATCH] Added section detection Unclosed sections now raise an error --- chevron.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 -- 2.47.3