#NEWLINE# @ register . filter ( name = 'pretty_crop' ) #NEWLINE# @ stringfilter #NEWLINE# def pretty_crop( text , length ) : #NEWLINE# #INDENT# ' Crops text after whole words.\n        text - text to crop\n        length - maximum length of cropped text\n    ' #NEWLINE# if ( not text ) : #NEWLINE# #INDENT# return '' #NEWLINE# #UNINDENT# if ( type ( text ) not in [ str , unicode ] ) : #NEWLINE# #INDENT# text = unicode ( text ) #NEWLINE# #UNINDENT# if ( len ( text ) <= length ) : #NEWLINE# #INDENT# return text #NEWLINE# #UNINDENT# last_allowed_space_location = text [ 0 : length ] . rfind ( ' ' ) #NEWLINE# return ( text [ 0 : length ] + '...' )