-
Notifications
You must be signed in to change notification settings - Fork 3
/
templating.py
executable file
·349 lines (320 loc) · 11.7 KB
/
templating.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python3
'''
PROPERTY OF THE HOLY SOCIETY OF TEMPLATEIA. UNAUTHORISED EDITING WILL BE PROSECUTED
This code parses the templates into python and html.
The python code can then be evaluated and added to the html to be displayed by a web browser
'''
import html
IF_TAG = ' if '
INCLUDE_TAG = ' include '
FOR_TAG = ' for '
class ParseError(Exception):
'''
Defines an exception for errors encounterd while parsing
'''
pass
class Node(object):
'''
Defines a basic node object
'''
def __init__(self, content):
#stuff inside the node
self.content = content
def eval(self, scope):
raise NotImplementedError()
class PythonNode(Node):
'''
Defines a node for the execution of python code
Syntax: {{ python_expression }}
'''
def eval(self, scope):
#return the execution of the python code
return html.escape(str(eval(self.content, scope)))
class TextNode(Node):
'''
Defines a node for html code/other stuff that is not part of the templating language
'''
def eval(self, scope):
return self.content
class GroupNode(Node):
'''
Defines a node to group nodes in the same area, e.g within a for/if statement
'''
def __init__(self, children):
#List of nodes that the group node contains
self.children = children
def eval(self, scope):
result = ''
#Evaluate all the child nodes and add them to output
for node in self.children:
result += str(node.eval(scope))
return result
class IfNode(Node):
'''
Defines a node for execution if statements
Syntax: {% if <condition> %}stuff{% end if %}
'''
def __init__(self, predicate, true_node):
#Part to be evaluated to true or false
self.predicate = predicate
#Group node to be executed if the predicate is true
self.true_node = true_node
def eval(self, scope):
if eval(self.predicate, scope):
return self.true_node.eval(scope)
else:
return ''
class IncludeNode(Node):
'''
Defines a node for including other html files
Syntax: {% include <file> %}
'''
def __init__(self, path):
#Path to the file to be included
self.path = path.strip()
def eval(self, scope):
#Open the file
with open(self.path) as p:
#Read the file
lines = [line.strip() for line in p]
template = ''.join(lines)
#return the evaluation of the file
return Parser(template).eval(scope)
class ForNode(Node):
'''
Defines a node for executing for loops
Syntax: {% for <var_name> in <itterable_name> %}<stuff>{% end for %}
'''
def __init__(self, var_name, itterable_name, true_node):
#Name of the variable to store content from the itterable
self.var_name = var_name
#Varible to be itterated over
self.itterable_name = itterable_name
#Group node to be executed in every itteration
self.true_node = true_node
def eval(self, scope):
output = ''
modified_scope = scope
#Get the itterable
itterable = eval(self.itterable_name, scope)
#Itterate over the itterable
for var in itterable:
#Set var_name to be the contents of the current position in itterable
modified_scope[self.var_name] = var
#Evaluate the group node and concatenate it to the output
output += str(self.true_node.eval(modified_scope)).strip()
return output
class Parser(object):
'''
Class used to parse a template file into html
>>> Parser("abcd{{1+1}}efgh{% if 1==1 %}2{% end if %}{% if 1==3 %}3{% end if %}1234").eval({})
'abcd2efgh21234'
>>> Parser("{% if 1==1 %}2{% end if %}{{5*2}}{% if 1==3 %}3{% end if %}1234").eval({})
'2101234'
>>> Parser("{{a+b}}{% if a==d %}{{str(b)*5}}{% end if %}").eval({'a':1, 'b':2, 'c':3, 'd':1})
'322222'
>>> Parser("{{a+b}}{% if a==d %}{{str(b)*5}}{% end if %}{% include tests/template_include_test.test %}{{a+b}}{% if a==d %}{{str(b)*5}}{% end if %}").eval({'a':1, 'b':2, 'c':3, 'd':1})
'322222abcd2efgh212342101234333333333322222'
>>> Parser("{% for i in items %}{{i}}{% end for %}").eval({'items':[1,2,3]})
'123'
>>> Parser("abc{% for i in items %}{{'something'+str(i)}}{% end for %}def").eval({'items':[1,2,3]})
'abcsomething1something2something3def'
>>> Parser("abc{% for i in items %}something{{i}}{% end for %}def").eval({'items':[1,2,3]})
'abcsomething1something2something3def'
'''
def __init__(self, tokens):
#String to be parsed
self.tokens = tokens
#Current position in tokens
self.index = 0
#Length of tokens
self.length = len(tokens)
def _parse_python_node(self):
'''
Function to parse a python node
'''
content = ''
#Add the current position to the content
content += self.tokens[self.index]
#all everything before the end of the node into content
while self.read_next() != '}':
self.next()
content += self.tokens[self.index]
if self.is_end():
raise ParseError("Unexpected end of input.")
self.next()
if self.read_next() != '}':
raise ParseError("'}' expected at end of python node")
self.next()
self.next()
return PythonNode(content)
def _parse_text_node(self):
'''
Function to parse a text node
'''
content = ''
#Add the current token to content
content += self.tokens[self.index]
#Keep adding to content until a '{' is reached
while not self.is_end() and self.read_next() != '{':
self.next()
content += self.tokens[self.index]
self.next()
return TextNode(content)
def _parse_if_node(self):
'''
Function to parse an if node
'''
predicate = ''
predicate += self.tokens[self.index]
#find the predicate for the if statement
while self.read_next() != '%':
self.next()
predicate += self.tokens[self.index]
if self.is_end():
raise ParseError("Unexpected end of input.")
self.next()
#check for the end of the if decleration
if self.read_next() != '}':
raise ParseError("'}' expected")
self.next()
self.next()
#create a GroupNode for all the stuff in the for loop
true_node = self._parse_group_node('{% end if %}')
return IfNode(predicate, true_node)
def _parse_for_node(self):
'''
Function to parse a for node
'''
var_name = ''
itterable_name = ''
var_name += self.tokens[self.index]
#find the name of the variable to store the value in the itterable
while self.read_next() != ' ':
self.next()
var_name += self.tokens[self.index]
if self.is_end():
raise ParseError("Unexpected end of input.")
#check if the variable name exists
if var_name == '':
raise ParseError('Missing variable name.')
self.next()
#check for " in "
if self.tokens[self.index:self.index+4] != ' in ':
raise ParseError("'in' expected.")
self.index += 3
#find the name of the itterable that is being looped through
while self.read_next() != ' ':
self.next()
itterable_name += self.tokens[self.index]
if self.is_end():
raise ParseError("Unexpected end of input.")
#check if the itterable name exists
if itterable_name == '':
raise ParseError("Missing name for itterable.")
#check for the end of the for decleration
self.next()
if self.read_next() != '%':
raise ParseError("'%' expected")
self.next()
if self.read_next() != '}':
raise ParseError("'}' expected")
self.next()
self.next()
#create a GroupNode for all the stuff in the foor loop
true_node = self._parse_group_node('{% end for %}')
return ForNode(var_name, itterable_name, true_node)
def _parse_include_node(self):
'''
Function to parse an include node
'''
path = ''
#Add current token to that path of file to be included
path += self.tokens[self.index]
while self.read_next() != '%':
#Add to the path until an '%' is reached
self.next()
path += self.tokens[self.index]
if self.is_end():
raise ParseError("Unexpected end of input.")
self.next()
if self.read_next() != '}':
raise ParseError("'}' expected")
self.next()
self.next()
return IncludeNode(path)
def _parse_group_node(self, end):
'''
Function to group nodes in similar areas together into group nodes
'''
children = []
#While not at the end of the node, or the end of the file
while self.tokens[self.index:self.index+len(end)] != end or (end == '' and not self.is_end()):
if self.is_end():
return ParseError("Unexpected end of input.")
#check for a '{'
if self.tokens[self.index] == '{':
self.next()
#if there is another '{' it must be a python statement
if self.tokens[self.index] == '{':
self.next()
children.append(self._parse_python_node())
#If there is a '%' it could be include, if, or for
elif self.tokens[self.index] == '%':
self.next()
#Check if it is an if
if self.tokens[self.index:self.index+len(IF_TAG)] == IF_TAG:
self.index += len(IF_TAG)
children.append(self._parse_if_node())
#check if it is an include
elif self.tokens[self.index:self.index+len(INCLUDE_TAG)] == INCLUDE_TAG:
self.index += len(INCLUDE_TAG)
children.append(self._parse_include_node())
#check if it is a for
elif self.tokens[self.index:self.index+len(FOR_TAG)] == FOR_TAG:
self.index += len(FOR_TAG)
children.append(self._parse_for_node())
else:
#Otherwise it is an error
raise ParseError("Unexpected token after '{'")
else:
#If there was not a '{' then it is a text node
children.append(self._parse_text_node())
self.index += len(end)
return GroupNode(children)
def parse(self):
'''
Function to start parsing
'''
node = self._parse_group_node('')
if not self.is_end():
raise ParseError("Extra input")
return node
def eval(self, scope):
return self.parse().eval(scope)
def read_next(self):
'''
Return the next character in tokens
'''
return self.tokens[self.index + 1]
def is_end(self):
'''
Check if we are at the ned of tokens
'''
return self.index >= self.length - 1
def next(self):
'''
Incriment the current index
'''
self.index += 1
def render_template(path, scope):
'''
Function which uses the Parser class to parse a html file
'''
with open(path) as p:
lines = [line.strip() for line in p]
template = ''.join(lines)
return Parser(template).eval(scope)
if __name__ == "__main__":
import doctest
doctest.testmod()