55
66import re
77import textwrap
8- from textwrap import dedent
98from textwrap import indent as _indent
109from typing import List
1110
@@ -14,6 +13,25 @@ def indent(val: str) -> str:
1413 return _indent (val , " " )
1514
1615
16+ def _dedent (text : str ) -> str :
17+ """Equivalent of textwrap.dedent that ignores unindented first line."""
18+
19+ if text .startswith ("\n " ):
20+ # text starts with blank line, don't ignore the first line
21+ return textwrap .dedent (text )
22+
23+ # split first line
24+ splits = text .split ("\n " , 1 )
25+ if len (splits ) == 1 :
26+ # only one line
27+ return textwrap .dedent (text )
28+
29+ first , rest = splits
30+ # dedent everything but the first line
31+ rest = textwrap .dedent (rest )
32+ return "\n " .join ([first , rest ])
33+
34+
1735def wrap_paragraphs (text : str , ncols : int = 80 ) -> List [str ]:
1836 """Wrap multiple paragraphs to fit a specified width.
1937
@@ -26,7 +44,7 @@ def wrap_paragraphs(text: str, ncols: int = 80) -> List[str]:
2644 list of complete paragraphs, wrapped to fill `ncols` columns.
2745 """
2846 paragraph_re = re .compile (r"\n(\s*\n)+" , re .MULTILINE )
29- text = dedent (text ).strip ()
47+ text = _dedent (text ).strip ()
3048 paragraphs = paragraph_re .split (text )[::2 ] # every other entry is space
3149 out_ps = []
3250 indent_re = re .compile (r"\n\s+" , re .MULTILINE )
0 commit comments