Skip to content

Commit

Permalink
💡 Contrib limited html renderer (miyuchina#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
skairunner committed Dec 21, 2018
1 parent 846a419 commit 4a60046
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions contrib/limited_html_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import html
from mistletoe.html_renderer import HTMLRenderer

class LimitedHTMLRenderer(HTMLRenderer):
@staticmethod
def render_html_block(token):
return html.escape(token.content)

@staticmethod
def render_html_span(token):
return html.escape(token.content)
31 changes: 31 additions & 0 deletions test/test_contrib/test_limited_html_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from contrib.limited_html_renderer import LimitedHTMLRenderer
from mistletoe.block_token import Document
import random
import string
from unittest import TestCase

class TestLimitedHTMLRenderer(TestCase):
def setUp(self):
self.renderer = LimitedHTMLRenderer()
self.renderer.__enter__()
self.addCleanup(self.renderer.__exit__, None, None, None)

def check_render(self, inputString, expected, errormsg):
output = self.renderer.render(Document(inputString))
output = output.strip()
self.assertEqual(output, expected, errormsg)

def test_render_inline_div(self):
input = '<div>hello</div>'
output = '&lt;div&gt;hello&lt;/div&gt;'
self.check_render(input, output, 'One line div is not escaped')

def test_render_inline_span(self):
input = '<span>hello</span>'
output = '<p>&lt;span&gt;hello&lt;/span&gt;</p>'
self.check_render(input, output, 'One line span is not escaped')

def test_render_embedded_markdown(self):
input = '<div>\n\n*hello*\n\n</div>'
output = '&lt;div&gt;\n<p><em>hello</em></p>\n&lt;/div&gt;'
self.check_render(input, output, 'Markdown inside div is unexpectedly escaped')

0 comments on commit 4a60046

Please sign in to comment.