5858
5959__all__ = ["CodeBlockError" , "RSTReformatter" , "reformat_file" ]
6060
61- INDENT_RE = re .compile ("^[ \t ]+(?=[^ ])" , re .MULTILINE )
6261TRAILING_NL_RE = re .compile (r'\n+\Z' , re .MULTILINE )
6362
6463
@@ -127,10 +126,10 @@ def run(self) -> bool:
127126 rf'(?P<before>'
128127 rf'^(?P<indent>[ \t]*)\.\.[ \t]*('
129128 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]+`
131130 rf'\n*'
132131 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)+)' ,
134133 re .MULTILINE ,
135134 )
136135
@@ -159,7 +158,6 @@ def process_match(self, match: Match[str]) -> str:
159158 lang_config = {}
160159 formatter = noformat
161160
162- min_indent = min (INDENT_RE .findall (match ["code" ]))
163161 trailing_ws_match = TRAILING_NL_RE .search (match ["code" ])
164162 assert trailing_ws_match
165163 trailing_ws = trailing_ws_match .group ()
@@ -169,7 +167,7 @@ def process_match(self, match: Match[str]) -> str:
169167 with syntaxerror_for_file (self .filename ):
170168 code = formatter (code , ** lang_config )
171169
172- code = textwrap .indent (code , min_indent )
170+ code = textwrap .indent (code , match [ "body_indent" ] )
173171 return f'{ match ["before" ]} { code .rstrip ()} { trailing_ws } '
174172
175173 def get_diff (self ) -> str :
0 commit comments