5
5
6
6
import re
7
7
import textwrap
8
- from textwrap import dedent
9
8
from textwrap import indent as _indent
10
9
from typing import List
11
10
@@ -14,6 +13,25 @@ def indent(val: str) -> str:
14
13
return _indent (val , " " )
15
14
16
15
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
+
17
35
def wrap_paragraphs (text : str , ncols : int = 80 ) -> List [str ]:
18
36
"""Wrap multiple paragraphs to fit a specified width.
19
37
@@ -26,7 +44,7 @@ def wrap_paragraphs(text: str, ncols: int = 80) -> List[str]:
26
44
list of complete paragraphs, wrapped to fill `ncols` columns.
27
45
"""
28
46
paragraph_re = re .compile (r"\n(\s*\n)+" , re .MULTILINE )
29
- text = dedent (text ).strip ()
47
+ text = _dedent (text ).strip ()
30
48
paragraphs = paragraph_re .split (text )[::2 ] # every other entry is space
31
49
out_ps = []
32
50
indent_re = re .compile (r"\n\s+" , re .MULTILINE )
0 commit comments