Skip to content

Commit dde54fe

Browse files
committed
Add utils module for the dedent method & home for future utilities
1 parent 0430712 commit dde54fe

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

allofplos/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import textwrap
2+
3+
def dedent(text):
4+
"""Equivalent of textwrap.dedent that ignores unindented first line.
5+
This means it will still dedent strings like:
6+
'''foo
7+
is a bar
8+
'''
9+
For use in wrap_paragraphs.
10+
11+
Taken from https://github.com/ipython/ipython_genutils/text.py
12+
"""
13+
14+
if text.startswith('\n'):
15+
# text starts with blank line, don't ignore the first line
16+
return textwrap.dedent(text)
17+
18+
# split first line
19+
splits = text.split('\n',1)
20+
if len(splits) == 1:
21+
# only one line
22+
return textwrap.dedent(text)
23+
24+
first, rest = splits
25+
# dedent everything but the first line
26+
rest = textwrap.dedent(rest)
27+
return '\n'.join([first, rest])

0 commit comments

Comments
 (0)