Skip to content

Commit

Permalink
Support tabbed indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Jun 30, 2024
1 parent acffa03 commit d705634
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 47 deletions.
18 changes: 9 additions & 9 deletions src/blacken_docs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +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\r\n]*"
r"(?P<before>^(?P<indent>[ \t]*)```[^\S\r\n]*"
+ PYGMENTS_PY_LANGS_RE_FRAGMENT
+ r"( .*?)?\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)```[^\S\r\n]*$)",
re.DOTALL | re.MULTILINE,
)
MD_PYCON_RE = re.compile(
r"(?P<before>^(?P<indent> *)```[^\S\r\n]*pycon( .*?)?\n)"
r"(?P<before>^(?P<indent>[ \t]*)```[^\S\r\n]*pycon( .*?)?\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)```[^\S\r\n]*$)",
re.DOTALL | re.MULTILINE,
Expand All @@ -33,20 +33,20 @@
DOCTEST_TYPES = "(testsetup|testcleanup|testcode)"
RST_RE = re.compile(
rf"(?P<before>"
rf"^(?P<indent> *)\.\. ("
rf"^(?P<indent>[ \t]*)\.\. ("
rf"jupyter-execute::|"
rf"{BLOCK_TYPES}:: (?P<lang>\w+)|"
rf"{DOCTEST_TYPES}::.*"
rf")\n"
rf"((?P=indent) +:.*\n)*"
rf"( *\n)*"
rf"([ \t]*\n)*"
rf")"
rf"(?P<code>(^((?P=indent) +.*)?\n)+)",
re.MULTILINE,
)
RST_LITERAL_BLOCKS_RE = re.compile(
r"(?P<before>"
r"^(?! *\.\. )(?P<indent> *).*::\n"
r"^(?! *\.\. )(?P<indent>[ \t]*).*::\n"
r"((?P=indent) +:.*\n)*"
r"\n*"
r")"
Expand All @@ -55,7 +55,7 @@
)
RST_PYCON_RE = re.compile(
r"(?P<before>"
r"(?P<indent> *)\.\. ((code|code-block):: pycon|doctest::.*)\n"
r"(?P<indent>[ \t]*)\.\. ((code|code-block):: pycon|doctest::.*)\n"
r"((?P=indent) +:.*\n)*"
r"\n*"
r")"
Expand All @@ -68,20 +68,20 @@
rf"^{re.escape(PYCON_CONTINUATION_PREFIX)}( |$)",
)
LATEX_RE = re.compile(
r"(?P<before>^(?P<indent> *)\\begin{minted}(\[.*?\])?{python}\n)"
r"(?P<before>^(?P<indent>[ \t]*)\\begin{minted}(\[.*?\])?{python}\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)\\end{minted}\s*$)",
re.DOTALL | re.MULTILINE,
)
LATEX_PYCON_RE = re.compile(
r"(?P<before>^(?P<indent> *)\\begin{minted}(\[.*?\])?{pycon}\n)"
r"(?P<before>^(?P<indent>[ \t]*)\\begin{minted}(\[.*?\])?{pycon}\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)\\end{minted}\s*$)",
re.DOTALL | re.MULTILINE,
)
PYTHONTEX_LANG = r"(?P<lang>pyblock|pycode|pyconsole|pyverbatim)"
PYTHONTEX_RE = re.compile(
rf"(?P<before>^(?P<indent> *)\\begin{{{PYTHONTEX_LANG}}}\n)"
rf"(?P<before>^(?P<indent>[ \t]*)\\begin{{{PYTHONTEX_LANG}}}\n)"
rf"(?P<code>.*?)"
rf"(?P<after>^(?P=indent)\\end{{(?P=lang)}}\s*$)",
re.DOTALL | re.MULTILINE,
Expand Down
198 changes: 160 additions & 38 deletions tests/test_blacken_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@ def test_format_src_indented_markdown():
)


def test_format_src_markdown_indented_tabs():
before = dedent(
"""\
Example:
\t```python
\tf(1,2,3)
\t```
"""
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == dedent(
"""\
Example:
\t```python
\tf(1, 2, 3)
\t```
"""
)


def test_format_src_markdown_pycon():
before = (
"hello\n"
Expand Down Expand Up @@ -403,22 +423,50 @@ def test_format_src_latex_minted_opt():
def test_format_src_latex_minted_indented():
# Personaly I would have minted python code all flush left,
# with only the Python code's own four space indentation:
before = (
"hello\n"
" \\begin{minted}{python}\n"
" if True:\n"
" f(1,2,3)\n"
" \\end{minted}\n"
"world!"
before = dedent(
"""\
hello
\\begin{minted}{python}
if True:
f(1,2,3)
\\end{minted}
world!
"""
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == (
"hello\n"
" \\begin{minted}{python}\n"
" if True:\n"
" f(1, 2, 3)\n"
" \\end{minted}\n"
"world!"
assert after == dedent(
"""\
hello
\\begin{minted}{python}
if True:
f(1, 2, 3)
\\end{minted}
world!
"""
)


def test_format_src_latex_minted_indented_tabs():
before = dedent(
"""\
hello
\t\\begin{minted}{python}
\t if True:
\t f(1,2,3)
\t\\end{minted}
world!
"""
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == dedent(
"""\
hello
\t\\begin{minted}{python}
\t if True:
\t f(1, 2, 3)
\t\\end{minted}
world!
"""
)


Expand Down Expand Up @@ -531,13 +579,29 @@ def test_format_src_rst():


def test_format_src_rst_literal_blocks():
before = "hello::\n" "\n" " f(1,2,3)\n" "\n" "world\n"
before = dedent(
"""\
hello::
f(1,2,3)
world
"""
)
after, _ = blacken_docs.format_str(
before,
BLACK_MODE,
rst_literal_blocks=True,
)
assert after == ("hello::\n" "\n" " f(1, 2, 3)\n" "\n" "world\n")
assert after == dedent(
"""\
hello::
f(1, 2, 3)
world
"""
)


def test_format_src_rst_literal_blocks_nested():
Expand All @@ -559,6 +623,28 @@ def test_format_src_rst_literal_blocks_nested():
assert errors == []


def test_format_src_rst_literal_blocks_indented_tabs():
before = dedent(
"""\
\thello::
\t f(1,2,3)
""",
)
after, _ = blacken_docs.format_str(
before,
BLACK_MODE,
rst_literal_blocks=True,
)
assert after == dedent(
"""\
hello::
\t f(1, 2, 3)
""",
)


def test_format_src_rst_literal_blocks_empty():
before = dedent(
"""
Expand Down Expand Up @@ -637,30 +723,66 @@ def test_format_src_rst_sphinx_doctest():


def test_format_src_rst_indented():
before = (
".. versionadded:: 3.1\n"
"\n"
" hello\n"
"\n"
" .. code-block:: python\n"
"\n"
" def hi():\n"
" f(1,2,3)\n"
"\n"
" world\n"
before = dedent(
"""\
.. versionadded:: 3.1
hello
.. code-block:: python
def hi():
f(1,2,3)
world
"""
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == (
".. versionadded:: 3.1\n"
"\n"
" hello\n"
"\n"
" .. code-block:: python\n"
"\n"
" def hi():\n"
" f(1, 2, 3)\n"
"\n"
" world\n"
assert after == dedent(
"""\
.. versionadded:: 3.1
hello
.. code-block:: python
def hi():
f(1, 2, 3)
world
"""
)


def test_format_src_rst_indented_tabs():
before = dedent(
"""\
.. versionadded:: 3.1
\thello
\t.. code-block:: python
\t\tdef hi():
\t\tf(1,2,3)
\tworld
"""
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == dedent(
"""\
.. versionadded:: 3.1
\thello
\t.. code-block:: python
\t def hi():
\t f(1, 2, 3)
\tworld
"""
)


Expand Down

0 comments on commit d705634

Please sign in to comment.