in reply to Game Nicknames to HTML

With no offence intended to ELISHEVA, I had some fun with this and here is some code. You can look at it later if you want to try yourself first. It may be most helpful as an example of how not to do it. There is no attempt to minimize nesting of tags. You will have to play with CSS to get the effects you want.

update: revised, with thanks to ELISHEVA for guidance, and with tests added (which I should have done in the first place). There may still be bugs, as the test cases aren't exhaustive, but it's less buggy than it was.

#! /usr/local/bin/perl use strict; use warnings; # $i: italic # $s: shadowed # $w: wide spacing # $n: narrow spacing # $m: normal setting # $g: default color # $t: changes the text to capitals # $z: reset all # $$: to write a "$" my %classes = ( i => 'italics', s => 'shadowed', w => 'wide', n => 'narrow', m => 'normal', ); my %testcases = ( '$iMy$z$w$f00Nickname' => '<span class="italics">My</span><span cl +ass="wide"><font color="ff0000">Nickname</font></span>', '$s$5aaD$ddfia$5aaD$ddfe$5cc§' => '<span class="shadowed"><font co +lor="55aaaa">D<font color="ddddff">ia<font color="55aaaa">D<font colo +r="ddddff">e<font color="55cccc">§</font></font></font></font></font> +</span>', '$iT$sh$wi$ns is $m$444silly$z $tsmall stuff$z and BIG STUFF' => ' +<span class="italics">T<span class="shadowed">h<span class="wide">i<s +pan class="narrow">s is <span class="normal"><font color="444444">sil +ly</font></span></span></span></span></span> SMALL STUFF and BIG STUF +F', '$wWIDE$nNARROW$mnormal' => '<span class="wide">WIDE<span class="n +arrow">NARROW<span class="normal">normal</span></span></span>', '$ta$$zas$zdf' => 'A$ZASdf', '$tasdf' => 'ASDF', ); foreach ( keys %testcases ) { my $string = $_; my @tags; while ( $string =~ m/(\$[iswnmgtz\$]|\$[0-9a-f]{3,3})/ig ) { my $pos = pos($string); my $taglen = 2; my $replacement = ""; my $c = lc(substr($1,1,1)); if(my $class = $classes{$c}) { $replacement = "<span class=\"$class\">"; unshift(@tags, '</span>'); } elsif($c eq 'g') { $replacement = '<font color="#000000">'; unshift(@tags, '</font>'); } elsif($c eq 't') { my $length = length($string) - $pos; while ( $string =~ m/(\$[iswnmgtz\$]|\$[0-9a-f]{3,3})/ig ) + { if(lc($1) eq '$z') { $length = pos($string) - $pos - 2; last; } } substr($string, $pos, $length, uc( substr($string, $pos, $ +length) ) ); } elsif($c eq 'z') { local $" = ""; $replacement = "@tags"; @tags = (); } elsif($c eq '$') { $replacement = '$'; } else { $taglen = 4; my ($x, $r, $g, $b) = split(//,$1); $replacement = "<font color=\"$r$r$g$g$b$b\">"; unshift(@tags, '</font>'); } substr($string, $pos - $taglen, $taglen, $replacement); pos($string) = $pos - $taglen + length($replacement); } { local $" = ""; $string .= "@tags"; } if($string eq $testcases{$_}) { print "Ok '$_' =>\n '$string'\n"; } else { print "Not Ok '$_' =>\n '$string' expecting:\n '$testcases{$_ +}'\n"; } }

Following is the original buggy version.

