MCPcopy Create free account
hub / github.com/ipython/traitlets / wrap_paragraphs

Function wrap_paragraphs

traitlets/utils/text.py:34–57  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

32
33
34def 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

Callers 6

class_get_trait_helpMethod · 0.90
cMethod · 0.90
emit_options_helpMethod · 0.90
emit_subcommands_helpMethod · 0.90
emit_helpMethod · 0.90
emit_descriptionMethod · 0.90

Calls 2

_dedentFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…