Add support for image sizing in Markdown

This commit is contained in:
Skia 2017-05-14 01:03:50 +02:00
parent 0a84ef8438
commit 8c151fa498

View File

@ -23,7 +23,7 @@
#
import re
from mistune import Renderer, InlineGrammar, InlineLexer, Markdown
from mistune import Renderer, InlineGrammar, InlineLexer, Markdown, escape, escape_link
from django.core.urlresolvers import reverse_lazy, reverse
@ -40,6 +40,43 @@ class SithRenderer(Renderer):
def underline(self, text):
return """<span class="underline">%s</span>""" % text
def image(self, original_src, title, text):
"""Rendering a image with title and text.
:param src: source link of the image.
:param title: title text of the image.
:param text: alt text of the image.
"""
style = None
if '?' in original_src:
src, params = original_src.rsplit('?', maxsplit=1)
m = re.search(r'(\d+%?)(x(\d+%?))?', params)
if not m:
src = original_src
else:
width = m.group(1)
if not width.endswith('%'): width += "px"
style = "width: %s; " % width
try:
height = m.group(3)
if not height.endswith('%'): height += "px"
style += "height: %s; " % height
except: pass
else:
params = None
src = original_src
src = escape_link(src)
text = escape(text, quote=True)
if title:
title = escape(title, quote=True)
html = '<img src="%s" alt="%s" title="%s"' % (src, text, title)
else:
html = '<img src="%s" alt="%s"' % (src, text)
if style:
html = '%s style="%s"' % (html, style)
if self.options.get('use_xhtml'):
return '%s />' % html
return '%s>' % html
class SithInlineGrammar(InlineGrammar):
double_emphasis = re.compile(
r'^\*{2}([\s\S]+?)\*{2}(?!\*)' # **word**