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

This commit is contained in:
Danielle McLean 2018-06-25 10:11:52 +10:00
parent e4aa5c6e6e
commit b145f4ada9
Signed by: 00dani
GPG key ID: 8EB789DDF3ABD240
4 changed files with 41 additions and 2 deletions

View file

@ -10,7 +10,7 @@
{{i}}<h4 class="card-title p-name">{{ entry.name }}</h4> {{i}}<h4 class="card-title p-name">{{ entry.name }}</h4>
{% endif %} {% endif %}
{{i}}<div class="e-content"> {{i}}<div class="e-content">
{{i}}{{ entry.content | indent(indent_width + 6) }} {{i}}{{ entry.content | markdown }}
{{i}}</div> {{i}}</div>
{{i}}</div> {{i}}</div>

View file

@ -7,7 +7,8 @@ from compressor.contrib.jinja2ext import CompressorExtension
from django_activeurl.ext.django_jinja import ActiveUrl from django_activeurl.ext.django_jinja import ActiveUrl
from entries.kinds import all as entry_kinds 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): def environment(**options):
@ -19,6 +20,7 @@ def environment(**options):
) )
env.filters.update({ env.filters.update({
'friendly_url': friendly_url, 'friendly_url': friendly_url,
'markdown': markdown,
'naturaltime': naturaltime, 'naturaltime': naturaltime,
}) })
env.globals.update({ env.globals.update({

View file

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

View file

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