37 lines
887 B
Python
37 lines
887 B
Python
"""
|
|
>>> clip('banana ', 6)
|
|
'banana'
|
|
>>> clip('banana ', 7)
|
|
'banana'
|
|
>>> clip('banana ', 5)
|
|
'banana'
|
|
>>> clip('banana split', 6)
|
|
'banana'
|
|
>>> clip('banana split', 7)
|
|
'banana'
|
|
>>> clip('banana split', 10)
|
|
'banana'
|
|
>>> clip('banana split', 11)
|
|
'banana'
|
|
>>> clip('banana split', 12)
|
|
'banana split'
|
|
"""
|
|
|
|
# tag::CLIP[]
|
|
def clip(text, max_len=80):
|
|
"""Return text clipped at the last space before or after max_len
|
|
"""
|
|
end = None
|
|
if len(text) > max_len:
|
|
space_before = text.rfind(' ', 0, max_len)
|
|
if space_before >= 0:
|
|
end = space_before
|
|
else:
|
|
space_after = text.rfind(' ', max_len)
|
|
if space_after >= 0:
|
|
end = space_after
|
|
if end is None: # no spaces were found
|
|
return text.rstrip()
|
|
return text[:end].rstrip()
|
|
# end::CLIP[]
|