Creating a webchat client using POE::Component::IRC is fun, but many get stuck when it gets to colors.
A conversion also helps when displaying IRC logs in a browser.
I copied and pasted from an xterm, so things might not be right. I named it colorcode.pm for now, but I'll need to think of a new name before I put it on CPAN.
# Made by Juerd <juerd@juerd.nl>
use strict;
sub colorcode ($) {
(my $str = $_[0]) =~ s/\e\[.*?[a-zA-Z]//g; # Immediately strip ANS
+I colors
if ($str =~ /(<br>)/) {
$str =~ s//\cO$1/g;
} else {
$str .= "\cO";
}
$str =~ s/\cV(.*?)\cV/\cC0,1$1\cO/g;
$str =~ s/\cV/\cC0,1/;
my @chars = split //, $str;
my $ret = '';
my ($CCfc, $CCbc, $CCnc);
my $CCb = 0;
my $CCu = 0;
while (@chars) {
my $char = shift @chars;
if ($char eq "\cB") { $ret .= ($CCb ? '</b>' : '<b>'); $CCb
+ = !$CCb; }
elsif ($char eq "\c_") { $ret .= ($CCu ? '</u>' : '<u>'); $CCu
+ = !$CCu; }
elsif ($char eq "\cO") { if ($CCb) { $CCb = 0; $ret .= '</b>';
+ }
if ($CCu) { $CCu = 0; $ret .= '</u>';
+ }
if ($CCnc){
$CCbc= 0; $ret .= '</span
+>' x $CCnc;
$CCfc= 0; $CCnc = 0; }
}
elsif ($char eq "\cC") {
my $CCcs = '';
while (($char = shift @chars) =~ /[\d,]/) {
$CCcs .= $char;
}
if ($char =~ /[^\d,]/) {
unshift @chars, $char;
}
if ($CCcs eq '') {
if ($CCbc) { $ret .= '</span>'; $CCnc--; }
if ($CCfc) { $ret .= '</span>'; $CCnc--; }
$CCbc = undef; $CCfc = undef;
} else {
my @k = split /,/, $CCcs;
if (defined($k[0]) && $k[0] ne '') {
$CCfc = $k[0];
$ret .= "<span class=fc$CCfc>";
$CCnc++;
}
if (defined($k[1]) && $k[1] ne '') {
$CCbc = $k[1];
$ret .= "<span class=bc$CCbc>";
$CCnc++;
}
}
} else {
$ret .= $char;
}
}
return $ret;
}
sub colorstyles () { # Call this sub before you call colorcode()
print qq{<style type="text/css"><!--\n};
my $i = 0;
for (qw'white black navy green red darkred purple orange yellow li
+me
aqua C0C0FF blue magenta gray silver') {
print qq{.fc$i { color: $_ } .bc$i { background-color: $_ }\n}
+;
$i++;
}
print '.fixed {font-family: fixedsys, courier new, courier, fixed;
+font-size: xx-xmall;}
--></style>';
}
1;