blob: c08b3924b6e06e153fb2b60a747f9cd1471b7a63 (
plain)
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
|
"""
python-creole utils
~~~~~~~~~~~~~~~~~~~
:copyleft: 2008-2011 by python-creole team, see AUTHORS for more details.
:license: GNU GPL v3 or above, see LICENSE for more details.
"""
import re
space_re = re.compile(r"^(\s*)(.*?)(\s*)$", re.DOTALL)
def clean_whitespace(txt):
"""
Special whitespaces cleanup
>>> clean_whitespace("\\n\\nfoo bar\\n\\n")
'foo bar\\n'
>>> clean_whitespace(" foo bar \\n \\n")
' foo bar\\n'
>>> clean_whitespace(" \\n \\n foo bar ")
' foo bar '
>>> clean_whitespace("foo bar")
'foo bar'
"""
def cleanup(match):
start, txt, end = match.groups()
if " " in start:
start = " "
else:
start = ""
if "\n" in end:
end = "\n"
elif " " in end:
end = " "
return start + txt + end
return space_re.sub(cleanup, txt)
if __name__ == '__main__':
import doctest
print(doctest.testmod())
|