root/lang/perl/mobirc/trunk/mobirc/lib/Mobirc/HTTPD/Filter/DecorateIRCColor.pm @ 976

Revision 976, 3.3 kB (checked in by tokuhirom, 6 years ago)

lang/perl/mobirc: now text filter is pluggable(WARNINGS: INCOMPATIBLE CHANGE!!)

Line 
1package Mobirc::HTTPD::Filter::DecorateIRCColor;
2use strict;
3use warnings;
4
5sub process {
6    my ( $class, $text, $conf ) = @_;
7
8    return _decorate_irc_color($text);
9}
10
11sub _decorate_irc_color {
12    my $src = shift;
13
14    if ( $src !~ /[\x02\x03\x0f\x16\x1f]/ ) {
15
16        # skip without colorcode
17        return $src;
18    }
19
20    my $colorized     = '';
21    my %default_state = (
22        bold      => 0,
23        inverse   => 0,
24        underline => 0,
25    );
26    my %state       = (%default_state);
27    my $oldstyle    = '';
28    my $output_span = sub {
29        my $style = '';
30        if ( $state{bold} ) {
31            $style .= "font-weight:bold;";
32        }
33        if ( $state{underline} ) {
34            $style .= "text-decoration:underline;";
35        }
36        if ( $state{inverse} ) {
37
38            # xxx not sure this is correct
39            @state{qw(color bgcolor)} = @state{qw(bgcolor color)};
40
41            # XXX too bad
42            delete $state{inverse};
43        }
44        if ( $state{color} ) {
45            if ( my $color = _irc_color( $state{color} ) ) {
46                $style .= "color:$color;";
47            }
48        }
49        if ( $state{bgcolor} ) {
50            if ( my $color = _irc_color( $state{bgcolor} ) ) {
51                $style .= "background-color:$color;";
52            }
53        }
54        my $output = '';
55        if ( $oldstyle ne $style ) {
56            if ($oldstyle) {
57                $output .= '</span>';
58            }
59            if ($style) {
60                $output .= qq{<span style="$style">};
61            }
62        }
63        $oldstyle = $style;
64        $output;
65    };
66
67    while ($src) {
68        if ( $src =~ s/^\x02// ) {
69            $state{bold} = !$state{bold};
70        }
71        elsif ( $src =~ s/^\x03(?:(\d{1,2})(?:,(\d{1,2}))?)?// ) {
72
73            # if it has bad color specifiers, just ignore it.
74            $state{color}   = int($1) if $1;
75            $state{bgcolor} = int($2) if $2;
76        }
77        elsif ( $src =~ s/^\x0f// ) {
78            %state = (%default_state);
79        }
80        elsif ( $src =~ s/^\x16// ) {
81            $state{inverse} = !$state{inverse};
82        }
83        elsif ( $src =~ s/^\x1f// ) {
84            $state{underline} = !$state{underline};
85        }
86        elsif ( $src =~ s/^([^\x02\x03\x16\x1f\x0f]+)// ) {
87            $colorized .= $output_span->() . $1;
88        }
89        else {
90            if ( $src =~ s/^(.*)$// ) {
91                # garbase
92                use Data::Dumper;
93                # DEBUG "garbage: " . Dumper($1);
94                $colorized .= $output_span->() . $1;
95            }
96            last;
97        }
98    }
99    %state = (%default_state);
100    $colorized .= $output_span->();
101
102    return $colorized;
103}
104
105my %color_table;
106
107BEGIN {
108    %color_table = (
109        0  => [qw(white)],
110        1  => [qw(black)],
111        2  => [qw(blue         navy)],
112        3  => [qw(green)],
113        4  => [qw(red)],
114        5  => [qw(brown        maroon)],
115        6  => [qw(purple)],
116        7  => [qw(orange       olive)],
117        8  => [qw(yellow)],
118        9  => [qw(lightgreen   lime)],
119        10 => [qw(teal)],
120        11 => [qw(lightcyan    cyan aqua)],
121        12 => [qw(lightblue    royal)],
122        13 => [qw(pink         lightpurple  fuchsia)],
123        14 => [qw(grey)],
124        15 => [qw(lightgrey    silver)],
125    );
126}
127
128sub _irc_color {
129    my $num = shift;
130    $color_table{$num}->[0];
131}
132
1331;
Note: See TracBrowser for help on using the browser.