Remove leading whitespace, replace tabs and multiple spaces with single spaces.
(content: str)
| 6 | |
| 7 | |
| 8 | def normalize_whitespace(content: str) -> str: |
| 9 | """Remove leading whitespace, replace tabs and multiple spaces with single spaces.""" |
| 10 | lines = content.splitlines(keepends=True) |
| 11 | normalized_lines = [] |
| 12 | |
| 13 | for line in lines: |
| 14 | # Remove leading whitespace and tabs |
| 15 | line = line.lstrip(' \t') |
| 16 | # Replace tabs with single space |
| 17 | line = line.replace('\t', ' ') |
| 18 | # Replace multiple spaces with single space |
| 19 | line = re.sub(r' +', ' ', line) |
| 20 | normalized_lines.append(line) |
| 21 | |
| 22 | return ''.join(normalized_lines) |
| 23 | |
| 24 | |
| 25 | def main(): |