From 12c3378ab9a46fae416a7bb5549e3f123a11d22a Mon Sep 17 00:00:00 2001 From: Liang-Bo Wang Date: Thu, 19 May 2022 02:08:55 +0800 Subject: Pass language to Pygments formatter in CodeHilite * Add an extra option `lang_str` to pass the language of the code block to the specified Pygments formatter. * Include an example custom Pygments formatter in the documentation that includes the language of the code in the output using the new option. Resolves #1255. --- tests/test_syntax/extensions/test_code_hilite.py | 86 ++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'tests/test_syntax/extensions/test_code_hilite.py') diff --git a/tests/test_syntax/extensions/test_code_hilite.py b/tests/test_syntax/extensions/test_code_hilite.py index 41502d9..09dd523 100644 --- a/tests/test_syntax/extensions/test_code_hilite.py +++ b/tests/test_syntax/extensions/test_code_hilite.py @@ -354,6 +354,22 @@ class TestCodeHiliteExtension(TestCase): if has_pygments and pygments.__version__ != required_pygments_version: self.skipTest(f'Pygments=={required_pygments_version} is required') + # Define a custom Pygments formatter (same example in the documentation) + if has_pygments: + class CustomAddLangHtmlFormatter(pygments.formatters.HtmlFormatter): + def __init__(self, lang_str='', **options): + super().__init__(**options) + self.lang_str = lang_str + + def _wrap_code(self, source): + yield 0, f'' + yield from source + yield 0, '' + else: + CustomAddLangHtmlFormatter = None + + self.custom_pygments_formatter = CustomAddLangHtmlFormatter + maxDiff = None def testBasicCodeHilite(self): @@ -676,3 +692,73 @@ class TestCodeHiliteExtension(TestCase): expected, extensions=[CodeHiliteExtension(pygments_style="native", noclasses=True)] ) + + def testFormatterLangStr(self): + if has_pygments: + expected = ( + '
'
+                '# A Code Comment\n'
+                '
' + ) + else: + expected = ( + '
# A Code Comment\n'
+                '
' + ) + + self.assertMarkdownRenders( + '\t:::Python\n' + '\t# A Code Comment', + expected, + extensions=[ + CodeHiliteExtension( + guess_lang=False, + pygments_formatter=self.custom_pygments_formatter + ) + ] + ) + + def testFormatterLangStrGuessLang(self): + if has_pygments: + expected = ( + '
'
+                '<?php '
+                'print('
+                '"Hello World"'
+                '); ?>\n'
+                '
' + ) + else: + expected = ( + '
<?php print("Hello World"); ?>\n'
+                '
' + ) + # Use PHP as the the starting `', + expected, + extensions=[CodeHiliteExtension(pygments_formatter=self.custom_pygments_formatter)] + ) + + def testFormatterLangStrEmptyLang(self): + if has_pygments: + expected = ( + '
'
+                '# A Code Comment\n'
+                '
' + ) + else: + expected = ( + '
# A Code Comment\n'
+                '
' + ) + self.assertMarkdownRenders( + '\t# A Code Comment', + expected, + extensions=[ + CodeHiliteExtension( + guess_lang=False, + pygments_formatter=self.custom_pygments_formatter, + ) + ] + ) -- cgit v1.2.1