#! /usr/local/bin/perl use strict; use warnings; # $i: italic # $s: shadowed # $w: wide spacing # $n: narrow spacing # $m: normal setting # $g: default color # $t: changes the text to capitals # $z: reset all # $$: to write a "$" my %classes = ( i => 'italics', s => 'shadowed', w => 'wide', n => 'narrow', m => 'normal', ); foreach ( '$iMy$z$w$f00Nickname', '$s$5aaD$ddfia$5aaD$ddfe$5cc§', '$iT$sh$wi$ns is $m$444silly$z $tsmall stuff$z and BIG STUFF', '$wWIDE$nNARROW$mnormal', ) { my $string = $_; my @tags; while ( $string =~ m/(\$[iswnmgtz\$]|\$[0-9a-f]{3,3})/ig ) { my $c = lc(substr($1,1,1)); if(my $class = $classes{$c}) { substr($string,pos($string) - 2, 2, "<span class=\"$class\ +">"); push(@tags, '</span>'); } elsif($c eq 'g') { substr($string,pos($string) - 2, 2, '<font color="#000000" +>'); push(@tags, '</font>'); } elsif($c eq 't') { my $end = index($string, '$z', pos($string)); $end = length($string) if($end == -1); substr($string,pos($string)-2 ,$end - pos($string) +2, uc( +substr($string,pos($string), $end - pos($string)))); } elsif($c eq 'z') { { local $" = ""; substr($string,pos($string) - 2, 2, "@tag +s"); } @tags = (); } elsif($c eq '$') { substr($string,pos($string) - 2, 2, '$'); } else { my ($x, $r, $g, $b) = split(//,$1); substr($string,pos($string) - 4, 4, "<font color=\"$r$r$g$ +g$b$b\">"); push(@tags, '</font>'); } } { local $" = ""; $string .= "@tags"; } print "$string\n"; }

CSS something like the following may be a start:

