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.
In reply to A call to keyboards: Better chatterbox wrapping by demerphq
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |