File Coverage

blib/lib/IRC/Formatting/HTML/Output.pm
Criterion Covered Total %
statement 77 82 93.9
branch 43 48 89.5
condition 17 23 73.9
subroutine 11 11 100.0
pod 0 1 0.0
total 148 165 89.7


line stmt bran cond sub pod time code
1             package IRC::Formatting::HTML::Output;
2              
3 4     4   25 use warnings;
  4         8  
  4         111  
4 4     4   23 use strict;
  4         8  
  4         132  
5              
6 4     4   2410 use IRC::Formatting::HTML::Common;
  4         11  
  4         4852  
7              
8             my ($b, $i, $u, $fg, $bg);
9             my $italic_invert = 0;
10             my $use_classes = 0;
11              
12             sub _parse_formatted_string {
13 17     17   559 my $line = shift;
14 17         37 _reset();
15 17         20 my @segments;
16 17         349 my @chunks = ("", split($FORMAT_SEQUENCE, $line));
17 17         33 $line = "";
18 17         213 while (scalar(@chunks)) {
19 40         66 my $format_sequence = shift(@chunks);
20 40         225 my $text = shift(@chunks);
21 40         81 _accumulate($format_sequence);
22 40 100 100     404 next unless defined $text and length $text;
23 20         35 $text =~ s/ {2}/  /g;
24 20 100       227 if ($use_classes) {
25 4         10 $line .= "$text";
26             } else {
27 16         36 $line .= "$text";
28             }
29             }
30 17         250 return $line;
31             }
32              
33              
34             sub _reset {
35 17     17   33 ($b, $i, $u) = (0, 0, 0);
36 17         21 undef $fg;
37 17         24 undef $bg;
38             }
39              
40             sub _accumulate {
41 40     40   53 my $format_sequence = shift;
42 40 100       591 if ($format_sequence eq $BOLD) {
    100          
    100          
    50          
    100          
43 6         14 $b = !$b;
44             }
45             elsif ($format_sequence eq $UNDERLINE) {
46 4         9 $u = !$u;
47             }
48             elsif ($format_sequence eq $INVERSE) {
49 8         17 $i = !$i;
50             }
51             elsif ($format_sequence eq $RESET) {
52 0         0 _reset;
53             }
54             elsif ($format_sequence =~ $COLORM) {
55 5         13 ($fg, $bg) = _extract_colors_from($format_sequence);
56             }
57             }
58              
59             sub _extract_colors_from {
60 5     5   10 my $format_sequence = shift;
61 5         13 $format_sequence = substr($format_sequence, 1);
62 5         31 my ($_fg, $_bg) = ($format_sequence =~ $COLOR_SEQUENCE);
63 5 50       21 if (! defined $_fg) {
    50          
64 0         0 return undef, undef;
65             }
66             elsif (! defined $_bg) {
67 0         0 return $_fg, $bg;
68             }
69             else {
70 5         22 return $_fg, $_bg;
71             }
72             }
73              
74             sub _to_css {
75 16     16   30 my $styles = "";
76              
77 16         20 my ($_fg, $_bg);
78              
79             # italicize inverted text if that option is set
80 16 100       31 if ($i) {
81 8 100       16 if ($italic_invert) {
82 2         5 $styles .= "font-style: italic;";
83 2         6 ($_fg, $_bg) = ($fg, $bg);
84             } else {
85 6   100     227 ($_fg, $_bg) = ($bg || 0, $fg || 1);
      100        
86             }
87             } else {
88 8         18 ($_fg, $_bg) = ($fg, $bg);
89             }
90              
91 16 100 66     265 $styles .= "color: #$COLORS[$_fg];" if defined $_fg and $COLORS[$_fg];
92 16 100 66     86 $styles .= "background-color: #$COLORS[$_bg];" if defined $_bg and $COLORS[$_bg];
93 16 100       45 $styles .= "font-weight: bold;" if $b;
94 16 100       39 $styles .= "text-decoration: underline;" if $u;
95 16         292 return $styles;
96             }
97              
98             sub _to_classes {
99 4     4   4 my @classes;
100              
101 4         5 my ($_fg, $_bg);
102              
103             # italicize inverted text if that option is set
104 4 100       10 if ($i) {
105 1 50       5 if ($italic_invert) {
106 0         0 push @classes, "italic";
107 0         0 ($_fg, $_bg) = ($fg, $bg);
108             } else {
109 1   50     12 ($_fg, $_bg) = ($bg || 0, $fg || 1);
      50        
110             }
111             } else {
112 3         6 ($_fg, $_bg) = ($fg, $bg);
113             }
114              
115 4 100 66     20 push @classes, "fg-$COLORS[$_fg]" if defined $_fg and $COLORS[$_fg];
116 4 100 66     18 push @classes, "bg-$COLORS[$_bg]" if defined $_bg and $COLORS[$_bg];
117 4 100       11 push @classes, "bold" if $b;
118 4 100       9 push @classes, "ul" if $u;
119 4         26 return join " ", @classes;
120             }
121              
122             sub parse {
123 12     12 0 25 my ($string, $italic, $classes) = @_;
124              
125 12 100       28 $italic_invert = 1 if $italic;
126 12 100       29 $use_classes = 1 if $classes;
127 12         30 _encode_entities(\$string);
128              
129 17         44 my $text = join "\n",
130 12         45 map {_parse_formatted_string($_)}
131             split "\n", $string;
132              
133 12         24 $italic_invert = 0;
134 12         15 $use_classes = 0;
135              
136 12         51 return $text;
137             }
138              
139             sub _encode_entities {
140 12     12   14 my $string = shift;
141 12 50       199 return unless $string;
142 12         25 $$string =~ s/&/&/g;
143 12         17 $$string =~ s/
144 12         18 $$string =~ s/>/>/g;
145 12         186 $$string =~ s/"/"/g;
146             }
147              
148             1;