blib/lib/String/Tagged/IRC.pm

filter: Statementstmt 89.7% 96 / 107 Branchbran 77.3% 65 / 84 Conditioncond 64.7% 22 / 34 MC/DCmcdc 36.8% 7 / 19 Subroutinesub 84.6% 11 / 13 Podpod 50.0% 2 / 4 totaltotal 77.7% 203 / 261 CC 57 SCAR 31.6
CC
57
Cov
81%
parse_irc
39.5
__ANON__
17.3
_parse_colour_mirc
5.9
CRAP 23.6
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2014-2016 -- leonerd@leonerd.org.uk
5  
6 package String::Tagged::IRC;
7  
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use 5.010; # //
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use base qw( String::Tagged );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 String::Tagged->VERSION( '0.11' ); # ->clone
13  
14 4 our $VERSION = '0.03';
15  
16 4 ▶use Convert::Color::mIRC;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Convert::Color::RGB8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 C<String::Tagged::IRC> - parse and format IRC messages using C<String::Tagged>
22  
23 =head1 TAGS
24  
25 This module provides the following tags, conforming to the
26 L<String::Tagged::Formatting> API specification.
27  
28 =head2 bold, under, italic, reverse
29  
30 Boolean values indicating bold, underline, italics, or reverse-video.
31  
32 =head2 fg, bg
33  
34 L<Convert::Color> objects encoding the color. These will likely be instances
35 of L<Convert::Color::mIRC>, unless a full RGB triplet colour code has been
36 provided; in which case it will be an instance of L<Convert::Color::RGB8>.
37  
38 =cut
39  
40 # IRC [well, technically mIRC but other clients have adopted it] uses Ctrl
41 # characters to toggle formatting
42 # ^B = bold
43 # ^U = underline
44 # ^_ = underline
45 # ^R = reverse or italic - we'll use italic
46 # ^V = reverse
47 # ^] = italics
48 # ^O = reset
49 # ^C = colour; followed by a code
50 # ^C = reset colours
51 # ^Cff = foreground
52 # ^Cff,bb = background
53 #
54 # irssi uses the following
55 # ^D$$ = foreground/background, in chr('0'+$colour),
56 # ^Db = underline
57 # ^Dc = bold
58 # ^Dd = reverse or italic - we'll use italic
59 # ^Dg = reset colours
60 #
61 # As a side effect we'll also strip all the other Ctrl chars
62  
63 # We'll also look for "poor-man's" highlighting
64 # *bold*
65 # _underline_
66 # /italic/
67  
68 =head1 METHODS
69  
70 =cut
71  
72 =head2 $st = String::Tagged::IRC->parse_irc( $raw, %opts )
73  
74 Parses a text string containing IRC formatting codes and returns a new
75 C<String::Tagged::IRC> instance.
76  
77 Takes the following named options:
78  
79 =over 8
80  
81 =item parse_plain_formatting => BOOL
82  
83 If true, also parse "poor-man's" plain-text formatting of B<*bold*>,
84 I</italic/> and _underline_. In this case, formatting tags are added but the
85 original text formatting is preserved.
86  
87 =back
88  
89 =cut
90  
91 sub _parse_colour_mirc
92 3 ▶{
Subroutine _parse_colour_mirc: called CC=5 SCAR=17.8
93 3 shift;
94 3 my ( $colcode ) = @_;
95  
96 # RRGGBB hex triplet
97 3 ▶ $colcode =~ m/^#([0-9a-f]{6})/i and
Branch: 1 branch
BranchTrueFalseTotal
$colcode =~ /^#([0-9a-f]{6})/i and return "Convert::Color::RGB8"->new($1) 1 2 3
98 return Convert::Color::RGB8->new( $1 );
99  
100 # RGB hex triplet
101 2 ▶ $colcode =~ m/^#([0-9a-f])([0-9a-f])([0-9a-f])/i and
Branch: 1 branch
BranchTrueFalseTotal
$colcode =~ /^#([0-9a-f])([0-9a-f])([0-9a-f])/i and return "Convert::Color::RGB8"->new("$1$1$2$2$3$3") 0 2 2
102 return Convert::Color::RGB8->new( "$1$1$2$2$3$3" );
103  
104 # mIRC index
105 2 ▶ $colcode =~ m/^(\d\d?)/ and $1 < 16 and
Branch: 1 branch
BranchTrueFalseTotal
$colcode =~ /^(\d\d?)/ and $1 < 16 and return "Convert::Color::mIRC"->new($1) 2 0 2
Condition33%1/3
$colcode =~ /^(\d\d?)/ and $1 < 16
!ll&&!rl&&r
002
MC/DC0%0/2
$colcode =~ /^(\d\d?)/ and $1 < 16
$colcode =~ /^(\d\d?)/ $1 < 16
Truth table33%1/3
A and B
A= $colcode =~ /^(\d\d?)/B= $1 < 16
ABresult
0X0
100
111
106 return Convert::Color::mIRC->new( $1 );
107  
108 0 return undef;
109 }
110  
111 my @termcolours =
112 4 map { chomp; Convert::Color::RGB8->new( $_ ) } <DATA>;
113 4 close DATA;
114  
115 sub _parse_colour_ansiterm
116 3 ▶{
Subroutine _parse_colour_ansiterm: called CC=3 SCAR=13.3
117 3 shift;
118 3 my ( $idx ) = @_;
119  
120 3 ▶ $idx >= 0 and $idx < @termcolours and
Branch: 1 branch
BranchTrueFalseTotal
$idx >= 0 and $idx < @termcolours and return $termcolours[$idx] 3 0 3
Condition33%1/3
$idx >= 0 and $idx < @termcolours
!ll&&!rl&&r
003
MC/DC0%0/2
$idx >= 0 and $idx < @termcolours
$idx >= 0 $idx < @termcolours
Truth table33%1/3
A and B
A= $idx >= 0B= $idx < @termcolours
ABresult
0X0
100
111
121 return $termcolours[$idx];
122  
123 0 return undef;
124 }
125  
126 sub parse_irc
127 7 ▶{
Subroutine parse_irc: called CC=32 SCAR=36.8
Pod documented
128 7 my $class = shift;
129 7 my ( $text, %opts ) = @_;
130  
131 7 my $self = $class->new( "" );
132  
133 7 my %format;
134  
135 7 ▶ while( length $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text) 35 7 42
136 35 ▶ if( $text =~ s/^([\x00-\x1f])// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^([\x00-\x1f])//) { } 16 19 35
137 16 my $ctrl = chr(ord($1)+0x40);
138  
139 16 ▶ if( $ctrl eq "B" ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($ctrl eq 'B') { } 2 14 16
elsif ($ctrl eq 'U' or $ctrl eq '_') { } 0 14 14
elsif ($ctrl eq 'R' or $ctrl eq ']') { } 2 12 14
elsif ($ctrl eq 'V') { } 0 12 12
elsif ($ctrl eq 'O') { } 0 12 12
elsif ($ctrl eq 'C') { } 4 8 12
elsif ($ctrl eq 'D') { } 8 0 8
140 2 ▶ $format{bold} ? delete $format{bold} : ( $format{bold} = 1 );
Branch: 1 branch
BranchTrueFalseTotal
$format{'bold'} ? : 1 1 2
141 }
142 ▶ elsif( $ctrl eq "U" or $ctrl eq "_" ) {
Condition33%1/3
$ctrl eq "U" or $ctrl eq "_"
l!l&&r!l&&!r
0014
MC/DC0%0/2
$ctrl eq "U" or $ctrl eq "_"
$ctrl eq "U" $ctrl eq "_"
Truth table33%1/3
A or B
A= $ctrl eq "U"B= $ctrl eq "_"
ABresult
1X1
011
000
143 0 ▶ $format{under} ? delete $format{under} : ( $format{under} = 1 );
Branch: 1 branch
BranchTrueFalseTotal
$format{'under'} ? : 0 0 0
144 }
145 ▶ elsif( $ctrl eq "R" or $ctrl eq "]" ) {
Condition66%2/3
$ctrl eq "R" or $ctrl eq "]"
l!l&&r!l&&!r
0212
MC/DC50%1/2
$ctrl eq "R" or $ctrl eq "]"
$ctrl eq "R" $ctrl eq "]"
Truth table66%2/3
A or B
A= $ctrl eq "R"B= $ctrl eq "]"
ABresult
1X1
011
000
146 2 ▶ $format{italic} ? delete $format{italic} : ( $format{italic} = 1 );
Branch: 1 branch
BranchTrueFalseTotal
$format{'italic'} ? : 1 1 2
147 }
148 elsif( $ctrl eq "V" ) {
149 0 ▶ $format{reverse} ? delete $format{reverse} : ( $format{reverse} = 1 );
Branch: 1 branch
BranchTrueFalseTotal
$format{'reverse'} ? : 0 0 0
150 }
151 elsif( $ctrl eq "O" ) {
152 0 undef %format;
153 }
154 elsif( $ctrl eq "C" ) {
155 4 my $colourre = qr/#[0-9a-f]{6}|#[0-9a-f]{3}|\d\d?/i;
156  
157 4 ▶ if( $text =~ s/^($colourre),($colourre)// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ s/^($colourre),($colourre)//) { } 1 3 4
elsif ($text =~ s/^($colourre)//) { } 1 2 3
158 1 $format{fg} = $self->_parse_colour_mirc( $1 );
159 1 $format{bg} = $self->_parse_colour_mirc( $2 );
160 }
161 elsif( $text =~ s/^($colourre)// ) {
162 1 $format{fg} = $self->_parse_colour_mirc( $1 );
163 }
164 else {
165 2 delete $format{fg};
166 2 delete $format{bg};
167 }
168 }
169 elsif( $ctrl eq "D" ) {
170 8 ▶ if( $text =~ s/^b// ) { # underline
Branch: 4 branches
BranchTrueFalseTotal
if ($text =~ s/^b//) { } 0 8 8
elsif ($text =~ s/^c//) { } 2 6 8
elsif ($text =~ s/^d//) { } 2 4 6
elsif ($text =~ s/^g//) { } 2 2 4
171 0 ▶ $format{under} ? delete $format{under} : ( $format{under} = 1 );
Branch: 1 branch
BranchTrueFalseTotal
$format{'under'} ? : 0 0 0
172 }
173 elsif( $text =~ s/^c// ) { # bold
174 2 ▶ $format{bold} ? delete $format{bold} : ( $format{bold} = 1 );
Branch: 1 branch
BranchTrueFalseTotal
$format{'bold'} ? : 1 1 2
175 }
176 elsif( $text =~ s/^d// ) { # revserse/italic
177 2 ▶ $format{italic} ? delete $format{italic} : ( $format{italic} = 1 );
Branch: 1 branch
BranchTrueFalseTotal
$format{'italic'} ? : 1 1 2
178 }
179 elsif( $text =~ s/^g// ) {
180 2 undef %format
181 }
182 else {
183 2 $text =~ s/^(.)(.)//;
184 2 my ( $fg, $bg ) = map { ord( $_ ) - ord('0') } ( $1, $2 );
185 2 ▶ if( $fg > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fg > 0) 2 0 2
186 2 $format{fg} = $self->_parse_colour_ansiterm( $fg );
187 }
188 2 ▶ if( $bg > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bg > 0) 1 1 2
189 1 $format{bg} = $self->_parse_colour_ansiterm( $bg );
190 }
191 }
192 }
193 }
194 else {
195 19 $text =~ s/^([^\x00-\x1f]+)//;
196 19 my $piece = $1;
197  
198 # Now scan this piece for the text-based ones
199 19 ▶ while( length $piece and $opts{parse_plain_formatting} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $piece and $opts{"parse_plain_formatting"}) 2 19 21
Condition100%3/3
length $piece and $opts{"parse_plain_formatting"}
!ll&&!rl&&r
1182
MC/DC100%2/2
length $piece and $opts{"parse_plain_formatting"}
length $piece $opts{"parse_plain_formatting"}
Truth table100%3/3
A and B
A= length $pieceB= $opts{"parse_plain_formatting"}
ABresult
0X0
100
111
200 # Look behind/ahead asserts to ensure we don't capture e.g.
201 # /usr/bin/perl by mistake
202 2 ▶ $piece =~ s/^(.*?)(?<!\w)(([\*_\/])\w+\3)(?!\w)// or
Branch: 1 branch
BranchTrueFalseTotal
$piece =~ s[^(.*?)(?<!\w)(([\*_/])\w+\3)(?!\w)][] or last 2 0 2
203 last;
204  
205 2 my ( $pre, $inner, $flag ) = ( $1, $2, $3 );
206  
207 2 ▶ $self->append_tagged( $pre, %format ) if length $pre;
Branch: 1 branch
BranchTrueFalseTotal
if length $pre 2 0 2
208  
209 2 my %innerformat = %format;
210  
211 $innerformat{
212 2 { '*' => "bold", '_' => "under", '/' => "italic" }->{$flag}
213 } = 1;
214  
215 2 $self->append_tagged( $inner, %innerformat );
216 }
217  
218 19 ▶ $self->append_tagged( $piece, %format ) if length $piece;
Branch: 1 branch
BranchTrueFalseTotal
if length $piece 18 1 19
219 }
220 }
221  
222 7 return $self;
223 }
224  
225 =head2 $raw = $st->build_irc
226  
227 Returns a plain text string containing IRC formatting codes built from the
228 given instance. When outputting a colour index, this method always outputs it
229 as a two-digit number, to avoid parsing ambiguity if the coloured text starts
230 with a digit.
231  
232 Currently this will only output F<mIRC>-style formatting, not F<irssi>-style.
233  
234 Takes the following options:
235  
236 =over 8
237  
238 =item default_fg => INT
239  
240 Default foreground colour to emit for extents that have only the C<bg> tag
241 set. This is required because F<mIRC> formatting codes cannot set just the
242 background colour without setting the foreground as well.
243  
244 =back
245  
246 =cut
247  
248 sub build_irc
249 3 ▶{
Subroutine build_irc: called CC=4 SCAR=13.9
Pod documented
250 3 my $self = shift;
251 3 my %opts = @_;
252  
253 3 ▶ my $default_fg = $opts{default_fg} // 0;
Condition50%1/2
$opts{"default_fg"} // 0
l!l
03
MC/DC0%0/1
$opts{"default_fg"} // 0
$opts{"default_fg"}
Truth table50%1/2
A
A= $opts{"default_fg"}
Aresult
11
00
254  
255 3 my $ret = "";
256 3 my %formats;
257  
258 9 ▶ $self->iter_extents_nooverlap( sub {
Subroutine __ANON__: called CC=17 SCAR=28.5
259 9 my ( $extent, %tags ) = @_;
260  
261 9 ▶ $ret .= "\cB" if !$formats{bold} != !$tags{bold};
Branch: 1 branch
BranchTrueFalseTotal
if !$formats{"bold"} != !$tags{"bold"} 2 7 9
262 9 ▶ $ret .= "\c_" if !$formats{under} != !$tags{under};
Branch: 1 branch
BranchTrueFalseTotal
if !$formats{"under"} != !$tags{"under"} 0 9 9
263 9 ▶ $ret .= "\c]" if !$formats{italic} != !$tags{italic};
Branch: 1 branch
BranchTrueFalseTotal
if !$formats{"italic"} != !$tags{"italic"} 1 8 9
264 9 ▶ $ret .= "\cV" if !$formats{reverse} != !$tags{reverse};
Branch: 1 branch
BranchTrueFalseTotal
if !$formats{"reverse"} != !$tags{"reverse"} 0 9 9
265 9 $formats{$_} = $tags{$_} for qw( bold under italic reverse );
266  
267 9 ▶ my $fg = $tags{fg} ? $tags{fg}->as_mirc->index : undef;
Branch: 1 branch
BranchTrueFalseTotal
$tags{'fg'} ? : 1 8 9
268 9 ▶ my $bg = $tags{bg} ? $tags{bg}->as_mirc->index : undef;
Branch: 1 branch
BranchTrueFalseTotal
$tags{'bg'} ? : 1 8 9
269  
270 9 ▶ if( ( $fg//'' ) ne ( $formats{fg}//'' ) or ( $bg//'' ) ne ( $formats{bg}//'' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($fg // "") ne ($formats{"fg"} // "") or ($bg // "") ne ($formats{"bg"} // "")) 3 6 9
Condition90%10/11
($fg // "") ne ($formats{"fg"} // "") or ($bg // "") ne ($formats{"bg"} // "")
l!l&&r!l&&!r
216
$fg // ""
l!l
18
$formats{"fg"} // ""
l!l
18
$bg // ""
l!l
16
$formats{"bg"} // ""
l!l
07
MC/DC100%2/2
($fg // "") ne ($formats{"fg"} // "") or ($bg // "") ne ($formats{"bg"} // "")
$fg $bg
MC/DC100%1/1
$formats{"fg"} // ""
$formats{"fg"}
MC/DC0%0/1
$formats{"bg"} // ""
$formats{"bg"}
Truth table100%3/3
A or B
A= $fgB= $bg
ABresult
1X1
011
000
Truth table100%2/2
A
A= $formats{"fg"}
Aresult
11
00
Truth table50%1/2
A
A= $formats{"bg"}
Aresult
11
00
271 3 ▶ if( defined $bg ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $bg) { } 1 2 3
elsif (defined $fg) { } 1 1 2
272 # Can't set just bg alone, so if fg isn't defined, use the default
273 1 ▶ $fg //= $default_fg;
Condition33%1/3
$fg //= $default_fg
l!l&&r!l&&!r
010
MC/DC0%0/2
$fg //= $default_fg
$fg $default_fg
Truth table33%1/3
A //= B
A= $fgB= $default_fg
ABresult
1X1
011
000
274  
275 1 $ret .= sprintf "\cC%02d,%02d", $fg, $bg;
276 }
277 elsif( defined $fg ) {
278 1 $ret .= sprintf "\cC%02d", $fg;
279 }
280 else {
281 1 $ret .= "\cC";
282 }
283 }
284  
285 9 $formats{fg} = $fg;
286 9 $formats{bg} = $bg;
287  
288 # TODO: colours
289  
290 9 $ret .= $extent->plain_substr;
291 3 });
292  
293 # Be polite and reset colours at least
294 3 ▶ $ret .= "\cC" if defined $formats{fg} or defined $formats{bg};
Branch: 1 branch
BranchTrueFalseTotal
if defined $formats{"fg"} or defined $formats{"bg"} 1 2 3
Condition66%2/3
defined $formats{"fg"} or defined $formats{"bg"}
l!l&&r!l&&!r
102
MC/DC50%1/2
defined $formats{"fg"} or defined $formats{"bg"}
defined $formats{"fg"} defined $formats{"bg"}
Truth table66%2/3
A or B
A= defined $formats{"fg"}B= defined $formats{"bg"}
ABresult
1X1
011
000
295  
296 3 return $ret;
297 }
298  
299 sub new_from_formatted
300 0 ▶{
Subroutine new_from_formatted: not called CC=1 SCAR=6.9
Pod undocumented
301 0 my $class = shift;
302 0 my ( $orig ) = @_;
303  
304 0 return $class->clone( $orig,
305 only_tags => [qw( bold under italic reverse fg bg )]
306 );
307 }
308  
309 sub as_formatted
310 0 ▶{
Subroutine as_formatted: not called CC=1 SCAR=6.9
Pod undocumented
311 0 my $self = shift;
312 0 return $self;
313 }
314  
315 =head1 TODO
316  
317 =over 4
318  
319 =item *
320  
321 Define a nicer way to do the ANSI terminal colour space of F<irssi>-style
322 formatting codes.
323  
324 =back
325  
326 =head1 AUTHOR
327  
328 Paul Evans <leonerd@leonerd.org.uk>
329  
330 =cut
331  
332 4 0x55AA;
333  
334 # Palette used for irssi->RGB8 conversion
335  
336 __DATA__
↑ Summary