def softwraphtml(value, max_line_length=20):
import re
whitespace_re = re.compile('\s')
new_value = []
unbroken_chars = 0
in_tag = False
in_xhtml_entity = False
for idx, char in enumerate(value):
if char == '<':
in_tag = True
elif char == '>':
in_tag = False
unbroken_chars = 0
elif char == '&' and not in_tag:
in_xhtml_entity = True
elif char == ';' and in_xhtml_entity:
in_xhtml_entity = False
elif whitespace_re.match(char):
unbroken_chars = 0
new_value.append(char)
if not in_xhtml_entity:
if unbroken_chars >= max_line_length-1 and not in_tag:
new_value.append("")
unbroken_chars = 0
else:
unbroken_chars += 1
return ''.join(new_value)