Browse Source

Added spaceless tag for jinja

main
Lakshmi Vyasarajan 14 years ago
parent
commit
1fa3c6a4a7
3 changed files with 69 additions and 1 deletions
  1. +32
    -0
      hyde/ext/templates/jinja.py
  2. +2
    -1
      hyde/model.py
  3. +35
    -0
      hyde/tests/test_jinja2template.py

+ 32
- 0
hyde/ext/templates/jinja.py View File

@@ -3,6 +3,8 @@
Jinja template utilties Jinja template utilties
""" """


import re

from hyde.fs import File, Folder from hyde.fs import File, Folder
from hyde.model import Expando from hyde.model import Expando
from hyde.template import HtmlWrap, Template from hyde.template import HtmlWrap, Template
@@ -96,6 +98,35 @@ def syntax(env, value, lexer=None, filename=None):
'\n\n<div class="code"><figcaption>%s</figcaption>%s</div>\n\n' '\n\n<div class="code"><figcaption>%s</figcaption>%s</div>\n\n'
% (caption, code)) % (caption, code))


class Spaceless(Extension):
"""
Emulates the django spaceless template tag.
"""

tags = set(['spaceless'])

def parse(self, parser):
"""
Parses the statements and calls back to strip spaces.
"""
lineno = parser.stream.next().lineno
body = parser.parse_statements(['name:endspaceless'],
drop_needle=True)
return nodes.CallBlock(
self.call_method('_render_spaceless'),
[], [], body).set_lineno(lineno)

def _render_spaceless(self, caller=None):
"""
Strip the spaces between tags using the regular expression
from django. Stolen from `django.util.html` Returns the given HTML
with spaces between tags removed.
"""
if not caller:
return ''
return re.sub(r'>\s+<', '><', unicode(caller().strip()))


class Markdown(Extension): class Markdown(Extension):
""" """
A wrapper around the markdown filter for syntactic sugar. A wrapper around the markdown filter for syntactic sugar.
@@ -437,6 +468,7 @@ class Jinja2Template(Template):
trim_blocks=True, trim_blocks=True,
bytecode_cache=FileSystemBytecodeCache(), bytecode_cache=FileSystemBytecodeCache(),
extensions=[IncludeText, extensions=[IncludeText,
Spaceless,
Markdown, Markdown,
Syntax, Syntax,
Reference, Reference,


+ 2
- 1
hyde/model.py View File

@@ -110,7 +110,8 @@ class Dependents(IterableUserDict):
""" """
Saves the dependency graph (just a dict for now). Saves the dependency graph (just a dict for now).
""" """
self.deps_file.write(yaml.dump(self.data))
if self.deps_file.parent.exists:
self.deps_file.write(yaml.dump(self.data))


class Config(Expando): class Config(Expando):
""" """


+ 35
- 0
hyde/tests/test_jinja2template.py View File

@@ -88,6 +88,41 @@ def test_typogrify():
html = t.render(source, {}).strip() html = t.render(source, {}).strip()
assert html == u'One <span class="amp">&amp;</span>&nbsp;two' assert html == u'One <span class="amp">&amp;</span>&nbsp;two'


def test_spaceless():
source = """
{%spaceless%}
<html>
<body>
<ul>
<li>
One
</li>
<li>
Two
</li>
<li>
Three
</li>
</ul>
</body>
</html>
{%endspaceless%}
"""
t = Jinja2Template(JINJA2.path)
t.configure(None)
html = t.render(source, {}).strip()
expected = u"""
<html><body><ul><li>
One
</li><li>
Two
</li><li>
Three
</li></ul></body></html>
"""
assert html.strip() == expected.strip()


def test_markdown(): def test_markdown():
source = """ source = """
{%markdown%} {%markdown%}


Loading…
Cancel
Save