
I'm looking for a way to wrap lines in archived messages....
And the lines in that block that seem responsible for the PRE tags are ...
lines.insert(0, '<PRE>') lines.append('</PRE>')
My question is: Can those PRE tags be removed and replaced with something equivalent to PHP's "nl2br" (which inserts a line break BR in place of new line entries)?
No, because there *are no* newlines to break those very long lines. These MUAs use newline to mean "paragraph break", not "line break".
But there are "newlines" and there isn't any need to insert linebreaks into those long lines -- they just need to wrap.
Looking at, for example, a message that originally has 3 paragraphs of text:
<!--beginarticle--> %(body)s <!--endarticle-->
<!--beginarticle--> <PRE> First paragraph that is a really long line of text.
Second paragraph that is a really long line of text.
Third paragraph that is a really long line of text. </PRE> <!--endarticle-->
If those PRE tags are removed, then all 3 lines get joined up and displayed as one continuous line. It solves the non-wrap problem but it looses the "paragraphs". So that's a no go.
As said, if this was PHP we could use the "nl2br" function - which inserts line breaks before all newlines.
<?= nl2br($body) ?> -- would give us ...
/////////// <!--beginarticle--> <br>First paragraph of body that is a really long line of text. <br> <br>Second paragraph of body that is a really long line of text. <br> <br>Third paragraph of body that is a really long line of text. <!--endarticle--> ////////
The BR tags would preserve the space between the lines and give the appearance of paragraphs in the HTML Pipermail archive page. Granted the HTML would not be strictly kosher, but then neither are the PRE tags strictly kosher as they're are not being used as they should be. The main thing is that the lines would wrap according the to width of the window and eliminate the need for horizontal scrolling.
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.
Agreed. To horrible to even think about.
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)
This is sort of where I was looking to go, but as you've pointed out, there's no telling if the text will be in paragraphs, code blocks etc.
Does the Python code snippet that I mentioned ...
def nl2br(s): return '<br>\n'.join(s.split('\n'))
... make any sense as a Python equivalent of PHP's "nl2br" function (to accomplish the insertion of the BR line break tags)?
Cheers, Mark