diff --git a/hyde/ext/templates/jinja.py b/hyde/ext/templates/jinja.py index 67de2f1..ad717c5 100644 --- a/hyde/ext/templates/jinja.py +++ b/hyde/ext/templates/jinja.py @@ -110,6 +110,18 @@ def markdown(env, value): return marked.convert(output) +@environmentfilter +def restructuredtext(env, value): + """ + RestructuredText filter + """ + try: + from docutils.core import publish_parts + except ImportError: + print u"Requires docutils library to use restructuredtext tag." + raise + parts = publish_parts(source=value, writer_name="html") + return parts['html_body'] @environmentfilter def syntax(env, value, lexer=None, filename=None): @@ -200,6 +212,31 @@ class Markdown(Extension): output = caller().strip() return markdown(self.environment, output) +class restructuredText(Extension): + """ + A wrapper around the restructuredtext filter for syntactic sugar + """ + tags = set(['restructuredtext']) + + def parse(self, parser): + """ + Simply extract our content + """ + lineno = parser.stream.next().lineno + body = parser.parse_statements(['name:endrestructuredtext'], drop_needle=True) + + return nodes.CallBlock(self.call_method('_render_rst'), [], [], body + ).set_lineno(lineno) + + def _render_rst(self, caller=None): + """ + call our restructuredtext filter + """ + if not caller: + return '' + output = caller().strip() + return restructuredtext(self.environment, output) + class YamlVar(Extension): """ An extension that converts the content between the tags @@ -524,6 +561,7 @@ class Jinja2Template(Template): IncludeText, Spaceless, Markdown, + restructuredText, Syntax, Reference, Refer, diff --git a/hyde/tests/test_jinja2template.py b/hyde/tests/test_jinja2template.py index 9cb991e..755a1c7 100644 --- a/hyde/tests/test_jinja2template.py +++ b/hyde/tests/test_jinja2template.py @@ -136,6 +136,20 @@ def test_markdown(): html = t.render(source, {}).strip() assert html == u'

Heading 3

' +def test_restructuredtext(): + source = """ +{% restructuredtext %} +Hello +===== +{% endrestructuredtext %} + """ + t = Jinja2Template(JINJA2.path) + t.configure(None) + html = t.render(source, {}).strip() + assert html == u"""
+

Hello

+
""", html + def test_markdown_with_extensions(): source = """ {%markdown%}