Nice, intelligent truncation of text.
Project description
Nice, intelligent truncation of text.
Nicely truncate text
Intelligent truncation of text means that truncation does not take place inside a word but only between words. So the required length is only an approximation. The result text might be a bit longer than the required length:
>>> from icemac.truncatetext import truncate >>> 'I was here.'[:3] 'I w' >>> truncate('I was here.', 3) 'I was ...'
What can get truncated?
Only instances of basestring can be truncated:
>>> truncate(3, 3) Traceback (most recent call last): ValueError: 3 is no instance of basestring or None >>> truncate(u'Lorem ipsum', 5) u'Lorem ...'
None is handled nicely:
>>> truncate(None, 4) ''
What is returned?
Always at least one word is returned even when it is longer than the required length:
>>> truncate('Lorem ipsum', 1) 'Lorem ...'
If the text contains only of one word which is longer than the desired length it is returned without an ellipsis:
>>> truncate('The-really-long-word', 5) 'The-really-long-word'
If the text is shorter than the desired length it is returned without the ellipsis, too:
>>> truncate('Lorem ipsum', 11) 'Lorem ipsum'
Where gets the text truncated?
Truncation also takes place at tabs and linebreaks:
>>> truncate("I was here.\nNow I'm away", 11) 'I was here. ...' >>> truncate("I was here.\rNow I'm away", 12) 'I was here.\rNow ...' >>> truncate("I was here.\tNow I'm away", 11) 'I was here. ...'
I do not want … as ellipsis.
truncate takes an optional argument which defines the ellipsis string:
>>> truncate(u'Lorem ipsum', 5, ellipsis=u':::') u'Lorem :::'
Changelog
0.2.1 (2011-07-28)
Updated categories: Python 2.7 is supported, too.
0.2 (2009-09-26)
Added ability pass a string used as ellipsis string.
0.1 (2009-01-31)
Initial public release.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for icemac.truncatetext-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | dea5fef29e43300bd33ed267780090ea1242df4a5434980aed61c2683f40acc4 |
|
MD5 | 9861cbc4ed765f43f7f9516fcf6535cc |
|
BLAKE2b-256 | b7173c2a9fa238c5e8247f2fcd6f8f1a7f5712752df59e8aa9d7b46b84cb35ad |