Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def short_text(text, maxlength=200, endwith='...'):
- """
- Returns a shortened version of text up to maxlength long (not counting ending characters), breaking only on spaces to avoid weird looking summaries,
- and ending with endwith (default ellipsis written out with periods).
- >>> short_text('ab cd ef gh', 9)
- 'ab cd...'
- >>> short_text('ab cd', 5)
- 'ab cd'
- >>> short_text('ab cd ef gh', 9, ':')
- 'ab cd ef:'
- """
- if len(text) <= maxlength:
- return text
- return text[:maxlength - len(endwith) + 1].rpartition(' ')[0] + endwith
- @register.filter('short_text')
- def short_text(text, length, autoescape=None):
- """
- makes short_text, breaking on words, length in characters rather than words like truncatewords
- >>> short_text('some excessively long string', 21)
- 'some excessively...'
- >>> short_text('some excessively long string', 16) #don't forget the '...' counts!
- 'some...'
- """
- return short_text(text, length)
- short_text.needs_autoescape = True
Add Comment
Please, Sign In to add comment