Skip to content

Commit

Permalink
Ignore language specifiers after newlines in Markdown code blocks (#283)
Browse files Browse the repository at this point in the history
Co-Authored-By: Adam Johnson <me@adamj.eu>
  • Loading branch information
harupy and adamchainz committed Jan 28, 2024
1 parent 7c8d70f commit 555ec31
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -11,6 +11,10 @@ Changelog

Thanks to Peter Cock in `PR #313 <https://github.com/adamchainz/blacken-docs/pull/313>`__.

* Ignore language specifiers after newlines in Markdown code blocks.

Thanks to Harutaka Kawamura in `PR #283 <https://github.com/adamchainz/blacken-docs/pull/283>`__.

1.16.0 (2023-08-16)
-------------------

Expand Down
8 changes: 5 additions & 3 deletions src/blacken_docs/__init__.py
Expand Up @@ -16,13 +16,15 @@
PYGMENTS_PY_LANGS = frozenset(("python", "py", "sage", "python3", "py3", "numpy"))
PYGMENTS_PY_LANGS_RE_FRAGMENT = f"({'|'.join(PYGMENTS_PY_LANGS)})"
MD_RE = re.compile(
r"(?P<before>^(?P<indent> *)```\s*" + PYGMENTS_PY_LANGS_RE_FRAGMENT + r"( .*?)?\n)"
r"(?P<before>^(?P<indent> *)```[^\S\r\n]*"
+ PYGMENTS_PY_LANGS_RE_FRAGMENT
+ r"( .*?)?\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)```\s*$)",
r"(?P<after>^(?P=indent)```[^\S\r\n]*$)",
re.DOTALL | re.MULTILINE,
)
MD_PYCON_RE = re.compile(
r"(?P<before>^(?P<indent> *)```\s*pycon( .*?)?\n)"
r"(?P<before>^(?P<indent> *)```[^\S\r\n]*pycon( .*?)?\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)```.*$)",
re.DOTALL | re.MULTILINE,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_blacken_docs.py
Expand Up @@ -53,6 +53,20 @@ def test_format_src_markdown_leading_whitespace():
)


def test_format_src_markdown_python_after_newline():
before = dedent(
"""\
```
python --version
echo "python"
```
"""
)
after, errors = blacken_docs.format_str(before, BLACK_MODE)
assert errors == []
assert after == before


def test_format_src_markdown_short_name():
before = dedent(
"""\
Expand Down Expand Up @@ -144,6 +158,20 @@ def test_format_src_markdown_pycon():
)


def test_format_src_markdown_pycon_after_newline():
before = dedent(
"""\
```
pycon is great
>>> yes it is
```
"""
)
after, errors = blacken_docs.format_str(before, BLACK_MODE)
assert errors == []
assert after == before


def test_format_src_markdown_pycon_options():
before = (
"hello\n"
Expand Down

0 comments on commit 555ec31

Please sign in to comment.