Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

irc colors to html

by Juerd (Abbot)
on Dec 16, 2001 at 01:30 UTC ( #132260=snippet: print w/ replies, xml ) Need Help??

Description: 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;

Comment on irc colors to html
Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://132260]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-08-23 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (172 votes), past polls