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>
{% endif %}
{{i}}<div class="e-content">
{{i}}{{ entry.content | indent(indent_width + 6) }}
{{i}}{{ entry.content | markdown }}
{{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 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({

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