def dedent(text): |
"""dedent(text : string) -> string |
|
Remove any whitespace than can be uniformly removed from the left |
of every line in `text`. |
|
This can be used e.g. to make triple-quoted strings line up with |
the left edge of screen/whatever, while still presenting it in the |
source code in indented form. |
|
For example: |
|
def test(): |
# end first line with \ to avoid the empty line! |
s = '''\ |
hello |
world |
''' |
print repr(s) # prints ' hello\n world\n ' |
print repr(dedent(s)) # prints 'hello\n world\n' |
""" |
lines = text.expandtabs().split('\n') |
margin = None |
for line in lines: |
content = line.lstrip() |
if not content: |
continue |
indent = len(line) - len(content) |
if margin is None: |
margin = indent |
else: |
margin = min(margin, indent) |
|
if margin is not None and margin > 0: |
for i in range(len(lines)): |
lines[i] = lines[i][margin:] |
|
return '\n'.join(lines) |