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;

In reply to irc colors to html by Juerd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.