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("<wbr/>")
unbroken_chars = 0
else:
unbroken_chars += 1
return ''.join(new_value)
Comments
How about inserting of the in the xhtml entities? This may be a big issue if trying to output a correct xhtml code.
For example, any user can place a long URL containing in the comments.
#
That's a very good point. As I said, it's very basic still, but you're right -- it should treat xhtml entities atomically. Thanks for pointing that out.
#