forked from Pitmairen/hamlish-jinja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhamlish_jinja.py
501 lines (325 loc) · 13.8 KB
/
hamlish_jinja.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# -*- coding: utf-8 -*-
import re
import os.path
from jinja2 import Environment, TemplateSyntaxError
from jinja2.ext import Extension
__version__ = '0.1.1'
class HamlishExtension(Extension):
def __init__(self, environment):
super(HamlishExtension, self).__init__(environment)
environment.extend(
hamlish_mode='compact',
hamlish_file_extensions=('.haml',),
hamlish_indent_string=' ',
hamlish_newline_string='\n',
hamlish_debug=False,
hamlish_enable_div_shortcut=False,
)
def preprocess(self, source, name, filename=None):
if not os.path.splitext(name)[1] in \
self.environment.hamlish_file_extensions:
return source
h = self.get_preprocessor(self.environment.hamlish_mode)
try:
return h.convert_source(source)
except TemplateIndentationError, e:
raise TemplateSyntaxError(e.message, e.lineno, name=name, filename=filename)
except TemplateSyntaxError, e:
raise TemplateSyntaxError(e.message, e.lineno, name=name, filename=filename)
def get_preprocessor(self, mode):
if mode == 'compact':
output = Output(indent_string='', newline_string='')
elif mode == 'debug':
output = Output(indent_string=' ', newline_string='\n')
else:
output = Output(indent_string=self.environment.hamlish_indent_string,
newline_string=self.environment.hamlish_newline_string)
return Hamlish(output, mode == 'debug',
self.environment.hamlish_enable_div_shortcut)
class TemplateIndentationError(TemplateSyntaxError):
pass
class Hamlish(object):
#Separator used for inline block data
INLINE_DATA_SEP = ' << '
SELF_CLOSING_TAG = '.'
JINJA_TAG = '-'
JINJA_VARIABLE = '='
HTML_TAG = '%'
ESCAPE_LINE = '\\'
PREFORMATED_LINE = '|'
CONTINUED_LINE = '\\'
ID_SHORTCUT = '#'
CLASS_SHORTCUT = '.'
LINE_COMMENT = ';'
# This is tags that can be continued on the same indent level
# as the starting tag.
# For example the "if" tag can have a "elif" or "else" on the same
# indent level as the starting "if".
extended_jinja_tags = set(['for', 'if', 'trans'])
# This is the tags that can continue the extended tags above
continued_jinja_tags = set(['else', 'elif', 'pluralize'])
self_closing_jinja_tags = set([
'include', 'extends', 'import', 'set', 'from', 'do', 'break',
'continue',
])
self_closing_html_tags = set([
'br', 'img', 'link', 'hr', 'meta', 'input'
])
def __init__(self, output, debug=False, use_div_shortcut=False):
self.output = output
self.debug = debug
self._use_div_shortcut = use_div_shortcut
def convert_source(self, source):
blocks = self.get_haml_blocks(source.split('\n'))
return self.create_output(blocks)
def get_haml_blocks(self, source_lines):
"""Splits the haml formatted text into a list of blocks.
A block is a tuple with this format:
(lineno, linecontent, [sub blocks])
"""
indent_levels = [-1]
root = (-1, 'ROOT', [])
block_stack = [root[2]]
continued_line = None
for lineno, line in enumerate(source_lines):
lineno += 1
if not line.strip():
if self.debug:
block_stack[-1].append((lineno, '##empty_line##', ()))
continue
new_block = []
if line[-1] == self.CONTINUED_LINE and line.lstrip()[0] != self.LINE_COMMENT:
if continued_line is None:
continued_line = (lineno, [line[:-1]], [])
else:
continued_line[1].append(line.lstrip()[:-1])
if self.debug:
continued_line[2].append((lineno, '##empty_line##', ()))
continue
elif continued_line is not None:
continued_line[1].append(line.lstrip())
lineno = continued_line[0]
line = ''.join(continued_line[1])
new_block = continued_line[2]
continued_line = None
indent = 0
m = re.match(r'^(\s+)', line)
if m:
indent = m.group(1)
if ' ' in indent and '\t' in indent:
raise TemplateIndentationError('Mixed tabs and spaces', lineno)
indent = len(indent)
if indent > indent_levels[-1]:
indent_levels.append(indent)
else:
while indent < indent_levels[-1]:
indent_levels.pop()
block_stack.pop()
block_stack.pop()
if indent != indent_levels[-1]:
raise TemplateIndentationError('Unindent does not match any outer indentation level', lineno)
block_stack[-1].append((lineno, line.lstrip(), new_block))
block_stack.append(new_block)
return root[2]
def create_output(self, blocks, depth=0):
continued_block = None
for block in blocks:
if self.debug and block[1] == '##empty_line##':
self.output.newline()
continue
#line comment
elif block[1][0] == self.LINE_COMMENT:
continued_block = self.close_continued_block(continued_block, depth)
if self.debug:
self.output.newline()
# We remove one indent level for the block below the comment
# so whe won't add 1 to depth.
self.create_output(block[2], depth)
#jinja tag
elif block[1][0] == self.JINJA_TAG:
continued_block = self.parse_jinja_block(block, depth,
continued_block)
elif block[1][0] == self.JINJA_VARIABLE:
continued_block = self.close_continued_block(continued_block, depth)
if self.debug:
self.output.newline()
self.output.indent(depth)
self.output.write('{{ %s }}' % block[1][1:])
if not self.debug:
self.output.newline()
self.create_output(block[2], depth + 1)
#html block
elif block[1][0] == self.HTML_TAG:
continued_block = self.close_continued_block(continued_block, depth)
self.parse_html_block(block, depth)
#preformated block
elif block[1][0] == self.PREFORMATED_LINE:
continued_block = self.close_continued_block(continued_block, depth)
self.parse_preformated_block(block, depth)
elif block[1][0] == self.ID_SHORTCUT or block[1][0] == self.CLASS_SHORTCUT \
and self._use_div_shortcut:
continued_block = self.close_continued_block(continued_block, depth)
self.parse_shortcuts(block, depth)
#data block
else:
continued_block = self.close_continued_block(continued_block, depth)
if self.debug:
self.output.newline()
self.output.indent(depth)
if block[1][0] == self.ESCAPE_LINE:
self.output.write(block[1][1:])
else:
self.output.write(block[1])
if not self.debug:
self.output.newline()
self.create_output(block[2], depth + 1)
self.close_continued_block(continued_block, depth)
if self.debug:
return ''.join(self.output.output)[1:]
return ''.join(self.output.output).strip()
def close_continued_block(self, continued_block, depth):
if continued_block is not None:
self._close_block(depth, lambda: self.output.close_jinja(continued_block))
def parse_jinja_block(self, block, depth, continued_block=None):
line = block[1][1:]
m = re.match('^(\w+)(.*)$', line)
if m is None:
raise TemplateSyntaxError('Expected jinja tag, got "%s".' % line, block[0])
name = m.group(1)
if continued_block is not None:
if name not in self.continued_jinja_tags:
continued_block = self.close_continued_block(continued_block, depth)
if name in self.extended_jinja_tags:
continued_block = name
data = ''
if self.INLINE_DATA_SEP in line:
line, data = line.split(self.INLINE_DATA_SEP, 1)
if self.debug:
self.output.newline()
self.output.indent(depth)
self.output.open_jinja(name, line)
if data:
self.output.write(data)
if name not in self.self_closing_jinja_tags and \
continued_block is None and (data or not block[2]):
self.output.close_jinja(name)
if not self.debug: # and block[2]:
self.output.newline()
self.create_output(block[2], depth + 1)
if not data and name not in self.self_closing_jinja_tags and continued_block is None\
and block[2]:
self._close_block(depth, lambda: self.output.close_jinja(name))
if name in self.extended_jinja_tags:
return name
return continued_block
def parse_html_block(self, block, depth):
m = re.match('^(\w+)(.*)$', block[1][1:])
if m is None:
raise TemplateSyntaxError('Expected html tag, got "%s".' % block[1][1:], block[0])
tag = m.group(1)
attrs = m.group(2)
data = ''
if self.INLINE_DATA_SEP in attrs:
attrs, data = attrs.split(self.INLINE_DATA_SEP, 1)
if self.debug:
self.output.newline()
self.output.indent(depth)
self_closing = False
if attrs and attrs[-1] == self.SELF_CLOSING_TAG:
attrs = attrs[:-1]
self_closing = True
elif tag in self.self_closing_html_tags:
self_closing = True
attrs = attrs.rstrip()
if attrs and attrs[0] in (self.ID_SHORTCUT, self.CLASS_SHORTCUT):
attrs = self._parse_shortcut_attributes(attrs)
if self_closing and (data or block[2]):
if not self.debug:
raise TemplateSyntaxError("Self closing tags can't have content", block[0])
else:
#In debug mode a self closing tag can contain empty lines
#if it contains something other than empty lines, throw an error.
if filter(lambda b: b[1] != '##empty_line##', block[2]):
raise TemplateSyntaxError("Self closing tags can't have content", block[0])
if self_closing:
self.output.self_closing_html(tag, attrs)
else:
self.output.open_html(tag, attrs)
if not self_closing and (data or not block[2]):
if data:
self.output.write(data)
self.output.close_html(tag)
if not self.debug:
self.output.newline()
self.create_output(block[2], depth + 1)
if not data and not self_closing and block[2]:
self._close_block(depth, lambda: self.output.close_html(tag))
def parse_preformated_block(self, block, depth):
if self.debug:
self.output.write('\n')
self.output.write(block[1][1:])
if not self.debug:
self.output.write('\n')
self.create_output(block[2], depth + 1)
def parse_shortcuts(self, block, depth):
new_block = (block[0], '%div'+block[1], block[2])
self.parse_html_block(new_block, depth)
def _close_block(self, depth, close_callback):
if not self.debug:
self.output.indent(depth)
if self.debug:
prev = []
while self.output.output[-1].isspace():
prev.append(self.output.output.pop())
close_callback()
if self.debug:
self.output.write(''.join(prev))
if not self.debug:
self.output.newline()
def _parse_shortcut_attributes(self, value):
extra_attrs = ''
if ' ' in value:
value, extra_attrs = value.split(' ', 1)
match = re.findall(r'([\.#]\w+)', value)
classes = []
ids = []
#We make the class and id the same order as in the template
if value[0] == self.CLASS_SHORTCUT:
attrs = (('class', classes), ('id', ids))
else:
attrs = (('id', ids), ('class', classes))
for m in match:
if m[0] == self.CLASS_SHORTCUT:
classes.append(m[1:])
else:
ids.append(m[1:])
rv = ' '.join('%s="%s"' % (k, ' '.join(v))
for k, v in attrs if v)
if extra_attrs:
rv += ' ' + extra_attrs
if rv:
return ' ' + rv
return rv
class Output(object):
def __init__(self, indent_string=' ', newline_string='\n'):
self.output = []
self.indent_string = indent_string
self.newline_string = newline_string
def open_html(self, tag, attrs):
self.write('<%s%s>' % (tag, attrs and attrs))
def close_html(self, tag):
self.write('</%s>' % tag)
def self_closing_html(self, tag, attrs):
self.write('<%s%s />' % (tag, attrs and attrs))
def open_jinja(self, tag, content):
self.write('{%% %s %%}' % content)
def close_jinja(self, tag):
self.write('{%% end%s %%}' % tag)
def indent(self, level):
if level and self.indent_string:
self.write(self.indent_string * level)
def newline(self):
if self.newline_string:
self.write(self.newline_string)
def write(self, data):
self.output.append(data)