Remove any common leading whitespace from every line in `text`.
(self, text)
| 192 | return current_page_start, future_page_start |
| 193 | |
| 194 | def dedent(self, text): |
| 195 | """Remove any common leading whitespace from every line in `text`. |
| 196 | """ |
| 197 | # Look for the longest leading string of spaces and tabs common to |
| 198 | # all lines. |
| 199 | margin = None |
| 200 | _whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE) |
| 201 | _leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE) |
| 202 | text = _whitespace_only_re.sub('', text) |
| 203 | indents = _leading_whitespace_re.findall(text) |
| 204 | for indent in indents: |
| 205 | if margin is None: |
| 206 | margin = indent |
| 207 | |
| 208 | # Current line more deeply indented than previous winner: |
| 209 | # no change (previous winner is still on top). |
| 210 | elif indent.startswith(margin): |
| 211 | pass |
| 212 | |
| 213 | # Current line consistent with and no deeper than previous winner: |
| 214 | # it's the new winner. |
| 215 | elif margin.startswith(indent): |
| 216 | margin = indent |
| 217 | |
| 218 | # Find the largest common whitespace between current line and previous |
| 219 | # winner. |
| 220 | else: |
| 221 | for i, (x, y) in enumerate(zip(margin, indent)): |
| 222 | if x != y: |
| 223 | margin = margin[:i] |
| 224 | break |
| 225 | |
| 226 | # sanity check (testing/debugging only) |
| 227 | if 0 and margin: |
| 228 | for line in text.split("\n"): |
| 229 | assert not line or line.startswith(margin), \ |
| 230 | "line = %r, margin = %r" % (line, margin) |
| 231 | |
| 232 | if margin: |
| 233 | text = re.sub(r'(?m)^' + margin, '', text) |
| 234 | return text, len(margin) |
| 235 | |
| 236 | def get_next_batch(self): |
| 237 | current_page_start, future_page_start = self._get_next_window() |