Professional Web Applications Themes

Line wrapping - Ruby

On 2003-08-23 03:18:26 +0900, Andreas Schwarz wrote: > def wrap(s) > return s.gsub(/.{1,74}(?:\s|\Z)/){($& + 5.chr). > gsub(/\n

Thread: Line wrapping

5/,"\n").gsub(/

Thread: Line wrapping

5/,"\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

Thread: Line wrapping

5/,"\n").gsub(/

Thread: Line wrapping

5/,"\n")} end -- I have long suspected a cozy little link between virus writers and antivirus software makers. The latter certainly needs the former, both to keep viruses in the news and to provide a steady revenue stream from updates. -- Bruce Schneier, CRYPTO-GRAM, July ...

  1. #1

    Default Re: Line wrapping

    On 2003-08-23 03:18:26 +0900, Andreas Schwarz wrote:
    > 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

    --
    I have long suspected a cozy little link between virus writers and
    antivirus software makers. The latter certainly needs the former, both to
    keep viruses in the news and to provide a steady revenue stream from updates.
    -- Bruce Schneier, CRYPTO-GRAM, July 15, 2002

    Florian Frank Guest

  2. #2

    Default Re: Line wrapping

    Florian Frank wrote:
    > On 2003-08-23 03:18:26 +0900, Andreas Schwarz wrote:
    >> 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

    --
    AVR-Tutorial, über 350 Links
    Forum für AVRGCC und MSPGCC
    -> [url]http://www.mikrocontroller.net[/url]
    Andreas Schwarz Guest

  3. #3

    Default Re: Line wrapping

    Andreas Schwarz wrote:
    > Florian Frank wrote:
    >> On 2003-08-23 03:18:26 +0900, Andreas Schwarz wrote:
    >>> 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...

    --
    AVR-Tutorial, über 350 Links
    Forum für AVRGCC und MSPGCC
    -> [url]http://www.mikrocontroller.net[/url]
    Andreas Schwarz Guest

  4. #4

    Default Re: Line wrapping

    Andreas Schwarz wrote:
    > 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

    --
    AVR-Tutorial, über 350 Links
    Forum für AVRGCC und MSPGCC
    -> [url]http://www.mikrocontroller.net[/url]
    Andreas Schwarz Guest

  5. #5

    Default Re: Line wrapping

    Hi --

    On Sat, 23 Aug 2003, Andreas Schwarz wrote:
    > Andreas Schwarz wrote:
    > > It doesn't work correctly, I have to think about it again...
    See if this comes close....

    def wrap(s,len)
    s.gsub(/^[^>|].*?(?=^[>|]|\Z)/m) do |x|
    x.squeeze(" ").split(/\s+/).join(" ").
    gsub(/(\S.{1,#{len}}( |\Z))/) { |s| "#{s}\n" }
    end
    end


    David

    --
    David Alan Black
    home: [email]dblacksuperlink.net[/email]
    work: [email]blackdavshu.edu[/email]
    Web: [url]http://pirate.shu.edu/~blackdav[/url]


    dblack@superlink.net Guest

  6. #6

    Default Re: Line wrapping

    Andreas,
    > 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?
    First of all, note that this routine leaves trailing spaces before the
    inserted newlines ("\n") and also requires that the string *not* contain any
    ASCII 5 characters ("\005"). Both of these can be (mostly) fixed by
    changing the routine to the much simpler:

    def wrap(s)
    s.gsub(/(.{1,74})(\s+|\Z)/,"\\1\n")
    end

    However if the string contains multiple adjacent spaces, they may not
    all be removed. This can be remedied by appending a ".gsub(/ +\n/,'')" to
    the end of the existing gsub().

    Also note that this method is complicated by the fact that it allows for
    preexisting newlines in the string parameter. If your strings don't already
    have newlines (and you still don't want trailing spaces), you can rewrite
    the method to:

    def wrap(s)
    s.gsub(/(.{1,74})(\s+|$)/,"\\1\n")
    end

    All that being said, your question is a little ambiguous. The word
    "lines" could refer to the method's string parameter as a whole, or it could
    refer to individual substrings in that parameter separated by newlines. If
    you meant the former, any of these routines can be easily modified to ignore
    strings beginning with ">" or "|" by adding one line:

    def wrap(s)
    return s if s =~ /^[>|]/
    s.gsub(/(.{1,74})(\s+|$)/,"\\1\n")
    end

    If you meant the latter, it's a little more complicated - you basically
    need to split the strings into an array, word wrap all the single lines in
    the array, and then join them all back together:

    def wrap(s)
    s.split("\n").map do |t|
    if t =~ /^[>|]/
    t + "\n"
    else
    t.gsub(/(.{1,74})(\s+|$)/,"\\1\n")
    end
    end.join('')
    end

    I hope this helps!

    - Warren Brown


    Warren Brown Guest

  7. #7

    Default Re: Line wrapping

    Scripsit ille aut illa »Warren Brown« <wkbairmail.net>:
    > def wrap(s)
    > s.gsub(/(.{1,74})(\s+|\Z)/,"\\1\n")
    > end
    >
    > However if the string contains multiple adjacent spaces, they may not
    > all be removed. This can be remedied by appending a ".gsub(/ +\n/,'')" to
    > the end of the existing gsub().
    Why not just

    s.gsub(/(.{0,73}\S)(\s+|\Z)/,"\\1\n")

    (untested)


    --
    Steck Dir Deine Erkenntnisse da hin wo sie hingehören! Du kleiner\n
    verklemmter Hosenmatz!\nIch wette, Du bist picklig und für jede Frau
    abschreckend. Kein Wunder, daß\nDu Dich hier so produzierst.
    ["Lucrezia d`Lacroix" aka "Rebecca di Lorenzo" aka "Thomas Wendt"]
    Rudolf Polzer Guest

Similar Threads

  1. FORM wrapping
    By bill tie in forum ASP.NET Building Controls
    Replies: 3
    Last Post: August 12th, 12:52 AM
  2. wrapping?
    By ll in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: December 2nd, 06:04 AM
  3. DataList Wrapping
    By Jim Christiano in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: September 25th, 03:58 PM
  4. Wrapping ENV
    By Hal E. Fulton in forum Ruby
    Replies: 11
    Last Post: August 24th, 02:29 AM
  5. Wrapping old ASP with ASP.NET
    By Michael Dyremo in forum ASP.NET General
    Replies: 4
    Last Post: August 13th, 07:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •