From a4b2bb76441511e6e61f5f1e5d4c92af571fb8c4 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Mon, 3 Jan 2011 23:25:51 +0530 Subject: [PATCH] Markdown support added --- hyde/ext/templates/jinja.py | 36 +++++++++++++++++++++++++++++-- hyde/tests/test_jinja2template.py | 26 ++++++++++++++++++++++ hyde/tests/test_typogrify.py | 0 3 files changed, 60 insertions(+), 2 deletions(-) delete mode 100644 hyde/tests/test_typogrify.py diff --git a/hyde/ext/templates/jinja.py b/hyde/ext/templates/jinja.py index 3fa41a3..16e83d1 100644 --- a/hyde/ext/templates/jinja.py +++ b/hyde/ext/templates/jinja.py @@ -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 diff --git a/hyde/tests/test_jinja2template.py b/hyde/tests/test_jinja2template.py index 94499f6..d342c84 100644 --- a/hyde/tests/test_jinja2template.py +++ b/hyde/tests/test_jinja2template.py @@ -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 & two' + +def test_markdown(): + source = """ + {%markdown%} + ### Heading 3 + {%endmarkdown%} + """ + t = Jinja2Template(JINJA2.path) + t.configure(None) + html = t.render(source, {}).strip() + assert html == u'

Heading 3

' + +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'

Heading 3

' diff --git a/hyde/tests/test_typogrify.py b/hyde/tests/test_typogrify.py deleted file mode 100644 index e69de29..0000000