def shorten(string, length=<NUM_LIT>, ellipsis=None):
string = string.strip()<EOL>if len(string) > length:<EOL><INDENT>if ellipsis is True:<EOL><INDENT>ellipsis = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>ellipsis = ellipsis or '<STR_LIT>'<EOL><DEDENT>L = length - len(ellipsis)<EOL>return '<STR_LIT:U+0020>'.join(string[:L].split('<STR_LIT:U+0020>')[:-<NUM_LIT:1>]).strip('<STR_LIT>') + ellipsis<EOL><DEDENT>else:<EOL><INDENT>return string<EOL><DEDENT>
Shorten a string to 140 characters without breaking words.
Optionally add an ellipsis character: '…' if ellipsis=True, or a given string
e.g. ellipsis=' (cut)'