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

Support tabbed indentation #349

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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
98 changes: 98 additions & 0 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 @@ -426,6 +446,30 @@ def test_format_src_latex_minted_indented():
)


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!
"""
)


def test_format_src_latex_minted_pycon():
before = (
"Preceeding text\n"
Expand Down Expand Up @@ -579,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 @@ -688,6 +754,38 @@ def hi():
)


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
"""
)


def test_format_src_rst_code_block_indent():
before = "\n".join(
[
Expand Down
Loading