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 function and tests for markdown chunks using pygment langs like {.py and {.python etc #282

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/blacken_docs/__init__.py
Expand Up @@ -13,10 +13,14 @@
from black.mode import TargetVersion


PYGMENTS_PY_LANGS = frozenset(("python", "py", "sage", "python3", "py3", "numpy"))
PYGMENTS_PY_LANGS = frozenset(
("python", "py", "sage", "python3", "py3", "numpy", ".py", ".python")
)
PYGMENTS_PY_LANGS_RE_FRAGMENT = f"({'|'.join(PYGMENTS_PY_LANGS)})"
adamchainz marked this conversation as resolved.
Show resolved Hide resolved
MD_RE = re.compile(
r"(?P<before>^(?P<indent> *)```\s*" + PYGMENTS_PY_LANGS_RE_FRAGMENT + r"( .*?)?\n)"
r"(?P<before>^(?P<indent> *)```\s*(\{?\s*)?"
+ PYGMENTS_PY_LANGS_RE_FRAGMENT
+ r"( .*?)?\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)```\s*$)",
re.DOTALL | re.MULTILINE,
Expand Down
54 changes: 54 additions & 0 deletions tests/test_blacken_docs.py
Expand Up @@ -944,3 +944,57 @@ def test_format_src_rst_pycon_comment_before_promopt():
" # Comment about next line\n"
" >>> pass\n"
)


def test_format_src_markdown_pymdown_1():
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_pymdown_2():
before = dedent(
"""\
``` {.py title='example.py'}
f(1,2,3)
```
"""
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == dedent(
"""\
``` {.py title='example.py'}
f(1, 2, 3)
```
"""
)


def test_format_src_markdown_pymdown_3():
before = dedent(
"""\
```{ .py .python .extra-class #id linenums="1" title="example.py"}
f(1,2,3)
```
"""
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == dedent(
"""\
```{ .py .python .extra-class #id linenums="1" title="example.py"}
f(1, 2, 3)
```
"""
)