Wrap multiple paragraphs to fit a specified width. This is equivalent to textwrap.wrap, but with support for multiple paragraphs, as separated by empty lines. Returns ------- list of complete paragraphs, wrapped to fill `ncols` columns.
(text: str, ncols: int = 80)
| 32 | |
| 33 | |
| 34 | def wrap_paragraphs(text: str, ncols: int = 80) -> list[str]: |
| 35 | """Wrap multiple paragraphs to fit a specified width. |
| 36 | |
| 37 | This is equivalent to textwrap.wrap, but with support for multiple |
| 38 | paragraphs, as separated by empty lines. |
| 39 | |
| 40 | Returns |
| 41 | ------- |
| 42 | |
| 43 | list of complete paragraphs, wrapped to fill `ncols` columns. |
| 44 | """ |
| 45 | paragraph_re = re.compile(r"\n(\s*\n)+", re.MULTILINE) |
| 46 | text = _dedent(text).strip() |
| 47 | paragraphs = paragraph_re.split(text)[::2] # every other entry is space |
| 48 | out_ps = [] |
| 49 | indent_re = re.compile(r"\n\s+", re.MULTILINE) |
| 50 | for p in paragraphs: |
| 51 | # presume indentation that survives dedent is meaningful formatting, |
| 52 | # so don't fill unless text is flush. |
| 53 | if indent_re.search(p) is None: |
| 54 | # wrap paragraph |
| 55 | p = textwrap.fill(p, ncols) |
| 56 | out_ps.append(p) |
| 57 | return out_ps |
no test coverage detected
searching dependent graphs…