Browse Source

Jinja: Add basic support for restructuredText formatting

main
Benoit Allard 13 years ago
parent
commit
1acf11b05c
2 changed files with 52 additions and 0 deletions
  1. +38
    -0
      hyde/ext/templates/jinja.py
  2. +14
    -0
      hyde/tests/test_jinja2template.py

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

@@ -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,


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

@@ -136,6 +136,20 @@ def test_markdown():
html = t.render(source, {}).strip()
assert html == u'<h3>Heading 3</h3>'

def test_restructuredtext():
source = """
{% restructuredtext %}
Hello
=====
{% endrestructuredtext %}
"""
t = Jinja2Template(JINJA2.path)
t.configure(None)
html = t.render(source, {}).strip()
assert html == u"""<div class="document" id="hello">
<h1 class="title">Hello</h1>
</div>""", html

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


Loading…
Cancel
Save