-
Notifications
You must be signed in to change notification settings - Fork 8
/
__init__.py
389 lines (346 loc) · 15.8 KB
/
__init__.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
import itertools
from cgi import escape
try:
from sys import intern
except ImportError:
pass
TOKEN_RAW = intern('raw')
TOKEN_TAGOPEN = intern('tagopen')
TOKEN_TAGINVERT = intern('taginvert')
TOKEN_TAGCLOSE = intern('tagclose')
TOKEN_TAGCOMMENT = intern('tagcomment')
TOKEN_TAGDELIM = intern('tagdelim')
TOKEN_TAG = intern('tag')
TOKEN_PARTIAL = intern('partial')
TOKEN_PUSH = intern('push')
TOKEN_BOOL = intern('bool')
BOOTSRAP_PRE = """
(function(data){
var isArray = Array.isArray || function(obj) {
return toString.call(obj) == '[object Array]';
},
each = function(obj, iterator, context) {
if (obj == null) return;
if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (iterator.call(context, obj[key], key, obj) === breaker) return;
}
}
}
},
map = function(obj, iterator, context) {
var results = [];
if (obj == null) return results;
if (Array.prototype.map && obj.map === Array.prototype.map) return obj.map(iterator, context);
each(obj, function(value, index, list) {
results[results.length] = iterator.call(context, value, index, list);
});
if (obj.length === +obj.length) results.length = obj.length;
return results;
},
htmlEncode = function(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
},
lookup = function (data, datum) {
var i = 0,
l = data ? data.length : 0;
for (; i < l; i += 1) {
if (datum === '.') {
return data[i]
} else if (data[i] !== void 0 && data[i][datum] !== void 0 && data[i][datum] !== false) {
if (toString.call(data[i][datum]) == '[object Function]') {
return data[i][datum](data)
} else {
return data[i][datum]
}
}
}
return '';
},
section = function(data, tagvalue, callback, invert){
invert = invert || false;
if (isArray(tagvalue)) {
if (!invert && tagvalue.length > 0) {
return map(tagvalue, function(v) { return callback([v].concat(data))}).join('')
} else if (invert && tagvalue.length == 0) {
return callback(data);
}
} else {
if((!invert && tagvalue) || (invert && !tagvalue)) {
if (tagvalue !== void 0 || tagvalue !== true) {
return callback([tagvalue].concat(data));
} else {
return callback(data);
}
}
}
};
"""
BOOTSRAP_POST = """
})
"""
def _checkprefix(tag, prefix):
return tag[1:].strip() if tag and tag[0] == prefix else None
def _lookup(data, datum):
for scope in data:
if datum == '.':
return str(scope)
elif datum in scope:
return scope[datum]
elif hasattr(scope, datum):
return getattr(scope, datum)
return None
def _renderjsfunction(parts, prefix = "", postfix = "", params="data, tag"):
return "function({params}) {{{prefix} return {content} {postfix} }}".format(
content=_renderjsjoin(*parts),
prefix=prefix,
postfix=postfix,
params=params)
def _renderjsjoin(*args):
return "[{0}].join('');".format(','.join(args))
def render(template, data):
return Stache().render(template, data)
def render_js(template):
return Stache().render_js(template)
class Stache(object):
def __init__(self):
self.otag = '{{'
self.ctag = '}}'
self.templates = {}
self.hoist = {}
self.hoist_data = {}
self.section_counter = 0
def copy(self):
copy = Stache()
copy.templates = self.templates
return copy
def add_template(self, name, template):
self.templates[name] = list(self._tokenize(template))
def render(self, template, data={}):
self.otag = '{{'
self.ctag = '}}'
return ''.join(self._parse(self._tokenize(template), data))
def render_iter(self, template, data={}):
copy = self.copy()
return copy._parse(copy._tokenize(template), data)
def render_template(self, template_name, data={}):
self.otag = '{{'
self.ctag = '}}'
return ''.join(self._parse(iter(list(self.templates[template_name])), data))
def render_template_iter(self, template_name, data={}):
copy = self.copy()
return copy._parse(iter(list(copy.templates[template_name])), data)
def _js_hoisted(self, bare=True):
hoist = ''
if self.templates:
hoist += "\n var templates = {};\n"
for name in self.templates:
render_function = list(self._jsparse(iter(list(self.templates[name]))))
newparams = "data"
prefix = ""
if not bare and self.hoist_data:
hoisted = map(lambda x: '"{0}": {1}, '.format(x, self.hoist_data[x], "baseData"), self.hoist_data.keys())
prefix = ' var data = [dat2, {{{0}}}];'.format(', '.join(hoisted))
self.hoist_data = {}
newparams = 'dat2';
hoist += ' templates["{0}"] = {1};\n'.format(name, _renderjsfunction(render_function, prefix=prefix, params=newparams))
if self.hoist:
for name in self.hoist:
hoist += ' var {0} = {1};\n'.format(name, self.hoist[name])
if bare:
if self.hoist_data:
for name in self.hoist_data:
hoist += ' {2}["{0}"] = {1};\n'.format(name, self.hoist_data[name], "data")
return hoist
def render_js(self, template):
copy = self.copy()
renderedjs = _renderjsjoin(*list(copy._jsparse(copy._tokenize(template))))
hoist = copy._js_hoisted()
jstemplate = "{0}\n {1}\n data = [data];\n return {2};\n{3}"
return jstemplate.format(BOOTSRAP_PRE, hoist, renderedjs, BOOTSRAP_POST)
def render_js_template(self, template_name):
copy = self.copy()
hoist = copy._js_hoisted(bare=False)
jstemplate = "{0}\n {1}\n return templates['{2}']([data]);\n{3}"
return jstemplate.format(BOOTSRAP_PRE, hoist, template_name, BOOTSRAP_POST)
def render_all_js(self):
copy = self.copy()
hoist = copy._js_hoisted(bare=False)
jstemplate = "{0}\n var baseData={{}};\n {1}\n return templates;\n{2}"
return jstemplate.format(BOOTSRAP_PRE, hoist, BOOTSRAP_POST)
def _tokenize(self, template):
rest = template
scope = []
while rest and len(rest) > 0:
pre_section = rest.split(self.otag, 1)
pre, rest = pre_section if len(pre_section) == 2 else (pre_section[0], None)
taglabel, rest = rest.split(self.ctag, 1) if rest else (None, None)
taglabel = taglabel.strip() if taglabel else ''
open_tag = _checkprefix(taglabel, '#')
invert_tag = _checkprefix(taglabel, '^') if not open_tag else None
close_tag = _checkprefix(taglabel, '/') if not invert_tag else None
comment_tag = _checkprefix(taglabel, '!') if not close_tag else None
partial_tag = _checkprefix(taglabel, '>') if not comment_tag else None
push_tag = _checkprefix(taglabel, '<') if not partial_tag else None
bool_tag = _checkprefix(taglabel, '?') if not push_tag else None
booltern_tag = _checkprefix(taglabel, ':') if not bool_tag else None
unescape_tag = _checkprefix(taglabel, '{') if not booltern_tag else None
rest = rest[1:] if unescape_tag else rest
unescape_tag = (unescape_tag or _checkprefix(taglabel, '&')) if not booltern_tag else None
delim_tag = taglabel[1:-1] if not unescape_tag and len(taglabel) >= 2 and taglabel[0] == '=' and taglabel[-1] == '=' else None
delim_tag = delim_tag.split(' ', 1) if delim_tag else None
delim_tag = delim_tag if delim_tag and len(delim_tag) == 2 else None
if push_tag:
pre = pre.rstrip()
rest = rest.lstrip()
if pre:
yield TOKEN_RAW, pre, len(scope)
if open_tag:
scope.append(open_tag)
yield TOKEN_TAGOPEN, open_tag, len(scope)
elif bool_tag:
scope.append(bool_tag)
yield TOKEN_BOOL, bool_tag, len(scope)
elif invert_tag:
scope.append(invert_tag)
yield TOKEN_TAGINVERT, invert_tag, len(scope)
elif close_tag is not None:
current_scope = scope.pop()
if close_tag:
assert (current_scope == close_tag), 'Mismatch open/close blocks'
yield TOKEN_TAGCLOSE, current_scope, len(scope)+1
elif booltern_tag:
scope.append(booltern_tag)
yield TOKEN_TAG, booltern_tag, 0
yield TOKEN_TAGINVERT, booltern_tag, len(scope)
elif comment_tag:
yield TOKEN_TAGCOMMENT, comment_tag, 0
elif partial_tag:
yield TOKEN_PARTIAL, partial_tag, 0
elif push_tag:
scope.append(push_tag)
yield TOKEN_PUSH, push_tag, len(scope)
elif delim_tag:
yield TOKEN_TAGDELIM, delim_tag, 0
elif unescape_tag:
yield TOKEN_TAG, unescape_tag, True
else:
yield TOKEN_TAG, taglabel, False
def _parse(self, tokens, *data):
for token in tokens:
#print ' token:' + str(token)
tag, content, scope = token
if tag == TOKEN_RAW:
yield str(content)
elif tag == TOKEN_TAG:
tagvalue = _lookup(data, content)
#cant use if tagvalue because we need to render tagvalue if it's 0
#testing if tagvalue == 0, doesnt work since False == 0
if tagvalue is not None and tagvalue is not False:
try:
if len(tagvalue) > 0:
if scope:
yield str(tagvalue)
else:
yield escape(str(tagvalue))
except TypeError:
if scope:
yield str(tagvalue)
else:
yield escape(str(tagvalue))
elif tag == TOKEN_TAGOPEN or tag == TOKEN_TAGINVERT:
tagvalue = _lookup(data, content)
untilclose = itertools.takewhile(lambda x: x != (TOKEN_TAGCLOSE, content, scope), tokens)
if (tag == TOKEN_TAGOPEN and tagvalue) or (tag == TOKEN_TAGINVERT and not tagvalue):
if hasattr(tagvalue, 'items'):
#print ' its a dict!', tagvalue, untilclose
for part in self._parse(untilclose, tagvalue, *data):
yield part
else:
try:
iterlist = list(iter(tagvalue))
if len(iterlist) == 0:
raise TypeError
#print ' its a list!', list(rest)
#from http://docs.python.org/library/itertools.html#itertools.tee
#In general, if one iterator uses most or all of the data before
#another iterator starts, it is faster to use list() instead of tee().
rest = list(untilclose)
for listitem in iterlist:
for part in self._parse(iter(rest), listitem, *data):
yield part
except TypeError:
#print ' its a bool!'
for part in self._parse(untilclose, *data):
yield part
else:
for ignore in untilclose:
pass
elif tag == TOKEN_BOOL:
tagvalue = _lookup(data, content)
untilclose = itertools.takewhile(lambda x: x != (TOKEN_TAGCLOSE, content, scope), tokens)
if tagvalue:
for part in self._parse(untilclose, *data):
yield part
else:
for part in untilclose:
pass
elif tag == TOKEN_PARTIAL:
if content in self.templates:
for part in self._parse(iter(list(self.templates[content])), *data):
yield part
elif tag == TOKEN_PUSH:
untilclose = itertools.takewhile(lambda x: x != (TOKEN_TAGCLOSE, content, scope), tokens)
data[-1][content] = ''.join(self._parse(untilclose, *data))
elif tag == TOKEN_TAGDELIM:
self.otag, self.ctag = content
def _jsparse(self, tokens):
self.otag = '{{'
self.ctag = '}}'
for token in tokens:
tag, content, scope = token
if tag == TOKEN_RAW:
yield "'{0}'".format(str(content))
elif tag == TOKEN_TAG:
if content != '':
if scope:
yield "lookup(data, '{0}')".format(content)
else:
yield "htmlEncode(lookup(data, '{0}'))".format(content)
elif tag == TOKEN_TAGOPEN or tag == TOKEN_TAGINVERT or tag == TOKEN_BOOL:
untilclose = itertools.takewhile(lambda x: x != (TOKEN_TAGCLOSE, content, scope), tokens)
inside = self._jsparse(untilclose)
if tag == TOKEN_TAGOPEN:
pre = "return section(data, lookup(data, tag), function (data) {"
post = "});"
self.hoist["__section{0}".format(len(self.hoist))] = _renderjsfunction(inside, pre, post)
yield "__section{1}(data, '{0}')".format(content, len(self.hoist)-1)
elif tag == TOKEN_TAGINVERT:
pre = "return section(data, lookup(data, tag), function (data) {"
post = "}, true);"
self.hoist["__section{0}".format(len(self.hoist))] = _renderjsfunction(inside, pre, post)
yield "__section{1}(data, '{0}')".format(content, len(self.hoist)-1)
elif tag == TOKEN_BOOL:
pre = "var tagvalue = lookup(data, tag); if ((!isArray(tagvalue) && tagvalue) || (isArray(tagvalue)) && tagvalue.length > 0){"
post = "}"
self.hoist["__section{0}".format(len(self.hoist))] = _renderjsfunction(inside, pre, post)
yield "__section{1}(data, '{0}')".format(content, len(self.hoist)-1)
elif tag == TOKEN_PARTIAL:
yield "templates['{0}'](data)".format(content)
elif tag == TOKEN_PUSH:
untilclose = itertools.takewhile(lambda x: x != (TOKEN_TAGCLOSE, content, scope), tokens)
self.hoist_data[content] = _renderjsfunction(self._jsparse(untilclose), params="data")
elif tag == TOKEN_TAGDELIM:
self.otag, self.ctag = content