Skip to content

Commit

Permalink
Added inverted stripper
Browse files Browse the repository at this point in the history
  • Loading branch information
bil-bas committed Aug 7, 2015
1 parent 6fb5f02 commit 1df4a7d
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion jinja2htmlcompress.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def _make_dict_from_listing(listing):


class HTMLCompress(Extension):
"""Compression always on"""

isolated_elements = set(['script', 'style', 'noscript', 'textarea'])
void_elements = set(['br', 'img', 'area', 'hr', 'param', 'input',
'embed', 'col'])
Expand Down Expand Up @@ -124,6 +126,7 @@ def filter_stream(self, stream):


class SelectiveHTMLCompress(HTMLCompress):
"""Compression off by default; on inside {% strip %} {% endstrip %} tags"""

def filter_stream(self, stream):
ctx = StreamProcessContext(stream)
Expand Down Expand Up @@ -153,6 +156,37 @@ def filter_stream(self, stream):
stream.next()


class InvertedSelectiveHTMLCompress(HTMLCompress):
"""Compression on by default; off inside {% unstrip %} {% endunstrip %} tags"""

def filter_stream(self, stream):
ctx = StreamProcessContext(stream)
unstrip_depth = 0
while 1:
if stream.current.type == 'block_begin':
if stream.look().test('name:unstrip') or \
stream.look().test('name:endunstrip'):
stream.skip()
if stream.current.value == 'unstrip':
unstrip_depth += 1
else:
unstrip_depth -= 1
if unstrip_depth < 0:
ctx.fail('Unexpected tag endunstrip')
stream.skip()
if stream.current.type != 'block_end':
ctx.fail('expected end of block, got %s' %
describe_token(stream.current))
stream.skip()
if unstrip_depth == 0 and stream.current.type == 'data':
ctx.token = stream.current
value = self.normalize(ctx)
yield Token(stream.current.lineno, 'data', value)
else:
yield stream.current
stream.next()


def test():
from jinja2 import Environment
env = Environment(extensions=[HTMLCompress])
Expand All @@ -172,11 +206,13 @@ def test():
</body>
</html>
''')
print "-" * 30, "HTMLCompress"
print tmpl.render(title=42, href='index.html')


env = Environment(extensions=[SelectiveHTMLCompress])
tmpl = env.from_string('''
Normal <span> unchanged </span> stuff
Normal <span> unchanged </span> stuff
{% strip %}Stripped <span class=foo > test </span>
<a href="foo"> test </a> {{ foo }}
Normal <stuff> again {{ foo }} </stuff>
Expand All @@ -188,6 +224,27 @@ def test():
</p>
{% endstrip %}
''')
print "-" * 30, "SelectiveHTMLCompress"
print tmpl.render(foo=42)


env = Environment(extensions=[InvertedSelectiveHTMLCompress])
tmpl = env.from_string('''
{% unstrip %}
Normal <span> unchanged </span> stuff
{% endunstrip %}
Stripped <span class=foo > test </span>
<a href="foo"> test </a> {{ foo }}
Normal <stuff> again {{ foo }} </stuff>
<p>
Foo<br>Bar
Baz
<p>
Moep <span>Test</span> Moep
</p>
''')
print "-" * 30, "InvertedSelectiveHTMLCompress"
print tmpl.render(foo=42)


Expand Down

0 comments on commit 1df4a7d

Please sign in to comment.