Debian Codegraph @

tests/test_extensions/test_highlight.py

raw
1 """Test cases for Highlight."""
2 from .. import util
3
4
5 class TestHighlightGuess(util.MdCase):
6 """Test that highlighting works with guessing."""
7
8 extension = ['pymdownx.highlight', 'pymdownx.superfences']
9 extension_configs = {
10 'pymdownx.highlight': {
11 'guess_lang': True
12 }
13 }
14
15 def test_guess(self):
16 """Test guessing."""
17
18 self.check_markdown(
19 r'''
20 ```
21 import test
22 test.test()
23 ```
24 ''',
25 '''
26 <div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
27 <span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
28 </code></pre></div>
29 ''', # noqa: E501
30 True
31 )
32
33
34 class TestHighlightGuessBlock(util.MdCase):
35 """Test that highlighting works with guessing for block."""
36
37 extension = ['pymdownx.highlight', 'pymdownx.superfences']
38 extension_configs = {
39 'pymdownx.highlight': {
40 'guess_lang': "block"
41 }
42 }
43
44 def test_guess_block(self):
45 """Test guessing for block."""
46
47 self.check_markdown(
48 r'''
49 ```
50 import test
51 test.test()
52 ```
53 ''',
54 '''
55 <div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
56 <span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
57 </code></pre></div>
58 ''', # noqa: E501
59 True
60 )
61
62 def test_no_guess_inline(self):
63 """Test inline code is not language guessed."""
64 self.check_markdown(
65 r'''
66 `int i = std::numeric_limits<int>::min();`
67 ''',
68 '''
69 <p><code>int i = std::numeric_limits&lt;int&gt;::min();</code></p>
70 ''',
71 True
72 )
73
74
75 class TestHighlightAutoTitle(util.MdCase):
76 """Test title cases."""
77
78 extension = ['pymdownx.highlight', 'pymdownx.superfences']
79 extension_configs = {
80 'pymdownx.highlight': {
81 'auto_title': True
82 }
83 }
84
85 def test_auto_tile(self):
86 """Test auto title."""
87
88 self.check_markdown(
89 r'''
90 ```pycon
91 >>> import test
92 ```
93 ''',
94 r'''
95 <div class="highlight"><span class="filename">Python Console Session</span><pre><span></span><code><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
96 </code></pre></div>
97 ''', # noqa: E501
98 True
99 )
100
101
102 class TestHighlightAutoTitleMap(util.MdCase):
103 """Test title cases."""
104
105 extension = ['pymdownx.highlight', 'pymdownx.superfences']
106 extension_configs = {
107 'pymdownx.highlight': {
108 'auto_title': True,
109 "auto_title_map": {
110 "Python Console Session": "Python"
111 }
112 }
113 }
114
115 def test_auto_tile_map(self):
116 """Test auto title."""
117
118 self.check_markdown(
119 r'''
120 ```pycon
121 >>> import test
122 ```
123 ''',
124 r'''
125 <div class="highlight"><span class="filename">Python</span><pre><span></span><code><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
126 </code></pre></div>
127 ''', # noqa: E501
128 True
129 )
130
131
132 class TestHighlightInline(util.MdCase):
133 """Test highlight inline."""
134
135 extension = ['pymdownx.highlight', 'pymdownx.superfences']
136 extension_configs = {
137 'pymdownx.highlight': {
138 'linenums_style': 'pymdownx-inline'
139 }
140 }
141
142 def test_pymdownx_inline(self):
143 """Test new inline mode."""
144
145 self.check_markdown(
146 r'''
147 ```python linenums="1"
148 import test
149 test.test()
150 ```
151 ''',
152 r'''
153 <div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="1 "></span><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
154 <span class="linenos" data-linenos="2 "></span><span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
155 </code></pre></div>
156 ''', # noqa: E501
157 True
158 )
159
160
161 class TestNoClass(util.MdCase):
162 """Test no class."""
163
164 extension = ['pymdownx.highlight', 'pymdownx.superfences', 'markdown.extensions.attr_list']
165 extension_configs = {
166 'pymdownx.highlight': {
167 'css_class': ''
168 }
169 }
170
171 def test_no_class(self):
172 """Test with no class."""
173
174 self.check_markdown(
175 r'''
176 ```python
177 import test
178 test.test()
179 ```
180 ''',
181 r'''
182 <div><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
183 <span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
184 </code></pre></div>
185 ''', # noqa: E501
186 True
187 )
188
189 def test_no_class_and_user_class(self):
190 """Test with no class and user class."""
191
192 self.check_markdown(
193 r'''
194 ```{.python .more}
195 import test
196 test.test()
197 ```
198 ''',
199 r'''
200 <div class="more"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
201 <span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
202 </code></pre></div>
203 ''', # noqa: E501
204 True
205 )
206
207 def test_no_class_and_user_class_linenums(self):
208 """Test with no class and user class and table format."""
209
210 self.check_markdown(
211 r'''
212 ```{.python .more linenums="1"}
213 import test
214 test.test()
215 ```
216 ''',
217 r'''
218 <div class="more"><table class="table"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
219 <span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
220 <span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
221 </code></pre></div></td></tr></table></div>
222 ''', # noqa: E501
223 True
224 )
225
226
227 class TestNoClassNoPygments(util.MdCase):
228 """Test no class."""
229
230 extension = ['pymdownx.highlight', 'pymdownx.superfences']
231 extension_configs = {
232 'pymdownx.highlight': {
233 'css_class': '',
234 'use_pygments': False
235 }
236 }
237
238 def test_no_class_no_pygments(self):
239 """Test with no class and no Pygments."""
240
241 self.check_markdown(
242 r'''
243 ```python
244 import test
245 test.test()
246 ```
247 ''',
248 r'''
249 <pre><code class="language-python">import test
250 test.test()</code></pre>
251 ''',
252 True
253 )
254
255
256 class TestCustomLangPrefixNoPygments(util.MdCase):
257 """Test custom language prefix."""
258
259 extension = ['pymdownx.highlight', 'pymdownx.superfences', 'pymdownx.inlinehilite']
260 extension_configs = {
261 'pymdownx.highlight': {
262 'language_prefix': 'lang-',
263 'use_pygments': False
264 }
265 }
266
267 def test_custom_prefix_no_pygments(self):
268 """Test with custom prefix and no Pygments."""
269
270 self.check_markdown(
271 r'''
272 ```python
273 import test
274 test.test()
275 ```
276 ''',
277 r'''
278 <pre class="highlight"><code class="lang-python">import test
279 test.test()</code></pre>
280 ''',
281 True
282 )
283
284 def test_custom_prefix_no_pygments_inline(self):
285 """Test with custom prefix and no Pygments with inline code."""
286
287 self.check_markdown(
288 '`#!python import test`',
289 '<p><code class="lang-python highlight">import test</code></p>'
290 )
291
292
293 class TestNoPygments(util.MdCase):
294 """Test no Pygments."""
295
296 extension = ['pymdownx.highlight', 'pymdownx.superfences']
297 extension_configs = {
298 'pymdownx.highlight': {
299 'use_pygments': False
300 }
301 }
302
303 def test_no_pygments(self):
304 """Test with no Pygments."""
305
306 self.check_markdown(
307 r'''
308 ```python
309 import test
310 test.test()
311 ```
312 ''',
313 r'''
314 <pre class="highlight"><code class="language-python">import test
315 test.test()</code></pre>
316 ''',
317 True
318 )
319
320 def test_no_pygments_linenums(self):
321 """Test with no Pygments and line numbers."""
322
323 self.check_markdown(
324 r'''
325 ```python linenums="1"
326 import test
327 test.test()
328 ```
329 ''',
330 r'''
331 <p><code>python linenums="1"
332 import test
333 test.test()</code></p>
334 ''',
335 True
336 )
337
338 def test_no_pygments_extraneous_new_lines(self):
339 """Test that extraneous new lines are not included."""
340
341 self.check_markdown(
342 r'''
343 Test
344
345 import test
346 test.test()
347
348
349 Test
350 ''',
351 r'''
352 <p>Test</p>
353 <pre class="highlight"><code>import test
354 test.test()</code></pre>
355 <p>Test</p>
356 ''',
357 True
358 )
359
360
361 class TestNoPygmentsCustomLineClass(util.MdCase):
362 """Test no Pygments with custom line number class."""
363
364 extension = ['pymdownx.highlight', 'pymdownx.superfences']
365 extension_configs = {
366 'pymdownx.highlight': {
367 'use_pygments': False,
368 'linenums_class': 'line-numbers',
369 'linenums': True
370 }
371 }
372
373 def test_no_pygments_linenums_custom_class(self):
374 """Test with no Pygments and line numbers."""
375
376 self.check_markdown(
377 r'''
378 Text
379
380 import test
381 test.test()
382
383 Text
384 ''',
385 r'''
386 <p>Text</p>
387 <pre class="highlight"><code>import test
388 test.test()</code></pre>
389 <p>Text</p>
390 ''',
391 True
392 )
393
394 def test_no_pygments_linenums_custom_class_fences(self):
395 """Test with no Pygments and line numbers in fences."""
396
397 self.check_markdown(
398 r'''
399 ```python
400 import test
401 test.test()
402 ```
403 ''',
404 r'''
405 <pre class="highlight"><code class="language-python">import test
406 test.test()</code></pre>
407 ''',
408 True
409 )
410
411
412 class TestHighlightSpecial(util.MdCase):
413 """Test highlight global special."""
414
415 extension = ['pymdownx.highlight', 'pymdownx.superfences']
416 extension_configs = {
417 'pymdownx.highlight': {
418 'linenums_style': 'pymdownx-inline',
419 'linenums_special': 2
420 }
421 }
422
423 def test_special(self):
424 """Test global special mode."""
425
426 self.check_markdown(
427 r'''
428 ```python linenums="1"
429 import test
430 test.test()
431 ```
432 ''',
433 r'''
434 <div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="1 "></span><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
435 <span class="linenos special" data-linenos="2 "></span><span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
436 </code></pre></div>
437 ''', # noqa: E501
438 True
439 )
440
441 def test_special_override(self):
442 """Test global special mode override."""
443
444 self.check_markdown(
445 r'''
446 ```python linenums="1 1 1"
447 import test
448 test.test()
449 ```
450 ''',
451 r'''
452 <div class="highlight"><pre><span></span><code><span class="linenos special" data-linenos="1 "></span><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
453 <span class="linenos special" data-linenos="2 "></span><span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
454 </code></pre></div>
455 ''', # noqa: E501
456 True
457 )
458
459
460 class TestDisabledLinenums(util.MdCase):
461 """Test with line numbers globally disabled."""
462
463 extension = ['pymdownx.highlight', 'pymdownx.superfences']
464 extension_configs = {
465 'pymdownx.highlight': {
466 'linenums': False
467 }
468 }
469
470 def test_global_disable(self):
471 """Test with line numbers globally disabled."""
472
473 self.check_markdown(
474 r'''
475 ```python linenums="1"
476 import test
477 test.test()
478 ```
479 ''',
480 r'''
481 <div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
482 <span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
483 </code></pre></div>
484 ''', # noqa: E501
485 True
486 )
487
488
489 class TestDisabledLinenumsNoPygments(util.MdCase):
490 """Test with line numbers globally disabled with no Pygments."""
491
492 extension = ['pymdownx.highlight', 'pymdownx.superfences']
493 extension_configs = {
494 'pymdownx.highlight': {
495 'linenums': False,
496 'use_pygments': False
497 }
498 }
499
500 def test_global_disable(self):
501 """Test with line numbers globally disabled and no Pygments."""
502
503 self.check_markdown(
504 r'''
505 ```python linenums="1"
506 import test
507 test.test()
508 ```
509 ''',
510 r'''
511 <p><code>python linenums="1"
512 import test
513 test.test()</code></p>
514 ''',
515 True
516 )
517
518
519 class TestGlobalLinenums(util.MdCase):
520 """Test global line number cases."""
521
522 extension = ['pymdownx.highlight', 'pymdownx.superfences']
523 extension_configs = {
524 'pymdownx.highlight': {
525 'linenums': True
526 }
527 }
528
529 def test_global_line_numbers(self):
530 """Test that global line numbers works."""
531
532 self.check_markdown(
533 r'''
534 ```python
535 import test
536 test.test()
537 ```
538 ''',
539 r'''
540 <div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
541 <span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
542 <span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
543 </code></pre></div></td></tr></table></div>
544 ''', # noqa: E501
545 True
546 )
547
548 def test_global_disabling_of_line_numbers(self):
549 """Test that global line numbers can be disabled."""
550
551 self.check_markdown(
552 r'''
553 ```{.python linenums="0"}
554 import test
555 test.test()
556 ```
557 ''',
558 r'''
559 <div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
560 <span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
561 </code></pre></div>
562 ''', # noqa: E501
563 True
564 )
565
566
567 class TestPygmentsLangClass(util.MdCase):
568 """Test no Pygments with custom line number class."""
569
570 extension = ['pymdownx.highlight', 'pymdownx.inlinehilite', 'pymdownx.superfences']
571 extension_configs = {
572 'pymdownx.highlight': {
573 'pygments_lang_class': True
574 }
575 }
576
577 def test_superfences(self):
578 """Test language classes with SuperFences."""
579
580 self.check_markdown(
581 r'''
582 ```python
583 import test
584 ```
585 ''',
586 r'''
587 <div class="language-python highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">test</span>
588 </code></pre></div>
589 ''', # noqa: E501
590 True
591 )
592
593 def test_inlinehilite(self):
594 """Test language classes with InlineHilite."""
595
596 self.check_markdown(
597 '''
598 `#!python import test`
599 ''',
600 '''
601 <p><code class="language-python highlight"><span class="kn">import</span><span class="w"> </span><span class="nn">test</span></code></p>
602 ''', # noqa: E501
603 True
604 )
605
606
607 class TestExtendedLang(util.MdCase):
608 """Test extended language cases."""
609
610 extension = ['pymdownx.highlight', 'pymdownx.superfences', 'pymdownx.inlinehilite']
611 extension_configs = {
612 'pymdownx.highlight': {
613 'extend_pygments_lang': [
614 {'name': 'php-inline', 'lang': 'php', 'options': {'startinline': True}}
615 ]
616 }
617 }
618
619 def test_extended_lang_inlinehilite(self):
620 """Test extended language in InlineHilite."""
621
622 self.check_markdown(
623 '''
624 `#!php-inline $a = array("foo" => 0, "bar" => 1);`
625 ''',
626 '''
627 <p><code class="highlight"><span class="nv">$a</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s2">&quot;foo&quot;</span> <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;bar&quot;</span> <span class="o">=&gt;</span> <span class="mi">1</span><span class="p">);</span></code></p>
628 ''', # noqa: E501
629 True
630 )
631
632 def test_extended_lang_superfences(self):
633 """Test extended language in SuperFences."""
634
635 self.check_markdown(
636 '''
637 ```php-inline
638 $a = array("foo" => 0, "bar" => 1);
639 ```
640 ''',
641 '''
642 <div class="highlight"><pre><span></span><code><span class="nv">$a</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s2">&quot;foo&quot;</span> <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;bar&quot;</span> <span class="o">=&gt;</span> <span class="mi">1</span><span class="p">);</span>
643 </code></pre></div>
644 ''', # noqa: E501
645 True
646 )
647
648 def test_extended_lang_case(self):
649 """Test extended language in SuperFences."""
650
651 self.check_markdown(
652 '''
653 ```PHP-Inline
654 $a = array("foo" => 0, "bar" => 1);
655 ```
656 ''',
657 '''
658 <div class="highlight"><pre><span></span><code><span class="nv">$a</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s2">&quot;foo&quot;</span> <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;bar&quot;</span> <span class="o">=&gt;</span> <span class="mi">1</span><span class="p">);</span>
659 </code></pre></div>
660 ''', # noqa: E501
661 True
662 )
663
664
665 class TestDefaultLang(util.MdCase):
666 """Test default block language cases."""
667
668 extension = ['pymdownx.highlight', 'pymdownx.superfences', 'pymdownx.inlinehilite']
669 extension_configs = {
670 'pymdownx.highlight': {
671 'default_lang': 'python'
672 }
673 }
674
675 def test_default_block(self):
676 """Test that default language affects block, but not inline code."""
677
678 self.check_markdown(
679 '''
680 `import code`
681
682 import code
683
684 ```
685 import code
686 ```
687 ''',
688 '''
689 <p><code>import code</code></p>
690 <div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">code</span>
691 </code></pre></div>
692 <div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">code</span>
693 </code></pre></div>
694 ''', # noqa: E501
695 True
696 )
697
698
699 class TestDefaultLangInline(util.MdCase):
700 """Test default inline language cases."""
701
702 extension = ['pymdownx.highlight', 'pymdownx.superfences', 'pymdownx.inlinehilite']
703 extension_configs = {
704 'pymdownx.inlinehilite': {
705 'style_plain_text': 'python'
706 }
707 }
708
709 def test_default_inline(self):
710 """Test that default language affects block, but not inline code."""
711
712 self.check_markdown(
713 '''
714 `import code`
715
716 import code
717
718 ```
719 import code
720 ```
721 ''',
722 '''
723 <p><code class="highlight"><span class="kn">import</span><span class="w"> </span><span class="nn">code</span></code></p>
724 <div class="highlight"><pre><span></span><code>import code
725 </code></pre></div>
726 <div class="highlight"><pre><span></span><code>import code
727 </code></pre></div>
728 ''', # noqa: E501
729 True
730 )