Debian Codegraph @

tests/test_extensions/test_inlinehilite.py

raw
1 """Test cases for Highlight."""
2 from .. import util
3 import pymdownx.arithmatex as arithmatex
4 from pymdownx.inlinehilite import InlineHiliteException
5 import warnings
6
7
8 def _format(src, language, class_name, md):
9 """Inline math formatter."""
10
11 return '<span class="lang-{} {}">{}</span>'.format(language, class_name, src)
12
13
14 def _default_format(src, language, class_name, md):
15 """Inline math formatter."""
16
17 return '<custom class="lang-{} {}">{}</custom>'.format(language, class_name, src)
18
19
20 def _format_exploder(src, language, class_name, md):
21 """Inline math formatter."""
22
23 raise Exception('Boom!')
24
25
26 def _format_exploder_fail(src, language, class_name, md):
27 """Inline math formatter."""
28
29 raise InlineHiliteException('Boom!')
30
31
32 class TestInlineHilite(util.MdCase):
33 """Test general cases for inline highlight."""
34
35 extension = [
36 'markdown.extensions.attr_list',
37 'pymdownx.highlight',
38 'pymdownx.inlinehilite',
39 ]
40 extension_configs = {
41 'pymdownx.inlinehilite': {
42 'style_plain_text': True,
43 'css_class': 'inlinehilite'
44 }
45 }
46
47 def test_language(self):
48 """Test language handling."""
49
50 # Test #! original syntax
51 self.check_markdown(
52 r'`#!python import module`.',
53 R'<p><code class="inlinehilite">'
54 R'<span class="kn">import</span><span class="w"> </span><span class="nn">module</span>'
55 R'</code>.</p>'
56 )
57
58 # Test ::: syntax
59 self.check_markdown(
60 r'`:::python import module`.',
61 r'<p><code class="inlinehilite"><span class="kn">import</span><span class="w"> </span><span class="nn">module</span></code>.</p>' # noqa: E501
62 )
63
64 # Test escaping language with space
65 self.check_markdown(
66 r'` #!python import module`.',
67 r'<p><code class="inlinehilite">#!python import module</code>.</p>'
68 )
69
70 # Test bad language
71 self.check_markdown(
72 r'`#!bad import module`.',
73 r'<p><code class="inlinehilite">import module</code>.</p>'
74 )
75
76 def test_escape(self):
77 """Test backtick escape logic."""
78
79 self.check_markdown(
80 r'`Code`',
81 r'<p><code class="inlinehilite">Code</code></p>'
82 )
83
84 self.check_markdown(
85 r'\`Not code`',
86 r'<p>`Not code`</p>'
87 )
88
89 self.check_markdown(
90 r'\\`Code`',
91 r'<p>\<code class="inlinehilite">Code</code></p>'
92 )
93
94 self.check_markdown(
95 r'\\\`Not code`',
96 r'<p>\`Not code`</p>'
97 )
98
99 self.check_markdown(
100 r'\\\\`Code`',
101 r'<p>\\<code class="inlinehilite">Code</code></p>'
102 )
103
104 def test_attributes(self):
105 """Test with attribute extension."""
106
107 self.check_markdown(
108 r'`#!python import module`{: .test}',
109 r'<p><code class="inlinehilite test">'
110 '<span class="kn">import</span><span class="w"> </span><span class="nn">module</span>'
111 '</code></p>'
112 )
113
114
115 class TestInlineHilitePlainText(util.MdCase):
116 """Test inline highlight when not styling plain text."""
117
118 extension = [
119 'pymdownx.highlight',
120 'pymdownx.inlinehilite',
121 ]
122 extension_configs = {
123 'pymdownx.inlinehilite': {
124 'style_plain_text': False
125 }
126 }
127
128 def test_unstyled_plaintext(self):
129 """Test unstyled plain text."""
130
131 self.check_markdown(
132 r'Lets test inline highlight no guessing and no text styling `import module`.',
133 r'<p>Lets test inline highlight no guessing and no text styling <code>import module</code>.</p>'
134 )
135
136
137 class TestInlineHiliteNoClass(util.MdCase):
138 """Test with no class."""
139
140 extension = [
141 'pymdownx.highlight',
142 'pymdownx.inlinehilite',
143 ]
144 extension_configs = {
145 'pymdownx.highlight': {
146 'css_class': ''
147 }
148 }
149
150 def test_no_class(self):
151 """Test with no class."""
152
153 self.check_markdown(
154 r'Lets test inline highlight no guessing and no text styling `#!python import module`.',
155 r'<p>Lets test inline highlight no guessing and no text styling <code><span class="kn">import</span><span class="w"> </span><span class="nn">module</span></code>.</p>' # noqa: E501
156 )
157
158
159 class TestInlineHiliteNoClassNoPygments(util.MdCase):
160 """Test with no class and no Pygments."""
161
162 extension = [
163 'pymdownx.highlight',
164 'pymdownx.inlinehilite',
165 ]
166 extension_configs = {
167 'pymdownx.highlight': {
168 'css_class': '',
169 'use_pygments': False
170 }
171 }
172
173 def test_no_class_no_pygments(self):
174 """Test with no class and no Pygments."""
175
176 self.check_markdown(
177 r'Lets test inline highlight no guessing and no text styling `#!python import module`.',
178 r'<p>Lets test inline highlight no guessing and no text styling <code class="language-python">import module</code>.</p>' # noqa: E501
179 )
180
181
182 class TestInlineHiliteNoPygments(util.MdCase):
183 """Test inline highlight without Pygments."""
184
185 extension = [
186 'pymdownx.highlight',
187 'pymdownx.inlinehilite',
188 ]
189 extension_configs = {
190 'pymdownx.highlight': {
191 'use_pygments': False
192 },
193 'pymdownx.inlinehilite': {
194 'css_class': 'inlinehilite'
195 }
196 }
197
198 def test_no_pygments(self):
199 """Ensure proper behavior when disabling Pygments."""
200
201 self.check_markdown(
202 r'`#!python import module`.',
203 r'<p><code class="language-python inlinehilite">import module</code>.</p>'
204 )
205
206
207 class TestInlineHiliteGuess(util.MdCase):
208 """Test inline highlight with guessing."""
209
210 extension = [
211 'pymdownx.highlight',
212 'pymdownx.inlinehilite',
213 ]
214 extension_configs = {
215 'pymdownx.highlight': {
216 'guess_lang': True
217 },
218 'pymdownx.inlinehilite': {
219 'css_class': 'inlinehilite',
220 'style_plain_text': True
221 }
222 }
223
224 def test_guessing(self):
225 """Ensure guessing can be enabled."""
226
227 self.check_markdown(
228 r'`import module`.',
229 r'<p><code class="inlinehilite"><span class="kn">import</span><span class="w"> </span><span class="nn">module</span></code>.</p>' # noqa: E501
230 )
231
232
233 class TestInlineHiliteGuessInline(util.MdCase):
234 """Test inline highlight with guessing set to be inline only."""
235
236 extension = [
237 'pymdownx.highlight',
238 'pymdownx.inlinehilite',
239 'pymdownx.superfences'
240 ]
241 extension_configs = {
242 'pymdownx.highlight': {
243 'guess_lang': 'inline'
244 },
245 'pymdownx.inlinehilite': {
246 'css_class': 'inlinehilite',
247 'style_plain_text': True
248 }
249 }
250
251 def test_guessing_inline(self):
252 """Ensure guessing can be enabled for inline only."""
253
254 self.check_markdown(
255 r'`import module`.',
256 r'<p><code class="inlinehilite"><span class="kn">import</span><span class="w"> </span><span class="nn">module</span></code>.</p>' # noqa: E501
257 )
258
259 def test_no_guessing_block(self):
260 """Ensure block is not guessed when set as inline only."""
261
262 self.check_markdown(
263 r'''
264 ```
265 <!DOCTYPE html>
266 <html>
267 <body>
268 <h1>My great test</h1>
269 <p>Thou shalt be re-educated through labour should this test ever fails.</p>
270 </body>
271 </html>
272 ```
273 ''',
274 r'''
275 <div class="highlight"><pre><span></span><code>&lt;!DOCTYPE html&gt;
276 &lt;html&gt;
277 &lt;body&gt;
278 &lt;h1&gt;My great test&lt;/h1&gt;
279 &lt;p&gt;Thou shalt be re-educated through labour should this test ever fails.&lt;/p&gt;
280 &lt;/body&gt;
281 &lt;/html&gt;
282 </code></pre></div>
283 ''',
284 True
285 )
286
287
288 class TestInlineHiliteCodeHilite(util.MdCase):
289 """Test inline highlight with CodeHilite."""
290
291 extension = [
292 'markdown.extensions.codehilite',
293 'pymdownx.inlinehilite',
294 ]
295 extension_configs = {
296 'markdown.extensions.codehilite': {
297 'guess_lang': False
298 },
299 'pymdownx.inlinehilite': {
300 'style_plain_text': True
301 }
302 }
303
304 def test_codehilite(self):
305 """Test CodeHilite."""
306
307 # Test #! original syntax
308 self.check_markdown(
309 r'`#!python import module`.',
310 r'<p><code class="highlight"><span class="kn">import</span><span class="w"> </span><span class="nn">module</span></code>.</p>' # noqa: E501
311 )
312
313 # Test ::: syntax
314 self.check_markdown(
315 r'`:::python import module`.',
316 r'<p><code class="highlight"><span class="kn">import</span><span class="w"> </span><span class="nn">module</span></code>.</p>' # noqa: E501
317 )
318
319 # Test escaping language with space
320 self.check_markdown(
321 r'` #!python import module`.',
322 r'<p><code class="highlight">#!python import module</code>.</p>'
323 )
324
325 # Test bad language
326 self.check_markdown(
327 r'`#!bad import module`.',
328 r'<p><code class="highlight">import module</code>.</p>'
329 )
330
331
332 class TestInlineHiliteCustom1(util.MdCase):
333 """Test custom InlineHilite cases."""
334
335 extension = [
336 'pymdownx.highlight',
337 'pymdownx.inlinehilite',
338 ]
339 extension_configs = {
340 'pymdownx.inlinehilite': {
341 'css_class': 'inlinehilite',
342 'custom_inline': [
343 {
344 'name': 'math',
345 'class': 'arithmatex',
346 'format': arithmatex.arithmatex_inline_format(mode='mathjax')
347 }
348 ]
349 }
350 }
351
352 def test_arithmatex(self):
353 """Test Arithmatex."""
354
355 self.check_markdown(
356 r'`#!math 3 + 3`',
357 r'''
358 <p><span class="arithmatex"><script type="math/tex">3 + 3</script></span></p>
359 ''',
360 True
361 )
362
363
364 class TestLegacyInlineHiliteCustom1(util.MdCase):
365 """Test custom InlineHilite cases."""
366
367 extension = [
368 'pymdownx.highlight',
369 'pymdownx.inlinehilite',
370 ]
371 extension_configs = {
372 'pymdownx.inlinehilite': {
373 'css_class': 'inlinehilite',
374 'custom_inline': [
375 {
376 'name': 'math',
377 'class': 'arithmatex',
378 'format': arithmatex.inline_mathjax_format
379 }
380 ]
381 }
382 }
383
384 def test_legacy_arithmatex(self):
385 """Test Arithmatex."""
386
387 with warnings.catch_warnings(record=True) as w:
388 self.check_markdown(
389 r'`#!math 3 + 3`',
390 r'''
391 <p><span class="arithmatex"><script type="math/tex">3 + 3</script></span></p>
392 ''',
393 True
394 )
395 self.assertTrue(len(w) == 1)
396 self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
397
398
399 class TestInlineHiliteCustom2(util.MdCase):
400 """Test custom InlineHilite cases."""
401
402 extension = [
403 'pymdownx.highlight',
404 'pymdownx.inlinehilite',
405 ]
406 extension_configs = {
407 'pymdownx.inlinehilite': {
408 'css_class': 'inlinehilite',
409 'custom_inline': [
410 {
411 'name': 'math',
412 'class': 'arithmatex',
413 'format': arithmatex.arithmatex_inline_format(mode="mathjax", preview=True)
414 }
415 ]
416 }
417 }
418
419 def test_preview_arithmatex(self):
420 """Test preview Arithmatex."""
421
422 self.check_markdown(
423 r'`#!math 3 + 3`',
424 r'<p><span class="arithmatex"><span class="MathJax_Preview">3 + 3</span>'
425 r'<script type="math/tex">3 + 3</script></span></p>'
426 )
427
428
429 class TestLegacyInlineHiliteCustom2(util.MdCase):
430 """Test custom InlineHilite cases."""
431
432 extension = [
433 'pymdownx.highlight',
434 'pymdownx.inlinehilite',
435 ]
436 extension_configs = {
437 'pymdownx.inlinehilite': {
438 'css_class': 'inlinehilite',
439 'custom_inline': [
440 {
441 'name': 'math',
442 'class': 'arithmatex',
443 'format': arithmatex.inline_mathjax_preview_format
444 }
445 ]
446 }
447 }
448
449 def test_legacy_preview_arithmatex(self):
450 """Test preview Arithmatex."""
451
452 with warnings.catch_warnings(record=True) as w:
453 self.check_markdown(
454 r'`#!math 3 + 3`',
455 r'<p><span class="arithmatex"><span class="MathJax_Preview">3 + 3</span>'
456 r'<script type="math/tex">3 + 3</script></span></p>'
457 )
458 self.assertTrue(len(w) == 1)
459 self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
460
461
462 class TestInlineHiliteCustom3(util.MdCase):
463 """Test custom InlineHilite cases."""
464
465 extension = [
466 'pymdownx.highlight',
467 'pymdownx.inlinehilite',
468 ]
469 extension_configs = {
470 'pymdownx.inlinehilite': {
471 'css_class': 'inlinehilite',
472 'custom_inline': [
473 {
474 'name': 'math',
475 'class': 'arithmatex',
476 'format': arithmatex.arithmatex_inline_format(mode="generic")
477 }
478 ]
479 }
480 }
481
482 def test_arithmatex_generic(self):
483 """Test generic Arithmatex."""
484
485 self.check_markdown(
486 r'`#!math 3 + 3`',
487 r'<p><span class="arithmatex">\(3 + 3\)</span></p>'
488 )
489
490
491 class TestLegacyInlineHiliteCustom3(util.MdCase):
492 """Test custom InlineHilite cases."""
493
494 extension = [
495 'pymdownx.highlight',
496 'pymdownx.inlinehilite',
497 ]
498 extension_configs = {
499 'pymdownx.inlinehilite': {
500 'css_class': 'inlinehilite',
501 'custom_inline': [
502 {
503 'name': 'math',
504 'class': 'arithmatex',
505 'format': arithmatex.inline_generic_format
506 }
507 ]
508 }
509 }
510
511 def test_legacy_arithmatex_generic(self):
512 """Test generic Arithmatex."""
513
514 with warnings.catch_warnings(record=True) as w:
515 self.check_markdown(
516 r'`#!math 3 + 3`',
517 r'<p><span class="arithmatex">\(3 + 3\)</span></p>'
518 )
519 self.assertTrue(len(w) == 1)
520 self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
521
522
523 class TestInlineHiliteCustom4(util.MdCase):
524 """Test custom InlineHilite cases."""
525
526 extension = [
527 'pymdownx.highlight',
528 'pymdownx.inlinehilite',
529 ]
530 extension_configs = {
531 'pymdownx.inlinehilite': {
532 'css_class': 'inlinehilite',
533 'custom_inline': [
534 {
535 'name': 'test',
536 'class': 'class-test',
537 'format': _format
538 }
539 ]
540 }
541 }
542
543 def test_custom(self):
544 """Test custom formatter."""
545
546 self.check_markdown(
547 r'`#!test src test`',
548 r'<p><span class="lang-test class-test">src test</span></p>'
549 )
550
551
552 class TestInlineHiliteCustom5(util.MdCase):
553 """Test custom InlineHilite cases."""
554
555 extension = [
556 'pymdownx.highlight',
557 'pymdownx.inlinehilite',
558 ]
559 extension_configs = {
560 'pymdownx.inlinehilite': {
561 'css_class': 'inlinehilite',
562 'custom_inline': [
563 {
564 'name': '*',
565 'class': 'overwrite',
566 'format': _default_format
567 },
568 {
569 'name': 'test',
570 'class': 'class-test',
571 'format': _format
572 }
573 ]
574 }
575 }
576
577 def test_custom(self):
578 """Test custom formatter."""
579
580 self.check_markdown(
581 r'`#!test src test` `#!python src test`',
582 r'<p><span class="lang-test class-test">src test</span> <custom class="lang-python overwrite">src test</custom></p>' # noqa: E501
583 )
584
585
586 class TestInlineHiliteCustomBrokenFormatter(util.MdCase):
587 """Test custom broken InlineHilite cases."""
588
589 extension = [
590 'pymdownx.highlight',
591 'pymdownx.inlinehilite',
592 ]
593 extension_configs = {
594 'pymdownx.inlinehilite': {
595 'custom_inline': [
596 {
597 'name': 'test',
598 'class': 'test',
599 'format': _format_exploder
600 }
601 ]
602 }
603 }
604
605 def test_broken(self):
606 """Test custom broken formatter."""
607
608 self.check_markdown(
609 r'`#!test boom`',
610 r'<p>`#!test boom`</p>'
611 )
612
613
614 class TestInlineHiliteCustomBrokenFormatterFail(util.MdCase):
615 """Test custom broken InlineHilite cases fails."""
616
617 extension = [
618 'pymdownx.highlight',
619 'pymdownx.inlinehilite',
620 ]
621 extension_configs = {
622 'pymdownx.inlinehilite': {
623 'custom_inline': [
624 {
625 'name': 'test',
626 'class': 'test',
627 'format': _format_exploder_fail
628 }
629 ]
630 }
631 }
632
633 def test_broken(self):
634 """Test custom broken formatter."""
635
636 with self.assertRaises(InlineHiliteException):
637 self.check_markdown(
638 r'`#!test boom`',
639 r''
640 )