]> Devi Nivas Git - chevron.git/commitdiff
Added section detection
authornoah morrison <noah@morrison.ph>
Fri, 31 Oct 2014 19:00:03 +0000 (15:00 -0400)
committernoah morrison <noah@morrison.ph>
Fri, 31 Oct 2014 19:00:03 +0000 (15:00 -0400)
Unclosed sections now raise an error

chevron.py

index 26973b913b0667d43ee6a5a5d0357867bba6829f..053c510b160cad140c87b77f98a5a12dc505e2df 100755 (executable)
@@ -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