Skip to content

Commit

Permalink
[BUG] Fix handling of leading whitespaces in code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
peytondmurray committed Jan 6, 2025
1 parent 5f508eb commit 5f11975
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nbconvert/filters/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def _pygments_highlight(source, output_formatter, language="ipython", metadata=N

if lexer is None:
try:
lexer = get_lexer_by_name(language, stripall=True)
lexer = get_lexer_by_name(language, stripall=False)
except ClassNotFound:
warn("No lexer found for language %r. Treating as plain text." % language, stacklevel=2)
from pygments.lexers.special import TextLexer
Expand Down
2 changes: 1 addition & 1 deletion nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def block_code(self, code: str, info: Optional[str] = None) -> str:
try:
if info.strip().split(None, 1):
lang = info.strip().split(maxsplit=1)[0]
lexer = get_lexer_by_name(lang, stripall=True)
lexer = get_lexer_by_name(lang, stripall=False)
except ClassNotFound:
code = f"{lang}\n{code}"
lang = None
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ ignore = [
"T201", # `print` found
"RUF012", # Mutable class attributes should be annotated
"UP031", # Use format specifiers instead of percent format

]
unfixable = [
"T201", # Don't touch print statements
Expand Down Expand Up @@ -253,6 +252,7 @@ unfixable = [
"nbconvert/__init__.py" = ["F401", "F403"]
# PLR2004 Magic value used in comparison
"nbconvert/filters/ansi.py" = ["PLR2004"]
"tests/exporters/test_html.py" = ["RUF001"]

[tool.interrogate]
ignore-init-module=true
Expand Down
35 changes: 35 additions & 0 deletions tests/exporters/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,38 @@ def test_language_code_error(self):
(output, resources) = exporter.from_filename(self._get_notebook())

assert '<html lang="en">' in output


def test_syntax_highlight_leading_whitespace():
"""Test that syntax highlight doesn't strip leading spaces."""
nb = v4.reads(r"""
{
"cells": [
{
"cell_type": "markdown",
"id": "29da71a9-ae40-4098-8c3b-31a98e79fc12",
"metadata": {},
"source": [
"```APL\n",
" 1+2×⍳3\n",
"3 5 7\n",
"```\n",
"\n",
"```\n",
" 1+2×⍳3\n",
"3 5 7\n",
"```"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
""")
output, _ = HTMLExporter().from_notebook_node(nb)
# Check that the second code block has the leading spaces
assert "<pre><code> 1+2×⍳3\n3 5 7\n</code></pre>" in output

# Check that the APL-formatted code block has the leading spaces
assert '<span class="w"> </span>' in output

0 comments on commit 5f11975

Please sign in to comment.