Skip to content

Commit 48495b7

Browse files
committed
Support tabbed indentation
1 parent c98972d commit 48495b7

File tree

2 files changed

+107
-9
lines changed

2 files changed

+107
-9
lines changed

src/blacken_docs/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
PYGMENTS_PY_LANGS = frozenset(("python", "py", "sage", "python3", "py3", "numpy"))
1717
PYGMENTS_PY_LANGS_RE_FRAGMENT = f"({'|'.join(PYGMENTS_PY_LANGS)})"
1818
MD_RE = re.compile(
19-
r"(?P<before>^(?P<indent> *)```[^\S\r\n]*"
19+
r"(?P<before>^(?P<indent>[ \t]*)```[^\S\r\n]*"
2020
+ PYGMENTS_PY_LANGS_RE_FRAGMENT
2121
+ r"( .*?)?\n)"
2222
r"(?P<code>.*?)"
2323
r"(?P<after>^(?P=indent)```[^\S\r\n]*$)",
2424
re.DOTALL | re.MULTILINE,
2525
)
2626
MD_PYCON_RE = re.compile(
27-
r"(?P<before>^(?P<indent> *)```[^\S\r\n]*pycon( .*?)?\n)"
27+
r"(?P<before>^(?P<indent>[ \t]*)```[^\S\r\n]*pycon( .*?)?\n)"
2828
r"(?P<code>.*?)"
2929
r"(?P<after>^(?P=indent)```[^\S\r\n]*$)",
3030
re.DOTALL | re.MULTILINE,
@@ -33,20 +33,20 @@
3333
DOCTEST_TYPES = "(testsetup|testcleanup|testcode)"
3434
RST_RE = re.compile(
3535
rf"(?P<before>"
36-
rf"^(?P<indent> *)\.\. ("
36+
rf"^(?P<indent>[ \t]*)\.\. ("
3737
rf"jupyter-execute::|"
3838
rf"{BLOCK_TYPES}:: (?P<lang>\w+)|"
3939
rf"{DOCTEST_TYPES}::.*"
4040
rf")\n"
4141
rf"((?P=indent) +:.*\n)*"
42-
rf"( *\n)*"
42+
rf"([ \t]*\n)*"
4343
rf")"
4444
rf"(?P<code>(^((?P=indent) +.*)?\n)+)",
4545
re.MULTILINE,
4646
)
4747
RST_LITERAL_BLOCKS_RE = re.compile(
4848
r"(?P<before>"
49-
r"^(?! *\.\. )(?P<indent> *).*::\n"
49+
r"^(?! *\.\. )(?P<indent>[ \t]*).*::\n"
5050
r"((?P=indent) +:.*\n)*"
5151
r"\n*"
5252
r")"
@@ -55,7 +55,7 @@
5555
)
5656
RST_PYCON_RE = re.compile(
5757
r"(?P<before>"
58-
r"(?P<indent> *)\.\. ((code|code-block):: pycon|doctest::.*)\n"
58+
r"(?P<indent>[ \t]*)\.\. ((code|code-block):: pycon|doctest::.*)\n"
5959
r"((?P=indent) +:.*\n)*"
6060
r"\n*"
6161
r")"
@@ -68,20 +68,20 @@
6868
rf"^{re.escape(PYCON_CONTINUATION_PREFIX)}( |$)",
6969
)
7070
LATEX_RE = re.compile(
71-
r"(?P<before>^(?P<indent> *)\\begin{minted}(\[.*?\])?{python}\n)"
71+
r"(?P<before>^(?P<indent>[ \t]*)\\begin{minted}(\[.*?\])?{python}\n)"
7272
r"(?P<code>.*?)"
7373
r"(?P<after>^(?P=indent)\\end{minted}\s*$)",
7474
re.DOTALL | re.MULTILINE,
7575
)
7676
LATEX_PYCON_RE = re.compile(
77-
r"(?P<before>^(?P<indent> *)\\begin{minted}(\[.*?\])?{pycon}\n)"
77+
r"(?P<before>^(?P<indent>[ \t]*)\\begin{minted}(\[.*?\])?{pycon}\n)"
7878
r"(?P<code>.*?)"
7979
r"(?P<after>^(?P=indent)\\end{minted}\s*$)",
8080
re.DOTALL | re.MULTILINE,
8181
)
8282
PYTHONTEX_LANG = r"(?P<lang>pyblock|pycode|pyconsole|pyverbatim)"
8383
PYTHONTEX_RE = re.compile(
84-
rf"(?P<before>^(?P<indent> *)\\begin{{{PYTHONTEX_LANG}}}\n)"
84+
rf"(?P<before>^(?P<indent>[ \t]*)\\begin{{{PYTHONTEX_LANG}}}\n)"
8585
rf"(?P<code>.*?)"
8686
rf"(?P<after>^(?P=indent)\\end{{(?P=lang)}}\s*$)",
8787
re.DOTALL | re.MULTILINE,

tests/test_blacken_docs.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ def test_format_src_indented_markdown():
140140
)
141141

142142

143+
def test_format_src_markdown_indented_tabs():
144+
before = dedent(
145+
"""\
146+
Example:
147+
\t```python
148+
\tf(1,2,3)
149+
\t```
150+
"""
151+
)
152+
after, _ = blacken_docs.format_str(before, BLACK_MODE)
153+
assert after == dedent(
154+
"""\
155+
Example:
156+
\t```python
157+
\tf(1, 2, 3)
158+
\t```
159+
"""
160+
)
161+
162+
143163
def test_format_src_markdown_pycon():
144164
before = (
145165
"hello\n"
@@ -426,6 +446,30 @@ def test_format_src_latex_minted_indented():
426446
)
427447

428448

449+
def test_format_src_latex_minted_indented_tabs():
450+
before = dedent(
451+
"""\
452+
hello
453+
\t\\begin{minted}{python}
454+
\t if True:
455+
\t f(1,2,3)
456+
\t\\end{minted}
457+
world!
458+
"""
459+
)
460+
after, _ = blacken_docs.format_str(before, BLACK_MODE)
461+
assert after == dedent(
462+
"""\
463+
hello
464+
\t\\begin{minted}{python}
465+
\t if True:
466+
\t f(1, 2, 3)
467+
\t\\end{minted}
468+
world!
469+
"""
470+
)
471+
472+
429473
def test_format_src_latex_minted_pycon():
430474
before = (
431475
"Preceeding text\n"
@@ -579,6 +623,28 @@ def test_format_src_rst_literal_blocks_nested():
579623
assert errors == []
580624

581625

626+
def test_format_src_rst_literal_blocks_indented_tabs():
627+
before = dedent(
628+
"""\
629+
\thello::
630+
631+
\t f(1,2,3)
632+
""",
633+
)
634+
after, _ = blacken_docs.format_str(
635+
before,
636+
BLACK_MODE,
637+
rst_literal_blocks=True,
638+
)
639+
assert after == dedent(
640+
"""\
641+
hello::
642+
643+
\t f(1, 2, 3)
644+
""",
645+
)
646+
647+
582648
def test_format_src_rst_literal_blocks_empty():
583649
before = dedent(
584650
"""
@@ -688,6 +754,38 @@ def hi():
688754
)
689755

690756

757+
def test_format_src_rst_indented_tabs():
758+
before = dedent(
759+
"""\
760+
.. versionadded:: 3.1
761+
762+
\thello
763+
764+
\t.. code-block:: python
765+
766+
\t\tdef hi():
767+
\t\tf(1,2,3)
768+
769+
\tworld
770+
"""
771+
)
772+
after, _ = blacken_docs.format_str(before, BLACK_MODE)
773+
assert after == dedent(
774+
"""\
775+
.. versionadded:: 3.1
776+
777+
\thello
778+
779+
\t.. code-block:: python
780+
781+
\t def hi():
782+
\t f(1, 2, 3)
783+
784+
\tworld
785+
"""
786+
)
787+
788+
691789
def test_format_src_rst_code_block_indent():
692790
before = "\n".join(
693791
[

0 commit comments

Comments
 (0)