Browse Source

Markdown support added

main
Lakshmi Vyasarajan 14 years ago
parent
commit
a4b2bb7644
3 changed files with 60 additions and 2 deletions
  1. +34
    -2
      hyde/ext/templates/jinja.py
  2. +26
    -0
      hyde/tests/test_jinja2template.py
  3. +0
    -0
      hyde/tests/test_typogrify.py

+ 34
- 2
hyde/ext/templates/jinja.py View File

@@ -4,7 +4,10 @@ Jinja template utilties

from hyde.fs import File, Folder
from hyde.template import Template
from jinja2 import contextfunction, Environment, FileSystemLoader, Undefined
from jinja2 import contextfunction, Environment, FileSystemLoader, Undefined, nodes
from jinja2.ext import Extension
from jinja2.exceptions import TemplateError


class SilentUndefined(Undefined):
def __getattr__(self, name):
@@ -27,6 +30,32 @@ def content_url(context, path):
site = context['site']
return Folder(site.config.base_url).child(path)

class Markdown(Extension):
tags = set(['markdown'])

def parse(self, parser):
lineno = parser.stream.next().lineno
body = parser.parse_statements(['name:endmarkdown'], drop_needle=True)

return nodes.CallBlock(
self.call_method('_render_markdown', [], [], None, None),
[], [], body
).set_lineno(lineno)

def _render_markdown(self, caller=None):
try:
import markdown
except ImportError:
raise TemplateError("Cannot load the markdown library")
if not caller:
return ''
output = caller().strip()
d = {}
if hasattr(self.environment.config, 'markdown'):
d['extensions'] = getattr(self.environment.config.markdown, 'extensions', [])
d['extension_configs'] = getattr(self.environment.config.markdown, 'extension_configs', {})
md = markdown.Markdown(**d)
return md.convert(output)

# pylint: disable-msg=W0104,E0602,W0613,R0201
class Jinja2Template(Template):
@@ -48,9 +77,12 @@ class Jinja2Template(Template):
])
else:
loader = FileSystemLoader(str(self.sitepath))
self.env = Environment(loader=loader, undefined=SilentUndefined)
self.env = Environment(loader=loader,
undefined=SilentUndefined,
extensions=[Markdown])
self.env.globals['media_url'] = media_url
self.env.globals['content_url'] = content_url
self.env.extend(config=config)

try:
from typogrify.templatetags import jinja2_filters


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

@@ -9,6 +9,8 @@ Code borrowed from rwbench.py from the jinja2 examples
from datetime import datetime
from hyde.ext.templates.jinja import Jinja2Template
from hyde.fs import File, Folder
from hyde.model import Config

import jinja2
from jinja2.utils import generate_lorem_ipsum
from random import choice, randrange
@@ -81,3 +83,27 @@ def test_typogrify():
t.configure(None)
html = t.render(source, {}).strip()
assert html == u'One <span class="amp">&amp;</span>&nbsp;two'

def test_markdown():
source = """
{%markdown%}
### Heading 3
{%endmarkdown%}
"""
t = Jinja2Template(JINJA2.path)
t.configure(None)
html = t.render(source, {}).strip()
assert html == u'<h3>Heading 3</h3>'
def test_markdown_with_extensions():
source = """
{%markdown%}
### Heading 3
{%endmarkdown%}
"""
t = Jinja2Template(JINJA2.path)
c = Config(JINJA2.path, dict(markdown=dict(extensions=['headerid'])))
t.configure(c)
html = t.render(source, {}).strip()
assert html == u'<h3 id="heading_3">Heading 3</h3>'

+ 0
- 0
hyde/tests/test_typogrify.py View File


Loading…
Cancel
Save