3 from django.utils.functional import allow_lazy
\r
4 from django.utils.encoding import force_unicode
\r
5 from django.template import Node, Library
\r
9 def strip_empty_lines(value):
\r
10 """Return the given HTML with empty and all-whitespace lines removed."""
\r
11 return re.sub(r'(\n[ \t]*|(?<=\n)\\\\)(?=\n)', '', force_unicode(value))
\r
12 strip_empty_lines = allow_lazy(strip_empty_lines, unicode)
\r
14 class GaplessNode(Node):
\r
15 def __init__(self, nodelist):
\r
16 self.nodelist = nodelist
\r
18 def render(self, context):
\r
19 return strip_empty_lines(self.nodelist.render(context).strip())
\r
21 def gapless(parser, token):
\r
23 Remove empty and whitespace-only lines. Useful for getting rid of those
\r
24 empty lines caused by template lines with only template tags and possibly
\r
31 <a href="foo/">Foo</a>
\r
33 {% endgapless %}</p>
\r
35 This example would return this HTML::
\r
38 <a href="foo/">Foo</a>
\r
42 nodelist = parser.parse(('endgapless',))
\r
43 parser.delete_first_token()
\r
44 return GaplessNode(nodelist)
\r
45 gapless = register.tag(gapless)
\r
47 # vim: set sw=4 sts=4 expandtab:
\r