Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > problems with making wordwrap

Reply
Thread Tools

problems with making wordwrap

 
 
luke
Guest
Posts: n/a
 
      11-20-2005
hi,

i've found a great bit of code from

http://dev.rubyonrails.org/ticket/1449

that takes some text and makes it wrap at 80 lines. the only issue is that
it takes multiple line breaks (like paragraphs), and makes them into single
line breaks.

this is the code:

text.gsub( /\n/, "\n\n" ).gsub( /(.{1,80})(\s+|$)/, "\\1\n")

i've tinkered with it for a while, but i can't work out how to not have it
eat lines breaks so hungrily.

thanks for any help
luke


 
Reply With Quote
 
 
 
 
Pit Capitain
Guest
Posts: n/a
 
      11-20-2005
luke schrieb:
> i've found a great bit of code from
>
> http://dev.rubyonrails.org/ticket/1449
>
> that takes some text and makes it wrap at 80 lines. the only issue is that
> it takes multiple line breaks (like paragraphs), and makes them into single
> line breaks.
>
> this is the code:
>
> text.gsub( /\n/, "\n\n" ).gsub( /(.{1,80})(\s+|$)/, "\\1\n")
>
> i've tinkered with it for a while, but i can't work out how to not have it
> eat lines breaks so hungrily.


Hi Luke,

can you tell us how you want to treat whitespace in the given text? You
talked about newlines, but what should be the result in the following cases:

"abcde\n c".wrap(5) # => "abcde\nc" or "abcde\n c"
"a e\na".wrap(5) # => "a e\na" or "a e a"

Regards,
Pit


 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      11-20-2005
On Nov 19, 2005, at 10:07 PM, luke wrote:

> hi,
>
> i've found a great bit of code from
>
> http://dev.rubyonrails.org/ticket/1449
>
> that takes some text and makes it wrap at 80 lines. the only issue
> is that
> it takes multiple line breaks (like paragraphs), and makes them
> into single
> line breaks.


I use Text::Format for this:

def wrap(text)
formatter = Text::Format.new
formatter.columns = 72 # 70 + '> ' = 72
formatter.format_style = Text::Format::LEFT_ALIGN
formatter.first_indent = 0

formatted = []

text.split(/\r?\n\r?\n/).each do |chunk|
formatted << formatter.paragraphs(chunk)
end

return formatted.join("\n")
end

http://www.halostatue.ca/ruby/Text__Format.html

--
Eric Hodel - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04




 
Reply With Quote
 
luke
Guest
Posts: n/a
 
      11-20-2005

> > i've found a great bit of code from
> >
> > http://dev.rubyonrails.org/ticket/1449
> >
> > that takes some text and makes it wrap at 80 lines. the only issue is

that
> > it takes multiple line breaks (like paragraphs), and makes them into

single
> > line breaks.
> >
> > this is the code:
> >
> > text.gsub( /\n/, "\n\n" ).gsub( /(.{1,80})(\s+|$)/, "\\1\n")
> >
> > i've tinkered with it for a while, but i can't work out how to not have

it
> > eat lines breaks so hungrily.

>
> Hi Luke,
>
> can you tell us how you want to treat whitespace in the given text? You
> talked about newlines, but what should be the result in the following

cases:
>
> "abcde\n c".wrap(5) # => "abcde\nc" or "abcde\n c"
> "a e\na".wrap(5) # => "a e\na" or "a e a"
>
> Regards,
> Pit
>


hi pit,

thanks for your help.

ideally how i would like it to behave given your examples, would be like:

"abcde\n c".wrap(5) # => "abcde\nc"
"a e\na".wrap(5) # => "a e\na"

does that make sense? so text would be flush with the left margin with no
white space at the beginning of new lines. but whitespace within a line
should remain intact.

thanks

luke




 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      11-20-2005
luke schrieb:
> ideally how i would like it to behave given your examples, would be like:
>
> "abcde\n c".wrap(5) # => "abcde\nc"
> "a e\na".wrap(5) # => "a e\na"
>
> does that make sense? so text would be flush with the left margin with no
> white space at the beginning of new lines. but whitespace within a line
> should remain intact.


Of course it makes sense. Here's one version:

