From ed417a1a555bf2206ceea84ba29ce37322ca8261 Mon Sep 17 00:00:00 2001 From: Gert van Dijk Date: Fri, 1 Apr 2022 23:56:45 +0200 Subject: extensions: copy config dict on each highlighted block This fixes a bug where any subsequent highlighted block with codehilite would result in the omission of the style setting, because it was popped off the dict. It would then fall back to pygments_style 'default' after the first block. Fixes #1240 --- docs/change_log/index.md | 1 + markdown/extensions/codehilite.py | 5 +-- tests/test_syntax/extensions/test_code_hilite.py | 32 +++++++++++++++++ tests/test_syntax/extensions/test_fenced_code.py | 45 ++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/docs/change_log/index.md b/docs/change_log/index.md index ca4792b..5320fc7 100644 --- a/docs/change_log/index.md +++ b/docs/change_log/index.md @@ -6,6 +6,7 @@ Python-Markdown Change Log (under development): version 3.3.7 (a bug-fix release). * Disallow square brackets in reference link ids (#1209). +* Retain configured `pygments_style` after first code block (#1240). Nov 17, 2021: version 3.3.6 (a bug-fix release). diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py index 1a8761b..a768b73 100644 --- a/markdown/extensions/codehilite.py +++ b/markdown/extensions/codehilite.py @@ -237,11 +237,12 @@ class HiliteTreeprocessor(Treeprocessor): blocks = root.iter('pre') for block in blocks: if len(block) == 1 and block[0].tag == 'code': + local_config = self.config.copy() code = CodeHilite( self.code_unescape(block[0].text), tab_length=self.md.tab_length, - style=self.config.pop('pygments_style', 'default'), - **self.config + style=local_config.pop('pygments_style', 'default'), + **local_config ) placeholder = self.md.htmlStash.store(code.hilite()) # Clear codeblock in etree instance diff --git a/tests/test_syntax/extensions/test_code_hilite.py b/tests/test_syntax/extensions/test_code_hilite.py index b2acd2f..41502d9 100644 --- a/tests/test_syntax/extensions/test_code_hilite.py +++ b/tests/test_syntax/extensions/test_code_hilite.py @@ -644,3 +644,35 @@ class TestCodeHiliteExtension(TestCase): expected, extensions=[CodeHiliteExtension(unknown='some value')], ) + + def testMultipleBlocksSameStyle(self): + if has_pygments: + # See also: https://github.com/Python-Markdown/markdown/issues/1240 + expected = ( + '
'
+                '# First Code Block\n'
+                '
\n\n' + '

Normal paragraph

\n' + '
'
+                '# Second Code Block\n'
+                '
' + ) + else: + expected = ( + '
# First Code Block\n'
+                '
\n\n' + '

Normal paragraph

\n' + '
# Second Code Block\n'
+                '
' + ) + self.assertMarkdownRenders( + ( + '\t:::Python\n' + '\t# First Code Block\n\n' + 'Normal paragraph\n\n' + '\t:::Python\n' + '\t# Second Code Block' + ), + expected, + extensions=[CodeHiliteExtension(pygments_style="native", noclasses=True)] + ) diff --git a/tests/test_syntax/extensions/test_fenced_code.py b/tests/test_syntax/extensions/test_fenced_code.py index 5c7104f..76c8769 100644 --- a/tests/test_syntax/extensions/test_fenced_code.py +++ b/tests/test_syntax/extensions/test_fenced_code.py @@ -781,3 +781,48 @@ class TestFencedCodeWithCodehilite(TestCase): expected, extensions=['codehilite', 'fenced_code'] ) + + def testFencedMultpleBlocksSameStyle(self): + if has_pygments: + # See also: https://github.com/Python-Markdown/markdown/issues/1240 + expected = ( + '
'
+                '# First Code Block\n'
+                '
\n\n' + '

Normal paragraph

\n' + '
'
+                '# Second Code Block\n'
+                '
' + ) + else: + expected = ''' +
# First Code Block
+            
+ +

Normal paragraph

+
# Second Code Block
+            
+ ''' + + self.assertMarkdownRenders( + self.dedent( + ''' + ``` { .python } + # First Code Block + ``` + + Normal paragraph + + ``` { .python } + # Second Code Block + ``` + ''' + ), + self.dedent( + expected + ), + extensions=[ + markdown.extensions.codehilite.CodeHiliteExtension(pygments_style="native", noclasses=True), + 'fenced_code' + ] + ) -- cgit v1.2.1