diff --git a/hyde/ext/templates/jinja.py b/hyde/ext/templates/jinja.py index d81b85f..e1f0c9f 100644 --- a/hyde/ext/templates/jinja.py +++ b/hyde/ext/templates/jinja.py @@ -3,6 +3,8 @@ Jinja template utilties """ +import re + from hyde.fs import File, Folder from hyde.model import Expando from hyde.template import HtmlWrap, Template @@ -96,6 +98,35 @@ def syntax(env, value, lexer=None, filename=None): '\n\n
%s
%s
\n\n' % (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): """ A wrapper around the markdown filter for syntactic sugar. @@ -437,6 +468,7 @@ class Jinja2Template(Template): trim_blocks=True, bytecode_cache=FileSystemBytecodeCache(), extensions=[IncludeText, + Spaceless, Markdown, Syntax, Reference, diff --git a/hyde/model.py b/hyde/model.py index 5e73212..75ac602 100644 --- a/hyde/model.py +++ b/hyde/model.py @@ -110,7 +110,8 @@ class Dependents(IterableUserDict): """ 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): """ diff --git a/hyde/tests/test_jinja2template.py b/hyde/tests/test_jinja2template.py index 98eb1b5..8e3354c 100644 --- a/hyde/tests/test_jinja2template.py +++ b/hyde/tests/test_jinja2template.py @@ -88,6 +88,41 @@ def test_typogrify(): html = t.render(source, {}).strip() assert html == u'One & two' +def test_spaceless(): + source = """ + {%spaceless%} + + + + + + {%endspaceless%} + """ + t = Jinja2Template(JINJA2.path) + t.configure(None) + html = t.render(source, {}).strip() + expected = u""" + +""" + assert html.strip() == expected.strip() + + def test_markdown(): source = """ {%markdown%}