From 97aebb7dae81facdbda9b831d7c576eb0c40e03c Mon Sep 17 00:00:00 2001 From: Matthew Anderson Date: Wed, 16 Aug 2023 01:36:59 -0500 Subject: [PATCH] Allow Markdown fence options (#246) Co-authored-by: Adam Johnson --- CHANGELOG.rst | 4 +++ src/blacken_docs/__init__.py | 4 +-- tests/test_blacken_docs.py | 64 ++++++++++++++++++++++++++---------- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c259575..75613e3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ Changelog ========= +* Allow Markdown fence options. + + Thanks to initial work from Matthew Anderson in `PR #246 `__. + * Remove ``language_version`` from ``.pre-commit-hooks.yaml``. This change allows ``default_language_version`` in ``.pre-commit-config.yaml` to take precedence. diff --git a/src/blacken_docs/__init__.py b/src/blacken_docs/__init__.py index 153828e..6d4a216 100644 --- a/src/blacken_docs/__init__.py +++ b/src/blacken_docs/__init__.py @@ -15,13 +15,13 @@ MD_RE = re.compile( - r"(?P^(?P *)```\s*python\n)" + r"(?P^(?P *)```\s*python( .*?)?\n)" r"(?P.*?)" r"(?P^(?P=indent)```\s*$)", re.DOTALL | re.MULTILINE, ) MD_PYCON_RE = re.compile( - r"(?P^(?P *)```\s*pycon\n)" + r"(?P^(?P *)```\s*pycon( .*?)?\n)" r"(?P.*?)" r"(?P^(?P=indent)```.*$)", re.DOTALL | re.MULTILINE, diff --git a/tests/test_blacken_docs.py b/tests/test_blacken_docs.py index f441c59..98f9b98 100644 --- a/tests/test_blacken_docs.py +++ b/tests/test_blacken_docs.py @@ -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) @@ -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!" @@ -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" - )