Skip to content

Commit ebc9a85

Browse files
committed
Handle exception raised from markdown processing
If an exception is raised from the markdown processor, return plain text to the user.
1 parent e0bb3f5 commit ebc9a85

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Fixed:
3434
url. Attempts to override cheeseshop_url failed. Replace call to
3535
cheeseshop in docs with raw html and remove references to
3636
cheeseshop. (John Rouillard)
37+
- issue2551093 - return plain text if markdown formatter throws exception
38+
(reported by Cedric Krier, fix by John Rouillard)
3739

3840
Features:
3941
- issue2550522 - Add 'filter' command to command-line

roundup/cgi/templating.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,11 @@ def markdown(self, hyperlink=1):
17301730
s = self.plain(escape=0, hyperlink=0)
17311731
if hyperlink:
17321732
s = self.hyper_re.sub(self._hyper_repl_markdown, s)
1733-
return u2s(markdown(s2u(s)))
1733+
try:
1734+
s = u2s(markdown(s2u(s)))
1735+
except Exception: # when markdown formatting fails return markup
1736+
return self.plain(escape=0, hyperlink=hyperlink)
1737+
return s
17341738

17351739
def field(self, **kwargs):
17361740
""" Render the property as a field in HTML.

test/test_templating.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,23 @@ def test_string_markdown_forced_line_break(self):
460460
self.assertEqual(3, m.count('<br'))
461461

462462
def test_string_markdown_code_block(self):
463-
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'embedded code block\n\n```\nline 1\nline 2\n```\n\nnew paragraph'))
464-
self.assertEqual(p.markdown().strip().replace('\n\n', '\n'), u2s(u'<p>embedded code block</p>\n<pre><code>line 1\nline 2\n</code></pre>\n<p>new paragraph</p>'))
463+
''' also verify that embedded html is escaped '''
464+
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'embedded code block <pre>\n\n```\nline 1\nline 2\n```\n\nnew </pre> paragraph'))
465+
self.assertEqual(p.markdown().strip().replace('\n\n', '\n'), u2s(u'<p>embedded code block &lt;pre&gt;</p>\n<pre><code>line 1\nline 2\n</code></pre>\n<p>new &lt;/pre&gt; paragraph</p>'))
466+
467+
def test_string_markdown_code_block_attribute(self):
468+
''' also verify that embedded html is escaped '''
469+
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'embedded code block <pre>\n\n``` python\nline 1\nline 2\n```\n\nnew </pre> paragraph'))
470+
m = p.markdown().strip()
471+
print(m)
472+
if type(self) == MistuneTestCase:
473+
self.assertEqual(m.replace('\n\n','\n'), '<p>embedded code block &lt;pre&gt;</p>\n<pre><code class="lang-python">line 1\nline 2\n</code></pre>\n<p>new &lt;/pre&gt; paragraph</p>')
474+
elif type(self) == MarkdownTestCase:
475+
self.assertEqual(m.replace('\n\n','\n'), '<p>embedded code block &lt;pre&gt;</p>\n<pre><code class="language-python">line 1\nline 2\n</code></pre>\n<p>new &lt;/pre&gt; paragraph</p>')
476+
else: # markdown2 doesn't handle attributes with code blocks
477+
# so processing it returns original text
478+
self.assertEqual(m.replace('\n\n', '\n'), u2s(u'embedded code block &lt;pre&gt;\n``` python\nline 1\nline 2\n```\nnew &lt;/pre&gt; paragraph'))
479+
465480

466481
@skip_mistune
467482
class MistuneTestCase(TemplatingTestCase, MarkdownTests) :

0 commit comments

Comments
 (0)