Ensure markdown is optional. Closes #4941

This commit is contained in:
Tom Christie 2017-03-07 10:54:00 +00:00
parent 0b708f04e1
commit 2c4ad8c0e2

View File

@ -9,7 +9,6 @@ from django.utils import six
from django.utils.encoding import force_text, iri_to_uri from django.utils.encoding import force_text, iri_to_uri
from django.utils.html import escape, format_html, smart_urlquote from django.utils.html import escape, format_html, smart_urlquote
from django.utils.safestring import SafeData, mark_safe from django.utils.safestring import SafeData, mark_safe
from markdown.extensions.fenced_code import FencedBlockPreprocessor
from rest_framework.compat import ( from rest_framework.compat import (
NoReverseMatch, markdown, reverse, template_render NoReverseMatch, markdown, reverse, template_render
@ -24,22 +23,6 @@ register = template.Library()
class_re = re.compile(r'(?<=class=["\'])(.*)(?=["\'])') class_re = re.compile(r'(?<=class=["\'])(.*)(?=["\'])')
class CustomFencedBlockPreprocessor(FencedBlockPreprocessor):
CODE_WRAP = '<pre%s><code>%s</code></pre>'
LANG_TAG = ' class="highlight %s"'
class FencedCodeExtension(markdown.Extension):
def extendMarkdown(self, md, md_globals):
""" Add FencedBlockPreprocessor to the Markdown instance. """
md.registerExtension(self)
md.preprocessors.add('fenced_code_block',
CustomFencedBlockPreprocessor(md),
">normalize_whitespace")
@register.tag(name='code') @register.tag(name='code')
def highlight_code(parser, token): def highlight_code(parser, token):
code = token.split_contents()[-1] code = token.split_contents()[-1]
@ -92,7 +75,9 @@ def form_for_link(link):
@register.simple_tag @register.simple_tag
def render_markdown(markdown_text): def render_markdown(markdown_text):
return markdown.markdown(markdown_text, extensions=[FencedCodeExtension(), "tables"]) if not markdown:
return markdown_text
return markdown.markdown(markdown_text)
@register.simple_tag @register.simple_tag