blib/lib/HTML/FromANSI/Tiny.pm

filter: Statementstmt 100% 108 / 108 Branchbran 87.5% 28 / 32 Conditioncond 76.4% 13 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 19 / 19 Podpod 100% 9 / 9 totaltotal 92.3% 181 / 196 CC 26 SCAR 11.3
CC
26
Cov
95%
_css_class_attr
5.0
html
5.0
import
4.0
CRAP 3.1
↑ 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 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package HTML::FromANSI::Tiny;
14 # git description: v0.107-3-gefc628d
15  
16 14 our $AUTHORITY = 'cpan:RWSTAUNER';
17 # ABSTRACT: Easily convert colored command line output to HTML
18 14 $HTML::FromANSI::Tiny::VERSION = '0.108';
19 14 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 14 my @c;
32 14 for my $r ( 0 .. 5 ){
33 84 for my $g ( 0 .. 5 ){
34 504 for my $b ( 0 .. 5 ){
35 3024 push @c, '#' . join('', map { sprintf '%02x', $_ * (255/5) } ($r, $g, $b));
36 }
37 }
38 }
39 14 @c; # return
40 },
41 # "nearly black to nearly white"
42 14 (map { '#' . join('', (sprintf '%02x', ($_ + 1) * 10) x 3) } (0 .. 23)),
43 );
44  
45 14 our @ALLCOLORS = (@COLORS, @COLORS256);
46  
47  
48 61 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
49 61 my $class = shift;
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 61 ▶ @_ == 1 ? %{ $_[0] } : @_,
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 5 56 61
57 };
58  
59 require Parse::ANSIColor::Tiny
60 61 ▶ if !$self->{ansi_parser};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ansi_parser"} 59 2 61
61 require HTML::Entities
62 61 ▶ if !$self->{html_encode};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"html_encode"} 59 2 61
63  
64 61 bless $self, $class;
65 }
66  
67  
68 82 ▶sub ansi_parser {
Subroutine ansi_parser: called CC=2 SCAR=7.0
Pod documented
69 82 my ($self) = @_;
70 82 ▶ 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
24580
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 58 my (@fields, %copy) = qw(
73 auto_reverse
74 foreground background
75 remove_escapes
76 );
77 58 @copy{ @fields } = @$self{ @fields };
78 58 Parse::ANSIColor::Tiny->new(%copy);
79 };
80 }
81  
82  
83 7217 ▶sub attr_to_class {
Subroutine attr_to_class: called CC=1 SCAR=0.0
Pod documented
84 7217 $_[1];
85 }
86  
87  
88 7696 ▶sub attr_to_style {
Subroutine attr_to_style: called CC=2 SCAR=7.1
Pod documented
89 7696 my ($self, $attr) = @_;
90 7696 ▶ return $self->_css_class_attr->{ $attr } || $self->_rgb_style($attr);
Condition66%2/3
$self->_css_class_attr->{$attr} || $self->_rgb_style($attr)
l!l&&r!l&&!r
769150
MC/DC0%0/2
$self->_css_class_attr->{$attr} || $self->_rgb_style($attr)
$self->_css_class_attr->{$attr} $self->_rgb_style($attr)
Truth table66%2/3
A || B
A= $self->_css_class_attr->{$attr}B= $self->_rgb_style($attr)
ABresult
1X1
011
000
91 }
92  
93 # The 24-bit color attribute names are those Parse::ANSIColor::Tiny produces
94 # (the same ones Term::ANSIColor uses). Note that the 256-palette names
95 # ('rgb515', 'on_rgb000') deliberately don't match this.
96 99 ▶sub _rgb_style {
Subroutine _rgb_style: called CC=3 SCAR=11.0
97 99 my ($self, $attr) = @_;
98  
99 99 ▶ my ($on, @rgb) = $attr =~ /\A(on_)?r([0-9]+)g([0-9]+)b([0-9]+)\z/
Branch: 1 branch
BranchTrueFalseTotal
() = $attr =~ /\A(on_)?r([0-9]+)g([0-9]+)b([0-9]+)\z/ or return {} 14 85 99
100 or return {};
101  
102 return {
103 14 ▶ ($on ? 'background-color' : 'color') => sprintf('#%02x%02x%02x', @rgb),
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 4 10 14
104 };
105 }
106  
107  
108 14 ▶sub css {
Subroutine css: called CC=2 SCAR=7.0
Pod documented
109 14 my ($self) = @_;
110 14 my $prefix = $self->{selector_prefix} . '.' . $self->{class_prefix};
111  
112 14 my $styles = $self->_css_class_attr;
113  
114 my @css = (
115 map {
116 14 sprintf "%s%s { %s }",
117 ${prefix},
118 $self->attr_to_class($_),
119 $self->_css_attr_string($self->attr_to_style($_))
120 }
121 sort keys %$styles
122 );
123  
124 14 ▶ return wantarray ? @css : join('', @css);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 0 14
125 }
126  
127 7710 ▶sub _css_class_attr {
Subroutine _css_class_attr: called CC=5 SCAR=16.1
128 7710 my ($self) = @_;
129 7710 ▶ 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
7687230
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
130  
131 23 my $parser = $self->ansi_parser;
132 23 my $styles = {
133 bold => { 'font-weight' => 'bold' },
134 dark => { 'opacity' => '0.7' },
135 underline => { 'text-decoration' => 'underline' },
136 concealed => { 'visibility' => 'hidden' },
137 };
138 {
139 23 my $i = 0;
140 23 foreach my $fg ( $parser->foreground_colors ){
141 6256 $styles->{$fg} = { color => $ALLCOLORS[$i++] };
142 }
143 23 $i = 0;
144 23 foreach my $bg ( $parser->background_colors ){
145 6256 $styles->{$bg} = { 'background-color' => $ALLCOLORS[$i++] };
146 }
147 }
148  
149 # return
150 +{
151 %$styles,
152 23 ▶ %{ $self->{styles} || {} },
Condition100%2/2
$self->{'styles'} || {}
l!l
716
MC/DC100%1/1
$self->{'styles'} || {}
$self->{'styles'}
Truth table100%2/2
A
A= $self->{'styles'}
Aresult
11
00
153 };
154 };
155 }
156  
157 7785 ▶sub _css_attr_string {
Subroutine _css_attr_string: called CC=1 SCAR=0.0
158 7785 my ($self, $attr) = @_;
159 # Sorted so that a style with more than one property is reproducible.
160 # It also happens to put a shorthand before the longhands it can be
161 # overridden by, since its name is a prefix of theirs.
162 7785 return join ' ', map { "$_: $attr->{$_};" } sort keys %$attr;
163 }
164  
165  
166 51 ▶sub html {
Subroutine html: called CC=5 SCAR=16.1
Pod documented
167 51 my ($self, $text) = @_;
168 51 ▶ $text = $self->ansi_parser->parse($text)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "ARRAY" 49 2 51
169 unless ref($text) eq 'ARRAY';
170  
171 51 my $tag = $self->{tag};
172  
173 51 local $_;
174 my @html = map {
175 51 my ($attr, $text) = @$_;
176 112 my $h = $self->html_encode($text);
177  
178 112 ▶ $self->{no_plain_tags} && !@$attr
Branch: 1 branch
BranchTrueFalseTotal
$self->{'no_plain_tags'} && !@$attr ? : 10 102 112
Condition100%3/3
$self->{"no_plain_tags"} and not @$attr
!ll&&!rl&&r
97510
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
179 ? $h
180 : sprintf q[<%s %s>%s</%s>],
181 $tag, $self->_tag_attributes($attr), $h, $tag;
182  
183 } @$text;
184  
185 51 ▶ return wantarray ? @html : join('', @html);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 44 51
186 }
187  
188 # Build the tag attributes for one run of text. Note that the class path
189 # never asks for the style table: building it isn't free, and it makes the
190 # parser enumerate its colors.
191 102 ▶sub _tag_attributes {
Subroutine _tag_attributes: called CC=3 SCAR=11.0
192 102 my ($self, $attr) = @_;
193  
194 102 ▶ if( $self->{inline_style} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inline_style"}) 11 91 102
195 11 my $style = $self->_style_string(map { $self->attr_to_style($_) } @$attr);
196 11 return sprintf q[style="%s"], $style;
197 }
198  
199 91 my $prefix = $self->{class_prefix};
200 my @html = sprintf q[class="%s"], join ' ',
201 91 map { $prefix . $self->attr_to_class($_) } @$attr;
202  
203 91 my $style = $self->_style_string(map { $self->_classless_style($_) } @$attr);
204  
205 91 ▶ push @html, sprintf q[style="%s"], $style
Branch: 1 branch
BranchTrueFalseTotal
if length $style 8 83 91
206 if length $style;
207  
208 91 return join ' ', @html;
209 }
210  
211 # css() can't very well emit a rule for each of sixteen million colors, so a
212 # 24-bit color has to be styled inline even when we're generating classes.
213 # Consulting $self->{styles} rather than the merged table is what keeps the
214 # class path from building the style table; a 24-bit color is never one of the
215 # built-in styles anyway.
216 # -- claude, 2026-09-08
217 95 ▶sub _classless_style {
Subroutine _classless_style: called CC=3 SCAR=11.0
218 95 my ($self, $attr) = @_;
219  
220 # A color the caller gave us a style for gets a class and a rule from css()
221 # like any other attribute, so it needs nothing inline.
222 95 ▶ return {} if $self->{styles} && $self->{styles}->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"styles"} and $self->{"styles"}{$attr} 1 94 95
Condition66%2/3
$self->{"styles"} and $self->{"styles"}{$attr}
!ll&&!rl&&r
9401
MC/DC50%1/2
$self->{"styles"} and $self->{"styles"}{$attr}
$self->{"styles"} $self->{"styles"}{$attr}
Truth table66%2/3
A and B
A= $self->{"styles"}B= $self->{"styles"}{$attr}
ABresult
0X0
100
111
223  
224 94 return $self->_rgb_style($attr);
225 }
226  
227 102 ▶sub _style_string {
Subroutine _style_string: called CC=1 SCAR=0.0
228 102 my ($self, @styles) = @_;
229 102 return join ' ', grep { length } map { $self->_css_attr_string($_) } @styles;
230 }
231  
232  
233 112 ▶sub html_encode {
Subroutine html_encode: called CC=2 SCAR=6.9
Pod documented
234 112 my ($self, $text) = @_;
235 return $self->{html_encode}->($text)
236 112 ▶ if $self->{html_encode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"html_encode"} 5 107 112
237 107 return HTML::Entities::encode_entities($text);
238 }
239  
240  
241 1 ▶sub style_tag {
Subroutine style_tag: called CC=2 SCAR=7.1
Pod documented
242 1 my ($self) = @_;
243 1 my @style = ('<style type="text/css">', $self->css, '</style>');
244 1 ▶ return wantarray ? @style : join('', @style);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
245 }
246  
247  
248 14 our @EXPORT_OK = qw( html_from_ansi );
249 2 ▶sub html_from_ansi { __PACKAGE__->new->html(@_) }
Subroutine html_from_ansi: called CC=1 SCAR=0.0
Pod documented
250  
251 1 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
252 1 my $class = shift;
253 1 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
254  
255 1 my $caller = caller;
256 14 ▶ no strict 'refs'; ## no critic (NoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
257  
258 1 foreach my $arg ( @_ ){
259 die "'$arg' is not exported by $class"
260 1 ▶ unless grep { $arg eq $_ } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$arg eq $_;} @EXPORT_OK 0 1 1
261 1 *{"${caller}::$arg"} = *{"${class}::$arg"}{CODE};
262 }
263 }
264  
265 14 1;
266  
267 __END__
↑ Summary