Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Python Markdown SuperFences #274

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/blacken_docs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
r"(?P<after>^(?P=indent)```[^\S\r\n]*$)",
re.DOTALL | re.MULTILINE,
)
PYGMENTS_PY_LANG_CLASSES_RE_FRAGMENT = (
"(" + "|".join([r"\." + lang for lang in PYGMENTS_PY_LANGS]) + ")"
)
MD_BRACE_RE = re.compile(
r"(?P<before>^(?P<indent> *)```\s*\{\s*"
+ PYGMENTS_PY_LANG_CLASSES_RE_FRAGMENT
+ r"( [^\}\n]*?)?\}\s*?\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)```\s*$)",
re.DOTALL | re.MULTILINE,
)
MD_PYCON_RE = re.compile(
r"(?P<before>^(?P<indent> *)```[^\S\r\n]*pycon( .*?)?\n)"
r"(?P<code>.*?)"
Expand Down Expand Up @@ -265,6 +276,7 @@ def _latex_pycon_match(match: Match[str]) -> str:
return f'{match["before"]}{code}{match["after"]}'

src = MD_RE.sub(_md_match, src)
src = MD_BRACE_RE.sub(_md_match, src)
src = MD_PYCON_RE.sub(_md_pycon_match, src)
src = RST_RE.sub(_rst_match, src)
src = RST_PYCON_RE.sub(_rst_pycon_match, src)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_blacken_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ def test_format_src_markdown_options():
)


def test_format_src_markdown_braces():
before = dedent(
"""\
```{.python title='example.py'}
f(1,2,3)
```
"""
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == dedent(
"""\
```{.python title='example.py'}
f(1, 2, 3)
```
"""
)


def test_format_src_markdown_trailing_whitespace():
before = dedent(
"""\
Expand Down