problems with making wordwrap

Discussion in 'Ruby' started by luke, Nov 20, 2005.

  1. luke

    luke Guest

    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
    luke, Nov 20, 2005
    #1
    1. Advertising

  2. luke

    Pit Capitain Guest

    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
    Pit Capitain, Nov 20, 2005
    #2
    1. Advertising

  3. luke

    Eric Hodel Guest

    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://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
    Eric Hodel, Nov 20, 2005
    #3
  4. luke

    luke Guest


    > > 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
    luke, Nov 20, 2005
    #4
  5. luke

    Pit Capitain Guest

    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
    Pit Capitain, Nov 20, 2005
    #5
  6. On 11/20/05, Eric Hodel <> 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 *
    * Alternate:
    Austin Ziegler, Nov 20, 2005
    #6
  7. 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")
    William James, Nov 20, 2005
    #7
  8. luke

    luke Guest

    thanks pit,

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

    thanks a lot,
    luke




    "Pit Capitain" <> wrote in message
    news:...
    > 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
    >
    >
    luke, Nov 21, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    Wordwrap DataGrid Columns

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,807
    Saravana
    Nov 4, 2004
  2. Harvey Triana

    GridView without wordwrap

    Harvey Triana, Apr 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    524
    Harvey Triana
    Apr 3, 2006
  3. Matt
    Replies:
    1
    Views:
    514
    Sid Ismail
    Dec 23, 2004
  4. Christoph

    <pre> and wordwrap

    Christoph, Sep 23, 2006, in forum: HTML
    Replies:
    1
    Views:
    563
    Toby Inkster
    Sep 23, 2006
  5. =?Utf-8?B?Z3V5?=

    GridView forcing wordwrap

    =?Utf-8?B?Z3V5?=, Oct 29, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    563
    Cowboy \(Gregory A. Beamer\)
    Oct 29, 2007
Loading...

Share This Page