Debian Codegraph @

tests/test_syntax/extensions/test_toc.py

raw
1 """
2 Python Markdown
3
4 A Python implementation of John Gruber's Markdown.
5
6 Documentation: https://python-markdown.github.io/
7 GitHub: https://github.com/Python-Markdown/markdown/
8 PyPI: https://pypi.org/project/Markdown/
9
10 Started by Manfred Stienstra (http://www.dwerg.net/).
11 Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
12 Currently maintained by Waylan Limberg (https://github.com/waylan),
13 Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).
14
15 Copyright 2007-2019 The Python Markdown Project (v. 1.7 and later)
16 Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
17 Copyright 2004 Manfred Stienstra (the original version)
18
19 License: BSD (see LICENSE.md for details).
20 """
21
22 from markdown.test_tools import TestCase
23 from markdown import Markdown
24 from markdown.extensions.toc import TocExtension, strip_tags, unique
25 from markdown.extensions.nl2br import Nl2BrExtension
26
27
28 class TestTOC(TestCase):
29 maxDiff = None
30 default_kwargs = {
31 'extensions': [TocExtension()]
32 }
33
34 def testTOCMarker(self):
35 self.assertMarkdownRenders(
36 self.dedent(
37 '''
38 [TOC]
39
40 # Header 1
41
42 ## Header 2
43 '''
44 ),
45 '<div class="toc">\n'
46 '<ul>\n' # noqa
47 '<li><a href="#header-1">Header 1</a>' # noqa
48 '<ul>\n' # noqa
49 '<li><a href="#header-2">Header 2</a></li>\n' # noqa
50 '</ul>\n' # noqa
51 '</li>\n' # noqa
52 '</ul>\n' # noqa
53 '</div>\n'
54 '<h1 id="header-1">Header 1</h1>\n'
55 '<h2 id="header-2">Header 2</h2>'
56 )
57
58 def testNoTOCMarker(self):
59 self.assertMarkdownRenders(
60 self.dedent(
61 '''
62 # Header 1
63
64 ## Header 2
65 '''
66 ),
67 self.dedent(
68 '''
69 <h1 id="header-1">Header 1</h1>
70 <h2 id="header-2">Header 2</h2>
71 '''
72 ),
73 expected_attrs={
74 'toc': (
75 '<div class="toc">\n'
76 '<ul>\n' # noqa
77 '<li><a href="#header-1">Header 1</a>' # noqa
78 '<ul>\n' # noqa
79 '<li><a href="#header-2">Header 2</a></li>\n' # noqa
80 '</ul>\n' # noqa
81 '</li>\n' # noqa
82 '</ul>\n' # noqa
83 '</div>\n'
84 )
85 }
86 )
87
88 def testAlternateTOCMarker(self):
89 self.assertMarkdownRenders(
90 self.dedent(
91 '''
92 {{marker}}
93
94 # Header 1
95
96 ## Header 2
97 '''
98 ),
99 '<div class="toc">\n'
100 '<ul>\n' # noqa
101 '<li><a href="#header-1">Header 1</a>' # noqa
102 '<ul>\n' # noqa
103 '<li><a href="#header-2">Header 2</a></li>\n' # noqa
104 '</ul>\n' # noqa
105 '</li>\n' # noqa
106 '</ul>\n' # noqa
107 '</div>\n'
108 '<h1 id="header-1">Header 1</h1>\n'
109 '<h2 id="header-2">Header 2</h2>',
110 extensions=[TocExtension(marker='{{marker}}')]
111 )
112
113 def testDisabledTOCMarker(self):
114 self.assertMarkdownRenders(
115 self.dedent(
116 '''
117 [TOC]
118
119 # Header 1
120
121 ## Header 2
122 '''
123 ),
124 self.dedent(
125 '''
126 <p>[TOC]</p>
127 <h1 id="header-1">Header 1</h1>
128 <h2 id="header-2">Header 2</h2>
129 '''
130 ),
131 expected_attrs={
132 'toc': (
133 '<div class="toc">\n'
134 '<ul>\n' # noqa
135 '<li><a href="#header-1">Header 1</a>' # noqa
136 '<ul>\n' # noqa
137 '<li><a href="#header-2">Header 2</a></li>\n' # noqa
138 '</ul>\n' # noqa
139 '</li>\n' # noqa
140 '</ul>\n' # noqa
141 '</div>\n'
142 )
143 },
144 extensions=[TocExtension(marker='')]
145 )
146
147 def testTOCReset(self):
148 md = Markdown(extensions=[TocExtension()])
149 self.assertEqual(md.toc, '')
150 self.assertEqual(md.toc_tokens, [])
151 md.convert('# Header 1')
152 self.assertEqual('<div class="toc">', md.toc[:17])
153 self.assertEqual(len(md.toc_tokens), 1)
154 md.reset()
155 self.assertEqual(md.toc, '')
156 self.assertEqual(md.toc_tokens, [])
157
158 def testUniqueIds(self):
159 self.assertMarkdownRenders(
160 self.dedent(
161 '''
162 #Header
163 #Header
164 #Header
165 '''
166 ),
167 self.dedent(
168 '''
169 <h1 id="header">Header</h1>
170 <h1 id="header_1">Header</h1>
171 <h1 id="header_2">Header</h1>
172 '''
173 ),
174 expected_attrs={
175 'toc': (
176 '<div class="toc">\n'
177 '<ul>\n' # noqa
178 '<li><a href="#header">Header</a></li>\n' # noqa
179 '<li><a href="#header_1">Header</a></li>\n' # noqa
180 '<li><a href="#header_2">Header</a></li>\n' # noqa
181 '</ul>\n' # noqa
182 '</div>\n'
183 ),
184 'toc_tokens': [
185 {
186 'level': 1,
187 'id': 'header',
188 'name': 'Header',
189 'html': 'Header',
190 'data-toc-label': '',
191 'children': []
192 },
193 {
194 'level': 1,
195 'id': 'header_1',
196 'name': 'Header',
197 'html': 'Header',
198 'data-toc-label': '',
199 'children': []
200 },
201 {
202 'level': 1,
203 'id': 'header_2',
204 'name': 'Header',
205 'html': 'Header',
206 'data-toc-label': '',
207 'children': []
208 },
209 ]
210 }
211 )
212
213 def testHtmlEntitiesInTOC(self):
214 self.assertMarkdownRenders(
215 '# Foo &amp; bar',
216 '<h1 id="foo-bar">Foo &amp; bar</h1>',
217 expected_attrs={
218 'toc': (
219 '<div class="toc">\n'
220 '<ul>\n' # noqa
221 '<li><a href="#foo-bar">Foo &amp; bar</a></li>\n' # noqa
222 '</ul>\n' # noqa
223 '</div>\n'
224 ),
225 'toc_tokens': [{
226 'level': 1,
227 'id': 'foo-bar',
228 'name': 'Foo &amp; bar',
229 'html': 'Foo &amp; bar',
230 'data-toc-label': '',
231 'children': []
232 }]
233 }
234 )
235
236 def testHtmlSpecialCharsInTOC(self):
237 self.assertMarkdownRenders(
238 '# Foo > & bar',
239 '<h1 id="foo-bar">Foo &gt; &amp; bar</h1>',
240 expected_attrs={
241 'toc': (
242 '<div class="toc">\n'
243 '<ul>\n' # noqa
244 '<li><a href="#foo-bar">Foo &gt; &amp; bar</a></li>\n' # noqa
245 '</ul>\n' # noqa
246 '</div>\n'
247 ),
248 'toc_tokens': [{
249 'level': 1,
250 'id': 'foo-bar',
251 'name': 'Foo &gt; &amp; bar',
252 'html': 'Foo &gt; &amp; bar',
253 'data-toc-label': '',
254 'children': []
255 }]
256 }
257 )
258
259 def testRawHtmlInTOC(self):
260 self.assertMarkdownRenders(
261 '# Foo <b>Bar</b> Baz.',
262 '<h1 id="foo-bar-baz">Foo <b>Bar</b> Baz.</h1>',
263 expected_attrs={
264 'toc': (
265 '<div class="toc">\n'
266 '<ul>\n' # noqa
267 '<li><a href="#foo-bar-baz">Foo Bar Baz.</a></li>\n' # noqa
268 '</ul>\n' # noqa
269 '</div>\n'
270 ),
271 'toc_tokens': [{
272 'level': 1,
273 'id': 'foo-bar-baz',
274 'name': 'Foo Bar Baz.',
275 'html': 'Foo <b>Bar</b> Baz.',
276 'data-toc-label': '',
277 'children': []
278 }]
279 }
280 )
281
282 def testTOCBaseLevel(self):
283 self.assertMarkdownRenders(
284 self.dedent(
285 '''
286 # Some Header
287 ## Next Level
288 ### Too High
289 '''
290 ),
291 self.dedent(
292 '''
293 <h5 id="some-header">Some Header</h5>
294 <h6 id="next-level">Next Level</h6>
295 <h6 id="too-high">Too High</h6>
296 '''
297 ),
298 expected_attrs={
299 'toc': (
300 '<div class="toc">\n'
301 '<ul>\n' # noqa
302 '<li><a href="#some-header">Some Header</a>' # noqa
303 '<ul>\n' # noqa
304 '<li><a href="#next-level">Next Level</a></li>\n' # noqa
305 '<li><a href="#too-high">Too High</a></li>\n' # noqa
306 '</ul>\n' # noqa
307 '</li>\n' # noqa
308 '</ul>\n' # noqa
309 '</div>\n'
310 ),
311 'toc_tokens': [{
312 'level': 5,
313 'id': 'some-header',
314 'name': 'Some Header',
315 'html': 'Some Header',
316 'data-toc-label': '',
317 'children': [
318 {
319 'level': 6,
320 'id': 'next-level',
321 'name': 'Next Level',
322 'html': 'Next Level',
323 'data-toc-label': '',
324 'children': []
325 },
326 {
327 'level': 6,
328 'id': 'too-high',
329 'name': 'Too High',
330 'html': 'Too High',
331 'data-toc-label': '',
332 'children': []
333 }
334 ]
335 }]
336 },
337 extensions=[TocExtension(baselevel=5)]
338 )
339
340 def testHeaderInlineMarkup(self):
341 self.assertMarkdownRenders(
342 '#Some *Header* with [markup](http://example.com).',
343 '<h1 id="some-header-with-markup">Some <em>Header</em> with '
344 '<a href="http://example.com">markup</a>.</h1>',
345 expected_attrs={
346 'toc': (
347 '<div class="toc">\n'
348 '<ul>\n' # noqa
349 '<li><a href="#some-header-with-markup">' # noqa
350 'Some Header with markup.</a></li>\n' # noqa
351 '</ul>\n' # noqa
352 '</div>\n'
353 ),
354 'toc_tokens': [{
355 'level': 1,
356 'id': 'some-header-with-markup',
357 'name': 'Some Header with markup.',
358 'html': 'Some <em>Header</em> with <a href="http://example.com">markup</a>.',
359 'data-toc-label': '',
360 'children': []
361 }]
362 }
363 )
364
365 def testTOCTitle(self):
366 self.assertMarkdownRenders(
367 self.dedent(
368 '''
369 # Header 1
370
371 ## Header 2
372 '''
373 ),
374 self.dedent(
375 '''
376 <h1 id="header-1">Header 1</h1>
377 <h2 id="header-2">Header 2</h2>
378 '''
379 ),
380 expected_attrs={
381 'toc': (
382 '<div class="toc"><span class="toctitle">Table of Contents</span>'
383 '<ul>\n' # noqa
384 '<li><a href="#header-1">Header 1</a>' # noqa
385 '<ul>\n' # noqa
386 '<li><a href="#header-2">Header 2</a></li>\n' # noqa
387 '</ul>\n' # noqa
388 '</li>\n' # noqa
389 '</ul>\n' # noqa
390 '</div>\n'
391 )
392 },
393 extensions=[TocExtension(title='Table of Contents')]
394 )
395
396 def testTOCUniqueFunc(self):
397 ids = {'foo'}
398 self.assertEqual(unique('foo', ids), 'foo_1')
399 self.assertEqual(ids, {'foo', 'foo_1'})
400
401 def testTocInHeaders(self):
402 self.assertMarkdownRenders(
403 self.dedent(
404 '''
405 [TOC]
406 #[TOC]
407 '''
408 ),
409 '<div class="toc">\n' # noqa
410 '<ul>\n' # noqa
411 '<li><a href="#toc">[TOC]</a></li>\n' # noqa
412 '</ul>\n' # noqa
413 '</div>\n' # noqa
414 '<h1 id="toc">[TOC]</h1>' # noqa
415 )
416
417 self.assertMarkdownRenders(
418 self.dedent(
419 '''
420 #[TOC]
421 [TOC]
422 '''
423 ),
424 '<h1 id="toc">[TOC]</h1>\n' # noqa
425 '<div class="toc">\n' # noqa
426 '<ul>\n' # noqa
427 '<li><a href="#toc">[TOC]</a></li>\n' # noqa
428 '</ul>\n' # noqa
429 '</div>' # noqa
430 )
431
432 self.assertMarkdownRenders(
433 self.dedent(
434 '''
435 [TOC]
436 # *[TOC]*
437 '''
438 ),
439 '<div class="toc">\n' # noqa
440 '<ul>\n' # noqa
441 '<li><a href="#toc">[TOC]</a></li>\n' # noqa
442 '</ul>\n' # noqa
443 '</div>\n' # noqa
444 '<h1 id="toc"><em>[TOC]</em></h1>' # noqa
445 )
446
447 def testTOCPermalink(self):
448 self.assertMarkdownRenders(
449 self.dedent(
450 '''
451 # Hd 1
452
453 ## Hd 2
454 '''
455 ),
456 '<h1 id="hd-1">'
457 'Hd 1' # noqa
458 '<a class="headerlink" href="#hd-1" title="PL">' # noqa
459 '&para;' # noqa
460 '</a>' # noqa
461 '</h1>\n'
462 '<h2 id="hd-2">'
463 'Hd 2' # noqa
464 '<a class="headerlink" href="#hd-2" title="PL">' # noqa
465 '&para;' # noqa
466 '</a>' # noqa
467 '</h2>',
468 extensions=[TocExtension(permalink=True, permalink_title="PL")]
469 )
470
471 def testTOCPermalinkLeading(self):
472 self.assertMarkdownRenders(
473 self.dedent(
474 '''
475 # Hd 1
476 ## Hd 2
477 '''
478 ),
479 '<h1 id="hd-1">'
480 '<a class="headerlink" href="#hd-1" title="PL">' # noqa
481 '&para;' # noqa
482 '</a>' # noqa
483 'Hd 1' # noqa
484 '</h1>\n'
485 '<h2 id="hd-2">'
486 '<a class="headerlink" href="#hd-2" title="PL">' # noqa
487 '&para;' # noqa
488 '</a>' # noqa
489 'Hd 2' # noqa
490 '</h2>',
491 extensions=[TocExtension(permalink=True, permalink_title="PL", permalink_leading=True)]
492 )
493
494 def testTOCInlineMarkupPermalink(self):
495 self.assertMarkdownRenders(
496 '# Code `in` hd',
497 '<h1 id="code-in-hd">'
498 'Code <code>in</code> hd' # noqa
499 '<a class="headerlink" href="#code-in-hd" title="PL">' # noqa
500 '&para;' # noqa
501 '</a>' # noqa
502 '</h1>',
503 extensions=[TocExtension(permalink=True, permalink_title="PL")]
504 )
505
506 def testTOCInlineMarkupPermalinkLeading(self):
507 self.assertMarkdownRenders(
508 '# Code `in` hd',
509 '<h1 id="code-in-hd">'
510 '<a class="headerlink" href="#code-in-hd" title="PL">' # noqa
511 '&para;' # noqa
512 '</a>' # noqa
513 'Code <code>in</code> hd' # noqa
514 '</h1>',
515 extensions=[TocExtension(permalink=True, permalink_title="PL", permalink_leading=True)]
516 )
517
518 def testAnchorLink(self):
519 self.assertMarkdownRenders(
520 self.dedent(
521 '''
522 # Header 1
523
524 ## Header *2*
525 '''
526 ),
527 self.dedent(
528 '''
529 <h1 id="header-1"><a class="toclink" href="#header-1">Header 1</a></h1>
530 <h2 id="header-2"><a class="toclink" href="#header-2">Header <em>2</em></a></h2>
531 '''
532 ),
533 extensions=[TocExtension(anchorlink=True)]
534 )
535
536 def testAnchorLinkWithSingleInlineCode(self):
537 self.assertMarkdownRenders(
538 '# This is `code`.',
539 '<h1 id="this-is-code">' # noqa
540 '<a class="toclink" href="#this-is-code">' # noqa
541 'This is <code>code</code>.' # noqa
542 '</a>' # noqa
543 '</h1>', # noqa
544 extensions=[TocExtension(anchorlink=True)]
545 )
546
547 def testAnchorLinkWithDoubleInlineCode(self):
548 self.assertMarkdownRenders(
549 '# This is `code` and `this` too.',
550 '<h1 id="this-is-code-and-this-too">' # noqa
551 '<a class="toclink" href="#this-is-code-and-this-too">' # noqa
552 'This is <code>code</code> and <code>this</code> too.' # noqa
553 '</a>' # noqa
554 '</h1>', # noqa
555 extensions=[TocExtension(anchorlink=True)]
556 )
557
558 def testPermalink(self):
559 self.assertMarkdownRenders(
560 '# Header',
561 '<h1 id="header">' # noqa
562 'Header' # noqa
563 '<a class="headerlink" href="#header" title="Permanent link">&para;</a>' # noqa
564 '</h1>', # noqa
565 extensions=[TocExtension(permalink=True)]
566 )
567
568 def testPermalinkWithSingleInlineCode(self):
569 self.assertMarkdownRenders(
570 '# This is `code`.',
571 '<h1 id="this-is-code">' # noqa
572 'This is <code>code</code>.' # noqa
573 '<a class="headerlink" href="#this-is-code" title="Permanent link">&para;</a>' # noqa
574 '</h1>', # noqa
575 extensions=[TocExtension(permalink=True)]
576 )
577
578 def testPermalinkWithDoubleInlineCode(self):
579 self.assertMarkdownRenders(
580 '# This is `code` and `this` too.',
581 '<h1 id="this-is-code-and-this-too">' # noqa
582 'This is <code>code</code> and <code>this</code> too.' # noqa
583 '<a class="headerlink" href="#this-is-code-and-this-too" title="Permanent link">&para;</a>' # noqa
584 '</h1>', # noqa
585 extensions=[TocExtension(permalink=True)]
586 )
587
588 def testMinMaxLevel(self):
589 self.assertMarkdownRenders(
590 self.dedent(
591 '''
592 # Header 1 not in TOC
593
594 ## Header 2 not in TOC
595
596 ### Header 3
597
598 #### Header 4
599
600 ##### Header 5 not in TOC
601 '''
602 ),
603 self.dedent(
604 '''
605 <h1 id="header-1-not-in-toc">Header 1 not in TOC</h1>
606 <h2 id="header-2-not-in-toc">Header 2 not in TOC</h2>
607 <h3 id="header-3">Header 3</h3>
608 <h4 id="header-4">Header 4</h4>
609 <h5 id="header-5-not-in-toc">Header 5 not in TOC</h5>
610 '''
611 ),
612 expected_attrs={
613 'toc': (
614 '<div class="toc">\n'
615 '<ul>\n' # noqa
616 '<li><a href="#header-3">Header 3</a>' # noqa
617 '<ul>\n' # noqa
618 '<li><a href="#header-4">Header 4</a></li>\n' # noqa
619 '</ul>\n' # noqa
620 '</li>\n' # noqa
621 '</ul>\n' # noqa
622 '</div>\n' # noqa
623 ),
624 'toc_tokens': [
625 {
626 'level': 3,
627 'id': 'header-3',
628 'name': 'Header 3',
629 'html': 'Header 3',
630 'data-toc-label': '',
631 'children': [
632 {
633 'level': 4,
634 'id': 'header-4',
635 'name': 'Header 4',
636 'html': 'Header 4',
637 'data-toc-label': '',
638 'children': []
639 }
640 ]
641 }
642 ]
643 },
644 extensions=[TocExtension(toc_depth='3-4')]
645 )
646
647 def testMaxLevel(self):
648 self.assertMarkdownRenders(
649 self.dedent(
650 '''
651 # Header 1
652
653 ## Header 2
654
655 ### Header 3 not in TOC
656 '''
657 ),
658 self.dedent(
659 '''
660 <h1 id="header-1">Header 1</h1>
661 <h2 id="header-2">Header 2</h2>
662 <h3 id="header-3-not-in-toc">Header 3 not in TOC</h3>
663 '''
664 ),
665 expected_attrs={
666 'toc': (
667 '<div class="toc">\n'
668 '<ul>\n' # noqa
669 '<li><a href="#header-1">Header 1</a>' # noqa
670 '<ul>\n' # noqa
671 '<li><a href="#header-2">Header 2</a></li>\n' # noqa
672 '</ul>\n' # noqa
673 '</li>\n' # noqa
674 '</ul>\n' # noqa
675 '</div>\n' # noqa
676 ),
677 'toc_tokens': [
678 {
679 'level': 1,
680 'id': 'header-1',
681 'name': 'Header 1',
682 'html': 'Header 1',
683 'data-toc-label': '',
684 'children': [
685 {
686 'level': 2,
687 'id': 'header-2',
688 'name': 'Header 2',
689 'html': 'Header 2',
690 'data-toc-label': '',
691 'children': []
692 }
693 ]
694 }
695 ]
696 },
697 extensions=[TocExtension(toc_depth=2)]
698 )
699
700 def testMinMaxLevelwithAnchorLink(self):
701 self.assertMarkdownRenders(
702 self.dedent(
703 '''
704 # Header 1 not in TOC
705
706 ## Header 2 not in TOC
707
708 ### Header 3
709
710 #### Header 4
711
712 ##### Header 5 not in TOC
713 '''
714 ),
715 '<h1 id="header-1-not-in-toc">' # noqa
716 '<a class="toclink" href="#header-1-not-in-toc">Header 1 not in TOC</a></h1>\n' # noqa
717 '<h2 id="header-2-not-in-toc">' # noqa
718 '<a class="toclink" href="#header-2-not-in-toc">Header 2 not in TOC</a></h2>\n' # noqa
719 '<h3 id="header-3">' # noqa
720 '<a class="toclink" href="#header-3">Header 3</a></h3>\n' # noqa
721 '<h4 id="header-4">' # noqa
722 '<a class="toclink" href="#header-4">Header 4</a></h4>\n' # noqa
723 '<h5 id="header-5-not-in-toc">' # noqa
724 '<a class="toclink" href="#header-5-not-in-toc">Header 5 not in TOC</a></h5>', # noqa
725 expected_attrs={
726 'toc': (
727 '<div class="toc">\n'
728 '<ul>\n' # noqa
729 '<li><a href="#header-3">Header 3</a>' # noqa
730 '<ul>\n' # noqa
731 '<li><a href="#header-4">Header 4</a></li>\n' # noqa
732 '</ul>\n' # noqa
733 '</li>\n' # noqa
734 '</ul>\n' # noqa
735 '</div>\n' # noqa
736 ),
737 'toc_tokens': [
738 {
739 'level': 3,
740 'id': 'header-3',
741 'name': 'Header 3',
742 'html': 'Header 3',
743 'data-toc-label': '',
744 'children': [
745 {
746 'level': 4,
747 'id': 'header-4',
748 'name': 'Header 4',
749 'html': 'Header 4',
750 'data-toc-label': '',
751 'children': []
752 }
753 ]
754 }
755 ]
756 },
757 extensions=[TocExtension(toc_depth='3-4', anchorlink=True)]
758 )
759
760 def testMinMaxLevelwithPermalink(self):
761 self.assertMarkdownRenders(
762 self.dedent(
763 '''
764 # Header 1 not in TOC
765
766 ## Header 2 not in TOC
767
768 ### Header 3
769
770 #### Header 4
771
772 ##### Header 5 not in TOC
773 '''
774 ),
775 '<h1 id="header-1-not-in-toc">Header 1 not in TOC' # noqa
776 '<a class="headerlink" href="#header-1-not-in-toc" title="Permanent link">&para;</a></h1>\n' # noqa
777 '<h2 id="header-2-not-in-toc">Header 2 not in TOC' # noqa
778 '<a class="headerlink" href="#header-2-not-in-toc" title="Permanent link">&para;</a></h2>\n' # noqa
779 '<h3 id="header-3">Header 3' # noqa
780 '<a class="headerlink" href="#header-3" title="Permanent link">&para;</a></h3>\n' # noqa
781 '<h4 id="header-4">Header 4' # noqa
782 '<a class="headerlink" href="#header-4" title="Permanent link">&para;</a></h4>\n' # noqa
783 '<h5 id="header-5-not-in-toc">Header 5 not in TOC' # noqa
784 '<a class="headerlink" href="#header-5-not-in-toc" title="Permanent link">&para;</a></h5>', # noqa
785 expected_attrs={
786 'toc': (
787 '<div class="toc">\n'
788 '<ul>\n' # noqa
789 '<li><a href="#header-3">Header 3</a>' # noqa
790 '<ul>\n' # noqa
791 '<li><a href="#header-4">Header 4</a></li>\n' # noqa
792 '</ul>\n' # noqa
793 '</li>\n' # noqa
794 '</ul>\n' # noqa
795 '</div>\n' # noqa
796 ),
797 'toc_tokens': [
798 {
799 'level': 3,
800 'id': 'header-3',
801 'name': 'Header 3',
802 'html': 'Header 3',
803 'data-toc-label': '',
804 'children': [
805 {
806 'level': 4,
807 'id': 'header-4',
808 'name': 'Header 4',
809 'html': 'Header 4',
810 'data-toc-label': '',
811 'children': []
812 }
813 ]
814 }
815 ]
816 },
817 extensions=[TocExtension(toc_depth='3-4', permalink=True)]
818 )
819
820 def testMinMaxLevelwithBaseLevel(self):
821 self.assertMarkdownRenders(
822 self.dedent(
823 '''
824 # First Header
825
826 ## Second Level
827
828 ### Third Level
829
830 #### Forth Level
831 '''
832 ),
833 self.dedent(
834 '''
835 <h3 id="first-header">First Header</h3>
836 <h4 id="second-level">Second Level</h4>
837 <h5 id="third-level">Third Level</h5>
838 <h6 id="forth-level">Forth Level</h6>
839 '''
840 ),
841 expected_attrs={
842 'toc': (
843 '<div class="toc">\n'
844 '<ul>\n' # noqa
845 '<li><a href="#second-level">Second Level</a>' # noqa
846 '<ul>\n' # noqa
847 '<li><a href="#third-level">Third Level</a></li>\n' # noqa
848 '</ul>\n' # noqa
849 '</li>\n' # noqa
850 '</ul>\n' # noqa
851 '</div>\n' # noqa
852 ),
853 'toc_tokens': [
854 {
855 'level': 4,
856 'id': 'second-level',
857 'name': 'Second Level',
858 'html': 'Second Level',
859 'data-toc-label': '',
860 'children': [
861 {
862 'level': 5,
863 'id': 'third-level',
864 'name': 'Third Level',
865 'html': 'Third Level',
866 'data-toc-label': '',
867 'children': []
868 }
869 ]
870 }
871 ]
872 },
873 extensions=[TocExtension(toc_depth='4-5', baselevel=3)]
874 )
875
876 def testMaxLevelwithBaseLevel(self):
877 self.assertMarkdownRenders(
878 self.dedent(
879 '''
880 # Some Header
881
882 ## Next Level
883
884 ### Too High
885 '''
886 ),
887 self.dedent(
888 '''
889 <h2 id="some-header">Some Header</h2>
890 <h3 id="next-level">Next Level</h3>
891 <h4 id="too-high">Too High</h4>
892 '''
893 ),
894 expected_attrs={
895 'toc': (
896 '<div class="toc">\n'
897 '<ul>\n' # noqa
898 '<li><a href="#some-header">Some Header</a>' # noqa
899 '<ul>\n' # noqa
900 '<li><a href="#next-level">Next Level</a></li>\n' # noqa
901 '</ul>\n' # noqa
902 '</li>\n' # noqa
903 '</ul>\n' # noqa
904 '</div>\n' # noqa
905 ),
906 'toc_tokens': [
907 {
908 'level': 2,
909 'id': 'some-header',
910 'name': 'Some Header',
911 'html': 'Some Header',
912 'data-toc-label': '',
913 'children': [
914 {
915 'level': 3,
916 'id': 'next-level',
917 'name': 'Next Level',
918 'html': 'Next Level',
919 'data-toc-label': '',
920 'children': []
921 }
922 ]
923 }
924 ]
925 },
926 extensions=[TocExtension(toc_depth=3, baselevel=2)]
927 )
928
929 def test_escaped_code(self):
930 self.assertMarkdownRenders(
931 self.dedent(
932 '''
933 [TOC]
934
935 # `<test>`
936 '''
937 ),
938 self.dedent(
939 '''
940 <div class="toc">
941 <ul>
942 <li><a href="#test">&lt;test&gt;</a></li>
943 </ul>
944 </div>
945 <h1 id="test"><code>&lt;test&gt;</code></h1>
946 '''
947 ),
948 extensions=['toc']
949 )
950
951 def test_escaped_char_in_id(self):
952 self.assertMarkdownRenders(
953 r'# escaped\_character',
954 '<h1 id="escaped_character">escaped_character</h1>',
955 expected_attrs={
956 'toc': (
957 '<div class="toc">\n'
958 '<ul>\n' # noqa
959 '<li><a href="#escaped_character">escaped_character</a></li>\n' # noqa
960 '</ul>\n' # noqa
961 '</div>\n' # noqa
962 ),
963 'toc_tokens': [
964 {
965 'level': 1,
966 'id': 'escaped_character',
967 'name': 'escaped_character',
968 'html': 'escaped_character',
969 'data-toc-label': '',
970 'children': []
971 }
972 ]
973 },
974 extensions=['toc']
975 )
976
977 def test_escaped_char_in_attr_list(self):
978 self.assertMarkdownRenders(
979 r'# `*Foo*` { id="\*Foo\*" }',
980 '<h1 id="*Foo*"><code>*Foo*</code></h1>',
981 expected_attrs={
982 'toc': (
983 '<div class="toc">\n'
984 '<ul>\n' # noqa
985 '<li><a href="#*Foo*">*Foo*</a></li>\n' # noqa
986 '</ul>\n' # noqa
987 '</div>\n' # noqa
988 ),
989 'toc_tokens': [
990 {
991 'level': 1,
992 'id': '*Foo*',
993 'name': '*Foo*',
994 'html': '<code>*Foo*</code>',
995 'data-toc-label': '',
996 'children': []
997 }
998 ]
999 },
1000 extensions=['toc', 'attr_list']
1001 )
1002
1003 def testAutoLinkEmail(self):
1004 self.assertMarkdownRenders(
1005 '## <[email protected]>',
1006 '<h2 id="fooexampleorg"><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#102;&#111;&#111;&#64;&#101;'
1007 '&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#111;&#114;&#103;">&#102;&#111;&#111;&#64;&#101;&#120;&#97;'
1008 '&#109;&#112;&#108;&#101;&#46;&#111;&#114;&#103;</a></h2>',
1009 expected_attrs={
1010 'toc_tokens': [
1011 {
1012 'level': 2,
1013 'id': 'fooexampleorg',
1014 'name': '&#102;&#111;&#111;&#64;&#101;&#120;&#97;&#109;'
1015 '&#112;&#108;&#101;&#46;&#111;&#114;&#103;',
1016 'html': '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#102;&#111;&#111;&#64;&#101;'
1017 '&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#111;&#114;&#103;">&#102;&#111;&#111;'
1018 '&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#111;&#114;&#103;</a>',
1019 'data-toc-label': '',
1020 'children': []
1021 }
1022 ]
1023 },
1024 extensions=['toc']
1025 )
1026
1027 def testAnchorLinkWithCustomClass(self):
1028 self.assertMarkdownRenders(
1029 self.dedent(
1030 '''
1031 # Header 1
1032
1033 ## Header *2*
1034 '''
1035 ),
1036 self.dedent(
1037 '''
1038 <h1 id="header-1"><a class="custom" href="#header-1">Header 1</a></h1>
1039 <h2 id="header-2"><a class="custom" href="#header-2">Header <em>2</em></a></h2>
1040 '''
1041 ),
1042 extensions=[TocExtension(anchorlink=True, anchorlink_class="custom")]
1043 )
1044
1045 def testAnchorLinkWithCustomClasses(self):
1046 self.assertMarkdownRenders(
1047 self.dedent(
1048 '''
1049 # Header 1
1050
1051 ## Header *2*
1052 '''
1053 ),
1054 self.dedent(
1055 '''
1056 <h1 id="header-1"><a class="custom1 custom2" href="#header-1">Header 1</a></h1>
1057 <h2 id="header-2"><a class="custom1 custom2" href="#header-2">Header <em>2</em></a></h2>
1058 '''
1059 ),
1060 extensions=[TocExtension(anchorlink=True, anchorlink_class="custom1 custom2")]
1061 )
1062
1063 def testPermalinkWithEmptyText(self):
1064 self.assertMarkdownRenders(
1065 '# Header',
1066 '<h1 id="header">' # noqa
1067 'Header' # noqa
1068 '<a class="headerlink" href="#header" title="Permanent link"></a>' # noqa
1069 '</h1>', # noqa
1070 extensions=[TocExtension(permalink="")]
1071 )
1072
1073 def testPermalinkWithCustomClass(self):
1074 self.assertMarkdownRenders(
1075 '# Header',
1076 '<h1 id="header">' # noqa
1077 'Header' # noqa
1078 '<a class="custom" href="#header" title="Permanent link">&para;</a>' # noqa
1079 '</h1>', # noqa
1080 extensions=[TocExtension(permalink=True, permalink_class="custom")]
1081 )
1082
1083 def testPermalinkWithCustomClasses(self):
1084 self.assertMarkdownRenders(
1085 '# Header',
1086 '<h1 id="header">' # noqa
1087 'Header' # noqa
1088 '<a class="custom1 custom2" href="#header" title="Permanent link">&para;</a>' # noqa
1089 '</h1>', # noqa
1090 extensions=[TocExtension(permalink=True, permalink_class="custom1 custom2")]
1091 )
1092
1093 def testPermalinkWithCustomTitle(self):
1094 self.assertMarkdownRenders(
1095 '# Header',
1096 '<h1 id="header">' # noqa
1097 'Header' # noqa
1098 '<a class="headerlink" href="#header" title="custom">&para;</a>' # noqa
1099 '</h1>', # noqa
1100 extensions=[TocExtension(permalink=True, permalink_title="custom")]
1101 )
1102
1103 def testPermalinkWithEmptyTitle(self):
1104 self.assertMarkdownRenders(
1105 '# Header',
1106 '<h1 id="header">' # noqa
1107 'Header' # noqa
1108 '<a class="headerlink" href="#header">&para;</a>' # noqa
1109 '</h1>', # noqa
1110 extensions=[TocExtension(permalink=True, permalink_title="")]
1111 )
1112
1113 def testPermalinkWithUnicodeInID(self):
1114 from markdown.extensions.toc import slugify_unicode
1115 self.assertMarkdownRenders(
1116 '# Unicode ヘッダー',
1117 '<h1 id="unicode-ヘッダー">' # noqa
1118 'Unicode ヘッダー' # noqa
1119 '<a class="headerlink" href="#unicode-ヘッダー" title="Permanent link">&para;</a>' # noqa
1120 '</h1>', # noqa
1121 extensions=[TocExtension(permalink=True, slugify=slugify_unicode)]
1122 )
1123
1124 def testPermalinkWithUnicodeTitle(self):
1125 from markdown.extensions.toc import slugify_unicode
1126 self.assertMarkdownRenders(
1127 '# Unicode ヘッダー',
1128 '<h1 id="unicode-ヘッダー">' # noqa
1129 'Unicode ヘッダー' # noqa
1130 '<a class="headerlink" href="#unicode-ヘッダー" title="パーマリンク">&para;</a>' # noqa
1131 '</h1>', # noqa
1132 extensions=[TocExtension(permalink=True, permalink_title="パーマリンク", slugify=slugify_unicode)]
1133 )
1134
1135 def testPermalinkWithExtendedLatinInID(self):
1136 self.assertMarkdownRenders(
1137 '# Théâtre',
1138 '<h1 id="theatre">' # noqa
1139 'Théâtre' # noqa
1140 '<a class="headerlink" href="#theatre" title="Permanent link">&para;</a>' # noqa
1141 '</h1>', # noqa
1142 extensions=[TocExtension(permalink=True)]
1143 )
1144
1145 def testNl2brCompatibility(self):
1146 self.assertMarkdownRenders(
1147 '[TOC]\ntext',
1148 '<p>[TOC]<br />\ntext</p>',
1149 extensions=[TocExtension(), Nl2BrExtension()]
1150 )
1151
1152 def testTOCWithCustomClass(self):
1153
1154 self.assertMarkdownRenders(
1155 self.dedent(
1156 '''
1157 [TOC]
1158 # Header
1159 '''
1160 ),
1161 self.dedent(
1162 '''
1163 <div class="custom">
1164 <ul>
1165 <li><a href="#header">Header</a></li>
1166 </ul>
1167 </div>
1168 <h1 id="header">Header</h1>
1169 '''
1170 ),
1171 extensions=[TocExtension(toc_class="custom")]
1172 )
1173
1174 def testTOCWithCustomClasses(self):
1175 self.assertMarkdownRenders(
1176 self.dedent(
1177 '''
1178 [TOC]
1179 # Header
1180 '''
1181 ),
1182 self.dedent(
1183 '''
1184 <div class="custom1 custom2">
1185 <ul>
1186 <li><a href="#header">Header</a></li>
1187 </ul>
1188 </div>
1189 <h1 id="header">Header</h1>
1190 '''
1191 ),
1192 extensions=[TocExtension(toc_class="custom1 custom2")]
1193 )
1194
1195 def testTOCWithEmptyTitleClass(self):
1196
1197 self.assertMarkdownRenders(
1198 self.dedent(
1199 '''
1200 [TOC]
1201 # Header
1202 '''
1203 ),
1204 self.dedent(
1205 '''
1206 <div class="toc"><span>ToC</span><ul>
1207 <li><a href="#header">Header</a></li>
1208 </ul>
1209 </div>
1210 <h1 id="header">Header</h1>
1211 '''
1212 ),
1213 extensions=[TocExtension(title_class="", title='ToC')]
1214 )
1215
1216 def testTOCWithCustomTitleClass(self):
1217
1218 self.assertMarkdownRenders(
1219 self.dedent(
1220 '''
1221 [TOC]
1222 # Header
1223 '''
1224 ),
1225 self.dedent(
1226 '''
1227 <div class="toc"><span class="tocname">ToC</span><ul>
1228 <li><a href="#header">Header</a></li>
1229 </ul>
1230 </div>
1231 <h1 id="header">Header</h1>
1232 '''
1233 ),
1234 extensions=[TocExtension(title_class="tocname", title='ToC')]
1235 )
1236
1237 def testTocWithAttrList(self):
1238
1239 self.assertMarkdownRenders(
1240 self.dedent(
1241 '''
1242 # Header 1
1243
1244 ## Header 2 { #foo }
1245
1246 ## Header 3 { data-toc-label="Foo Bar" }
1247
1248 # Header 4 { data-toc-label="Foo > &amp; < Baz" }
1249
1250 # Header 5 { data-toc-label="Foo <b>Quux</b>" }
1251 '''
1252 ),
1253 self.dedent(
1254 '''
1255 <h1 id="header-1">Header 1</h1>
1256 <h2 id="foo">Header 2</h2>
1257 <h2 id="header-3">Header 3</h2>
1258 <h1 id="header-4">Header 4</h1>
1259 <h1 id="header-5">Header 5</h1>
1260 '''
1261 ),
1262 expected_attrs={
1263 'toc': (
1264 '<div class="toc">\n'
1265 '<ul>\n' # noqa
1266 '<li><a href="#header-1">Header 1</a>' # noqa
1267 '<ul>\n' # noqa
1268 '<li><a href="#foo">Header 2</a></li>\n' # noqa
1269 '<li><a href="#header-3">Foo Bar</a></li>\n' # noqa
1270 '</ul>\n' # noqa
1271 '</li>\n' # noqa
1272 '<li><a href="#header-4">Foo &gt; &amp; &lt; Baz</a></li>\n' # noqa
1273 '<li><a href="#header-5">Foo Quux</a></li>\n' # noqa
1274 '</ul>\n' # noqa
1275 '</div>\n'
1276 ),
1277 'toc_tokens': [
1278 {
1279 'level': 1,
1280 'id': 'header-1',
1281 'name': 'Header 1',
1282 'html': 'Header 1',
1283 'data-toc-label': '',
1284 'children': [
1285 {
1286 'level': 2,
1287 'id': 'foo',
1288 'name': 'Header 2',
1289 'html': 'Header 2',
1290 'data-toc-label': '',
1291 'children': []
1292 },
1293 {
1294 'level': 2,
1295 'id': 'header-3',
1296 'name': 'Foo Bar',
1297 'html': 'Header 3',
1298 'data-toc-label': 'Foo Bar',
1299 'children': []
1300 }
1301 ]
1302 },
1303 {
1304 'level': 1,
1305 'id': 'header-4',
1306 'name': 'Foo &gt; &amp; &lt; Baz',
1307 'html': 'Header 4',
1308 'data-toc-label': 'Foo > &amp; < Baz',
1309 'children': []
1310 },
1311 {
1312 'level': 1,
1313 'id': 'header-5',
1314 'name': 'Foo Quux',
1315 'html': 'Header 5',
1316 'data-toc-label': 'Foo <b>Quux</b>',
1317 'children': []
1318 },
1319 ]
1320 },
1321 extensions=[TocExtension(), 'attr_list']
1322 )
1323
1324 def testHeadingRemoveFootnoteRef(self):
1325
1326 self.assertMarkdownRenders(
1327 self.dedent(
1328 '''
1329 # Header 1[^1]
1330 # Header[^1] 2
1331 # Header *subelement*[^1] 3
1332 # Header[^1] double[^1] 4
1333
1334 [^1]: footnote
1335 '''
1336 ),
1337 (
1338 '<h1 id="header-1">Header 1<sup id="fnref:1"><a class="footnote-ref" href="#fn:1">1</a></sup></h1>\n'
1339 '<h1 id="header-2">Header<sup id="fnref2:1"><a class="footnote-ref" href="#fn:1">1</a></sup> 2</h1>\n'
1340 '<h1 id="header-subelement-3">'
1341 'Header <em>subelement</em><sup id="fnref3:1"><a class="footnote-ref" href="#fn:1">1</a></sup> 3'
1342 '</h1>\n'
1343 '<h1 id="header-double-4">'
1344 'Header<sup id="fnref4:1"><a class="footnote-ref" href="#fn:1">1</a></sup> double'
1345 '<sup id="fnref5:1"><a class="footnote-ref" href="#fn:1">1</a></sup> 4'
1346 '</h1>\n'
1347 '<div class="footnote">\n'
1348 '<hr />\n'
1349 '<ol>\n'
1350 '<li id="fn:1">\n'
1351 '<p>'
1352 'footnote&#160;'
1353 '<a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text">&#8617;</a>'
1354 '<a class="footnote-backref" href="#fnref2:1" title="Jump back to footnote 1 in the text">&#8617;</a>'
1355 '<a class="footnote-backref" href="#fnref3:1" title="Jump back to footnote 1 in the text">&#8617;</a>'
1356 '<a class="footnote-backref" href="#fnref4:1" title="Jump back to footnote 1 in the text">&#8617;</a>'
1357 '<a class="footnote-backref" href="#fnref5:1" title="Jump back to footnote 1 in the text">&#8617;</a>'
1358 '</p>\n'
1359 '</li>\n'
1360 '</ol>\n'
1361 '</div>'
1362 ),
1363 expected_attrs={
1364 'toc': (
1365 '<div class="toc">\n'
1366 '<ul>\n' # noqa
1367 '<li><a href="#header-1">Header 1</a></li>\n' # noqa
1368 '<li><a href="#header-2">Header 2</a></li>\n' # noqa
1369 '<li><a href="#header-subelement-3">Header subelement 3</a></li>\n' # noqa
1370 '<li><a href="#header-double-4">Header double 4</a></li>\n' # noqa
1371 '</ul>\n' # noqa
1372 '</div>\n' # noqa
1373 ),
1374 'toc_tokens': [
1375 {
1376 'level': 1,
1377 'id': 'header-1',
1378 'name': 'Header 1',
1379 'html': 'Header 1',
1380 'data-toc-label': '',
1381 'children': []
1382 },
1383 {
1384 'level': 1,
1385 'id': 'header-2',
1386 'name': 'Header 2',
1387 'html': 'Header 2',
1388 'data-toc-label': '',
1389 'children': []
1390 },
1391 {
1392 'level': 1,
1393 'id': 'header-subelement-3',
1394 'name': 'Header subelement 3',
1395 'html': 'Header <em>subelement</em> 3',
1396 'data-toc-label': '',
1397 'children': []
1398 },
1399 {
1400 'level': 1,
1401 'id': 'header-double-4',
1402 'name': 'Header double 4',
1403 'html': 'Header double 4',
1404 'data-toc-label': '',
1405 'children': []
1406 }
1407 ]
1408 },
1409 extensions=[TocExtension(), 'footnotes']
1410 )
1411
1412
1413 class testStripTags(TestCase):
1414
1415 def testStripElement(self):
1416 self.assertEqual(
1417 strip_tags('foo <em>bar</em>'),
1418 'foo bar'
1419 )
1420
1421 def testStripOpenElement(self):
1422 self.assertEqual(
1423 strip_tags('foo <em>bar'),
1424 'foo bar'
1425 )
1426
1427 def testStripEmptyElement(self):
1428 self.assertEqual(
1429 strip_tags('foo <br />bar'),
1430 'foo bar'
1431 )
1432
1433 def testDontStripOpenBracket(self):
1434 self.assertEqual(
1435 strip_tags('foo < bar'),
1436 'foo < bar'
1437 )
1438
1439 def testDontStripCloseBracket(self):
1440 self.assertEqual(
1441 strip_tags('foo > bar'),
1442 'foo > bar'
1443 )
1444
1445 def testStripCollapseWhitespace(self):
1446 self.assertEqual(
1447 strip_tags('foo <em>\tbar\t</em>'),
1448 'foo bar'
1449 )
1450
1451 def testStripElementWithNewlines(self):
1452 self.assertEqual(
1453 strip_tags('foo <meta content="tag\nwith\nnewlines"> bar'),
1454 'foo bar'
1455 )
1456
1457 def testStripComment(self):
1458 self.assertEqual(
1459 strip_tags('foo <!-- comment --> bar'),
1460 'foo bar'
1461 )
1462
1463 def testStripCommentWithInnerTags(self):
1464 self.assertEqual(
1465 strip_tags('foo <!-- comment with <em> --> bar'),
1466 'foo bar'
1467 )
1468
1469 def testStripCommentInElement(self):
1470 self.assertEqual(
1471 strip_tags('<em>foo <!-- comment --> bar<em>'),
1472 'foo bar'
1473 )
1474
1475 def testDontStripHTMLEntities(self):
1476 self.assertEqual(
1477 strip_tags('foo &lt; &amp; &lt; bar'),
1478 'foo &lt; &amp; &lt; bar'
1479 )