Skip to content
This repository was archived by the owner on Dec 17, 2019. It is now read-only.

Commit ede69ae

Browse files
committed
Complete test coverage of TOC Extension
1 parent 5ad00fa commit ede69ae

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

markdown/extensions/toc.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def order_toc_list(toc_list):
5959
for p in reversed(parents):
6060
if current_level <= p['level']:
6161
to_pop += 1
62-
else:
62+
else: # pragma: no cover
6363
break
6464
if to_pop:
6565
levels = levels[:-to_pop]
@@ -153,7 +153,6 @@ def run(self, doc):
153153
used_ids.add(c.attrib["id"])
154154

155155
toc_list = []
156-
marker_found = False
157156
for (p, c) in self.iterparent(doc):
158157
text = ''.join(itertext(c)).strip()
159158
if not text:
@@ -171,7 +170,6 @@ def run(self, doc):
171170
if p[i] == c:
172171
p[i] = div
173172
break
174-
marker_found = True
175173

176174
if header_rgx.match(c.tag):
177175

@@ -223,12 +221,12 @@ def __init__(self, *args, **kwargs):
223221
"title": ["",
224222
"Title to insert into TOC <div> - "
225223
"Defaults to an empty string"],
226-
"anchorlink": [0,
227-
"1 if header should be a self link - "
228-
"Defaults to 0"],
224+
"anchorlink": [False,
225+
"True if header should be a self link - "
226+
"Defaults to False"],
229227
"permalink": [0,
230-
"1 or link text if a Sphinx-style permalink should "
231-
"be added - Defaults to 0"]
228+
"True or link text if a Sphinx-style permalink should "
229+
"be added - Defaults to False"]
232230
}
233231

234232
super(TocExtension, self).__init__(*args, **kwargs)

tests/test_extensions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,48 @@ def testReset(self):
741741
self.md.reset()
742742
self.assertEqual(self.md.toc, '')
743743

744+
def testAnchorLink(self):
745+
""" Test TOC Anchorlink. """
746+
md = markdown.Markdown(
747+
extensions=[markdown.extensions.toc.TocExtension(anchorlink=True)]
748+
)
749+
text = '# Header 1\n\n## Header *2*'
750+
self.assertEqual(
751+
md.convert(text),
752+
'<h1 id="header-1"><a class="toclink" href="#header-1">Header 1</a></h1>\n'
753+
'<h2 id="header-2"><a class="toclink" href="#header-2">Header <em>2</em></a></h2>'
754+
)
755+
756+
def testTitle(self):
757+
""" Test TOC Title. """
758+
md = markdown.Markdown(
759+
extensions=[markdown.extensions.toc.TocExtension(title='Table of Contents')]
760+
)
761+
md.convert('# Header 1\n\n## Header 2')
762+
self.assertTrue(md.toc.startswith('<div class="toc"><span class="toctitle">Table of Contents</span><ul>'))
763+
764+
def testWithAttrList(self):
765+
""" Test TOC with attr_list Extension. """
766+
md = markdown.Markdown(extensions=['markdown.extensions.toc', 'markdown.extensions.attr_list'])
767+
text = '# Header 1\n\n## Header 2 { #foo }'
768+
self.assertEqual(
769+
md.convert(text),
770+
'<h1 id="header-1">Header 1</h1>\n'
771+
'<h2 id="foo">Header 2</h2>'
772+
)
773+
self.assertEqual(
774+
md.toc,
775+
'<div class="toc">\n'
776+
'<ul>\n' # noqa
777+
'<li><a href="#header-1">Header 1</a>' # noqa
778+
'<ul>\n' # noqa
779+
'<li><a href="#foo">Header 2</a></li>\n' # noqa
780+
'</ul>\n' # noqa
781+
'</li>\n' # noqa
782+
'</ul>\n' # noqa
783+
'</div>\n'
784+
)
785+
744786

745787
class TestSmarty(unittest.TestCase):
746788
def setUp(self):

0 commit comments

Comments
 (0)