-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
214 lines (182 loc) · 8.19 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from templatingNodes import *
import re
def render_template(string, context):
"""
>>> render_template("My name is {{ name }}", {"name": "James"})
'My name is James'
>>> render_template("{{ name }} is {{ age }} years old", {"name": "James", "age" : "25"})
'James is 25 years old'
>>> render_template("User is {{ age }} years old", {"name": "James", "age" : 25})
'User is 25 years old'
>>> render_template("{% if x %} x is True! {% end if %}", {'x': True})
' x is True! '
>>> render_template("{% if chicken %} x is True! {% end if %}", {'chicken': False})
''
>>> render_template("{% if x == y %} x equals y! {% end if %}", {'x': 10, 'y': 10})
' x equals y! '
>>> render_template("{% if x == y %} x equals y! {% end if %}", {'x': 11, 'y': 10})
''
>>> render_template("{% if x == y %} name: {{ chicken }} {% end if %}break ", {'x': 10, 'y': 10, 'chicken': 'hello'} )
' name: hello break '
>>> render_template("{% for i in chicken %} {{ i }} {% end for %}", {'chicken': [1,2,3,10]})
' 1 2 3 10 '
>>> render_template("{% if x == y %} name: {{ chicken }} {% end if %}", {'x': 10, 'y': 10, 'chicken': 'hello'} )
' name: hello '
>>> render_template("{% for i in range(5) %}m{% end for %}h",{})
'mmmmmh'
>>> render_template("{% for i in range(5) %}m{% end for %}h {{ food }}",{"food":"chicken"})
'mmmmmh chicken'
>>> render_template("{% if do %}{% for i in range(num) %}my num is {{ i }}!{% end for %}{% end if %}",{"do":True,"num":4})
'my num is 0!my num is 1!my num is 2!my num is 3!'
>>> render_template("{% for i in range(num) %}{% if i%2==0 %}{{ i }}{% end if %}{% end for %}",{"num":10})
'02468'
>>> render_template("{{ i}}",{"i":42})
'42'
>>> render_template("My name is{% comment %} please ignore {% end comment %} {{ name }}", {"name": "James"})
'My name is James'
>>> render_template("{% comment %} VERY IMPORTANT MESSAGE {% end comment %}", {})
''
>>> render_template("{% if True %} a {% comment %} very {% end comment %}important word {% end if %}", {})
' a important word '
>>> render_template('''{% include templateTesting/header.html %} and we can have text from no file and {% include templateTesting/footer.html %}''',{})
'we can get stuff from the header and we can have text from no file and we can get stuff from the footer!'
>>> render_template('''{% if show_header %}the header: {% include templateTesting/header.html %}. I think that is {{ description }}{%end if%}''',{"show_header":True,"description":"cool"})
'the header: we can get stuff from the header. I think that is cool'
>>> render_template("{% for i in range(5)%}m{% end for %}h",{})
'mmmmmh'
>>> render_template("{%for i in range(5) %}m{% end for %}h {{food }}",{"food":"chicken"})
'mmmmmh chicken'
>>> render_template("{%if do %}{% for i in range(num) %}my num is {{ i }}!{% end for %}{% end if %}",{"do":True,"num":4})
'my num is 0!my num is 1!my num is 2!my num is 3!'
>>> render_template("{% for i in range(num) %}{% if i%2==0 %}{{i }}{% end if%}{% end for %}",{"num":10})
'02468'
>>> render_template("{{x}}", {'x':'<script> alert("hacked"); </script>'})
'<script> alert("hacked"); </script>'
"""
node = Parser(string)._parse_group()
return node.render(context)
class TemplateException(Exception):
def __init__(self, name, msg):
super().__init__()
self.name = name
self.msg = msg
class Parser():
def __init__(self, characters: str):
self._characters = characters
self._length = len(characters)
self._upto = 0
def end(self):
return self._upto == self._length
def peek(self):
return None if self.end() else self._characters[self._upto]
def peekn(self, number):
return None if self.end() else self._characters[self._upto:self._upto + number]
def next(self):
if not self.end():
self._upto += 1
def remaining_text(self):
return self._characters[self._upto:]
def nextn(self, number):
if self._upto + number <= self._length:
self._upto += number
def _parse_group(self):
nodes = []
while not self.end():
if self.peek() != '{':
# we know this is a text node
nodes.append(self._parse_text())
elif re.match(r'^{%\s*include',self.remaining_text()):
nodes.append(self._parse_include())
elif self.peekn(2) == '{{':
nodes.append(self._parse_python())
elif re.match(r'^{%\s*if',self.remaining_text()):
nodes.append(self._parse_if())
elif re.match(r'^{%\s*fo',self.remaining_text()):
nodes.append(self._parse_for())
elif re.match(r'^{%\s*comment',self.remaining_text()):
self._parse_comment()
else:
break
return GroupNode(nodes)
def _parse_text(self):
node = ''
while self.peek() != '{' and not self.end():
node += self._characters[self._upto]
self.next()
return TextNode(node)
def _parse_python(self):
matched = re.match(r'^{{\s*(\w*)\s*}}', self.remaining_text())
variable = matched.group(1)
self.nextn(matched.end())
return PythonNode(variable)
def _parse_if(self):
#This function assumes we are on the "{" of a block that starts with "{%<Whitespace>if"
while self.peek()!='f':
self.next()
self.next()
condition = ""
while self.peekn(2)!= '%}':
condition += self.peek()
self.next()
self.next()
self.next()
body = self._parse_group()
endTag = re.match(r"^{%\s*end\s+if\s*%}", self.remaining_text())
if endTag is None:
raise TemplateException('Syntax Error', 'Expecting an end if tag')
self.nextn(endTag.end())
return IfNode(condition,body)
def _parse_for(self):
#This function assums we are on the "{" of a block that starts with "{%<Whitespace>for"
while self.peek()!='r':
self.next()
self.next()
variable = ''
while self.peekn(4)!=" in ":
variable+=self.peek()
self.next()
#We should currently be on the first " " of " in collection %}"
self.nextn(3)
#We should now be on the second " " of the above string
coln = ""
while self.peekn(2)!='%}':
coln+=self.peek()
self.next()
#We chould now be on the " " of " %}"
self.nextn(2)
body = self._parse_group()
endTag = re.match(r"^{%\s*end\s+for\s*%}", self.remaining_text())
self.nextn(endTag.end())
return ForNode(variable.strip(),coln,body)
def _parse_include(self):
r'''
>>> parser = Parser("{% include folder/file.html %} {% include folder2/file2.html %}")
>>> node = parser._parse_include()
>>> print(node.path)
folder/file.html
'''
#This functions assumes we are on the "{" of a block like this {% include fi.le %}
match = re.match(r'^{%\s*include\s+([\w\/]+\.[\w]+)\s*%}', self.remaining_text())
path = match.group(1)
self.nextn(match.end())
return IncludeNode(path)
def _parse_comment(self):
#This function assumes that we are on the first character of a block like this
#{% comment %} WOW, THIS LANGUAGE HAS COMMENTS! {% end comment %}
while self.peekn(2) != '%}':
self.next()
#Now we are past the first {% comment %}
while self.peekn(2) != '{%':
self.next()
endTag = re.match(r"^{%\s*end\s+comment\s*%}", self.remaining_text())
self.nextn(endTag.end())
if __name__ == '__main__':
import doctest
doctest.testmod()
print("All tests done, you are awesome :)")
def asserEx(invalid, context):
try:
render_template(invalid, context)
assert False, "should throw an exception"
except TemplateException:
pass