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:
parent
e4aa5c6e6e
commit
b145f4ada9
4 changed files with 41 additions and 2 deletions
|
@ -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>
|
||||
|
||||
|
|
|
@ -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({
|
21
lemoncurry/jinja2/bleach.py
Normal file
21
lemoncurry/jinja2/bleach.py
Normal 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
|
16
lemoncurry/jinja2/markdown.py
Normal file
16
lemoncurry/jinja2/markdown.py
Normal 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))
|
Loading…
Reference in a new issue