Debian Codegraph @

tests/test_syntax/blocks/test_headers.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-2023 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 import unittest
23 from markdown.test_tools import TestCase
24
25
26 class TestSetextHeaders(TestCase):
27
28 def test_setext_h1(self):
29 self.assertMarkdownRenders(
30 self.dedent(
31 """
32 This is an H1
33 =============
34 """
35 ),
36
37 '<h1>This is an H1</h1>'
38 )
39
40 def test_setext_h2(self):
41 self.assertMarkdownRenders(
42 self.dedent(
43 """
44 This is an H2
45 -------------
46 """
47 ),
48
49 '<h2>This is an H2</h2>'
50 )
51
52 def test_setext_h1_mismatched_length(self):
53 self.assertMarkdownRenders(
54 self.dedent(
55 """
56 This is an H1
57 ===
58 """
59 ),
60
61 '<h1>This is an H1</h1>'
62 )
63
64 def test_setext_h2_mismatched_length(self):
65 self.assertMarkdownRenders(
66 self.dedent(
67 """
68 This is an H2
69 ---
70 """
71 ),
72
73 '<h2>This is an H2</h2>'
74 )
75
76 def test_setext_h1_followed_by_p(self):
77 self.assertMarkdownRenders(
78 self.dedent(
79 """
80 This is an H1
81 =============
82 Followed by a Paragraph with no blank line.
83 """
84 ),
85 self.dedent(
86 """
87 <h1>This is an H1</h1>
88 <p>Followed by a Paragraph with no blank line.</p>
89 """
90 )
91 )
92
93 def test_setext_h2_followed_by_p(self):
94 self.assertMarkdownRenders(
95 self.dedent(
96 """
97 This is an H2
98 -------------
99 Followed by a Paragraph with no blank line.
100 """
101 ),
102 self.dedent(
103 """
104 <h2>This is an H2</h2>
105 <p>Followed by a Paragraph with no blank line.</p>
106 """
107 )
108 )
109
110 # TODO: fix this
111 # see https://johnmacfarlane.net/babelmark2/?normalize=1&text=Paragraph%0AAn+H1%0A%3D%3D%3D%3D%3D
112
113 def test_setext_mixed_chars_not_h1(self):
114 """Mixed = and - chars should not form a valid H1."""
115 self.assertMarkdownRenders(
116 self.dedent(
117 """
118 This is not an H1
119 =-
120 """
121 ),
122 self.dedent(
123 """
124 <p>This is not an H1
125 =-</p>
126 """
127 )
128 )
129
130 def test_setext_mixed_chars_not_h2(self):
131 """Mixed - and = chars should not form a valid H2."""
132 self.assertMarkdownRenders(
133 self.dedent(
134 """
135 This is not an H2
136 -=
137 """
138 ),
139 self.dedent(
140 """
141 <p>This is not an H2
142 -=</p>
143 """
144 )
145 )
146
147 def test_setext_mixed_multiple_chars(self):
148 """Multiple mixed = and - chars should not form a valid H1/H2."""
149 self.assertMarkdownRenders(
150 self.dedent(
151 """
152 Not a Header
153 =-=-
154 """
155 ),
156 self.dedent(
157 """
158 <p>Not a Header
159 =-=-</p>
160 """
161 )
162 )
163
164 @unittest.skip('This is broken in Python-Markdown')
165 def test_p_followed_by_setext_h1(self):
166 self.assertMarkdownRenders(
167 self.dedent(
168 """
169 This is a Paragraph.
170 Followed by an H1 with no blank line.
171 =====================================
172 """
173 ),
174 self.dedent(
175 """
176 <p>This is a Paragraph.</p>
177 <h1>Followed by an H1 with no blank line.</h1>
178 """
179 )
180 )
181
182 # TODO: fix this
183 # see https://johnmacfarlane.net/babelmark2/?normalize=1&text=Paragraph%0AAn+H2%0A-----
184 @unittest.skip('This is broken in Python-Markdown')
185 def test_p_followed_by_setext_h2(self):
186 self.assertMarkdownRenders(
187 self.dedent(
188 """
189 This is a Paragraph.
190 Followed by an H2 with no blank line.
191 -------------------------------------
192 """
193 ),
194 self.dedent(
195 """
196 <p>This is a Paragraph.</p>
197 <h2>Followed by an H2 with no blank line.</h2>
198 """
199 )
200 )
201
202
203 class TestHashHeaders(TestCase):
204
205 def test_hash_h1_open(self):
206 self.assertMarkdownRenders(
207 '# This is an H1',
208
209 '<h1>This is an H1</h1>'
210 )
211
212 def test_hash_h2_open(self):
213 self.assertMarkdownRenders(
214 '## This is an H2',
215
216 '<h2>This is an H2</h2>'
217 )
218
219 def test_hash_h3_open(self):
220 self.assertMarkdownRenders(
221 '### This is an H3',
222
223 '<h3>This is an H3</h3>'
224 )
225
226 def test_hash_h4_open(self):
227 self.assertMarkdownRenders(
228 '#### This is an H4',
229
230 '<h4>This is an H4</h4>'
231 )
232
233 def test_hash_h5_open(self):
234 self.assertMarkdownRenders(
235 '##### This is an H5',
236
237 '<h5>This is an H5</h5>'
238 )
239
240 def test_hash_h6_open(self):
241 self.assertMarkdownRenders(
242 '###### This is an H6',
243
244 '<h6>This is an H6</h6>'
245 )
246
247 def test_hash_gt6_open(self):
248 self.assertMarkdownRenders(
249 '####### This is an H6',
250
251 '<h6># This is an H6</h6>'
252 )
253
254 def test_hash_h1_open_missing_space(self):
255 self.assertMarkdownRenders(
256 '#This is an H1',
257
258 '<h1>This is an H1</h1>'
259 )
260
261 def test_hash_h2_open_missing_space(self):
262 self.assertMarkdownRenders(
263 '##This is an H2',
264
265 '<h2>This is an H2</h2>'
266 )
267
268 def test_hash_h3_open_missing_space(self):
269 self.assertMarkdownRenders(
270 '###This is an H3',
271
272 '<h3>This is an H3</h3>'
273 )
274
275 def test_hash_h4_open_missing_space(self):
276 self.assertMarkdownRenders(
277 '####This is an H4',
278
279 '<h4>This is an H4</h4>'
280 )
281
282 def test_hash_h5_open_missing_space(self):
283 self.assertMarkdownRenders(
284 '#####This is an H5',
285
286 '<h5>This is an H5</h5>'
287 )
288
289 def test_hash_h6_open_missing_space(self):
290 self.assertMarkdownRenders(
291 '######This is an H6',
292
293 '<h6>This is an H6</h6>'
294 )
295
296 def test_hash_gt6_open_missing_space(self):
297 self.assertMarkdownRenders(
298 '#######This is an H6',
299
300 '<h6>#This is an H6</h6>'
301 )
302
303 def test_hash_h1_closed(self):
304 self.assertMarkdownRenders(
305 '# This is an H1 #',
306
307 '<h1>This is an H1</h1>'
308 )
309
310 def test_hash_h2_closed(self):
311 self.assertMarkdownRenders(
312 '## This is an H2 ##',
313
314 '<h2>This is an H2</h2>'
315 )
316
317 def test_hash_h3_closed(self):
318 self.assertMarkdownRenders(
319 '### This is an H3 ###',
320
321 '<h3>This is an H3</h3>'
322 )
323
324 def test_hash_h4_closed(self):
325 self.assertMarkdownRenders(
326 '#### This is an H4 ####',
327
328 '<h4>This is an H4</h4>'
329 )
330
331 def test_hash_h5_closed(self):
332 self.assertMarkdownRenders(
333 '##### This is an H5 #####',
334
335 '<h5>This is an H5</h5>'
336 )
337
338 def test_hash_h6_closed(self):
339 self.assertMarkdownRenders(
340 '###### This is an H6 ######',
341
342 '<h6>This is an H6</h6>'
343 )
344
345 def test_hash_gt6_closed(self):
346 self.assertMarkdownRenders(
347 '####### This is an H6 #######',
348
349 '<h6># This is an H6</h6>'
350 )
351
352 def test_hash_h1_closed_missing_space(self):
353 self.assertMarkdownRenders(
354 '#This is an H1#',
355
356 '<h1>This is an H1</h1>'
357 )
358
359 def test_hash_h2_closed_missing_space(self):
360 self.assertMarkdownRenders(
361 '##This is an H2##',
362
363 '<h2>This is an H2</h2>'
364 )
365
366 def test_hash_h3_closed_missing_space(self):
367 self.assertMarkdownRenders(
368 '###This is an H3###',
369
370 '<h3>This is an H3</h3>'
371 )
372
373 def test_hash_h4_closed_missing_space(self):
374 self.assertMarkdownRenders(
375 '####This is an H4####',
376
377 '<h4>This is an H4</h4>'
378 )
379
380 def test_hash_h5_closed_missing_space(self):
381 self.assertMarkdownRenders(
382 '#####This is an H5#####',
383
384 '<h5>This is an H5</h5>'
385 )
386
387 def test_hash_h6_closed_missing_space(self):
388 self.assertMarkdownRenders(
389 '######This is an H6######',
390
391 '<h6>This is an H6</h6>'
392 )
393
394 def test_hash_gt6_closed_missing_space(self):
395 self.assertMarkdownRenders(
396 '#######This is an H6#######',
397
398 '<h6>#This is an H6</h6>'
399 )
400
401 def test_hash_h1_closed_mismatch(self):
402 self.assertMarkdownRenders(
403 '# This is an H1 ##',
404
405 '<h1>This is an H1</h1>'
406 )
407
408 def test_hash_h2_closed_mismatch(self):
409 self.assertMarkdownRenders(
410 '## This is an H2 #',
411
412 '<h2>This is an H2</h2>'
413 )
414
415 def test_hash_h3_closed_mismatch(self):
416 self.assertMarkdownRenders(
417 '### This is an H3 #',
418
419 '<h3>This is an H3</h3>'
420 )
421
422 def test_hash_h4_closed_mismatch(self):
423 self.assertMarkdownRenders(
424 '#### This is an H4 #',
425
426 '<h4>This is an H4</h4>'
427 )
428
429 def test_hash_h5_closed_mismatch(self):
430 self.assertMarkdownRenders(
431 '##### This is an H5 #',
432
433 '<h5>This is an H5</h5>'
434 )
435
436 def test_hash_h6_closed_mismatch(self):
437 self.assertMarkdownRenders(
438 '###### This is an H6 #',
439
440 '<h6>This is an H6</h6>'
441 )
442
443 def test_hash_gt6_closed_mismatch(self):
444 self.assertMarkdownRenders(
445 '####### This is an H6 ##################',
446
447 '<h6># This is an H6</h6>'
448 )
449
450 def test_hash_h1_followed_by_p(self):
451 self.assertMarkdownRenders(
452 self.dedent(
453 """
454 # This is an H1
455 Followed by a Paragraph with no blank line.
456 """
457 ),
458 self.dedent(
459 """
460 <h1>This is an H1</h1>
461 <p>Followed by a Paragraph with no blank line.</p>
462 """
463 )
464 )
465
466 def test_hash_h2_followed_by_p(self):
467 self.assertMarkdownRenders(
468 self.dedent(
469 """
470 ## This is an H2
471 Followed by a Paragraph with no blank line.
472 """
473 ),
474 self.dedent(
475 """
476 <h2>This is an H2</h2>
477 <p>Followed by a Paragraph with no blank line.</p>
478 """
479 )
480 )
481
482 def test_hash_h3_followed_by_p(self):
483 self.assertMarkdownRenders(
484 self.dedent(
485 """
486 ### This is an H3
487 Followed by a Paragraph with no blank line.
488 """
489 ),
490 self.dedent(
491 """
492 <h3>This is an H3</h3>
493 <p>Followed by a Paragraph with no blank line.</p>
494 """
495 )
496 )
497
498 def test_hash_h4_followed_by_p(self):
499 self.assertMarkdownRenders(
500 self.dedent(
501 """
502 #### This is an H4
503 Followed by a Paragraph with no blank line.
504 """
505 ),
506 self.dedent(
507 """
508 <h4>This is an H4</h4>
509 <p>Followed by a Paragraph with no blank line.</p>
510 """
511 )
512 )
513
514 def test_hash_h5_followed_by_p(self):
515 self.assertMarkdownRenders(
516 self.dedent(
517 """
518 ##### This is an H5
519 Followed by a Paragraph with no blank line.
520 """
521 ),
522 self.dedent(
523 """
524 <h5>This is an H5</h5>
525 <p>Followed by a Paragraph with no blank line.</p>
526 """
527 )
528 )
529
530 def test_hash_h6_followed_by_p(self):
531 self.assertMarkdownRenders(
532 self.dedent(
533 """
534 ###### This is an H6
535 Followed by a Paragraph with no blank line.
536 """
537 ),
538 self.dedent(
539 """
540 <h6>This is an H6</h6>
541 <p>Followed by a Paragraph with no blank line.</p>
542 """
543 )
544 )
545
546 def test_hash_h1_leading_space(self):
547 self.assertMarkdownRenders(
548 ' # This is an H1',
549
550 '<p># This is an H1</p>'
551 )
552
553 def test_hash_h2_leading_space(self):
554 self.assertMarkdownRenders(
555 ' ## This is an H2',
556
557 '<p>## This is an H2</p>'
558 )
559
560 def test_hash_h3_leading_space(self):
561 self.assertMarkdownRenders(
562 ' ### This is an H3',
563
564 '<p>### This is an H3</p>'
565 )
566
567 def test_hash_h4_leading_space(self):
568 self.assertMarkdownRenders(
569 ' #### This is an H4',
570
571 '<p>#### This is an H4</p>'
572 )
573
574 def test_hash_h5_leading_space(self):
575 self.assertMarkdownRenders(
576 ' ##### This is an H5',
577
578 '<p>##### This is an H5</p>'
579 )
580
581 def test_hash_h6_leading_space(self):
582 self.assertMarkdownRenders(
583 ' ###### This is an H6',
584
585 '<p>###### This is an H6</p>'
586 )
587
588 def test_hash_h1_open_trailing_space(self):
589 self.assertMarkdownRenders(
590 '# This is an H1 ',
591
592 '<h1>This is an H1</h1>'
593 )
594
595 def test_hash_h2_open_trailing_space(self):
596 self.assertMarkdownRenders(
597 '## This is an H2 ',
598
599 '<h2>This is an H2</h2>'
600 )
601
602 def test_hash_h3_open_trailing_space(self):
603 self.assertMarkdownRenders(
604 '### This is an H3 ',
605
606 '<h3>This is an H3</h3>'
607 )
608
609 def test_hash_h4_open_trailing_space(self):
610 self.assertMarkdownRenders(
611 '#### This is an H4 ',
612
613 '<h4>This is an H4</h4>'
614 )
615
616 def test_hash_h5_open_trailing_space(self):
617 self.assertMarkdownRenders(
618 '##### This is an H5 ',
619
620 '<h5>This is an H5</h5>'
621 )
622
623 def test_hash_h6_open_trailing_space(self):
624 self.assertMarkdownRenders(
625 '###### This is an H6 ',
626
627 '<h6>This is an H6</h6>'
628 )
629
630 def test_hash_gt6_open_trailing_space(self):
631 self.assertMarkdownRenders(
632 '####### This is an H6 ',
633
634 '<h6># This is an H6</h6>'
635 )
636
637 # TODO: Possibly change the following behavior. While this follows the behavior
638 # of markdown.pl, it is rather uncommon and not necessarily intuitive.
639 # See: https://johnmacfarlane.net/babelmark2/?normalize=1&text=%23+This+is+an+H1+%23+
640 def test_hash_h1_closed_trailing_space(self):
641 self.assertMarkdownRenders(
642 '# This is an H1 # ',
643
644 '<h1>This is an H1 #</h1>'
645 )
646
647 def test_hash_h2_closed_trailing_space(self):
648 self.assertMarkdownRenders(
649 '## This is an H2 ## ',
650
651 '<h2>This is an H2 ##</h2>'
652 )
653
654 def test_hash_h3_closed_trailing_space(self):
655 self.assertMarkdownRenders(
656 '### This is an H3 ### ',
657
658 '<h3>This is an H3 ###</h3>'
659 )
660
661 def test_hash_h4_closed_trailing_space(self):
662 self.assertMarkdownRenders(
663 '#### This is an H4 #### ',
664
665 '<h4>This is an H4 ####</h4>'
666 )
667
668 def test_hash_h5_closed_trailing_space(self):
669 self.assertMarkdownRenders(
670 '##### This is an H5 ##### ',
671
672 '<h5>This is an H5 #####</h5>'
673 )
674
675 def test_hash_h6_closed_trailing_space(self):
676 self.assertMarkdownRenders(
677 '###### This is an H6 ###### ',
678
679 '<h6>This is an H6 ######</h6>'
680 )
681
682 def test_hash_gt6_closed_trailing_space(self):
683 self.assertMarkdownRenders(
684 '####### This is an H6 ####### ',
685
686 '<h6># This is an H6 #######</h6>'
687 )
688
689 def test_no_blank_lines_between_hashs(self):
690 self.assertMarkdownRenders(
691 self.dedent(
692 """
693 # This is an H1
694 ## This is an H2
695 """
696 ),
697 self.dedent(
698 """
699 <h1>This is an H1</h1>
700 <h2>This is an H2</h2>
701 """
702 )
703 )
704
705 def test_random_hash_levels(self):
706 self.assertMarkdownRenders(
707 self.dedent(
708 """
709 ### H3
710 ###### H6
711 # H1
712 ##### H5
713 #### H4
714 ## H2
715 ### H3
716 """
717 ),
718 self.dedent(
719 """
720 <h3>H3</h3>
721 <h6>H6</h6>
722 <h1>H1</h1>
723 <h5>H5</h5>
724 <h4>H4</h4>
725 <h2>H2</h2>
726 <h3>H3</h3>
727 """
728 )
729 )
730
731 def test_hash_followed_by_p(self):
732 self.assertMarkdownRenders(
733 self.dedent(
734 """
735 # This is an H1
736 Followed by a Paragraph with no blank line.
737 """
738 ),
739 self.dedent(
740 """
741 <h1>This is an H1</h1>
742 <p>Followed by a Paragraph with no blank line.</p>
743 """
744 )
745 )
746
747 def test_p_followed_by_hash(self):
748 self.assertMarkdownRenders(
749 self.dedent(
750 """
751 This is a Paragraph.
752 # Followed by an H1 with no blank line.
753 """
754 ),
755 self.dedent(
756 """
757 <p>This is a Paragraph.</p>
758 <h1>Followed by an H1 with no blank line.</h1>
759 """
760 )
761 )
762
763 def test_escaped_hash(self):
764 self.assertMarkdownRenders(
765 "### H3 \\###",
766 self.dedent(
767 """
768 <h3>H3 #</h3>
769 """
770 )
771 )
772
773 def test_unescaped_hash(self):
774 self.assertMarkdownRenders(
775 "### H3 \\\\###",
776 self.dedent(
777 """
778 <h3>H3 \\</h3>
779 """
780 )
781 )