From 8c151fa4988eb80c6a9a68810adf8cd234f897c9 Mon Sep 17 00:00:00 2001 From: Skia Date: Sun, 14 May 2017 01:03:50 +0200 Subject: [PATCH] Add support for image sizing in Markdown --- core/markdown.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/core/markdown.py b/core/markdown.py index 00355f28..1bcb6ff3 100644 --- a/core/markdown.py +++ b/core/markdown.py @@ -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 """%s""" % 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 = '%s' % html + return '%s>' % html + class SithInlineGrammar(InlineGrammar): double_emphasis = re.compile( r'^\*{2}([\s\S]+?)\*{2}(?!\*)' # **word**