-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.py
436 lines (343 loc) · 14.2 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import re
import os
import hashlib
from html import escape
# Prettification disabled:
#from bs4 import BeautifulSoup
__version__ = '0.05'
# Abstract Node Class:
class Node(object):
def render(self, variables):
raise NotImplementedError()
# Node for plain text:
class TextNode(Node):
def __init__(self, text):
self.text = text
# Simply render the text:
def render(self, variables):
return self.text
def __repr__(self):
return 'TextNode: {}'.format(self.text)
# Node for holding groups of nodes:
class GroupNode(Node):
def __init__(self, sub_nodes):
self.sub_nodes = sub_nodes
# Adds a node to this node's children:
def add(self, node):
self.sub_nodes.append(node)
# Render each child node one after another:
def render(self, variables):
return ''.join(node.render(variables) for node in self.sub_nodes)
def __repr__(self):
return 'GroupNode (\n' + '\n'.join(repr(child) for child in self.sub_nodes) + '\n)'
# Node for holding python code to evaluate:
class PythonNode(Node):
def __init__(self, code, safe=True):
self.code = code
self.escape = (lambda s: escape(str(s), quote=True)) if safe else (lambda s: str(s))
# Evaluate and stringify the expression:
def render(self, variables):
try:
return self.escape(eval(self.code, {}, variables))
except Exception as e:
raise e.__class__('Error evaluating "{}" in eval node: {}'.format(self.code, str(e)))
def __repr__(self):
return 'PythonNode: {}'.format(self.code)
# Node for holding python code to execute:
class ExecNode(Node):
def __init__(self, code):
self.code = code
# Execute the expression and return nothing:
def render(self, variables):
exec(self.code, {}, variables)
return ''
def __repr__(self):
return 'ExecNode: {}'.format(self.code)
# Node for handling if and else blocks:
class IfNode(Node):
def __init__(self, expr, istrue, isfalse):
self.expr = expr
self.istrue = istrue
self.isfalse = isfalse
# Evaluate the precondition, and render the appropriate child node:
def render(self, variables):
try:
if eval(self.expr, {}, variables):
return self.istrue.render(variables)
else:
return self.isfalse.render(variables)
except Exception as e:
raise e.__class__('Error evaluating "{}" in if statement: {}'.format(self.expr, str(e)))
def __repr__(self):
return 'IfNode (\n{}\n) else (\n{}\n}'.format(repr(self.istrue), repr(self.isfalse))
# Node for generating gravatar image links
class GravatarNode(Node):
def __init__(self, email):
self.email = email
def render(self, variables):
try:
hashed = hashlib.md5(str(eval(self.email, {}, variables)).lower().strip().encode('ascii')).hexdigest()
return 'http://www.gravatar.com/avatar/' + hashed
except Exception as e:
raise e.__class__('Error evaluating "{}" in gravatar statement: {}'.format(self.email, str(e)))
def __repr__(self):
return 'GravatarNode: {}'.format(self.email)
# Node for handling for loops:
class ForNode(Node):
def __init__(self, variables, expr, enclosed):
self.variables = variables
self.expr = expr
self.enclosed = enclosed
# Loop over the iterable, rendering the child nodes:
def render(self, variables):
try:
variables['___iterator'] = iter(eval(self.expr, {}, variables))
except Exception as e:
raise e.__class__('Error evaluating "{}" in for node: {}'.format(self.expr, str(e)))
output = ''
while True:
try:
exec(self.variables + ' = next(___iterator)', {}, variables)
except StopIteration:
break
except Exception as e:
raise e.__class__('Error executing "{}" in for loop: {}'.format(self.variables + ' = next(___iterator)', str(e)))
else:
output += self.enclosed.render(variables)
del variables['___iterator']
return output
def __repr__(self):
return 'ForNode (\n{}\n)'.format(repr(self.enclosed))
# Node for remapping variables when including files:
class IncludeNode(Node):
def __init__(self, child, remappings):
self.child = child
self.remap = [r.strip().split('=') for r in remappings if r.strip()]
# Remap the variables, and render the included file:
def render(self, variables):
try:
new_vars = dict((variable.strip(), eval(expression, {}, variables)) for variable, expression in self.remap)
except Exception as e:
raise e.__class__('Error evaluating remappings in an include. Remap: "{}". Exception: "{}".'.format(repr(self.remap), str(e)))
return self.child.render(new_vars)
def __repr__(self):
return 'IncludeNode ({}) (\n{}\n)'.format(self.remappings, repr(self.child))
# Node for handling ifdefs and ifndefs:
class IfDefNode(Node):
def __init__(self, variable, istrue, isfalse, reverse):
self.variable = variable
self.istrue = istrue
self.isfalse = isfalse
self.reverse = reverse
# Check if variable is/isn't defined and if so execute inner expression:
def render(self, variables):
if (self.variable.strip() in variables) != self.reverse:
return self.istrue.render(variables)
else:
return self.isfalse.render(variables)
# Abstract class for parsing exceptions:
class TemplateException(Exception):
pass
# Exception raised when if/else/endif or for/endfor statements are not in correct blocks:
class NoMatchingEndToken(TemplateException):
pass
# 'Lex' the text into blocks for later parsing:
def lex(text):
# List of tokens:
tokens = []
### Token Descriptions: ###
#
# Below, each token is specified as an identifier
# followed by a regex string that matches it.
#
# Tokens are tried in the order they are listed,
# and any capturing groups (bracketed parts of the
# regex) are returned by the function.
#
############################
# Hackily get a list of labels and regex objects that match them:
token_reg = [(label, re.compile(regex, re.S)) for label, regex in (l.split() for l in r'''
eval {{(.*?)}}
exec {%\s*exec\s(.*?)%}
safe {%\s*safe\s(.*?)%}
if {%\s*if\s(.*?)%}
else {%\s*else\s*%}
endif {%\s*endif\s*%}
iif_else {%\s*iif\s(.*?)\sthen\s(.*?)\selse\s(.*?)%}
iif {%\s*iif\s(.*?)\sthen\s(.*?)\s%}
ifdef {%\s*ifdef\s(.*?)\sthen\s(.*?)\selse\s(.*?)%}
ifndef {%\s*ifndef\s(.*?)\sthen\s(.*?)\selse\s(.*?)%}
ifdef {%\s*ifdef\s(.*?)\sthen\s(.*?)%}
ifndef {%\s*ifndef\s(.*?)\sthen\s(.*?)%}
ifdef2 {%\s*ifdef\s(.*?)%}
ifndef2 {%\s*ifndef\s(.*?)%}
for {%\s*for\s(.*?)\s*in\s*(.*?)\s*%}
endfor {%\s*endfor\s*%}
include_remap {%\s*include\s\"(.*?)\"\swith\s(.*?=.*?(?:;.*?=.*?)*?)%}
include {%\s*include\s\"(.*?)\"\s*%}
comment {#.*?#}
gravatar {%\s*gravatar\s(.*?)%}
'''.splitlines() if l.strip())]
matches = [(0,0)]
for match in re.finditer(r'({{.*?}}|{%.*?%}|{#.*?#})', text, re.S | re.M):
start = match.start()
end = match.end()
matches.append((matches[-1][1], start))
matches.append((start, end))
matches.append((matches[-1][1],len(text)))
# For each relevant block:
for block in (text[start:end] for start,end in matches if start < end):
# Try matching each of the tokens:
label = None
for token, regex in token_reg:
match = regex.match(block)
if match:
# If there is a match, extract the matched text into expr or into a tuple if multiple capturing groups:
label = token
expr = match.groups()
if len(expr) == 1:
expr = expr[0]
elif len(expr) == 0:
expr = None
break
# If nothing was matched, mark the block as plain-text:
if label is None:
label = 'text'
expr = block
tokens.append((label, expr))
#print(tokens)
return tokens
# Reads a file and returns a parse tree for the file: (TODO: Caching parse trees.)
def parse_file(filename):
if not os.path.exists(filename):
old_name = filename
filename = os.path.join('templates', filename)
if not os.path.exists(filename):
raise IOError('The file "{}" could not be found in the root directory or in the templates folder.'.format(old_name))
with open(filename) as f:
text = f.read()
return parse(text)
# Lexes the template string and runs the parser, returning a parse tree:
def parse(template):
return parse_template(iter(lex(template)), template=template)
# Recursive template parser, returning a parse tree:
def parse_template(iterator, last=None, template=None):
if template is not None:
parse_template.cache = parse_template.__dict__.get('cache', dict())
if template in parse_template.cache:
return parse_template.cache[template]
# The grouping node to return as a result:
result = GroupNode([])
if template:
parse_template.cache[template] = result
while True:
# Consume tokens until there are none left:
try:
tok_type, parameters = next(iterator)
except StopIteration:
break
# If text, simply add a TextNode:
if tok_type == 'text':
result.add(TextNode(parameters))
# If an expression, add a PythonNode:
elif tok_type == 'eval':
result.add(PythonNode(parameters))
# If a safe expression, add a PythonNode that won't html escape the result:
elif tok_type == 'safe':
result.add(PythonNode(parameters, safe=False))
# If an executed expression, add an ExecNode:
elif tok_type == 'exec':
result.add(ExecNode(parameters))
# If an else, check for a matching if and recurse:
elif tok_type == 'else':
if last == 'if':
return (result, parse_template(iterator, 'else')[0])
else:
raise NoMatchingEndToken('An "{% else %}" block was supplied without an "{% if %}" block.')
# If an endif, check for a matching if/else and return:
elif tok_type == 'endif':
if last in ('if', 'else'):
return (result, TextNode(''))
else:
raise NoMatchingEndToken('An "{% endif %}" block was supplied without an "{% if %}" block.')
# If an endfor, check for a matching for and return:
elif tok_type == 'endfor':
if last == 'for':
return result
else:
raise NoMatchingEndToken('An "{% endfor %}" block was supplied without a "{% for %}" block.')
# If an if, recurse for the containing blocks, and add an IfNode:
elif tok_type == 'if':
istrue, isfalse = parse_template(iterator, 'if')
result.add(IfNode(parameters, istrue, isfalse))
# If a for, recurse for the contained blocks and add a ForNode:
elif tok_type == 'for':
variables, iterable = parameters
result.add(ForNode(variables, iterable, parse_template(iterator, 'for')))
# Inline if - create an IfNode with python nodes as children:
elif tok_type == 'iif':
condition, istrue = parameters
result.add(IfNode(condition, PythonNode(istrue), TextNode('')))
# Inline if with else:
elif tok_type == 'iif_else':
condition, istrue, isfalse = parameters
result.add(IfNode(condition, PythonNode(istrue), PythonNode(isfalse)))
# If an ifdef or ifndef, add a corresponding node:
elif tok_type in ('ifdef', 'ifndef'):
if len(parameters) == 2:
variable, istrue = parameters
isfalse = TextNode('')
else:
variable, istrue, isfalse = parameters
result.add(IfDefNode(variable, ExecNode(istrue), ExecNode(isfalse), tok_type == 'ifndef'))
# If an expanded ifdef or ifndef, add a corresponding node:
elif tok_type in ('ifdef2', 'ifndef2'):
istrue, isfalse = parse_template(iterator, 'if')
result.add(IfDefNode(parameters, istrue, isfalse, tok_type == 'ifndef2'))
# If an include, recursively parse the file:
elif tok_type == 'include':
result.add(parse_file(parameters))
# If a remapping include, construct a node for remapping the variables:
elif tok_type == 'include_remap':
filename, variables = parameters
result.add(IncludeNode(parse_file(filename), variables.split(';')))
# If a comment, ignore:
elif tok_type == 'comment':
pass # Ignore
### Special tokens! ###
# Gravatar URLs:
elif tok_type == 'gravatar':
result.add(GravatarNode(parameters))
# If a block was not terminated, raise an exception:
if last is not None:
raise NoMatchingEndToken('The end the input was reached before an {} block was closed.'.format('{% '+last+' %}'))
# Return the GroupNode:
return result
def render(template, variables={}):
"""
Renders a template string as text
Returns rendered template
>>> render("{{string}}", variables={'string': 'this is a string'})
'this is a string'
"""
return prettify( parse(template).render(dict(variables.items())) )
def render_file(filename, variables={}):
"""
Call with the relative path of the template as filename, and the list of variables as variables
"""
return prettify( parse_file(filename).render(dict(variables.items())) )
def prettify(rendered):
return rendered
# Prettification removed:
#rendered = re.sub(r'\s+', ' ', rendered)
#rendered = BeautifulSoup(rendered).prettify(formatter=None)
#return '\n'.join(re.sub(r'^(\s+)', r'\1'*2, line) for line in rendered.splitlines())
if __name__ == '__main__':
context = {
'user': 'Bob',
'friends': ['James', 'Dom', 'Who', 'The Doctor'],
'age': 17
}
result = r"""Bob'sPage:Ihave4:<ul><li>James(pooreffortofaname)</li><li>Dom(pooreffortofaname)</li><li>Who(pooreffortofaname)</li><li>TheDoctor(that'sareallylongname!)</li></ul><imgsrc="http://www.gravatar.com/avatar/5730cd5627b5cbed1c4b7b5f89fa9bd2"/>Thisis<b>escaped</b>htmlbydefault.<marquee>Thisisunescapedhtml!</marquee>I'mavariable!RIGHTRIGHTRIGHT"""
template = r"""{{ user }}'s Page: I have {{ len(friends) }}:<ul> {% for friend in friends %} <li> {{friend}} {% if len(friend) > (1000//160) %} (that's a really long name!) {% else %} (poor effort of a name) {% endif %} </li>{% endfor %}</ul><img src="{% gravatar '[email protected]' %}"/>{{ "This is <b> escaped </b> html by default." }}{% safe "<marquee> This is unescaped html! </marquee>" %}{% exec myvar = 'I exist!' %}{% ifdef myvar then myvar = "I'm a variable!" %}{{myvar}}{% ifndef myvar then myvar = "I don't exist!" else myvar = "I exist!" %}{% ifdef im_not_defined %} WRONG{% else %} RIGHT{% endif %}{% ifdef myvar %} RIGHT{% endif %}{% ifndef myvar %} WRONG{% else %} RIGHT{% endif %} {# this is a comment! #} {# I could {% include "footer.html" %} if I wanted to! #}"""
assert render(template, context).replace('\n','').replace(' ','').replace('\t','') == result.replace('\n','').replace(' ','').replace('\t','')