Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions markdown/postprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from __future__ import absolute_import
from __future__ import unicode_literals
from collections import OrderedDict
from . import util
from . import odict
import re
Expand Down Expand Up @@ -50,6 +51,7 @@ class RawHtmlPostprocessor(Postprocessor):

def run(self, text):
""" Iterate over html stash and restore "safe" html. """
replacements = OrderedDict()
for i in range(self.markdown.htmlStash.html_counter):
html, safe = self.markdown.htmlStash.rawHtmlBlocks[i]
if self.markdown.safeMode and not safe:
Expand All @@ -61,14 +63,15 @@ def run(self, text):
html = self.markdown.html_replacement_text
if (self.isblocklevel(html) and
(safe or not self.markdown.safeMode)):
text = text.replace(
"<p>%s</p>" %
(self.markdown.htmlStash.get_placeholder(i)),
replacements["<p>%s</p>" %
(self.markdown.htmlStash.get_placeholder(i))] = \
html + "\n"
)
text = text.replace(
self.markdown.htmlStash.get_placeholder(i), html
)
replacements[self.markdown.htmlStash.get_placeholder(i)] = html

if replacements:
pattern = re.compile("|".join(re.escape(k) for k in replacements))
text = pattern.sub(lambda m: replacements[m.group(0)], text)

return text

def escape(self, html):
Expand Down
65 changes: 39 additions & 26 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
import markdown


class TestCaseWithAssertStartsWith(unittest.TestCase):

def assertStartsWith(self, expectedPrefix, text, msg=None):
if not text.startswith(expectedPrefix):
if len(expectedPrefix) + 5 < len(text):
text = text[:len(expectedPrefix) + 5] + '...'
standardMsg = '%s not found at the start of %s' % (repr(expectedPrefix),
repr(text))
self.fail(self._formatMessage(msg, standardMsg))


class TestExtensionClass(unittest.TestCase):
""" Test markdown.extensions.Extension. """

Expand Down Expand Up @@ -86,7 +97,7 @@ def testNestedAbbr(self):
)


class TestCodeHilite(unittest.TestCase):
class TestCodeHilite(TestCaseWithAssertStartsWith):
""" Test codehilite extension. """

def setUp(self):
Expand All @@ -101,7 +112,7 @@ def testBasicCodeHilite(self):
md = markdown.Markdown(extensions=['markdown.extensions.codehilite'])
if self.has_pygments:
# Pygments can use random lexer here as we did not specify the language
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre>'))
self.assertStartsWith('<div class="codehilite"><pre>', md.convert(text))
else:
self.assertEqual(
md.convert(text),
Expand All @@ -117,10 +128,9 @@ def testLinenumsTrue(self):
# Different versions of pygments output slightly different markup.
# So we use 'startwith' and test just enough to confirm that
# pygments received and processed linenums.
self.assertTrue(
md.convert(text).startswith(
'<table class="codehilitetable"><tr><td class="linenos">'
)
self.assertStartsWith(
'<table class="codehilitetable"><tr><td class="linenos">',
md.convert(text)
)
else:
self.assertEqual(
Expand All @@ -134,7 +144,7 @@ def testLinenumsFalse(self):
md = markdown.Markdown(
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=False)])
if self.has_pygments:
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre><span'))
self.assertStartsWith('<div class="codehilite"><pre><span', md.convert(text))
else:
self.assertEqual(
md.convert(text),
Expand All @@ -148,7 +158,7 @@ def testLinenumsNone(self):
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=None)])
if self.has_pygments:
# Pygments can use random lexer here as we did not specify the language
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre>'))
self.assertStartsWith('<div class="codehilite"><pre>', md.convert(text))
else:
self.assertEqual(
md.convert(text),
Expand All @@ -164,10 +174,9 @@ def testLinenumsNoneWithShebang(self):
# Differant versions of pygments output slightly different markup.
# So we use 'startwith' and test just enough to confirm that
# pygments received and processed linenums.
self.assertTrue(
md.convert(text).startswith(
'<table class="codehilitetable"><tr><td class="linenos">'
)
self.assertStartsWith(
'<table class="codehilitetable"><tr><td class="linenos">',
md.convert(text)
)
else:
self.assertEqual(
Expand All @@ -182,7 +191,7 @@ def testLinenumsNoneWithColon(self):
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=None)]
)
if self.has_pygments:
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre><span'))
self.assertStartsWith('<div class="codehilite"><pre><span', md.convert(text))
else:
self.assertEqual(
md.convert(text),
Expand All @@ -198,10 +207,9 @@ def testHighlightLinesWithColon(self):
for text in (text0, text1):
md = markdown.Markdown(extensions=['markdown.extensions.codehilite'])
if self.has_pygments:
self.assertTrue(
md.convert(text).startswith(
'<div class="codehilite"><pre><span class="hll"'
)
self.assertStartsWith(
'<div class="codehilite"><pre><span class="hll"',
md.convert(text)
)
else:
self.assertEqual(
Expand All @@ -224,7 +232,7 @@ def testUsePygmentsFalse(self):
)


class TestFencedCode(unittest.TestCase):
class TestFencedCode(TestCaseWithAssertStartsWith):
""" Test fenced_code extension. """

def setUp(self):
Expand Down Expand Up @@ -320,8 +328,9 @@ def testFencedCodeWithHighlightLines(self):
)

if self.has_pygments:
self.assertTrue(
md.convert(text).startswith('<div class="codehilite"><pre><span class="hll"')
self.assertStartsWith(
'<div class="codehilite"><pre><span class="hll"',
md.convert(text)
)
else:
self.assertEqual(
Expand Down Expand Up @@ -354,8 +363,9 @@ def testFencedLanguageAndHighlightLines(self):
]
)
if self.has_pygments:
self.assertTrue(
md.convert(text).startswith('<div class="codehilite"><pre><span class="hll"')
self.assertStartsWith(
'<div class="codehilite"><pre><span class="hll"',
md.convert(text)
)
else:
self.assertEqual(
Expand Down Expand Up @@ -602,7 +612,7 @@ def testRE(self):
self.assertEqual(RE.match(test).groups(), expected)


class TestTOC(unittest.TestCase):
class TestTOC(TestCaseWithAssertStartsWith):
""" Test TOC Extension. """

def setUp(self):
Expand Down Expand Up @@ -680,13 +690,13 @@ def testDisabledMarker(self):
'<h1 id="header-1">Header 1</h1>\n'
'<h2 id="header-2">Header 2</h2>'
)
self.assertTrue(md.toc.startswith('<div class="toc">'))
self.assertStartsWith('<div class="toc">', md.toc)

def testReset(self):
""" Test TOC Reset. """
self.assertEqual(self.md.toc, '')
self.md.convert('# Header 1\n\n## Header 2')
self.assertTrue(self.md.toc.startswith('<div class="toc">'))
self.assertStartsWith('<div class="toc">', self.md.toc)
self.md.reset()
self.assertEqual(self.md.toc, '')

Expand Down Expand Up @@ -771,7 +781,10 @@ def testTitle(self):
extensions=[markdown.extensions.toc.TocExtension(title='Table of Contents')]
)
md.convert('# Header 1\n\n## Header 2')
self.assertTrue(md.toc.startswith('<div class="toc"><span class="toctitle">Table of Contents</span><ul>'))
self.assertStartsWith(
'<div class="toc"><span class="toctitle">Table of Contents</span><ul>',
md.toc
)

def testWithAttrList(self):
""" Test TOC with attr_list Extension. """
Expand Down