Skip to content

Commit e11cd25

Browse files
authored
Don't process shebangs in codehilite when processing fenced code
Fixes Python-Markdown#1156.
1 parent f0b7f98 commit e11cd25

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,6 @@ ENV/
6969

7070
# MkDocs documentation
7171
site/
72+
73+
# Mac files
74+
.DS_Store

docs/change_log/release-3.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ The following bug fixes are included in the 3.3 release:
102102
* Fix complex scenarios involving lists and admonitions (#1004).
103103
* Fix complex scenarios with nested ordered and unordered lists in a definition list (#918).
104104
* Fix corner cases with lists under admonitions.
105+
* Don't process shebangs in fenced code blocks when using CodeHilite (#1156).
105106

106107
[spec]: https://www.w3.org/TR/html5/text-level-semantics.html#the-code-element
107108
[fenced_code]: ../extensions/fenced_code_blocks.md

markdown/extensions/codehilite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def __init__(self, src, **options):
112112

113113
self.options = options
114114

115-
def hilite(self):
115+
def hilite(self, shebang=True):
116116
"""
117117
Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with
118118
optional line numbers. The output should then be styled with css to
@@ -125,7 +125,7 @@ def hilite(self):
125125

126126
self.src = self.src.strip('\n')
127127

128-
if self.lang is None:
128+
if self.lang is None and shebang:
129129
self._parseHeader()
130130

131131
if pygments and self.use_pygments:

markdown/extensions/fenced_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def run(self, lines):
116116
**local_config
117117
)
118118

119-
code = highliter.hilite()
119+
code = highliter.hilite(shebang=False)
120120
else:
121121
id_attr = lang_attr = class_attr = kv_pairs = ''
122122
if lang:

tests/test_syntax/extensions/test_fenced_code.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,36 @@ def setUp(self):
381381
if has_pygments and pygments.__version__ != required_pygments_version:
382382
self.skipTest(f'Pygments=={required_pygments_version} is required')
383383

384+
def test_shebang(self):
385+
386+
if has_pygments:
387+
expected = '''
388+
<div class="codehilite"><pre><span></span><code>#!test
389+
</code></pre></div>
390+
'''
391+
else:
392+
expected = '''
393+
<pre class="codehilite"><code>#!test
394+
</code></pre>
395+
'''
396+
397+
self.assertMarkdownRenders(
398+
self.dedent(
399+
'''
400+
```
401+
#!test
402+
```
403+
'''
404+
),
405+
self.dedent(
406+
expected
407+
),
408+
extensions=[
409+
markdown.extensions.codehilite.CodeHiliteExtension(linenums=None, guess_lang=False),
410+
'fenced_code'
411+
]
412+
)
413+
384414
def testFencedCodeWithHighlightLines(self):
385415
if has_pygments:
386416
expected = self.dedent(

0 commit comments

Comments
 (0)