From 17d547b497acf63e574fa3468bf23593e23049aa Mon Sep 17 00:00:00 2001
From: Thomas Ieong
Date: Sun, 28 Aug 2022 20:54:25 +0200
Subject: [PATCH] Added meaningful errors
---
notjinja2.py | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/notjinja2.py b/notjinja2.py
index bdb3b61..bab72c5 100644
--- a/notjinja2.py
+++ b/notjinja2.py
@@ -38,12 +38,14 @@ class Template:
elif is_loop:
self.parse_loop(split_token)
else:
- raise ValueError("Unknown control structure!")
+ raise ValueError("Unknown control structure!", " ", token)
if self.buffer and (is_conditional or is_loop):
self.add_line(" ".join(self.buffer) + ":")
self.indent()
self.buffer.clear()
elif token.startswith("{{"):
+ if len(split_token) <= 1:
+ raise ValueError("Can't parse this token", " ", token)
item = str(self.do_dots(str(split_token[1]).replace("\n", "")))
self.add_line("output += {}".format(item))
elif token.startswith("{#"):
@@ -55,7 +57,10 @@ class Template:
self.dedent()
assert not self.buffer
- assert self.INDENT == 0
+ if self.INDENT != 0:
+ raise ValueError(
+ "Control structures not balanced! Check the endif,endfor tags!"
+ )
def do_dots(self, token: str) -> str:
data = ""
@@ -82,7 +87,6 @@ class Template:
)
else:
data = "data['{func}']({d})".format(func=function, d=data)
- data = "str({})".format(data)
elif "." in token:
split_token = token.split(".")
if split_token[0] in self.globs:
@@ -94,6 +98,7 @@ class Template:
data = "data['{}']".format(token)
else:
data = token
+ data = "str({})".format(data)
else:
data = token
--
2.45.2