symposion_app/symposion/markdown_parser.py

18 lines
471 B
Python
Raw Normal View History

from __future__ import unicode_literals
from html5lib import html5parser, sanitizer
import markdown
def parse(text):
2014-07-30 18:19:26 +00:00
# First run through the Markdown parser
text = markdown.markdown(text, extensions=["extra"], safe_mode=False)
2014-07-30 18:19:26 +00:00
# Sanitize using html5lib
bits = []
parser = html5parser.HTMLParser(tokenizer=sanitizer.HTMLSanitizer)
for token in parser.parseFragment(text).childNodes:
bits.append(token.toxml())
return "".join(bits)