Skip to content

Commit

Permalink
Don't process shebangs in codehilite when processing fenced code
Browse files Browse the repository at this point in the history
Fixes #1156.
  • Loading branch information
facelessuser committed Aug 4, 2021
1 parent f0b7f98 commit e11cd25
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -69,3 +69,6 @@ ENV/

# MkDocs documentation
site/

# Mac files
.DS_Store
1 change: 1 addition & 0 deletions docs/change_log/release-3.3.md
Expand Up @@ -102,6 +102,7 @@ The following bug fixes are included in the 3.3 release:
* Fix complex scenarios involving lists and admonitions (#1004).
* Fix complex scenarios with nested ordered and unordered lists in a definition list (#918).
* Fix corner cases with lists under admonitions.
* Don't process shebangs in fenced code blocks when using CodeHilite (#1156).

[spec]: https://www.w3.org/TR/html5/text-level-semantics.html#the-code-element
[fenced_code]: ../extensions/fenced_code_blocks.md
Expand Down
4 changes: 2 additions & 2 deletions markdown/extensions/codehilite.py
Expand Up @@ -112,7 +112,7 @@ def __init__(self, src, **options):

self.options = options

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

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

if self.lang is None:
if self.lang is None and shebang:
self._parseHeader()

if pygments and self.use_pygments:
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/fenced_code.py
Expand Up @@ -116,7 +116,7 @@ def run(self, lines):
**local_config
)

code = highliter.hilite()
code = highliter.hilite(shebang=False)
else:
id_attr = lang_attr = class_attr = kv_pairs = ''
if lang:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_syntax/extensions/test_fenced_code.py
Expand Up @@ -381,6 +381,36 @@ def setUp(self):
if has_pygments and pygments.__version__ != required_pygments_version:
self.skipTest(f'Pygments=={required_pygments_version} is required')

def test_shebang(self):

if has_pygments:
expected = '''
<div class="codehilite"><pre><span></span><code>#!test
</code></pre></div>
'''
else:
expected = '''
<pre class="codehilite"><code>#!test
</code></pre>
'''

self.assertMarkdownRenders(
self.dedent(
'''
```
#!test
```
'''
),
self.dedent(
expected
),
extensions=[
markdown.extensions.codehilite.CodeHiliteExtension(linenums=None, guess_lang=False),
'fenced_code'
]
)

def testFencedCodeWithHighlightLines(self):
if has_pygments:
expected = self.dedent(
Expand Down

0 comments on commit e11cd25

Please sign in to comment.