Skip to content

Commit

Permalink
Gracefully ignore empty code blocks
Browse files Browse the repository at this point in the history
resolves #368
  • Loading branch information
sirosen committed Sep 11, 2024
1 parent 1602edb commit aaaa8d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/blacken_docs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def _rst_match(match: Match[str]) -> str:
lang = match["lang"]
if lang is not None and lang not in PYGMENTS_PY_LANGS:
return match[0]
if not match["code"].strip():
return match[0]
min_indent = min(INDENT_RE.findall(match["code"]))
trailing_ws_match = TRAILING_NL_RE.search(match["code"])
assert trailing_ws_match
Expand Down Expand Up @@ -240,6 +242,8 @@ def _rst_pycon_match(match: Match[str]) -> str:
if _within_off_range(match.span()):
return match[0]
code = _pycon_match(match)
if not code.strip():
return match[0]
min_indent = min(INDENT_RE.findall(match["code"]))
code = textwrap.indent(code, min_indent)
return f'{match["before"]}{code}'
Expand Down
12 changes: 12 additions & 0 deletions tests/test_blacken_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,3 +1323,15 @@ def test_format_src_rst_pycon_comments():
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == before


def test_format_empty_python_code_block():
before = "some text\n\n.. code-block:: python\n\n\nsome other text\n"
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == before


def test_format_empty_pycon_code_block():
before = "some text\n\n.. code-block:: pycon\n\n\nsome other text\n"
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == before

0 comments on commit aaaa8d9

Please sign in to comment.