58
58
59
59
__all__ = ["CodeBlockError" , "RSTReformatter" , "reformat_file" ]
60
60
61
- INDENT_RE = re .compile ("^[ \t ]+(?=[^ ])" , re .MULTILINE )
62
61
TRAILING_NL_RE = re .compile (r'\n+\Z' , re .MULTILINE )
63
62
64
63
@@ -127,10 +126,10 @@ def run(self) -> bool:
127
126
rf'(?P<before>'
128
127
rf'^(?P<indent>[ \t]*)\.\.[ \t]*('
129
128
rf'({ directives } )::\s*(?P<lang>[A-Za-z0-9-_]+)?)\n'
130
- rf'((?P=indent)[ \t]+:.*\n)*'
129
+ rf'((?P=indent)[ \t]+:.*\n)*' # Limitation: should be `(?P=body_indent)` rather than `[ \t]+`
131
130
rf'\n*'
132
131
rf')'
133
- rf'(?P<code>( ^((?P=indent)[ \t]+.*)?\n)+)' ,
132
+ rf'(?P<code>^((?P=indent)(?P<body_indent> [ \t]+).*)?\n(^((?P=indent)(?P=body_indent) .*)?\n)+)' ,
134
133
re .MULTILINE ,
135
134
)
136
135
@@ -159,7 +158,6 @@ def process_match(self, match: Match[str]) -> str:
159
158
lang_config = {}
160
159
formatter = noformat
161
160
162
- min_indent = min (INDENT_RE .findall (match ["code" ]))
163
161
trailing_ws_match = TRAILING_NL_RE .search (match ["code" ])
164
162
assert trailing_ws_match
165
163
trailing_ws = trailing_ws_match .group ()
@@ -169,7 +167,7 @@ def process_match(self, match: Match[str]) -> str:
169
167
with syntaxerror_for_file (self .filename ):
170
168
code = formatter (code , ** lang_config )
171
169
172
- code = textwrap .indent (code , min_indent )
170
+ code = textwrap .indent (code , match [ "body_indent" ] )
173
171
return f'{ match ["before" ]} { code .rstrip ()} { trailing_ws } '
174
172
175
173
def get_diff (self ) -> str :
0 commit comments