Hi folks. Something that always annoyed me a touch about PM is the way wrapping works in the CB. It isnt intelligent enough. It sticks spaces in at points where its inconvenient even when a char earlier would have been ideal. This makes it difficult to paste code from the cb into an editor. I started to tweak it, but kinda made a mess. Then I thought I should ask the monks at large to see what they think.

The following is the current code. It takes a string of html in $text and insert spaces into it to prevent there being more than 18 chars of consequentive non-whitespace. It is used often so it need to be fast and it musnt break any html or properly formed html entities in the text.

my $len= 0; $text =~ s{(\s+)|([^\s<&]+)|(<[^<>]*>)|(&#?\w{1,10};)|(.)}{ if( $1 ) { $len= 0; $1; } elsif( length( $2 ) ) { # $2 is the only case that can be "0" (ie. false) my $res= $2; my $tot= $len + length($res); if( 18 < $tot ) { substr( $res, 18-$tot, 0 )= " "; $res =~ s/(\S{18})(?=\S)/$1 /g; # replace previous with following for [tye]s improvment # $res =~ s/(\S{9,18}\b|\S{18})(?=\S)/$1 /g; $res =~ /(\S*)$/; $len= length( $1 ); } else { $len= $tot; } $res; } elsif( $3 ) { $3; } else { my $res= $4 || $5; my $add= $5 ? 1 : int( length($4)/3 ); $len += $add; if( 18 < $len ) { $len= $add; " $res"; } else { $res; } } }egis; return $text;

So, can anybody come up with a better solution? One that will rarely break code that is pasted? Ie that wraps for(1..20){$bar=$bop[1];print"$bar/$baz,$foo[$baz]"} in such a way the code doesnt break? Note it will be 'code'ified before being wrapped, so there may be html entities in the real version of the the previous string.

If we find a better way to do this that doesnt cost any more than we can use it for the site... So lets see what you folks can come up with. :-)

Note:Before posting this it was discussed in CB, tye mentioned the fix commented in the code as being a quick improvement. Its not currently used however.

---
demerphq


In reply to A call to keyboards: Better chatterbox wrapping by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.