sync with O'Reilly Atlas
This commit is contained in:
@@ -15,22 +15,26 @@
|
||||
'banana'
|
||||
>>> clip('banana split', 12)
|
||||
'banana split'
|
||||
>>> clip('bananasplit', 5)
|
||||
'bananasplit'
|
||||
>>> clip('banana split', 8)
|
||||
'banana'
|
||||
"""
|
||||
|
||||
# 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 clipped at the last space before or after max_len"""
|
||||
text = text.rstrip()
|
||||
end = len(text)
|
||||
if end <= max_len:
|
||||
return text
|
||||
space_before = text.rfind(' ', 0, max_len)
|
||||
if space_before >= 0:
|
||||
end = space_before
|
||||
else:
|
||||
space_after = text.find(' ', max_len)
|
||||
if space_after >= 0:
|
||||
end = space_after
|
||||
return text[:end].rstrip()
|
||||
# end::CLIP[]
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
>>> from clip import clip
|
||||
>>> from inspect import signature
|
||||
>>> sig = signature(clip)
|
||||
>>> sig # doctest: +ELLIPSIS
|
||||
<inspect.Signature object at 0x...>
|
||||
>>> sig
|
||||
<Signature (text, max_len=80)>
|
||||
>>> str(sig)
|
||||
'(text, max_len=80)'
|
||||
>>> for name, param in sig.parameters.items():
|
||||
|
||||
@@ -28,12 +28,9 @@ def tag(name, *content, class_=None, **attrs):
|
||||
"""Generate one or more HTML tags"""
|
||||
if class_ is not None:
|
||||
attrs['class'] = class_
|
||||
if attrs:
|
||||
attr_pairs = (f' {attr}="{value}"' for attr, value
|
||||
in sorted(attrs.items()))
|
||||
attr_str = ''.join(attr_pairs)
|
||||
else:
|
||||
attr_str = ''
|
||||
attr_pairs = (f' {attr}="{value}"' for attr, value
|
||||
in sorted(attrs.items()))
|
||||
attr_str = ''.join(attr_pairs)
|
||||
if content:
|
||||
elements = (f'<{name}{attr_str}>{c}</{name}>'
|
||||
for c in content)
|
||||
|
||||
Reference in New Issue
Block a user