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, ncols=80)
| 384 | |
| 385 | |
| 386 | def wrap_paragraphs(text, ncols=80): |
| 387 | """Wrap multiple paragraphs to fit a specified width. |
| 388 | |
| 389 | This is equivalent to textwrap.wrap, but with support for multiple |
| 390 | paragraphs, as separated by empty lines. |
| 391 | |
| 392 | Returns |
| 393 | ------- |
| 394 | |
| 395 | list of complete paragraphs, wrapped to fill `ncols` columns. |
| 396 | """ |
| 397 | paragraph_re = re.compile(r'\n(\s*\n)+', re.MULTILINE) |
| 398 | text = dedent(text).strip() |
| 399 | paragraphs = paragraph_re.split(text)[::2] # every other entry is space |
| 400 | out_ps = [] |
| 401 | indent_re = re.compile(r'\n\s+', re.MULTILINE) |
| 402 | for p in paragraphs: |
| 403 | # presume indentation that survives dedent is meaningful formatting, |
| 404 | # so don't fill unless text is flush. |
| 405 | if indent_re.search(p) is None: |
| 406 | # wrap paragraph |
| 407 | p = textwrap.fill(p, ncols) |
| 408 | out_ps.append(p) |
| 409 | return out_ps |
| 410 | |
| 411 | |
| 412 | def long_substr(data): |