<style type="text/css"> <!-- SPAN.italics { font-style: italic; } SPAN.shadowed { background-color: #666; color: inherit; } SPAN.wide { font-stretch: wider; } SPAN.narrow { font-stretch: narrower; } SPAN.normal { font-stretch: normal; } --> </style>

It creates results like the following:

<span class="italics">My</span><span class="wide"><font color="ff0000" +>Nickname</span></font> <span class="shadowed"><font color="55aaaa">D<font color="ddddff">ia<f +ont color="55aaaa">D<font color="ddddff">e<font color="55cccc">§</spa +n></font></font></font></font></font> <span class="italics">T<span class="shadowed">h<span class="wide">i<sp +an class="narrow">s is <span class="normal"><font color="444444">sill +y</span></span></span></span></span></font> SMALL STUFF and BIG STUFF <span class="wide">WIDE<span class="narrow">NARROW<span class="normal" +>normal</span></span></span>

Replies are listed 'Best First'.
Re^2: Game Nicknames to HTML
by baltic.sailor (Novice) on Mar 29, 2009 at 21:12 UTC
    Wow, thank you for that code. I will try to understand what I have got here.
    This site has a lot of experts as I see it now!

    Thanks a lot
    Thomas

Re^2: Game Nicknames to HTML
by baltic.sailor (Novice) on Mar 30, 2009 at 08:58 UTC
    Thanks a lot again

    here are some more test cases :-)

    $s$02fCamster $0bfb$018s$0bfc $00fS$fffa$00fo$fffr$00fs$fffa $00fB$fffs$00fc $i$s$f06Jasmine $i$s$f90Ario$06fñ$f90e $s$ff0 $s$fob $s$i$00aM$0afarcus b$00as$0afc $i$fffArthur $s$00fBSC $f00N$fffE$00fD $w$s$fffSalt$i$m$00fheart sonny $s$36FBSC $s$5aaD$ddfia$5aaD$ddfe$5cc§ $ff0hula $s$i$f00Ma$fffnta $00fGB $i$s$t $fffPolar Bear $w$325TULEBA$500(bsc) $s$i$fffAdoo$015nis $i$0BFb$018s$0BFc

      I have rewritten to avoid nesting of tags. The new version succeeds on all cases except "$i$s$f90Ario$06fñ$f90e $s$ff0 $s$fob" which has an invalid "$fob" at the end. For the moment it leaves the $f unchanged but maybe it should be removed, or maybe only the $ should be removed.

      This version of the script seems to work. I would welcome any suggestions for improvement. Are there modules I should consider? Better ways to test??

        Again, thanks a lot for the work you are doing here!!

        I will do some further testing next weekend and tell you what I found out. It was not my intention to create so much work. Initially I was just looking for a module to do this job.

        I totally appreciate your work on this!

        If you want some more input to test with here it is:

        $000bow$f00man$ff042 $00f(ayc200) $000det$i$F00mit$z$s$FF0 f $00fBar$fffbe Bl$f00eue $000 ~TEAM DEMO $00fCricka $ff0VSK-SwedenRace $00fJ$z$fffb$00fa$z$fffr $w$00f01 $00fjl$fffl$foo14 $00fS$fffa$00fo$fffr$00fs$fffa $00fB$fffs$00fc $00Fto$FFFm$F0014 $09fDelphisa $0f0A$ffflar$f00e $i$nSeppia Team $0F0r$0F5a$0FBf$0EFf$08Fa$03F $30F[vska] $0f9morgan $c90goldfinger II ayc700 $f00 VM - ($fffden$f00 259) $f00$sA$0f0r$ffft ( $0f wales$fff) $F00J$F88a$FFFm$FFFp$00ai$009t $F00Jaws$009(AUS10$009) $f00Steel$fffviking$f00(DEN) $ff0hula $i$3f0$sWazari-$00fBSC-$z$i$f00U$fffS$00fA $i$900<-j$fffT$900l-$n<< $i$900<-jTl-<$n$0bfb$018S$0bfc $i$900j$fffT$900l-$n$6cfb$00fS$6cfc $900 Happy Birthday $i$900Sweet emma of new Orleans $i$cc6$sTi'Chon $00fb.$fffw$f00.s $i$fffArthur $s$00fBSC $f00N$fffE$00fD $i$s$0f0Mivla /$z$i$s$f00/ por $i$s$3f3Franco$f00Drao $z$s$fff $I$S$F00Prev$fffeze$F00***$Footur$fffkey*** $i$s$f06Jasmine $i$s$f06Luna $i$s$f80l$59funa$f80s$59fard$f80a$z $i$s$f80Mart$fff ($f00n$fffe$00fd$fff) $i$s$f90Ario$06fñ$f90e $s$ff0 $s$fob $i$s$ff0WabbiT $fffAYC - $f00450 $i$s$fffLuna $i$s$fofLuna $i$s$t $fffPolar Bear $i$w$0f0cocal $n$ff$i_~_ $M$S$i$f00ANTISTENE $00fB.$fffW.$f00S $N$000kiwi_$FFFbardy $n$s$090Magnolia $s $f80Schollie $fff($f00n$fffe$00fd$fff) $s$02fCamster $0bfb$018s$0bfc $s$11aPaolofico $a11yc$s$0f0W$fffL$f00F $i$s$fd0I-Team $s$5aaD$ddfia$5aaD$ddfe$5cc§ $s$888SunnyVale Trailer Park $s$a00Crazycat $s$f01BSC $s$aaaTurebo$z$f81Mc$zQeck$f72($Foon$fffe$00ad$f72) $s$b01Crazycat $s$f01BSC $s$f80Thermic $s$fooCom$fffpul$00fsion - $i$nvsk-$t$f00u$fffs$00fa $s$i$00aM$0afarcus $s$i$00aM$0afarcus b$00as$0afc $s$i$444sun$e22b$444urn $S$i$C11euphoria $000VSK $C11Nordic $s$i$f00Ma$fffnta $00fGB $s$i$f00Ma$fffnta $00fGB $w$s$00cB$w$s$09fS$w$s$00cC $s$i$F90Davarch $s$i$fc0euphoria $000VSK $C11Nordic $s$i$fc0odin $s$i$fffAdoo$015nis $i$0BFb$018s$0BFc $s$i$fffAdoo$018nis $i$0BFB$018S$0BFC $s$i$fffAdoo$018nis $i$n$s$0BFB$018S$0BFC $s$i$fffAdoo$91Fnis $s$w$i$006GRE144$FF3 i-team $W$00cSWE 110 $n$ff0VSK Sweden Race $w$325TULEBA$500(bsc) $w$s$3cfnewworld-bsc $w$s$fffHenry$f00HH $w$s$fffSalt$i$m$00fheart $w$s$i$09fy$05fu$00fg $10fb.$10fw.$10fs $w$s$i$0cfaramis $w$s$i$0cfbre$0afta$09fgn$05fe 3$00f5 b.$fffw.$f00s $w$s$i$0cff$0afr$09f9$05f8$00f5 b.$fffw$f00.s $w$s$i$0cfgil$0afle$09fs 2 b$05fob$00fo b.$fffw.$f00s $w$s$i$0cfsurcouf Vic_Anacortes $m$f00B$fffS$00fC
        Thomas