1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
"""
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-2018 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).
Python-Markdown Extension Regression Tests
==========================================
A collection of regression tests to confirm that the included extensions
continue to work as advertised. This used to be accomplished by doctests.
"""
import unittest
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 = '{} not found at the start of {}'.format(repr(expectedPrefix),
repr(text))
self.fail(self._formatMessage(msg, standardMsg))
class TestExtensionClass(unittest.TestCase):
""" Test markdown.extensions.Extension. """
def setUp(self):
class TestExtension(markdown.extensions.Extension):
config = {
'foo': ['bar', 'Description of foo'],
'bar': ['baz', 'Description of bar']
}
self.ext = TestExtension()
self.ExtKlass = TestExtension
def testGetConfig(self):
self.assertEqual(self.ext.getConfig('foo'), 'bar')
def testGetConfigDefault(self):
self.assertEqual(self.ext.getConfig('baz'), '')
self.assertEqual(self.ext.getConfig('baz', default='missing'), 'missing')
def testGetConfigs(self):
self.assertEqual(self.ext.getConfigs(), {'foo': 'bar', 'bar': 'baz'})
def testGetConfigInfo(self):
self.assertEqual(
dict(self.ext.getConfigInfo()),
dict([
('foo', 'Description of foo'),
('bar', 'Description of bar')
])
)
def testSetConfig(self):
self.ext.setConfig('foo', 'baz')
self.assertEqual(self.ext.getConfigs(), {'foo': 'baz', 'bar': 'baz'})
def testSetConfigWithBadKey(self):
# self.ext.setConfig('bad', 'baz) ==> KeyError
self.assertRaises(KeyError, self.ext.setConfig, 'bad', 'baz')
def testConfigAsKwargsOnInit(self):
ext = self.ExtKlass(foo='baz', bar='blah')
self.assertEqual(ext.getConfigs(), {'foo': 'baz', 'bar': 'blah'})
class TestAbbr(unittest.TestCase):
""" Test abbr extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['abbr'])
def testSimpleAbbr(self):
""" Test Abbreviations. """
text = 'Some text with an ABBR and a REF. Ignore REFERENCE and ref.' + \
'\n\n*[ABBR]: Abbreviation\n' + \
'*[REF]: Abbreviation Reference'
self.assertEqual(
self.md.convert(text),
'<p>Some text with an <abbr title="Abbreviation">ABBR</abbr> '
'and a <abbr title="Abbreviation Reference">REF</abbr>. Ignore '
'REFERENCE and ref.</p>'
)
def testNestedAbbr(self):
""" Test Nested Abbreviations. """
text = '[ABBR](/foo) and _ABBR_\n\n' + \
'*[ABBR]: Abbreviation'
self.assertEqual(
self.md.convert(text),
'<p><a href="/foo"><abbr title="Abbreviation">ABBR</abbr></a> '
'and <em><abbr title="Abbreviation">ABBR</abbr></em></p>'
)
class TestMetaData(unittest.TestCase):
""" Test MetaData extension. """
def setUp(self):
self.md = markdown.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),
'<p>The body. This is paragraph one.</p>'
)
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),
'<p>The body. This is paragraph one.</p>'
)
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),
'<pre><code>Some Code - not extra lines of meta data.\n'
'</code></pre>'
)
self.assertEqual(self.md.Meta, {})
def testMetaDataWithoutNewline(self):
""" Test document 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 testMetaDataReset(self):
""" Test that reset call remove Meta entirely """
text = '''Title: A Test Doc.
Author: Waylan Limberg
John Doe
Blank_Data:
The body. This is paragraph one.'''
self.md.convert(text)
self.md.reset()
self.assertEqual(self.md.Meta, {})
class TestWikiLinks(unittest.TestCase):
""" Test Wikilinks Extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['wikilinks'])
self.text = "Some text with a [[WikiLink]]."
def testBasicWikilinks(self):
""" Test [[wikilinks]]. """
self.assertEqual(
self.md.convert(self.text),
'<p>Some text with a '
'<a class="wikilink" href="/WikiLink/">WikiLink</a>.</p>'
)
def testWikilinkWhitespace(self):
""" Test whitespace in wikilinks. """
self.assertEqual(
self.md.convert('[[ foo bar_baz ]]'),
'<p><a class="wikilink" href="/foo_bar_baz/">foo bar_baz</a></p>'
)
self.assertEqual(
self.md.convert('foo [[ ]] bar'),
'<p>foo bar</p>'
)
def testSimpleSettings(self):
""" Test Simple Settings. """
self.assertEqual(markdown.markdown(
self.text, extensions=[
markdown.extensions.wikilinks.WikiLinkExtension(
base_url='/wiki/',
end_url='.html',
html_class='foo')
]
),
'<p>Some text with a '
'<a class="foo" href="/wiki/WikiLink.html">WikiLink</a>.</p>')
def testComplexSettings(self):
""" Test Complex Settings. """
md = markdown.Markdown(
extensions=['wikilinks'],
extension_configs={
'wikilinks': [
('base_url', 'http://example.com/'),
('end_url', '.html'),
('html_class', '')
]
},
safe_mode=True
)
self.assertEqual(
md.convert(self.text),
'<p>Some text with a '
'<a href="http://example.com/WikiLink.html">WikiLink</a>.</p>'
)
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=['meta', 'wikilinks'])
self.assertEqual(
md.convert(text),
'<p>Some text with a '
'<a href="http://example.com/WikiLink.html">WikiLink</a>.</p>'
)
# MetaData should not carry over to next document:
self.assertEqual(
md.convert("No [[MetaData]] here."),
'<p>No <a class="wikilink" href="/MetaData/">MetaData</a> '
'here.</p>'
)
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]]'),
'<p><a class="wikilink" href="/bar/">foo</a></p>'
)
class TestAdmonition(unittest.TestCase):
""" Test Admonition Extension. """
def setUp(self):
self.md = markdown.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(TestCaseWithAssertStartsWith):
""" Test TOC Extension. """
def setUp(self):
self.md = markdown.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),
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#header-1">Header 1</a>' # noqa
'<ul>\n' # noqa
'<li><a href="#header-2">Header 2</a></li>\n' # noqa
'</ul>\n' # noqa
'</li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
'<h1 id="header-1">Header 1</h1>\n'
'<h2 id="header-2">Header 2</h2>'
)
def testNoMarker(self):
""" Test TOC without a Marker. """
text = '# Header 1\n\n## Header 2'
self.assertEqual(
self.md.convert(text),
'<h1 id="header-1">Header 1</h1>\n'
'<h2 id="header-2">Header 2</h2>'
)
self.assertEqual(
self.md.toc,
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#header-1">Header 1</a>' # noqa
'<ul>\n' # noqa
'<li><a href="#header-2">Header 2</a></li>\n' # noqa
'</ul>\n' # noqa
'</li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
)
def testAlternateMarker(self):
""" Test TOC with user defined marker. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(marker='{{marker}}')]
)
text = '{{marker}}\n\n# Header 1\n\n## Header 2'
self.assertEqual(
md.convert(text),
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#header-1">Header 1</a>' # noqa
'<ul>\n' # noqa
'<li><a href="#header-2">Header 2</a></li>\n' # noqa
'</ul>\n' # noqa
'</li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
'<h1 id="header-1">Header 1</h1>\n'
'<h2 id="header-2">Header 2</h2>'
)
def testDisabledMarker(self):
""" Test TOC with disabled marker. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(marker='')]
)
text = '[TOC]\n\n# Header 1\n\n## Header 2'
self.assertEqual(
md.convert(text),
'<p>[TOC]</p>\n'
'<h1 id="header-1">Header 1</h1>\n'
'<h2 id="header-2">Header 2</h2>'
)
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.assertStartsWith('<div class="toc">', self.md.toc)
self.md.reset()
self.assertEqual(self.md.toc, '')
self.assertEqual(self.md.toc_tokens, [])
def testUniqueIds(self):
""" Test Unique IDs. """
text = '#Header\n#Header\n#Header'
self.assertEqual(
self.md.convert(text),
'<h1 id="header">Header</h1>\n'
'<h1 id="header_1">Header</h1>\n'
'<h1 id="header_2">Header</h1>'
)
self.assertEqual(
self.md.toc,
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#header">Header</a></li>\n' # noqa
'<li><a href="#header_1">Header</a></li>\n' # noqa
'<li><a href="#header_2">Header</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
)
self.assertEqual(self.md.toc_tokens, [
{'level': 1, 'id': 'header', 'name': 'Header', 'children': []},
{'level': 1, 'id': 'header_1', 'name': 'Header', 'children': []},
{'level': 1, 'id': 'header_2', 'name': 'Header', 'children': []},
])
def testHtmlEntities(self):
""" Test Headers with HTML Entities. """
text = '# Foo & bar'
self.assertEqual(
self.md.convert(text),
'<h1 id="foo-bar">Foo & bar</h1>'
)
self.assertEqual(
self.md.toc,
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#foo-bar">Foo & bar</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
)
self.assertEqual(self.md.toc_tokens, [
{'level': 1, 'id': 'foo-bar', 'name': 'Foo & bar', 'children': []},
])
def testHtmlSpecialChars(self):
""" Test Headers with HTML special characters. """
text = '# Foo > & bar'
self.assertEqual(
self.md.convert(text),
'<h1 id="foo-bar">Foo > & bar</h1>'
)
self.assertEqual(
self.md.toc,
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#foo-bar">Foo > & bar</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
)
self.assertEqual(self.md.toc_tokens, [
{'level': 1, 'id': 'foo-bar', 'name': 'Foo > & bar', 'children': []},
])
def testRawHtml(self):
""" Test Headers with raw HTML. """
text = '# Foo <b>Bar</b> Baz.'
self.assertEqual(
self.md.convert(text),
'<h1 id="foo-bar-baz">Foo <b>Bar</b> Baz.</h1>'
)
self.assertEqual(
self.md.toc,
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#foo-bar-baz">Foo Bar Baz.</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
)
self.assertEqual(self.md.toc_tokens, [
{'level': 1, 'id': 'foo-bar-baz', 'name': 'Foo Bar Baz.', 'children': []},
])
def testBaseLevel(self):
""" Test Header Base Level. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(baselevel=5)]
)
text = '# Some Header\n\n## Next Level\n\n### Too High'
self.assertEqual(
md.convert(text),
'<h5 id="some-header">Some Header</h5>\n'
'<h6 id="next-level">Next Level</h6>\n'
'<h6 id="too-high">Too High</h6>'
)
self.assertEqual(
md.toc,
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#some-header">Some Header</a>' # noqa
'<ul>\n' # noqa
'<li><a href="#next-level">Next Level</a></li>\n' # noqa
'<li><a href="#too-high">Too High</a></li>\n' # noqa
'</ul>\n' # noqa
'</li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
)
self.assertEqual(md.toc_tokens, [
{'level': 5, 'id': 'some-header', 'name': 'Some Header', 'children': [
{'level': 6, 'id': 'next-level', 'name': 'Next Level', 'children': []},
{'level': 6, 'id': 'too-high', 'name': 'Too High', 'children': []},
]},
])
def testHeaderInlineMarkup(self):
""" Test Headers with inline markup. """
text = '#Some *Header* with [markup](http://example.com).'
self.assertEqual(
self.md.convert(text),
'<h1 id="some-header-with-markup">Some <em>Header</em> with '
'<a href="http://example.com">markup</a>.</h1>'
)
self.assertEqual(
self.md.toc,
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#some-header-with-markup">' # noqa
'Some Header with markup.</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
)
self.assertEqual(self.md.toc_tokens, [
{'level': 1, 'id': 'some-header-with-markup', 'name': 'Some Header with markup.', 'children': []},
])
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.assertStartsWith(
'<div class="toc"><span class="toctitle">Table of Contents</span><ul>',
md.toc
)
def testWithAttrList(self):
""" Test TOC with attr_list Extension. """
md = markdown.Markdown(extensions=['toc', 'attr_list'])
text = ('# Header 1\n\n'
'## Header 2 { #foo }\n\n'
'## Header 3 { data-toc-label="Foo Bar" }\n\n'
'# Header 4 { data-toc-label="Foo > Baz" }\n\n'
'# Header 5 { data-toc-label="Foo <b>Quux</b>" }')
self.assertEqual(
md.convert(text),
'<h1 id="header-1">Header 1</h1>\n'
'<h2 id="foo">Header 2</h2>\n'
'<h2 id="header-3">Header 3</h2>\n'
'<h1 id="header-4">Header 4</h1>\n'
'<h1 id="header-5">Header 5</h1>'
)
self.assertEqual(
md.toc,
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#header-1">Header 1</a>' # noqa
'<ul>\n' # noqa
'<li><a href="#foo">Header 2</a></li>\n' # noqa
'<li><a href="#header-3">Foo Bar</a></li>\n' # noqa
'</ul>\n' # noqa
'</li>\n' # noqa
'<li><a href="#header-4">Foo > Baz</a></li>\n' # noqa
'<li><a href="#header-5">Foo Quux</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
)
self.assertEqual(md.toc_tokens, [
{'level': 1, 'id': 'header-1', 'name': 'Header 1', 'children': [
{'level': 2, 'id': 'foo', 'name': 'Header 2', 'children': []},
{'level': 2, 'id': 'header-3', 'name': 'Foo Bar', 'children': []}
]},
{'level': 1, 'id': 'header-4', 'name': 'Foo > Baz', 'children': []},
{'level': 1, 'id': 'header-5', 'name': 'Foo Quux', 'children': []},
])
def testUniqueFunc(self):
""" Test 'unique' function. """
from markdown.extensions.toc import unique
ids = {'foo'}
self.assertEqual(unique('foo', ids), 'foo_1')
self.assertEqual(ids, {'foo', 'foo_1'})
def testTocInHeaders(self):
text = '[TOC]\n#[TOC]'
self.assertEqual(
self.md.convert(text),
'<div class="toc">\n' # noqa
'<ul>\n' # noqa
'<li><a href="#toc">[TOC]</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n' # noqa
'<h1 id="toc">[TOC]</h1>' # noqa
)
text = '#[TOC]\n[TOC]'
self.assertEqual(
self.md.convert(text),
'<h1 id="toc">[TOC]</h1>\n' # noqa
'<div class="toc">\n' # noqa
'<ul>\n' # noqa
'<li><a href="#toc">[TOC]</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>' # noqa
)
text = '[TOC]\n# *[TOC]*'
self.assertEqual(
self.md.convert(text),
'<div class="toc">\n' # noqa
'<ul>\n' # noqa
'<li><a href="#toc">[TOC]</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n' # noqa
'<h1 id="toc"><em>[TOC]</em></h1>' # noqa
)
class TestSmarty(unittest.TestCase):
def setUp(self):
config = {
'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=['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 = """<p>[ The „Unicode char of the year 2014“
is the ‚mdash‘: \u2014
Must not be confused with ‚ndash‘ (\u2013) \u2026 ]</p>"""
self.assertEqual(self.md.convert(text), correct)
|