Skip to content

Commit

Permalink
Allow Markdown fence options (#246)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Johnson <me@adamj.eu>
  • Loading branch information
maxb2 and adamchainz committed Aug 16, 2023
1 parent c5be8e0 commit 97aebb7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

* Allow Markdown fence options.

Thanks to initial work from Matthew Anderson in `PR #246 <https://github.com/adamchainz/blacken-docs/pull/246>`__.

* Remove ``language_version`` from ``.pre-commit-hooks.yaml``.
This change allows ``default_language_version`` in ``.pre-commit-config.yaml` to take precedence.
Expand Down
4 changes: 2 additions & 2 deletions src/blacken_docs/__init__.py
Expand Up @@ -15,13 +15,13 @@


MD_RE = re.compile(
r"(?P<before>^(?P<indent> *)```\s*python\n)"
r"(?P<before>^(?P<indent> *)```\s*python( .*?)?\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*pycon\n)"
r"(?P<before>^(?P<indent> *)```\s*pycon( .*?)?\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)```.*$)",
re.DOTALL | re.MULTILINE,
Expand Down
64 changes: 47 additions & 17 deletions tests/test_blacken_docs.py
Expand Up @@ -29,6 +29,12 @@ def test_format_src_markdown_leading_whitespace():
assert after == ("``` python\n" "f(1, 2, 3)\n" "```\n")


def test_format_src_markdown_options():
before = "```python title='example.py'\n" + "f(1,2,3)\n" + "```\n"
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == ("```python title='example.py'\n" + "f(1, 2, 3)\n" + "```\n")


def test_format_src_markdown_trailing_whitespace():
before = "```python\n" "f(1,2,3)\n" "``` \n"
after, _ = blacken_docs.format_str(before, BLACK_MODE)
Expand All @@ -43,6 +49,47 @@ def test_format_src_indented_markdown():
)


def test_format_src_markdown_pycon():
before = (
"hello\n"
"\n"
"```pycon\n"
"\n"
" >>> f(1,2,3)\n"
" output\n"
"```\n"
"world\n"
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == (
"hello\n" "\n" "```pycon\n" "\n" ">>> f(1, 2, 3)\n" "output\n" "```\n" "world\n"
)


def test_format_src_markdown_pycon_options():
before = (
"hello\n"
"\n"
"```pycon title='Session 1'\n"
"\n"
" >>> f(1,2,3)\n"
" output\n"
"```\n"
"world\n"
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == (
"hello\n"
"\n"
"```pycon title='Session 1'\n"
"\n"
">>> f(1, 2, 3)\n"
"output\n"
"```\n"
"world\n"
)


def test_format_src_latex_minted():
before = (
"hello\n" "\\begin{minted}{python}\n" "f(1,2,3)\n" "\\end{minted}\n" "world!"
Expand Down Expand Up @@ -744,20 +791,3 @@ def test_format_src_rst_pycon_comment_before_promopt():
" # Comment about next line\n"
" >>> pass\n"
)


def test_format_src_markdown_pycon():
before = (
"hello\n"
"\n"
"```pycon\n"
"\n"
" >>> f(1,2,3)\n"
" output\n"
"```\n"
"world\n"
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == (
"hello\n" "\n" "```pycon\n" "\n" ">>> f(1, 2, 3)\n" "output\n" "```\n" "world\n"
)

0 comments on commit 97aebb7

Please sign in to comment.