Returns a string with (consecutive) tabs replaced by a single space. Whitespace on empty lines and at the end of each line is removed. With indentation=True, retains leading whitespace on each line.
(string, indentation=False, replace=" ")
| 857 | return "\n".join(p) |
| 858 | |
| 859 | def collapse_tabs(string, indentation=False, replace=" "): |
| 860 | """ Returns a string with (consecutive) tabs replaced by a single space. |
| 861 | Whitespace on empty lines and at the end of each line is removed. |
| 862 | With indentation=True, retains leading whitespace on each line. |
| 863 | """ |
| 864 | p = [] |
| 865 | for x in string.splitlines(): |
| 866 | n = indentation and len(x) - len(x.lstrip()) or 0 |
| 867 | p.append(x[:n] + RE_TABS.sub(replace, x[n:]).strip()) |
| 868 | return "\n".join(p) |
| 869 | |
| 870 | def collapse_linebreaks(string, threshold=1): |
| 871 | """ Returns a string with consecutive linebreaks collapsed to at most the given threshold. |