Browse Source

Merge pull request #63 from benallard/hyde

---

This add basic support for restructuredText.
main
Lakshmi Vyas 13 years ago
parent
commit
e1652c158c
3 changed files with 57 additions and 0 deletions
  1. +39
    -0
      hyde/ext/templates/jinja.py
  2. +4
    -0
      hyde/server.py
  3. +14
    -0
      hyde/tests/test_jinja2template.py

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

@@ -110,6 +110,18 @@ def markdown(env, value):


return marked.convert(output) 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 @environmentfilter
def syntax(env, value, lexer=None, filename=None): def syntax(env, value, lexer=None, filename=None):
@@ -200,6 +212,31 @@ class Markdown(Extension):
output = caller().strip() output = caller().strip()
return markdown(self.environment, output) 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): class YamlVar(Extension):
""" """
An extension that converts the content between the tags An extension that converts the content between the tags
@@ -524,6 +561,7 @@ class Jinja2Template(Template):
IncludeText, IncludeText,
Spaceless, Spaceless,
Markdown, Markdown,
restructuredText,
Syntax, Syntax,
Reference, Reference,
Refer, Refer,
@@ -572,6 +610,7 @@ class Jinja2Template(Template):
self.env.globals['engine'] = engine self.env.globals['engine'] = engine
self.env.globals['deps'] = {} self.env.globals['deps'] = {}
self.env.filters['markdown'] = markdown self.env.filters['markdown'] = markdown
self.env.filters['restructuredtext'] = restructuredtext
self.env.filters['syntax'] = syntax self.env.filters['syntax'] = syntax
self.env.filters['date_format'] = date_format self.env.filters['date_format'] = date_format
self.env.filters['xmldatetime'] = xmldatetime self.env.filters['xmldatetime'] = xmldatetime


+ 4
- 0
hyde/server.py View File

@@ -8,6 +8,7 @@ import select
import threading import threading
import urlparse import urlparse
import urllib import urllib
import traceback
from datetime import datetime from datetime import datetime
from SimpleHTTPServer import SimpleHTTPRequestHandler from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer from BaseHTTPServer import HTTPServer
@@ -196,6 +197,7 @@ class HydeWebServer(HTTPServer):
except Exception, exception: except Exception, exception:
logger.error('Error occured when regenerating the site [%s]' logger.error('Error occured when regenerating the site [%s]'
% exception.message) % exception.message)
logger.error(traceback.format_exc())


def generate_node(self, node): def generate_node(self, node):
""" """
@@ -213,6 +215,7 @@ class HydeWebServer(HTTPServer):
logger.error( logger.error(
'Error [%s] occured when generating the node [%s]' 'Error [%s] occured when generating the node [%s]'
% (repr(exception), node)) % (repr(exception), node))
logger.error(traceback.format_exc())


def generate_resource(self, resource): def generate_resource(self, resource):
""" """
@@ -231,3 +234,4 @@ class HydeWebServer(HTTPServer):
logger.error( logger.error(
'Error [%s] occured when serving the resource [%s]' 'Error [%s] occured when serving the resource [%s]'
% (repr(exception), resource)) % (repr(exception), resource))
logger.error(traceback.format_exc())

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

@@ -136,6 +136,20 @@ def test_markdown():
html = t.render(source, {}).strip() html = t.render(source, {}).strip()
assert html == u'<h3>Heading 3</h3>' 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(): def test_markdown_with_extensions():
source = """ source = """
{%markdown%} {%markdown%}


Loading…
Cancel
Save