Line wrapping

A

Andreas Schwarz

Hello,

I'm using the following function for line wrapping (found it somewhere
in the list archive):

def wrap(s)
return s.gsub(/.{1,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
end

Any hints how I can change the function so that it doesn't touch lines
with quoting characters (">", "|") in the beginning?

Thanks
Andreas
 
F

Florian Frank

def wrap(s)
return s.gsub(/.{1,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
end

Any hints how I can change the function so that it doesn't touch lines
with quoting characters (">", "|") in the beginning?

This should do the job:

def wrap(s)
return s.gsub(/^[^>|].{0,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
end
 
A

Andreas Schwarz

Florian said:
def wrap(s)
return s.gsub(/.{1,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
end

Any hints how I can change the function so that it doesn't touch lines
with quoting characters (">", "|") in the beginning?

This should do the job:

def wrap(s)
return s.gsub(/^[^>|].{0,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
end

No, it doesn't, but I've found this solution:

def wrap(s)
s = s.gsub(/.{0,74}(?:\s|\Z)/){($& + 5.chr).gsub(/\n\005/,"\n")}
return s.gsub(/((\n|^)[>|\s]*[>|].*?)\005/, "\\1").gsub(/\005/,"\n")
end

Andreas
 
A

Andreas Schwarz

Andreas said:
Florian said:
def wrap(s)
return s.gsub(/.{1,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
end

Any hints how I can change the function so that it doesn't touch lines
with quoting characters (">", "|") in the beginning?

This should do the job:

def wrap(s)
return s.gsub(/^[^>|].{0,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
end

No, it doesn't, but I've found this solution:

It doesn't work correctly, I have to think about it again...
 
A

Andreas Schwarz

Andreas said:
It doesn't work correctly, I have to think about it again...

OK, now this finally seems to work:

def wrap(a, max_len)
s = a.gsub(/.{0,#{max_len}}(?:\s|\Z)/){($& + 5.chr).gsub(/\n\005/,"\n")}
while(s.sub!(/((\n|^)[>|\s]*[>|].*?)\005/, "\\1")) do end
return s.gsub(/\005/,"\n")
end

I don't think it is a very good solution, so if someone comes up
with a better one I would really like to see it.

Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,262
Latest member
EffiePju4

Latest Threads

Top