blib/lib/HTML/FromANSI/Tiny.pm

filter: Statementstmt 100% 88 / 88 Branchbran 85.7% 24 / 28 Conditioncond 77.7% 7 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 93.3% 141 / 151 SCAR 30.5
CC
21
Cov
95%
html
7.0
_css_class_attr
5.0
import
4.0
CRAP 21.0
↑ 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 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package HTML::FromANSI::Tiny;
14 # git description: v0.104-2-g306f93b
15  
16 11 our $AUTHORITY = 'cpan:RWSTAUNER';
17 # ABSTRACT: Easily convert colored command line output to HTML
18 11 $HTML::FromANSI::Tiny::VERSION = '0.105';
19 11 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 11 my @c;
32 11 for my $r ( 0 .. 5 ){
33 66 for my $g ( 0 .. 5 ){
34 396 for my $b ( 0 .. 5 ){
35 2376 push @c, '#' . join('', map { sprintf '%02x', $_ * (255/5) } ($r, $g, $b));
36 }
37 }
38 }
39 11 @c; # return
40 },
41 # "nearly black to nearly white"
42 11 (map { '#' . join('', (sprintf '%02x', ($_ + 1) * 10) x 3) } (0 .. 23)),
43 );
44  
45 11 our @ALLCOLORS = (@COLORS, @COLORS256);
46  
47  
48 sub new {
49 38 ▶ 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 38 ▶ @_ == 1 ? %{ $_[0] } : @_,
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 5 33 38
57 };
58  
59 require Parse::ANSIColor::Tiny
60 38 ▶ if !$self->{ansi_parser};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ansi_parser"} 36 2 38
61 require HTML::Entities
62 38 ▶ if !$self->{html_encode};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"html_encode"} 36 2 38
63  
64 38 bless $self, $class;
65 }
66  
67  
68 sub ansi_parser {
69 55 ▶ my ($self) = @_;
Subroutine ansi_parser: called CC=2 SCAR=7.0
Pod documented
70 55 ▶ 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
19360
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 36 my (@fields, %copy) = qw(
73 auto_reverse
74 foreground background
75 remove_escapes
76 );
77 36 @copy{ @fields } = @$self{ @fields };
78 36 Parse::ANSIColor::Tiny->new(%copy);
79 };
80 }
81  
82  
83 sub css {
84 9 ▶ my ($self) = @_;
Subroutine css: called CC=2 SCAR=7.0
Pod documented
85 9 my $prefix = $self->{selector_prefix} . '.' . $self->{class_prefix};
86  
87 9 my $styles = $self->_css_class_attr;
88  
89 my @css = (
90 9 map { "${prefix}$_ { " . $self->_css_attr_string($styles->{$_}) . " }" }
91 sort keys %$styles
92 );
93  
94 9 ▶ return wantarray ? @css : join('', @css);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 0 9
95 }
96  
97 sub _css_class_attr {
98 11 ▶ my ($self) = @_;
Subroutine _css_class_attr: called CC=5 SCAR=16.1
99 11 ▶ 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
1100
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
100  
101 10 my $parser = $self->ansi_parser;
102 10 my $styles = {
103 bold => { 'font-weight' => 'bold' },
104 dark => { 'opacity' => '0.7' },
105 underline => { 'text-decoration' => 'underline' },
106 concealed => { 'visibility' => 'hidden' },
107 };
108 {
109 10 my $i = 0;
110 10 foreach my $fg ( $parser->foreground_colors ){
111 2720 $styles->{$fg} = { color => $ALLCOLORS[$i++] };
112 }
113 10 $i = 0;
114 10 foreach my $bg ( $parser->background_colors ){
115 2720 $styles->{$bg} = { 'background-color' => $ALLCOLORS[$i++] };
116 }
117 }
118  
119 # return
120 +{
121 %$styles,
122 10 ▶ %{ $self->{styles} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$self->{"styles"} or {} 7 3 10
123 };
124 };
125 }
126  
127 sub _css_attr_string {
128 4944 ▶ my ($self, $attr) = @_;
Subroutine _css_attr_string: called CC=1 SCAR=0.0
129 4944 return join ' ', map { "$_: $attr->{$_};" } keys %$attr;
130 }
131  
132  
133 sub html {
134 36 ▶ my ($self, $text) = @_;
Subroutine html: called CC=7 SCAR=19.5
Pod documented
135 36 ▶ $text = $self->ansi_parser->parse($text)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "ARRAY" 35 1 36
136 unless ref($text) eq 'ARRAY';
137  
138 36 my $tag = $self->{tag};
139 36 my $prefix = $self->{class_prefix};
140 # Preload if needed; Don't load if not.
141 36 ▶ my $styles = $self->{inline_style} ? $self->_css_class_attr : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'inline_style'} ? : 2 34 36
142  
143 36 local $_;
144 my @html = map {
145 36 my ($attr, $text) = @$_;
146 91 my $h = $self->html_encode($text);
147  
148 $self->{no_plain_tags} && !@$attr
149 ? $h
150 91 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{'no_plain_tags'} && !@$attr ? : 9 82 91
Condition100%3/3
$self->{"no_plain_tags"} and not @$attr
!ll&&!rl&&r
7849
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
151 sprintf q[<%s %s="%s">%s</%s>], $tag,
152 ($self->{inline_style}
153 12 ? (style => join ' ', map { $self->_css_attr_string($styles->{$_}) } @$attr)
154 82 ▶ : (class => join ' ', map { $prefix . $_ } @$attr)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'inline_style'} ? : 8 74 82
155 ), $h, $tag;
156 }
157  
158 } @$text;
159  
160 36 ▶ return wantarray ? @html : join('', @html);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 29 36
161 }
162  
163  
164 sub html_encode {
165 91 ▶ my ($self, $text) = @_;
Subroutine html_encode: called CC=2 SCAR=6.9
Pod documented
166 return $self->{html_encode}->($text)
167 91 ▶ if $self->{html_encode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"html_encode"} 5 86 91
168 86 return HTML::Entities::encode_entities($text);
169 }
170  
171  
172 sub style_tag {
173 1 ▶ my ($self) = @_;
Subroutine style_tag: called CC=2 SCAR=7.1
Pod documented
174 1 my @style = ('<style type="text/css">', $self->css, '</style>');
175 1 ▶ return wantarray ? @style : join('', @style);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
176 }
177  
178  
179 11 our @EXPORT_OK = qw( html_from_ansi );
180 2 ▶sub html_from_ansi { __PACKAGE__->new->html(@_) }
Subroutine html_from_ansi: called CC=1 SCAR=0.0
Pod documented
181  
182 sub import {
183 1 ▶ my $class = shift;
Subroutine import: called CC=4 SCAR=13.9
184 1 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
185  
186 1 my $caller = caller;
187 11 ▶ no strict 'refs'; ## no critic (NoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
188  
189 1 foreach my $arg ( @_ ){
190 die "'$arg' is not exported by $class"
191 1 ▶ unless grep { $arg eq $_ } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$arg eq $_;} @EXPORT_OK 0 1 1
192 1 *{"${caller}::$arg"} = *{"${class}::$arg"}{CODE};
193 }
194 }
195  
196 11 1;
197  
198 __END__
↑ Summary