
You might get a better result in these messages by removing the "PRE" tags, and wrapping each line with "<P>...</P>", but that's a real hack, and almost certain to make RFC-conforming email look quite ugly, because every line becomes a paragraph, and you'll lose all indentation. Eg, in the code blocks you posted, all the lines will end up flush left. If your members are posting code or poetry, or using indented block quotations etc, they're likely to be extremely unhappy with the result.
D'oh! I just saw the error in my whole way of thinking.
And of course, any such "nl2br" equivalent will do exactly the same as wrapping with P tags -- with everything left aligned.
But thank you Steve, for taking the time and trouble to explain. It is indeed a whole can of worms. Much to learn.
/Mark
//////////////////////////////////////////////////////////////////////////
Python's standard library does have a textwrap module, but I'm not at all sure it's suitable for this. If you know that the long lines of a message are actually paragraphs, you can use something like
from textwrap import wrap
# work backward because wrapping changes indicies of later lines
for i in range(len(lines) - 1, -1, -1):
# NDT = detect_prefix(lines[i])
lines[i:i+1] = wrap(lines[i], initial_indent=NDT, subsequent_indent=NDT)
If a line is indented or has a quoting prefix, you have to detect that for yourself and set NDT to that prefix. Something like
import re
prefix_re = re.compile('[ >]*')
def detect_prefix(line):
m = prefix_re.match(line)
return m.group(0)
should capture most indentation and quoting prefixes, but there are other conventions.
Whether you use P elements or the textwrap module, it's probably a good idea to find out how long the long lines are, and what percentage of the message they are, and avoid trying to wrap a message that looks like it "mostly" has lines of reasonable length. If you don't, and your target is the old "typewriter standard" width of 66, and somebody using an RFC-conforming MUA just prefers 72, you'll reformat their mail into alternating lines of about 60 characters and 10 characters. Yuck ...
Which of the above would work better for you depends a lot on the typical content of your list. But issues with quoting and indentation are likely to have you tearing your hair out.
//////////////////////////////////////////////////////////////////////////