blib/lib/HTML/FromANSI/Tiny.pm

filter: Statementstmt 100% 89 / 89 Branchbran 85.7% 24 / 28 Conditioncond 77.7% 7 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 8 / 8 totaltotal 93.5% 144 / 154 CC 21 SCAR 13.3
CC
21
Cov
95%
html
7.0
_css_class_attr
5.0
import
4.0
CRAP 3.8
↑ Summary
1 # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2 #
3 # This file is part of HTML-FromANSI-Tiny
4 #
5 # This software is copyright (c) 2011 by Randy Stauner.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package HTML::FromANSI::Tiny;
14 # git description: v0.106-2-g1454a0d
15  
16 12 our $AUTHORITY = 'cpan:RWSTAUNER';
17 # ABSTRACT: Easily convert colored command line output to HTML
18 12 $HTML::FromANSI::Tiny::VERSION = '0.107';
19 12 our @COLORS = map { "#$_" }
20 qw(
21 000 f33 2c2 bb0 55c d3d 0cc bbb
22 555 f66 6d6 dd6 99f f6f 6dd fff
23 );
24  
25 # 256 palette.
26 our @COLORS256 = (
27 # First 16 are the same.
28 @COLORS,
29 # rgbXYZ
30 do {
31 12 my @c;
32 12 for my $r ( 0 .. 5 ){
33 72 for my $g ( 0 .. 5 ){
34 432 for my $b ( 0 .. 5 ){
35 2592 push @c, '#' . join('', map { sprintf '%02x', $_ * (255/5) } ($r, $g, $b));
36 }
37 }
38 }
39 12 @c; # return
40 },
41 # "nearly black to nearly white"
42 12 (map { '#' . join('', (sprintf '%02x', ($_ + 1) * 10) x 3) } (0 .. 23)),
43 );
44  
45 12 our @ALLCOLORS = (@COLORS, @COLORS256);
46  
47  
48 sub new {
49 41 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
50 my $self = {
51 class_prefix => '',
52 selector_prefix => '',
53 tag => 'span',
54 # It seems particularly unlikely that somebody would want these in their HTML.
55 remove_escapes => 1,
56 41 ▶ @_ == 1 ? %{ $_[0] } : @_,
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 5 36 41
57 };
58  
59 require Parse::ANSIColor::Tiny
60 41 ▶ if !$self->{ansi_parser};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ansi_parser"} 39 2 41
61 require HTML::Entities
62 41 ▶ if !$self->{html_encode};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"html_encode"} 39 2 41
63  
64 41 bless $self, $class;
65 }
66  
67  
68 sub ansi_parser {
69 58 ▶ my ($self) = @_;
Subroutine ansi_parser: called CC=2 SCAR=7.0
Pod documented
70 58 ▶ return $self->{ansi_parser} ||= do {
Condition66%2/3
$self->{"ansi_parser"} ||= do { () = ("auto_reverse", "foreground", "background", "remove_escapes"); @copy{@fields} = @$self{@fields}; "Parse::ANSIColor::Tiny"->new(%copy) }
l!l&&r!l&&!r
19390
MC/DC0%0/2
$self->{"ansi_parser"} ||= do { () = ("auto_reverse", "foreground", "background", "remove_escapes"); @copy{@fields} = @$self{@fields}; "Parse::ANSIColor::Tiny"->new(%copy) }
$self->{"ansi_parser"} do { () = ("auto_reverse", "foreground", "background", "remove_escapes"); @copy{@fields} = @$self{@fields}; "Parse::ANSIColor::Tiny"->new(%copy) }
Truth table66%2/3
A ||= B
A= $self->{"ansi_parser"}B= do { () = ("auto_reverse", "foreground", "background", "remove_escapes"); @copy{@fields} = @$self{@fields}; "Parse::ANSIColor::Tiny"->new(%copy) }
ABresult
1X1
011
000
71 # hash slice
72 39 my (@fields, %copy) = qw(
73 auto_reverse
74 foreground background
75 remove_escapes
76 );
77 39 @copy{ @fields } = @$self{ @fields };
78 39 Parse::ANSIColor::Tiny->new(%copy);
79 };
80 }
81  
82  
83 sub attr_to_class {
84 5010 ▶ $_[1];
Subroutine attr_to_class: called CC=1 SCAR=0.0
Pod documented
85 }
86  
87  
88 sub css {
89 10 ▶ my ($self) = @_;
Subroutine css: called CC=2 SCAR=7.0
Pod documented
90 10 my $prefix = $self->{selector_prefix} . '.' . $self->{class_prefix};
91  
92 10 my $styles = $self->_css_class_attr;
93  
94 my @css = (
95 map {
96 10 sprintf "%s%s { %s }",
97 ${prefix},
98 $self->attr_to_class($_),
99 5480 $self->_css_attr_string($styles->{$_})
100 }
101 sort keys %$styles
102 );
103  
104 10 ▶ return wantarray ? @css : join('', @css);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 0 10
105 }
106  
107 sub _css_class_attr {
108 12 ▶ my ($self) = @_;
Subroutine _css_class_attr: called CC=5 SCAR=16.1
109 12 ▶ return $self->{_all_styles} ||= do {
Condition66%2/3
$self->{"_all_styles"} ||= do { my $parser = $self->ansi_parser; my $styles = {"bold", {"font-weight", "bold"}, "dark", {"opacity", "0.7"}, "underline", {"text-decoration", "underline"}, "concealed", {"visibility", "hidden"}}; { my $i = 0; foreach my $fg ($parser->foreground_colors) { $styles->{$fg} = {"color", $ALLCOLORS[$i++]}; } ; $i = 0; foreach my $bg ($parser->background_colors) { $styles->{$bg} = {"background-color", $ALLCOLORS[$i++]}; } ; } ; +{%$styles, %{$self->{"styles"} or {};}} }
l!l&&r!l&&!r
1110
MC/DC0%0/2
$self->{"_all_styles"} ||= do { my $parser = $self->ansi_parser; my $styles = {"bold", {"font-weight", "bold"}, "dark", {"opacity", "0.7"}, "underline", {"text-decoration", "underline"}, "concealed", {"visibility", "hidden"}}; { my $i = 0; foreach my $fg ($parser->foreground_colors) { $styles->{$fg} = {"color", $ALLCOLORS[$i++]}; } ; $i = 0; foreach my $bg ($parser->background_colors) { $styles->{$bg} = {"background-color", $ALLCOLORS[$i++]}; } ; } ; +{%$styles, %{$self->{"styles"} or {};}} }
$self->{"_all_styles"} do { my $parser = $self->ansi_parser; my $styles = {"bold", {"font-weight", "bold"}, "dark", {"opacity", "0.7"}, "underline", {"text-decoration", "underline"}, "concealed", {"visibility", "hidden"}}; { my $i = 0; foreach my $fg ($parser->foreground_colors) { $styles->{$fg} = {"color", $ALLCOLORS[$i++]}; } ; $i = 0; foreach my $bg ($parser->background_colors) { $styles->{$bg} = {"background-color", $ALLCOLORS[$i++]}; } ; } ; +{%$styles, %{$self->{"styles"} or {};}} }
Truth table66%2/3
A ||= B
A= $self->{"_all_styles"}B= do { my $parser = $self->ansi_parser; my $styles = {"bold", {"font-weight", "bold"}, "dark", {"opacity", "0.7"}, "underline", {"text-decoration", "underline"}, "concealed", {"visibility", "hidden"}}; { my $i = 0; foreach my $fg ($parser->foreground_colors) { $styles->{$fg} = {"color", $ALLCOLORS[$i++]}; } ; $i = 0; foreach my $bg ($parser->background_colors) { $styles->{$bg} = {"background-color", $ALLCOLORS[$i++]}; } ; } ; +{%$styles, %{$self->{"styles"} or {};}} }
ABresult
1X1
011
000
110  
111 11 my $parser = $self->ansi_parser;
112 11 my $styles = {
113 bold => { 'font-weight' => 'bold' },
114 dark => { 'opacity' => '0.7' },
115 underline => { 'text-decoration' => 'underline' },
116 concealed => { 'visibility' => 'hidden' },
117 };
118 {
119 11 my $i = 0;
120 11 foreach my $fg ( $parser->foreground_colors ){
121 2992 $styles->{$fg} = { color => $ALLCOLORS[$i++] };
122 }
123 11 $i = 0;
124 11 foreach my $bg ( $parser->background_colors ){
125 2992 $styles->{$bg} = { 'background-color' => $ALLCOLORS[$i++] };
126 }
127 }
128  
129 # return
130 +{
131 %$styles,
132 11 ▶ %{ $self->{styles} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$self->{"styles"} or {} 8 3 11
133 };
134 };
135 }
136  
137 sub _css_attr_string {
138 5492 ▶ my ($self, $attr) = @_;
Subroutine _css_attr_string: called CC=1 SCAR=0.0
139 5492 return join ' ', map { "$_: $attr->{$_};" } keys %$attr;
140 }
141  
142  
143 sub html {
144 38 ▶ my ($self, $text) = @_;
Subroutine html: called CC=7 SCAR=19.5
Pod documented
145 38 ▶ $text = $self->ansi_parser->parse($text)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "ARRAY" 37 1 38
146 unless ref($text) eq 'ARRAY';
147  
148 38 my $tag = $self->{tag};
149 38 my $prefix = $self->{class_prefix};
150 # Preload if needed; Don't load if not.
151 38 ▶ my $styles = $self->{inline_style} ? $self->_css_class_attr : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'inline_style'} ? : 2 36 38
152  
153 38 local $_;
154 my @html = map {
155 38 my ($attr, $text) = @$_;
156 97 my $h = $self->html_encode($text);
157  
158 $self->{no_plain_tags} && !@$attr
159 ? $h
160 97 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{'no_plain_tags'} && !@$attr ? : 9 88 97
Condition100%3/3
$self->{"no_plain_tags"} and not @$attr
!ll&&!rl&&r
8449
MC/DC100%2/2
$self->{"no_plain_tags"} and not @$attr
$self->{"no_plain_tags"} not @$attr
Truth table100%3/3
A and not(B)
A= $self->{"no_plain_tags"}B= not @$attr
ABresult
0X0
100
111
161 sprintf q[<%s %s="%s">%s</%s>], $tag,
162 ($self->{inline_style}
163 12 ? (style => join ' ', map { $self->_css_attr_string($styles->{$_}) } @$attr)
164 88 ▶ : (class => join ' ', map { $prefix . $self->attr_to_class($_) } @$attr)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'inline_style'} ? : 8 80 88
165 ), $h, $tag;
166 }
167  
168 } @$text;
169  
170 38 ▶ return wantarray ? @html : join('', @html);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 31 38
171 }
172  
173  
174 sub html_encode {
175 97 ▶ my ($self, $text) = @_;
Subroutine html_encode: called CC=2 SCAR=6.9
Pod documented
176 return $self->{html_encode}->($text)
177 97 ▶ if $self->{html_encode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"html_encode"} 5 92 97
178 92 return HTML::Entities::encode_entities($text);
179 }
180  
181  
182 sub style_tag {
183 1 ▶ my ($self) = @_;
Subroutine style_tag: called CC=2 SCAR=7.1
Pod documented
184 1 my @style = ('<style type="text/css">', $self->css, '</style>');
185 1 ▶ return wantarray ? @style : join('', @style);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
186 }
187  
188  
189 12 our @EXPORT_OK = qw( html_from_ansi );
190 2 ▶sub html_from_ansi { __PACKAGE__->new->html(@_) }
Subroutine html_from_ansi: called CC=1 SCAR=0.0
Pod documented
191  
192 sub import {
193 1 ▶ my $class = shift;
Subroutine import: called CC=4 SCAR=13.9
194 1 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
195  
196 1 my $caller = caller;
197 12 ▶ no strict 'refs'; ## no critic (NoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
198  
199 1 foreach my $arg ( @_ ){
200 die "'$arg' is not exported by $class"
201 1 ▶ unless grep { $arg eq $_ } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$arg eq $_;} @EXPORT_OK 0 1 1
202 1 *{"${caller}::$arg"} = *{"${class}::$arg"}{CODE};
203 }
204 }
205  
206 12 1;
207  
208 __END__
↑ Summary