Opened 11 years ago

Closed 11 years ago

# wrap long lines

Reported by: Owned by: Rainer Sokoll Bas van der Vlies normal EmailtoTracScript normal email2trac line breaks 0.10

### Description

Could you add a feature that wraps long lines in incoming emails for better reading in trac?

### comment:1 Changed 11 years ago by Bas van der Vlies

Status: new → assigned

### comment:2 Changed 11 years ago by Rainer Sokoll

Resolution: → fixed assigned → closed
• import textwrap
• Change line 552 body_text = part.get_payload(decode=1) to body_text = textwrap.fill(part.get_payload(decode=1), 70)

### comment:3 follow-up:  4 Changed 11 years ago by Bas van der Vlies

Resolution: fixed closed → reopened

Thanks a lot ;-). I will make it an option. How many chars can be put on a line. I will close it when i have applied the patch to trunk. Just for me a reminder

### comment:4 in reply to:  3 Changed 11 years ago by Rainer Sokoll

Thanks a lot ;-). I will make it an option. How many chars can be put on a line. I will close it when i have applied the patch to trunk.

Unfortunataly, I was way too fast. A new problem araised: textwrap.fill replaces every whitespace, including newlines, with a space.

For example:

word word
word

word word


will become

word word word word word


Maybe I can come up with a solution tomorrow.

### comment:5 follow-up:  6 Changed 11 years ago by anonymous

Then this is the solution:

print textwrap.fill(a, width=70, replace_whitespace=False)


Can you test it

### comment:6 in reply to:  5 Changed 11 years ago by Rainer Sokoll

Then this is the solution:

print textwrap.fill(a, width=70, replace_whitespace=False)


Can you test it

textwrap eats empty lines (\n\n). Also, its output is somewhat unpredictable. (I'm a bloody novice to python, so hopefully noone gets offended). At he moment, I have the feeling that textwrap is not the right tool for our problem.

### comment:7 follow-up:  8 Changed 11 years ago by anonymous

It is the right tool. In python 2.4 it does not eat up the newlines. Maybe we have to set an option for python 2.3

### comment:8 in reply to:  7 Changed 11 years ago by Rainer Sokoll

It is the right tool. In python 2.4 it does not eat up the newlines. Maybe we have to set an option for python 2.3

You are correct. In my tests, I had this:

from textwrap import TextWrapper
rainer = TextWrapper()
rainer.replace_whitespace = False
rainer.width = 70
s = 'some long text with line\nbreaks'
print rainer.fill(s)


which seems to work slightly different from your (much easier) solution. BTW: I'm running python 2.4.1

### comment:9 Changed 11 years ago by Bas van der Vlies

Rainer i have just applied a patch to trunk that implements the textwrap functionality:

email2trac.conf:

use_textwrap: 72

can you test it? It will wrap line longer then 72 chars or something else if you want.

### comment:10 Changed 11 years ago by Rainer Sokoll

As you can see here, the 2nd long line has extra linebreaks, I do not know why :-(

### comment:11 Changed 11 years ago by Bas van der Vlies

Maybe it does not reset the count and is it a bug in textwrap.

### comment:12 Changed 11 years ago by Bas van der Vlies

Rainer,

Can you replace:

                    body_text = textwrap.fill( body_text,
width = self.USE_TEXTWRAP,
replace_whitespace = False )


with

                    body_text = textwrap.fill( body_text,
width = self.USE_TEXTWRAP)


### comment:13 Changed 11 years ago by r.sokoll@…

With "replace_whitespace = False" see here Without, see here

The original mail was:

• long line
• line break
• long line
• line break
• long line

I'm afraid textwrap.py is broken. I have "1.35.4.1 2005/03/05 02:38:32 gward Exp"

### comment:14 Changed 11 years ago by Bas van der Vlies

Thanks, just read the docs and it only works on paragraphs ;-( So it see the whole text as one paragraph and it does not reset the counters.

### comment:15 Changed 11 years ago by Rainer Sokoll

A friend of mine sent me this snipplet. As mentioned before, I am a newbie to python, and this code is far beyond my skills, but maybe you can get valuable input.

import textwrap

LINESEPARATOR = '\n'

def breakString(str='', width=30):
return LINESEPARATOR.join(textwrap.fill(s,width) for s in str.split(LINESEPARATOR))

if __name__ == '__main__':
testString = 'asdfghjkl\nasdfghjkl\nasdfghjkl asdfghjkl\n\nasdfghjkl'
print breakString(testString)


### comment:16 Changed 11 years ago by Rainer Sokoll

The attached patch seems to solve the problem. Also fixes http://www.trac-hacks.org/ticket/1295 ;-)

### comment:17 Changed 11 years ago by Bas van der Vlies

Thanks i will apply it to trunk and close the ticket if i have actually did it ;-),

### comment:18 Changed 11 years ago by Bas van der Vlies

Applied to trunk. Thanks a lot

### comment:19 Changed 11 years ago by Bas van der Vlies

Resolution: → fixed reopened → closed

### Modify Ticket

Change Properties