-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparawrap.py
86 lines (64 loc) · 2.74 KB
/
parawrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Paragraph wrapping and filling.
"""
# Copyright (C) 2013 Ecometrica.
# Written by Simon Law <[email protected]>
from functools import partial
import re
import textwrap
__all__ = ['ParagraphWrapper', 'split', 'wrap', 'fill']
class ParagraphWrapper(textwrap.TextWrapper):
parasep_re = re.compile(r'\n' # Newline
r'\s*' # Blank lines
r'\n') # Last newline
@classmethod
def split(cls, text):
"""split(text : string) -> [string]
Splits 'text' into multiple paragraphs and return a list of each
paragraph.
"""
result = [line.strip('\n') for line in cls.parasep_re.split(text)]
if result == ['', '']:
result = ['']
return result
def wrap(self, text):
"""wrap(text : string) -> [string]
Reformat the multiple paragraphs in 'text' so they fit in lines of
no more than 'self.width' columns, and return a list of wrapped
lines. Tabs in 'text' are expanded with string.expandtabs(),
and all other whitespace characters (including newline) are
converted to space.
"""
lines = []
linewrap = partial(textwrap.TextWrapper.wrap, self)
for para in self.split(text):
lines.extend(linewrap(para))
lines.append('') # Add newline between paragraphs
# Remove trailing newline
lines = lines[:-1]
return lines
# -- Convenience interface ---------------------------------------------
def split(text):
"""Splits text into multiple paragraphs, returning a list of paragraphs.
"""
return ParagraphWrapper.split(text)
def wrap(text, width=70, **kwargs):
"""Wrap multiple paragraphs of text, returning a list of wrapped lines.
Reformat the multiple paragraphs 'text' so they fit in lines of no
more than 'width' columns, and return a list of wrapped lines. By
default, tabs in 'text' are expanded with string.expandtabs(), and
all other whitespace characters (including newline) are converted to
space. See ParagraphWrapper class for available keyword args to customize
wrapping behaviour.
"""
w = ParagraphWrapper(width=width, **kwargs)
return w.wrap(text)
def fill(text, width=70, **kwargs):
"""Fill multiple paragraphs of text, returning a new string.
Reformat multiple paragraphs in 'text' to fit in lines of no more
than 'width' columns, and return a new string containing the entire
wrapped text. As with wrap(), tabs are expanded and other
whitespace characters converted to space. See ParagraphWrapper class for
available keyword args to customize wrapping behaviour.
"""
w = ParagraphWrapper(width=width, **kwargs)
return w.fill(text)