"""
Python Markdown
A Python implementation of John Gruber's Markdown.
Documentation: https://python-markdown.github.io/
GitHub: https://github.com/Python-Markdown/markdown/
PyPI: https://pypi.org/project/Markdown/
Started by Manfred Stienstra (http://www.dwerg.net/).
Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
Currently maintained by Waylan Limberg (https://github.com/waylan),
Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).
Copyright 2007-2019 The Python Markdown Project (v. 1.7 and later)
Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
Copyright 2004 Manfred Stienstra (the original version)
License: BSD (see LICENSE.md for details).
"""
from markdown.test_tools import TestCase
from markdown.extensions.toc import TocExtension
from markdown.extensions.nl2br import Nl2BrExtension
class TestTOC(TestCase):
maxDiff = None
# TODO: Move the rest of the TOC tests here.
def testAnchorLink(self):
self.assertMarkdownRenders(
self.dedent(
'''
# Header 1
## Header *2*
'''
),
self.dedent(
'''
'''
),
extensions=[TocExtension(anchorlink=True)]
)
def testAnchorLinkWithSingleInlineCode(self):
self.assertMarkdownRenders(
'# This is `code`.',
'', # noqa
extensions=[TocExtension(anchorlink=True)]
)
def testAnchorLinkWithDoubleInlineCode(self):
self.assertMarkdownRenders(
'# This is `code` and `this` too.',
'', # noqa
extensions=[TocExtension(anchorlink=True)]
)
def testPermalink(self):
self.assertMarkdownRenders(
'# Header',
'', # noqa
extensions=[TocExtension(permalink=True)]
)
def testPermalinkWithSingleInlineCode(self):
self.assertMarkdownRenders(
'# This is `code`.',
'' # noqa
'This is code
.' # noqa
'' # noqa
'
', # noqa
extensions=[TocExtension(permalink=True)]
)
def testPermalinkWithDoubleInlineCode(self):
self.assertMarkdownRenders(
'# This is `code` and `this` too.',
'' # noqa
'This is code
and this
too.' # noqa
'' # noqa
'
', # noqa
extensions=[TocExtension(permalink=True)]
)
def testMinMaxLevel(self):
self.assertMarkdownRenders(
self.dedent(
'''
# Header 1 not in TOC
## Header 2 not in TOC
### Header 3
#### Header 4
##### Header 5 not in TOC
'''
),
self.dedent(
'''
'''
),
expected_attrs={
'toc': (
'\n'
'
\n' # noqa
'- Header 3' # noqa
'\n' # noqa
'
\n' # noqa
'
\n' # noqa
'
\n' # noqa
),
'toc_tokens': [
{
'level': 3,
'id': 'header-3',
'name': 'Header 3',
'children': [
{
'level': 4,
'id': 'header-4',
'name': 'Header 4',
'children': []
}
]
}
]
},
extensions=[TocExtension(toc_depth='3-4')]
)
def testMaxLevel(self):
self.assertMarkdownRenders(
self.dedent(
'''
# Header 1
## Header 2
### Header 3 not in TOC
'''
),
self.dedent(
'''
'''
),
expected_attrs={
'toc': (
'\n'
'
\n' # noqa
'- Header 1' # noqa
'\n' # noqa
'
\n' # noqa
'
\n' # noqa
'
\n' # noqa
),
'toc_tokens': [
{
'level': 1,
'id': 'header-1',
'name': 'Header 1',
'children': [
{
'level': 2,
'id': 'header-2',
'name': 'Header 2',
'children': []
}
]
}
]
},
extensions=[TocExtension(toc_depth=2)]
)
def testMinMaxLevelwithAnchorLink(self):
self.assertMarkdownRenders(
self.dedent(
'''
# Header 1 not in TOC
## Header 2 not in TOC
### Header 3
#### Header 4
##### Header 5 not in TOC
'''
),
'\n' # noqa
'\n' # noqa
'\n' # noqa
'\n' # noqa
'', # noqa
expected_attrs={
'toc': (
'\n'
'
\n' # noqa
'- Header 3' # noqa
'\n' # noqa
'
\n' # noqa
'
\n' # noqa
'
\n' # noqa
),
'toc_tokens': [
{
'level': 3,
'id': 'header-3',
'name': 'Header 3',
'children': [
{
'level': 4,
'id': 'header-4',
'name': 'Header 4',
'children': []
}
]
}
]
},
extensions=[TocExtension(toc_depth='3-4', anchorlink=True)]
)
def testMinMaxLevelwithPermalink(self):
self.assertMarkdownRenders(
self.dedent(
'''
# Header 1 not in TOC
## Header 2 not in TOC
### Header 3
#### Header 4
##### Header 5 not in TOC
'''
),
'\n' # noqa
'\n' # noqa
'\n' # noqa
'\n' # noqa
'', # noqa
expected_attrs={
'toc': (
'\n'
'
\n' # noqa
'- Header 3' # noqa
'\n' # noqa
'
\n' # noqa
'
\n' # noqa
'
\n' # noqa
),
'toc_tokens': [
{
'level': 3,
'id': 'header-3',
'name': 'Header 3',
'children': [
{
'level': 4,
'id': 'header-4',
'name': 'Header 4',
'children': []
}
]
}
]
},
extensions=[TocExtension(toc_depth='3-4', permalink=True)]
)
def testMinMaxLevelwithBaseLevel(self):
self.assertMarkdownRenders(
self.dedent(
'''
# First Header
## Second Level
### Third Level
#### Forth Level
'''
),
self.dedent(
'''
Second Level
Third Level
Forth Level
'''
),
expected_attrs={
'toc': (
'\n' # noqa
),
'toc_tokens': [
{
'level': 4,
'id': 'second-level',
'name': 'Second Level',
'children': [
{
'level': 5,
'id': 'third-level',
'name': 'Third Level',
'children': []
}
]
}
]
},
extensions=[TocExtension(toc_depth='4-5', baselevel=3)]
)
def testMaxLevelwithBaseLevel(self):
self.assertMarkdownRenders(
self.dedent(
'''
# Some Header
## Next Level
### Too High
'''
),
self.dedent(
'''
Next Level
Too High
'''
),
expected_attrs={
'toc': (
'\n'
'
\n' # noqa
'- Some Header' # noqa
'\n' # noqa
'
\n' # noqa
'
\n' # noqa
'
\n' # noqa
),
'toc_tokens': [
{
'level': 2,
'id': 'some-header',
'name': 'Some Header',
'children': [
{
'level': 3,
'id': 'next-level',
'name': 'Next Level',
'children': []
}
]
}
]
},
extensions=[TocExtension(toc_depth=3, baselevel=2)]
)
def test_escaped_code(self):
self.assertMarkdownRenders(
self.dedent(
'''
[TOC]
# ``
'''
),
self.dedent(
'''
<test>
'''
),
extensions=['toc']
)
def test_escaped_char_in_id(self):
self.assertMarkdownRenders(
r'# escaped\_character',
'escaped_character
',
extensions=['toc']
)
def testAnchorLinkWithCustomClass(self):
self.assertMarkdownRenders(
self.dedent(
'''
# Header 1
## Header *2*
'''
),
self.dedent(
'''
'''
),
extensions=[TocExtension(anchorlink=True, anchorlink_class="custom")]
)
def testAnchorLinkWithCustomClasses(self):
self.assertMarkdownRenders(
self.dedent(
'''
# Header 1
## Header *2*
'''
),
self.dedent(
'''
'''
),
extensions=[TocExtension(anchorlink=True, anchorlink_class="custom1 custom2")]
)
def testPermalinkWithEmptyText(self):
self.assertMarkdownRenders(
'# Header',
'', # noqa
extensions=[TocExtension(permalink="")]
)
def testPermalinkWithCustomClass(self):
self.assertMarkdownRenders(
'# Header',
'', # noqa
extensions=[TocExtension(permalink=True, permalink_class="custom")]
)
def testPermalinkWithCustomClasses(self):
self.assertMarkdownRenders(
'# Header',
'', # noqa
extensions=[TocExtension(permalink=True, permalink_class="custom1 custom2")]
)
def testPermalinkWithCustomTitle(self):
self.assertMarkdownRenders(
'# Header',
'', # noqa
extensions=[TocExtension(permalink=True, permalink_title="custom")]
)
def testPermalinkWithEmptyTitle(self):
self.assertMarkdownRenders(
'# Header',
'', # noqa
extensions=[TocExtension(permalink=True, permalink_title="")]
)
def testPermalinkWithUnicodeInID(self):
from markdown.extensions.toc import slugify_unicode
self.assertMarkdownRenders(
'# Unicode ヘッダー',
'' # noqa
'Unicode ヘッダー' # noqa
'' # noqa
'
', # noqa
extensions=[TocExtension(permalink=True, slugify=slugify_unicode)]
)
def testPermalinkWithUnicodeTitle(self):
from markdown.extensions.toc import slugify_unicode
self.assertMarkdownRenders(
'# Unicode ヘッダー',
'' # noqa
'Unicode ヘッダー' # noqa
'' # noqa
'
', # noqa
extensions=[TocExtension(permalink=True, permalink_title="パーマリンク", slugify=slugify_unicode)]
)
def testPermalinkWithExtendedLatinInID(self):
self.assertMarkdownRenders(
'# Théâtre',
'' # noqa
'Théâtre' # noqa
'' # noqa
'
', # noqa
extensions=[TocExtension(permalink=True)]
)
def testNl2brCompatibility(self):
self.assertMarkdownRenders(
'[TOC]\ntext',
'[TOC]
\ntext
',
extensions=[TocExtension(), Nl2BrExtension()]
)
def testTOCWithCustomClass(self):
self.assertMarkdownRenders(
self.dedent(
'''
[TOC]
# Header
'''
),
self.dedent(
'''
'''
),
extensions=[TocExtension(toc_class="custom")]
)
def testTOCWithCustomClasses(self):
self.assertMarkdownRenders(
self.dedent(
'''
[TOC]
# Header
'''
),
self.dedent(
'''
'''
),
extensions=[TocExtension(toc_class="custom1 custom2")]
)