From b145f4ada9629a8604f9b6544e30b9f2df30b3b3 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Mon, 25 Jun 2018 10:11:52 +1000 Subject: [PATCH] Render the Markdown content for entries in Jinja2 - the resulting HTML isn't pretty yet, I'll probably need to write an html5lib filter that prettifies it --- entries/jinja2/entries/h-entry.html | 2 +- lemoncurry/{jinja2.py => jinja2/__init__.py} | 4 +++- lemoncurry/jinja2/bleach.py | 21 ++++++++++++++++++++ lemoncurry/jinja2/markdown.py | 16 +++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) rename lemoncurry/{jinja2.py => jinja2/__init__.py} (88%) create mode 100644 lemoncurry/jinja2/bleach.py create mode 100644 lemoncurry/jinja2/markdown.py diff --git a/entries/jinja2/entries/h-entry.html b/entries/jinja2/entries/h-entry.html index 0cf7a80..b9a05f6 100644 --- a/entries/jinja2/entries/h-entry.html +++ b/entries/jinja2/entries/h-entry.html @@ -10,7 +10,7 @@ {{i}}

{{ entry.name }}

{% endif %} {{i}}
- {{i}}{{ entry.content | indent(indent_width + 6) }} + {{i}}{{ entry.content | markdown }} {{i}}
{{i}} diff --git a/lemoncurry/jinja2.py b/lemoncurry/jinja2/__init__.py similarity index 88% rename from lemoncurry/jinja2.py rename to lemoncurry/jinja2/__init__.py index 57fcfdf..681f1d0 100644 --- a/lemoncurry/jinja2.py +++ b/lemoncurry/jinja2/__init__.py @@ -7,7 +7,8 @@ from compressor.contrib.jinja2ext import CompressorExtension from django_activeurl.ext.django_jinja import ActiveUrl from entries.kinds import all as entry_kinds -from .utils import friendly_url, load_package_json +from .markdown import markdown +from ..utils import friendly_url, load_package_json def environment(**options): @@ -19,6 +20,7 @@ def environment(**options): ) env.filters.update({ 'friendly_url': friendly_url, + 'markdown': markdown, 'naturaltime': naturaltime, }) env.globals.update({ diff --git a/lemoncurry/jinja2/bleach.py b/lemoncurry/jinja2/bleach.py new file mode 100644 index 0000000..de09c04 --- /dev/null +++ b/lemoncurry/jinja2/bleach.py @@ -0,0 +1,21 @@ +from bleach.sanitizer import Cleaner, ALLOWED_TAGS +from bleach.linkifier import LinkifyFilter +from jinja2 import evalcontextfilter, Markup + +TAGS = ['cite', 'code', 'p', 'pre', 'img', 'span'] +TAGS.extend(ALLOWED_TAGS) +ATTRIBUTES = { + 'a': ('href', 'title', 'class'), + 'img': ('alt', 'src', 'title'), + 'span': ('class',), +} + +cleaner = Cleaner(tags=TAGS, attributes=ATTRIBUTES, filters=(LinkifyFilter,)) + + +@evalcontextfilter +def bleach(ctx, html): + res = cleaner.clean(html) + if ctx.autoescape: + res = Markup(res) + return res diff --git a/lemoncurry/jinja2/markdown.py b/lemoncurry/jinja2/markdown.py new file mode 100644 index 0000000..951b3b8 --- /dev/null +++ b/lemoncurry/jinja2/markdown.py @@ -0,0 +1,16 @@ +from jinja2 import evalcontextfilter +from markdown import Markdown + +from .bleach import bleach + +md = Markdown(extensions=( + 'markdown.extensions.extra', + 'markdown.extensions.headerid', + 'markdown.extensions.sane_lists', + 'markdown.extensions.smarty', +)) + + +@evalcontextfilter +def markdown(ctx, source): + return bleach(ctx, md.reset().convert(source))