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 | |
|
Re^2: Game Nicknames to HTML
by baltic.sailor (Novice) on Mar 30, 2009 at 08:58 UTC | |
by ig (Vicar) on Mar 30, 2009 at 22:20 UTC | |
by baltic.sailor (Novice) on Mar 31, 2009 at 08:34 UTC |