diff options
author | Gert van Dijk <gertvdijk@gmail.com> | 2022-04-01 23:56:45 +0200 |
---|---|---|
committer | Waylan Limberg <waylan.limberg@icloud.com> | 2022-04-18 09:10:02 -0400 |
commit | ed417a1a555bf2206ceea84ba29ce37322ca8261 (patch) | |
tree | f870f8b6ff0b26ba2855c5c0925a7883ec79c8b6 | |
parent | f6ca75429562cfa7df333b3529838679e4bfd443 (diff) | |
download | python-markdown-ed417a1a555bf2206ceea84ba29ce37322ca8261.tar.gz |
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
-rw-r--r-- | docs/change_log/index.md | 1 | ||||
-rw-r--r-- | markdown/extensions/codehilite.py | 5 | ||||
-rw-r--r-- | tests/test_syntax/extensions/test_code_hilite.py | 32 | ||||
-rw-r--r-- | 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 = ( + '<div class="codehilite" style="background: #202020"><pre style="line-height: 125%; margin: 0;">' + '<span></span><code><span style="color: #999999; font-style: italic"># First Code Block</span>\n' + '</code></pre></div>\n\n' + '<p>Normal paragraph</p>\n' + '<div class="codehilite" style="background: #202020"><pre style="line-height: 125%; margin: 0;">' + '<span></span><code><span style="color: #999999; font-style: italic"># Second Code Block</span>\n' + '</code></pre></div>' + ) + else: + expected = ( + '<pre class="codehilite"><code class="language-python"># First Code Block\n' + '</code></pre>\n\n' + '<p>Normal paragraph</p>\n' + '<pre class="codehilite"><code class="language-python"># Second Code Block\n' + '</code></pre>' + ) + 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 = ( + '<div class="codehilite" style="background: #202020"><pre style="line-height: 125%; margin: 0;">' + '<span></span><code><span style="color: #999999; font-style: italic"># First Code Block</span>\n' + '</code></pre></div>\n\n' + '<p>Normal paragraph</p>\n' + '<div class="codehilite" style="background: #202020"><pre style="line-height: 125%; margin: 0;">' + '<span></span><code><span style="color: #999999; font-style: italic"># Second Code Block</span>\n' + '</code></pre></div>' + ) + else: + expected = ''' + <pre class="codehilite"><code class="language-python"># First Code Block + </code></pre> + + <p>Normal paragraph</p> + <pre class="codehilite"><code class="language-python"># Second Code Block + </code></pre> + ''' + + 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' + ] + ) |