class String
def wrap n
gsub(
/
\b # word boundary
[ \t\r\f]* # whitespace (no newline)
\n # newline
[ \t\r\f]* # whitespace (no newline)
\b # word boundary
/x,
" " # replaced by space
).gsub(
/
(.{1,#{n}}) # upto n characters
( # followed by either:
\n # exactly one newline
|\s+ # or other whitespace characters
)
/x,
"\\1\n" # insert newline after first part
)
end
end

I used extended regular expressions to show what they are doing. You can
shorten them if you want.

The first gsub replaces newlines inside of paragraphs into spaces, but
leaves newlines between paragraphs unchanged. The second gsub is mostly
the original one, but it consumes at most one newline character
(replacing it with itself). This has the effect that newlines between
paragraphs are preserved.

If you still have problems or questions, feel free to ask.

Regards,
Pit


 
Reply With Quote
 
Austin Ziegler
Guest
Posts: n/a
 
      11-20-2005
On 11/20/05, Eric Hodel <(E-Mail Removed)> wrote:
> On Nov 19, 2005, at 10:07 PM, luke wrote:
> I use Text::Format for this:


If I remember correctly, portions or the whole of Text::Format is
included with rails-core, too, but it may not be easily accessible.

-austin
--
Austin Ziegler * (E-Mail Removed)
* Alternate: (E-Mail Removed)


 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      11-20-2005
luke (dot) wrote:
> hi,
>
> i've found a great bit of code from
>
> http://dev.rubyonrails.org/ticket/1449
>
> that takes some text and makes it wrap at 80 lines. the only issue is that
> it takes multiple line breaks (like paragraphs), and makes them into single
> line breaks.
>
> this is the code:
>
> text.gsub( /\n/, "\n\n" ).gsub( /(.{1,80})(\s+|$)/, "\\1\n")
>
> i've tinkered with it for a while, but i can't work out how to not have it
> eat lines breaks so hungrily.
>
> thanks for any help
> luke


text.gsub( /(.{1,39}[^\s])([ \t]*\n[ \t]*|[ \t]+|$)/, "\\1\n")

 
Reply With Quote
 
luke
Guest
Posts: n/a
 
      11-21-2005

thanks pit,

works like a treat! nice idea of having it in the string class.

thanks a lot,
luke




"Pit Capitain" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> luke schrieb:
> > ideally how i would like it to behave given your examples, would be

like:
> >
> > "abcde\n c".wrap(5) # => "abcde\nc"
> > "a e\na".wrap(5) # => "a e\na"
> >
> > does that make sense? so text would be flush with the left margin with

no
> > white space at the beginning of new lines. but whitespace within a line
> > should remain intact.

>
> Of course it makes sense. Here's one version:
>
> class String
> def wrap n
> gsub(
> /
> \b # word boundary
> [ \t\r\f]* # whitespace (no newline)
> \n # newline
> [ \t\r\f]* # whitespace (no newline)
> \b # word boundary
> /x,
> " " # replaced by space
> ).gsub(
> /
> (.{1,#{n}}) # upto n characters
> ( # followed by either:
> \n # exactly one newline
> |\s+ # or other whitespace characters
> )
> /x,
> "\\1\n" # insert newline after first part
> )
> end
> end
>
> I used extended regular expressions to show what they are doing. You can
> shorten them if you want.
>
> The first gsub replaces newlines inside of paragraphs into spaces, but
> leaves newlines between paragraphs unchanged. The second gsub is mostly
> the original one, but it consumes at most one newline character
> (replacing it with itself). This has the effect that newlines between
> paragraphs are preserved.
>
> If you still have problems or questions, feel free to ask.
>
> Regards,
> Pit
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView forcing wordwrap =?Utf-8?B?Z3V5?= ASP .Net 1 10-29-2007 03:18 PM
<pre> and wordwrap Christoph HTML 1 09-23-2006 08:26 PM
GridView without wordwrap Harvey Triana ASP .Net 0 04-03-2006 03:12 PM
table and wordwrap formatting Matt HTML 1 12-23-2004 07:01 AM
Wordwrap DataGrid Columns =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?= ASP .Net 1 11-04-2004 04:41 AM



Advertisments