Forum: Ruby problems with making wordwrap

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E823a4df8d9d93c0f6097f9a118048e6?d=identicon&s=25 l.d.u.n.c.a.l.f.e (Guest)
on 2005-11-20 07:09
(Received via mailing list)
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
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 pit (Guest)
on 2005-11-20 08:34
(Received via mailing list)
luke schrieb:
> 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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 drbrain (Guest)
on 2005-11-20 09:02
(Received via mailing list)
On Nov 19, 2005, at 10:07 PM, luke wrote:

> 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
E823a4df8d9d93c0f6097f9a118048e6?d=identicon&s=25 l.d.u.n.c.a.l.f.e (Guest)
on 2005-11-20 10:18
(Received via mailing list)
> > 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
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 pit (Guest)
on 2005-11-20 13:51
(Received via mailing list)
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
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 halostatue (Guest)
on 2005-11-20 15:52
(Received via mailing list)
On 11/20/05, Eric Hodel <drbrain@segment7.net> 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
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 w_a_x_man (Guest)
on 2005-11-20 23:56
(Received via mailing list)
luke (dot) wrote:
> 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")
E823a4df8d9d93c0f6097f9a118048e6?d=identicon&s=25 l.d.u.n.c.a.l.f.e (Guest)
on 2005-11-21 02:33
(Received via mailing list)
thanks pit,

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

thanks a lot,
luke




"Pit Capitain" <pit@capitain.de> wrote in message
news:43807149.9050509@capitain.de...
> 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
This topic is locked and can not be replied to.