'
)
)
else:
self.assertEqual(
md.convert(text),
'# A Code Comment'
' '
)
def testLinenumsNoneWithColon(self):
text = '\t:::Python\n\t# A Code Comment'
md = markdown.Markdown(
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=None)]
)
if self.has_pygments:
self.assertEqual(
md.convert(text),
''
)
else:
self.assertEqual(
md.convert(text),
'# A Code Comment'
' '
)
def testHighlightLinesWithColon(self):
# Test with hl_lines delimited by single or double quotes.
text0 = '\t:::Python hl_lines="2"\n\t#line 1\n\t#line 2\n\t#line 3'
text1 = "\t:::Python hl_lines='2'\n\t#line 1\n\t#line 2\n\t#line 3"
for text in (text0, text1):
md = markdown.Markdown(extensions=['markdown.extensions.codehilite'])
if self.has_pygments:
self.assertEqual(
md.convert(text),
''
'#line 1\n'
'#line 2\n'
'#line 3\n'
' '
)
else:
self.assertEqual(
md.convert(text),
''
'#line 1\n'
'#line 2\n'
'#line 3 '
)
class TestFencedCode(unittest.TestCase):
""" Test fenced_code extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['markdown.extensions.fenced_code'])
self.has_pygments = True
try:
import pygments # noqa
except ImportError:
self.has_pygments = False
def testBasicFence(self):
""" Test Fenced Code Blocks. """
text = '''
A paragraph before a fenced code block:
~~~
Fenced code block
~~~'''
self.assertEqual(
self.md.convert(text),
'A paragraph before a fenced code block: \n'
'Fenced code block\n'
' '
)
def testSafeFence(self):
""" Test Fenced Code with safe_mode. """
text = '~~~\nCode\n~~~'
self.md.safeMode = 'replace'
self.assertEqual(
self.md.convert(text),
'Code\n'
' '
)
def testNestedFence(self):
""" Test nested fence. """
text = '''
~~~~~~~~
~~~~
~~~~~~~~'''
self.assertEqual(
self.md.convert(text),
'\n'
'~~~~\n'
' '
)
def testFencedLanguage(self):
""" Test Language Tags. """
text = '''
~~~~{.python}
# Some python code
~~~~'''
self.assertEqual(
self.md.convert(text),
'# Some python code\n'
' '
)
def testFencedBackticks(self):
""" Test Code Fenced with Backticks. """
text = '''
`````
# Arbitrary code
~~~~~ # these tildes will not close the block
`````'''
self.assertEqual(
self.md.convert(text),
'# Arbitrary code\n'
'~~~~~ # these tildes will not close the block\n'
' '
)
def testFencedCodeWithHighlightLines(self):
""" Test Fenced Code with Highlighted Lines. """
text = '''
```hl_lines="1 3"
line 1
line 2
line 3
```'''
md = markdown.Markdown(
extensions=[
markdown.extensions.codehilite.CodeHiliteExtension(linenums=None, guess_lang=False),
'markdown.extensions.fenced_code'
]
)
if self.has_pygments:
self.assertEqual(
md.convert(text),
''
'line 1\n'
'line 2\n'
'line 3\n'
' '
)
else:
self.assertEqual(
md.convert(text),
'line 1\n'
'line 2\n'
'line 3 '
)
def testFencedLanguageAndHighlightLines(self):
""" Test Fenced Code with Highlighted Lines. """
text0 = '''
```.python hl_lines="1 3"
#line 1
#line 2
#line 3
```'''
text1 = '''
~~~{.python hl_lines='1 3'}
#line 1
#line 2
#line 3
~~~'''
for text in (text0, text1):
md = markdown.Markdown(
extensions=[
markdown.extensions.codehilite.CodeHiliteExtension(linenums=None, guess_lang=False),
'markdown.extensions.fenced_code'
]
)
if self.has_pygments:
self.assertEqual(
md.convert(text),
''
'#line 1\n'
'#line 2\n'
'#line 3\n'
' '
)
else:
self.assertEqual(
md.convert(text),
'#line 1\n'
'#line 2\n'
'#line 3 '
)
class TestHeaderId(unittest.TestCase):
""" Test HeaderId Extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['markdown.extensions.headerid'])
def testBasicHeaderId(self):
""" Test Basic HeaderID """
text = "# Some Header #"
self.assertEqual(
self.md.convert(text),
''
)
def testUniqueFunc(self):
""" Test 'unique' function. """
from markdown.extensions.headerid import unique
ids = set(['foo'])
self.assertEqual(unique('foo', ids), 'foo_1')
self.assertEqual(ids, set(['foo', 'foo_1']))
def testUniqueIds(self):
""" Test Unique IDs. """
text = '#Header\n#Header\n#Header'
self.assertEqual(
self.md.convert(text),
'\n'
'\n'
''
)
def testBaseLevel(self):
""" Test Header Base Level. """
text = '#Some Header\n## Next Level'
self.assertEqual(
markdown.markdown(text, [markdown.extensions.headerid.HeaderIdExtension(level=3)]),
'\n'
'Next Level'
)
def testHeaderInlineMarkup(self):
""" Test Header IDs with inline markup. """
text = '#Some *Header* with [markup](http://example.com).'
self.assertEqual(
self.md.convert(text),
''
)
def testHtmlEntities(self):
""" Test HeaderIDs with HTML Entities. """
text = '# Foo & bar'
self.assertEqual(
self.md.convert(text),
'Foo & bar'
)
def testRawHtml(self):
""" Test HeaderIDs with raw HTML. """
text = '# Foo Bar Baz.'
self.assertEqual(
self.md.convert(text),
'Foo Bar Baz.'
)
def testNoAutoIds(self):
""" Test HeaderIDs with no auto generated IDs. """
text = '# Some Header\n# Another Header'
self.assertEqual(
markdown.markdown(text, [markdown.extensions.headerid.HeaderIdExtension(forceid=False)]),
'Some Header\n'
'Another Header'
)
def testHeaderIdWithMetaData(self):
""" Test Header IDs with MetaData extension. """
text = '''header_level: 2
header_forceid: Off
# A Header'''
self.assertEqual(
markdown.markdown(text, ['markdown.extensions.headerid', 'markdown.extensions.meta']),
'A Header'
)
def testHeaderIdWithAttr_List(self):
""" Test HeaderIDs with Attr_List extension. """
text = '# Header1 {: #foo }\n# Header2 {: .bar }'
self.assertEqual(
markdown.markdown(text, ['markdown.extensions.headerid', 'markdown.extensions.attr_list']),
'Header1\n'
''
)
# Switch order extensions are loaded - should be no change in behavior.
self.assertEqual(
markdown.markdown(text, ['markdown.extensions.attr_list', 'markdown.extensions.headerid']),
'Header1\n'
''
)
class TestMetaData(unittest.TestCase):
""" Test MetaData extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['markdown.extensions.meta'])
def testBasicMetaData(self):
""" Test basic metadata. """
text = '''Title: A Test Doc.
Author: Waylan Limberg
John Doe
Blank_Data:
The body. This is paragraph one.'''
self.assertEqual(
self.md.convert(text),
'The body. This is paragraph one. '
)
self.assertEqual(
self.md.Meta, {
'author': ['Waylan Limberg', 'John Doe'],
'blank_data': [''],
'title': ['A Test Doc.']
}
)
def testYamlMetaData(self):
""" Test metadata specified as simple YAML. """
text = '''---
Title: A Test Doc.
Author: [Waylan Limberg, John Doe]
Blank_Data:
---
The body. This is paragraph one.'''
self.assertEqual(
self.md.convert(text),
'The body. This is paragraph one. '
)
self.assertEqual(
self.md.Meta, {
'author': ['[Waylan Limberg, John Doe]'],
'blank_data': [''],
'title': ['A Test Doc.']
}
)
def testMissingMetaData(self):
""" Test document without Meta Data. """
text = ' Some Code - not extra lines of meta data.'
self.assertEqual(
self.md.convert(text),
'Some Code - not extra lines of meta data.\n'
' '
)
self.assertEqual(self.md.Meta, {})
def testMetaDataWithoutNewline(self):
""" Test doocument with only metadata and no newline at end."""
text = 'title: No newline'
self.assertEqual(self.md.convert(text), '')
self.assertEqual(self.md.Meta, {'title': ['No newline']})
def testYamlObjectMetaData(self):
""" Test metadata specified as a complex YAML object. """
md = markdown.Markdown(extensions=[markdown.extensions.meta.MetaExtension(yaml=True)])
text = '''---
Author: John Doe
Date: 2014-11-29 14:15:16
Integer: 0x16
---
Some content.'''
self.assertEqual(md.convert(text), 'Some content. ')
self.assertEqual(
md.Meta, {
'Author': 'John Doe',
'Date': datetime.datetime(2014, 11, 29, 14, 15, 16),
'Integer': 22
}
)
class TestWikiLinks(unittest.TestCase):
""" Test Wikilinks Extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['markdown.extensions.wikilinks'])
self.text = "Some text with a [[WikiLink]]."
def testBasicWikilinks(self):
""" Test [[wikilinks]]. """
self.assertEqual(
self.md.convert(self.text),
'Some text with a '
'WikiLink. '
)
def testWikilinkWhitespace(self):
""" Test whitespace in wikilinks. """
self.assertEqual(
self.md.convert('[[ foo bar_baz ]]'),
'foo bar_baz '
)
self.assertEqual(
self.md.convert('foo [[ ]] bar'),
'foo bar '
)
def testSimpleSettings(self):
""" Test Simple Settings. """
self.assertEqual(markdown.markdown(
self.text, [
markdown.extensions.wikilinks.WikiLinkExtension(
base_url='/wiki/',
end_url='.html',
html_class='foo')
]
),
'Some text with a '
'WikiLink. ')
def testComplexSettings(self):
""" Test Complex Settings. """
md = markdown.Markdown(
extensions=['markdown.extensions.wikilinks'],
extension_configs={
'markdown.extensions.wikilinks': [
('base_url', 'http://example.com/'),
('end_url', '.html'),
('html_class', '')
]
},
safe_mode=True
)
self.assertEqual(
md.convert(self.text),
'Some text with a '
'WikiLink. '
)
def testWikilinksMetaData(self):
""" test MetaData with Wikilinks Extension. """
text = """wiki_base_url: http://example.com/
wiki_end_url: .html
wiki_html_class:
Some text with a [[WikiLink]]."""
md = markdown.Markdown(extensions=['markdown.extensions.meta', 'markdown.extensions.wikilinks'])
self.assertEqual(
md.convert(text),
'Some text with a '
'WikiLink. '
)
# MetaData should not carry over to next document:
self.assertEqual(
md.convert("No [[MetaData]] here."),
'No MetaData '
'here. '
)
def testURLCallback(self):
""" Test used of a custom URL builder. """
from markdown.extensions.wikilinks import WikiLinkExtension
def my_url_builder(label, base, end):
return '/bar/'
md = markdown.Markdown(extensions=[WikiLinkExtension(build_url=my_url_builder)])
self.assertEqual(
md.convert('[[foo]]'),
'foo '
)
class TestAdmonition(unittest.TestCase):
""" Test Admonition Extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['markdown.extensions.admonition'])
def testRE(self):
RE = self.md.parser.blockprocessors['admonition'].RE
tests = [
('!!! note', ('note', None)),
('!!! note "Please Note"', ('note', 'Please Note')),
('!!! note ""', ('note', '')),
]
for test, expected in tests:
self.assertEqual(RE.match(test).groups(), expected)
class TestTOC(unittest.TestCase):
""" Test TOC Extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['markdown.extensions.toc'])
def testMarker(self):
""" Test TOC with a Marker. """
text = '[TOC]\n\n# Header 1\n\n## Header 2'
self.assertEqual(
self.md.convert(text),
'\n'
' \n' # noqa
'- Header 1' # noqa
'\n' # noqa
'
\n' # noqa
' \n' # noqa
' \n'
'\n'
''
)
def testNoMarker(self):
""" Test TOC without a Marker. """
text = '# Header 1\n\n## Header 2'
self.assertEqual(
self.md.convert(text),
'\n'
''
)
self.assertEqual(
self.md.toc,
'\n'
' \n' # noqa
'- Header 1' # noqa
'\n' # noqa
'
\n' # noqa
' \n' # noqa
' \n'
)
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(''))
self.md.reset()
self.assertEqual(self.md.toc, '')
def testAnchorLink(self):
""" Test TOC Anchorlink. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(anchorlink=True)]
)
text = '# Header 1\n\n## Header *2*'
self.assertEqual(
md.convert(text),
'\n'
''
)
def testTitle(self):
""" Test TOC Title. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(title='Table of Contents')]
)
md.convert('# Header 1\n\n## Header 2')
self.assertTrue(md.toc.startswith(' Table of Contents'))
def testWithAttrList(self):
""" Test TOC with attr_list Extension. """
md = markdown.Markdown(extensions=['markdown.extensions.toc', 'markdown.extensions.attr_list'])
text = '# Header 1\n\n## Header 2 { #foo }'
self.assertEqual(
md.convert(text),
'\n'
'Header 2'
)
self.assertEqual(
md.toc,
'\n'
' \n' # noqa
'- Header 1' # noqa
'\n' # noqa
'
\n' # noqa
' \n' # noqa
' \n'
)
class TestSmarty(unittest.TestCase):
def setUp(self):
config = {
'markdown.extensions.smarty': [
('smart_angled_quotes', True),
('substitutions', {
'ndash': '\u2013',
'mdash': '\u2014',
'ellipsis': '\u2026',
'left-single-quote': '‚', # sb is not a typo!
'right-single-quote': '‘',
'left-double-quote': '„',
'right-double-quote': '“',
'left-angle-quote': '[',
'right-angle-quote': ']',
}),
]
}
self.md = markdown.Markdown(
extensions=['markdown.extensions.smarty'],
extension_configs=config
)
def testCustomSubstitutions(self):
text = """<< The "Unicode char of the year 2014"
is the 'mdash': ---
Must not be confused with 'ndash' (--) ... >>
"""
correct = """[ The „Unicode char of the year 2014“
is the ‚mdash‘: \u2014
Must not be confused with ‚ndash‘ (\u2013) \u2026 ] """
self.assertEqual(self.md.convert(text), correct)
|