Skip to content

Commit

Permalink
core.markdown use default_rules instead of inline_html_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Dec 20, 2015
1 parent 75687e1 commit 28c0539
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
33 changes: 33 additions & 0 deletions spirit/core/tests/tests_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ def test_markdown_escape(self):
comment_md = Markdown().render(comment)
self.assertEqual(comment_md, '<p>&lt;span&gt;foo&lt;/span&gt;</p>')

def test_markdown_html(self):
"""
Should escape html
"""
# todo: fixed on mistune 0.7.2 ?
# markdown is not parsed within html tags, there is a way to parse it
# but it's broken since it gets escaped afterwards.
comment = (
"<div>\n"
"<em>*foo*</em>\n"
"<em>*bar*</em>\n"
"*foobar*\n"
"@nitely\n"
"*<em>foobar</em>*\n"
"</div>\n"
"<em>*foo*</em>\n"
"<em>@nitely</em>\n" # Why this gets parsed properly is beyond me
"*<em>foobar</em>*\n"
)
comment_md = Markdown().render(comment)
self.assertEqual(comment_md, (
'<p>&lt;div&gt;\n'
'&lt;em&gt;*foo*&lt;/em&gt;\n'
'&lt;em&gt;*bar*&lt;/em&gt;\n'
'*foobar*\n'
'@nitely\n'
'*&lt;em&gt;foobar&lt;/em&gt;*\n'
'&lt;/div&gt;<br>\n'
'&lt;em&gt;*foo*&lt;/em&gt;<br>\n'
'&lt;em&gt;<a class="comment-mention" rel="nofollow" href="/user/1/nitely/">@nitely</a>&lt;/em&gt;<br>\n'
'<em>&lt;em&gt;foobar&lt;/em&gt;</em></p>'
))

def test_markdown_mentions(self):
"""
markdown mentions
Expand Down
2 changes: 1 addition & 1 deletion spirit/core/utils/markdown/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def hard_wrap(self):

class InlineLexer(mistune.InlineLexer):

default_rules = copy.copy(mistune.InlineLexer.inline_html_rules)
default_rules = copy.copy(mistune.InlineLexer.default_rules)
default_rules.insert(2, 'emoji')
default_rules.insert(2, 'mention')

Expand Down
15 changes: 12 additions & 3 deletions spirit/core/utils/markdown/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@
class Markdown(mistune.Markdown):

def __init__(self, no_follow=True):
renderer = Renderer(escape=True, hard_wrap=True, no_follow=no_follow)
renderer = Renderer(
escape=True,
hard_wrap=True,
no_follow=no_follow
)
super(Markdown, self).__init__(
renderer=renderer,
block=BlockLexer,
inline=InlineLexer
inline=InlineLexer,
parse_block_html=False,
parse_inline_html=False
)

# Override
def __call__(self, text):
return super(Markdown, self).__call__(text).strip()

def render(self, text):
return super(Markdown, self).render(text).strip()
return self(text)

def get_mentions(self):
return self.inline.mentions
Expand Down

0 comments on commit 28c0539

Please sign in to comment.