blib/lib/HTML/FromANSI.pm

filter: Statementstmt 93.3% 98 / 105 Branchbran 63.1% 24 / 38 Conditioncond 54.2% 19 / 35 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 100% 19 / 19 Podpod 55.5% 5 / 9 totaltotal 75.2% 170 / 226 CC 35 SCAR 23.8
CC
35
Cov
79%
parse_vt
26.6
new
6.5
diff_attr
6.0
CRAP 10.8
↑ Summary
1 package HTML::FromANSI;
2 3 $HTML::FromANSI::VERSION = '2.03';
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use vars qw/@EXPORT @EXPORT_OK @Color %Options/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Term::VT102::Boundless;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 HTML::FromANSI - Mark up ANSI sequences as HTML
15  
16 =head1 SYNOPSIS
17  
18 use HTML::FromANSI (); # avoid exports if using OO
19 use Term::ANSIColor;
20  
21 my $h = HTML::FromANSI->new(
22 fill_cols => 1,
23 );
24  
25  
26 $h->add_text(color('bold blue'), "This text is bold blue.");
27  
28 print $h->html;
29  
30  
31 # you can append text in the new api:
32  
33 $h->add_text(color('bold blue'), " still blue.");
34  
35 print $h->html
36  
37  
38  
39 # The old API still works:
40  
41 $HTML::FromANSI::Options{fill_cols} = 1; # fill all 80 cols
42 print ansi2html(color('bold blue'), "This text is bold blue.");
43  
44 =head1 DESCRIPTION
45  
46 This small module converts ANSI text sequences to corresponding HTML
47 codes, using stylesheets to control color and blinking properties.
48  
49 It exports C<ansi2html()> by default, which takes an array, joins it
50 it into a single scalar, and returns its HTML rendering.
51  
52 From version 2.00 an object oriented api which is safer for multiple uses (no
53 more manipulation of shared C<%Options>) is available. It is reccomended that
54 you no longer import any functions by doing:
55  
56 use HTML::FromANSI ();
57  
58 and use the new documented API instead of the functional one.
59  
60 The underlying ANSI code processing is done by L<Term::VT102>, a DEC VT102
61 terminal emulator. To generate ANSI codes for color changes, cursor movements
62 etc, take a look at L<Term::ANSIScreen> and L<Term::ANSIColor>.
63  
64 =head1 METHODS
65  
66 =over 4
67  
68 =item new
69  
70 The constructor. See L</OPTIONS> for the options it takes.
71  
72 =item add_text @text
73  
74 Adds text input to the terminal emulator.
75  
76 =item html
77  
78 Renders the screen as computed by C<terminal_object> into HTML.
79  
80 =item ansi_to_html @text
81  
82 A convenience method.
83  
84 Calls C<add_text> and then C<html>.
85  
86 =item terminal_object
87  
88 The underlying terminal emulator object.
89  
90 =back
91  
92 =head1 OPTIONS
93  
94 These are parameters you can pass to C<new>.
95  
96 =over 4
97  
98 =item linewrap
99  
100 A boolean value to specify whether to wrap lines that exceeds
101 width specified by C<col>, or simply truncate them. Defaults to C<1>.
102  
103 Only takes effect if you override C<terminal_class> or C<terminal_object> with
104 a L<Term::VT102> (instead of L<Term::VT102::Boundless>).
105  
106 =item lf_to_crlf
107  
108 A boolean value to specify whether to translate all incoming
109 \n into C<\r\n> or not; you generally wants to use this if your
110 data is from a file using unix line endings. The default is C<0>
111 on MSWin32 and MacOS, and C<1> on other platforms.
112  
113 =item fill_cols
114  
115 A boolean value to specify whether to fill empty columns with
116 space; use this if you want to maintain a I<screen-like> appearance
117 in the resulting HTML, so that each row will be aligned properly.
118 Defaults to C<0>.
119  
120 =item html_entity
121  
122 A boolean value to specify whether to escape all high-bit characters
123 to HTML entities or not; defaults to C<0>, which means only C<E<lt>>,
124 C<E<gt>>, C<"> and C<&> will be escaped. (Handy when processing most
125 ANSI art entries.)
126  
127 =item cols
128  
129 A number specifying the width of the virtual terminal; defaults to 80.
130  
131 When C<Term::VT102::Boundless> is in use (the default) this specifies the
132 minimum number of rows to draw.
133  
134 When using C<Term::VT102> (by overriding C<terminal_class> or
135 C<terminal_object>) then the number of columns is fixed.
136  
137 =item rows
138  
139 When C<Term::VT102::Boundless> is in use (the default) this specifies the
140 minimum number of rows to draw.
141  
142 When L<Term::VT102> is in use (by overriding C<terminal_class> or
143 L<terminal_object>) then it sets the height of the virtual terminal; rows that
144 exceeds this number will be truncated.
145  
146 =item font_face
147  
148 A string used as the C<face> attribute to the C<font> tag enclosing the
149 HTML text; defaults to C<fixedsys, lucida console, terminal, vga, monospace>.
150  
151 If this option and the C<style> option are both set to empty strings, the
152 C<font> tag will be omitted.
153  
154 =item style
155  
156 A string used as the C<style> attribute to the C<font> tag enclosing the
157 HTML text; defaults to <line-height: 1; letter-spacing: 0; font-size: 12pt>.
158  
159 If this option and the C<font_face> option are both set to empty strings, the
160 C<font> tag will be omitted.
161  
162 =item tt
163  
164 A boolean value specifying whether the HTML text should be enclosed in a
165 C<tt> tag or not. Defaults to C<1>.
166  
167 =item show_cursor
168  
169 A boolean value to control whether to highlight the character under
170 the cursor position, by reversing its background and foregroud color.
171 Defaults to C<0>.
172  
173 If the cursor is on it's own line and C<show_cursor> is set, then that row will
174 be omitted.
175  
176 =item terminal_class
177  
178 The class to instantiate C<terminal_object> with. Defaults to
179 L<Term::VT102::Boundless>.
180  
181 =item terminal_object
182  
183 Any L<Term::VT102> compatible object should work here.
184  
185 If you override it most values like C<cols>, C<rows>, C<terminal_class> etc
186 will be ignored.
187  
188 =cut
189  
190 3 @EXPORT = '&ansi2html';
191 3 @EXPORT_OK = qw|@Color %Options|;
192  
193 3 @Color = (qw(
194 black darkred darkgreen),'#8b8b00',qw(darkblue darkmagenta darkcyan gray
195 dimgray red green yellow blue magenta cyan white
196 ));
197  
198 3 %Options = (
199 linewrap => 1, # wrap long lines
200 lf_to_crlf => ( # translate \n to \r\n on Unix
201 $^O !~ /^(?:MSWin32|MacOS)$/
202 ),
203 fill_cols => 0, # fill all (80) columns with space
204 html_entity => 0, # escape all HTML entities
205 cols => 80, # column width
206 rows => undef, # let ansi2html figure it out
207 font_face => 'fixedsys, lucida console, terminal, vga, monospace',
208 style => 'line-height: 1; letter-spacing: 0; font-size: 12pt',
209 tt => 1,
210 show_cursor => 0,
211  
212 terminal_class => 'Term::VT102::Boundless',
213 );
214  
215 3 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
216 3 my $class = shift;
217 3 ▶ while (my ($k, $v) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 1 3 4
218 1 $Options{$k} = $v;
219 }
220 3 $class->export_to_level(1);
221 }
222  
223 3 ▶sub new {
Subroutine new: called CC=4 SCAR=18.7
Pod documented
224 3 my ( $class, @args ) = @_;
225  
226 3 ▶ if ( @args == 1 && reftype($args[0]) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1 and builtin::reftype($args[0]) eq 'HASH') { } 0 3 3
elsif (@args % 2 == 0) { } 3 0 3
Condition33%1/3
@args == 1 and builtin::reftype($args[0]) eq "HASH"
!ll&&!rl&&r
300
MC/DC0%0/2
@args == 1 and builtin::reftype($args[0]) eq "HASH"
@args == 1 builtin::reftype($args[0]) eq "HASH"
Truth table33%1/3
A and B
A= @args == 1B= builtin::reftype($args[0]) eq "HASH"
ABresult
0X0
100
111
227 0 return bless { %Options, %{ $args[0] } }, $class;
228 } elsif ( @args % 2 == 0 ) {
229 3 return bless { %Options, @args }, $class;
230 } else {
231 0 croak "Constructor arguments must be an even sized list or a hash ref";
232 }
233 }
234  
235 117 ▶sub _obj_args {
Subroutine _obj_args: called CC=3 SCAR=11.0
236 117 ▶ if ( blessed($_[0]) and $_[0]->isa(__PACKAGE__) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)) { } 116 1 117
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
10116
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
237 116 return @_;
238 } else {
239 1 return ( __PACKAGE__->new(), @_ );
240 }
241 }
242  
243 1 ▶sub ansi2html {
Subroutine ansi2html: called CC=1 SCAR=0.0
Pod undocumented
244 1 my ( $self, @args ) = _obj_args(@_);
245 1 $self->ansi_to_html(@args);
246 }
247  
248 12 ▶sub terminal_object {
Subroutine terminal_object: called CC=2 SCAR=7.1
Pod documented
249 12 my ( $self, @args ) = @_;
250 12 ▶ $self->{terminal_object} ||= $self->create_terminal_object(@args);
Condition66%2/3
$self->{"terminal_object"} ||= $self->create_terminal_object(@args)
l!l&&r!l&&!r
930
MC/DC0%0/2
$self->{"terminal_object"} ||= $self->create_terminal_object(@args)
$self->{"terminal_object"} $self->create_terminal_object(@args)
Truth table66%2/3
A ||= B
A= $self->{"terminal_object"}B= $self->create_terminal_object(@args)
ABresult
1X1
011
000
251 }
252  
253 3 ▶sub create_terminal_object {
Subroutine create_terminal_object: called CC=3 SCAR=11.7
Pod undocumented
254 3 my ( $self, %args ) = @_;
255  
256 3 my $class = $self->{terminal_class};
257  
258 3 ▶ if ( $class ne 'Term::VT102::Boundless' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne "Term::VT102::Boundless") 0 3 3
259 0 ( my $file = "${class}.pm" ) =~ s{::}{/}g;
260 0 require $file;
261 }
262  
263 my $vt = $class->new(
264 cols => $self->{cols},
265 3 ▶ ( defined($self->{rows}) ? ( rows => $self->{rows} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'rows'} ? : 0 3 3
266 );
267  
268 3 $vt->option_set(LINEWRAP => $self->{linewrap});
269 3 $vt->option_set(LFTOCRLF => $self->{lf_to_crlf});
270  
271 3 $vt->_code_DECTCEM( $self->{show_cursor} );
272  
273 3 return $vt;
274 }
275  
276 4 ▶sub add_text {
Subroutine add_text: called CC=2 SCAR=6.9
Pod documented
277 4 my ( $self, @lines ) = @_;
278 4 $self->terminal_object->process($_) for @lines;
279 }
280  
281 2 ▶sub ansi_to_html {
Subroutine ansi_to_html: called CC=1 SCAR=0.0
Pod documented
282 2 my ( $self, @lines ) = @_;
283  
284 2 $self->add_text(@lines);
285  
286 2 return $self->html;
287 }
288  
289 4 ▶sub html {
Subroutine html: called CC=4 SCAR=15.0
Pod documented
290 4 my ( $self, @args ) = @_;
291  
292 4 my $result = $self->parse_vt($self->terminal_object);
293  
294 4 ▶ if (length $self->{font_face} or length $self->{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"font_face"} or length $self->{"style"}) 4 0 4
Condition33%1/3
length $self->{"font_face"} or length $self->{"style"}
l!l&&r!l&&!r
400
MC/DC0%0/2
length $self->{"font_face"} or length $self->{"style"}
length $self->{"font_face"} length $self->{"style"}
Truth table33%1/3
A or B
A= length $self->{"font_face"}B= length $self->{"style"}
ABresult
1X1
011
000
295 4 $result = "<font face='$self->{font_face}' style='$self->{style}'>".
296 $result."</font>";
297 }
298  
299 4 ▶ $result = "<tt>$result</tt>" if $self->{tt};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tt"} 4 0 4
300  
301 4 return $result;
302 }
303  
304 4 ▶sub parse_vt {
Subroutine parse_vt: called CC=17 SCAR=32.8
Pod undocumented
305 4 my ( $self, $vt ) = _obj_args(@_);
306  
307 4 my (%prev, %this); # attributes
308 4 my $out;
309  
310 4 my ($x, $y) = ($vt->x, $vt->y);
311  
312 4 my $total_rows = $vt->rows;
313  
314 4 foreach my $row_num (1 .. $total_rows) {
315 4 ▶ local $SIG{__WARN__} = sub {}; # abandon all hope, ye who enter here
Subroutine __ANON__: called CC=1 SCAR=0.0
316  
317 4 my $row = $vt->row_text($row_num);
318 4 my $att = $vt->row_attr($row_num);
319  
320 4 ▶ if ( $row_num == $total_rows and $total_rows != ($self->{rows}||0) # this is the last row
Branch: 1 branch
BranchTrueFalseTotal
if ($row_num == $total_rows and $total_rows != ($self->{"rows"} || 0) and $row =~ /^[\s\x00]*$/s and not $self->{"show_cursor"}) 0 4 4
Condition36%4/11
$row_num == $total_rows and $total_rows != ($self->{"rows"} || 0) and $row =~ /^[\s\x00]*$/s and not $self->{"show_cursor"}
!ll&&!rl&&r
400
$row_num == $total_rows and $total_rows != ($self->{"rows"} || 0) and $row =~ /^[\s\x00]*$/s
!ll&&!rl&&r
040
$row_num == $total_rows and $total_rows != ($self->{"rows"} || 0)
!ll&&!rl&&r
004
$self->{'rows'} || 0
l!l
04
MC/DC0%0/4
$row_num == $total_rows and $total_rows != ($self->{"rows"} || 0) and $row =~ /^[\s\x00]*$/s and not $self->{"show_cursor"}
$row_num == $total_rows $total_rows != ($self->{"rows"} || 0) $row =~ /^[\s\x00]*$/s not $self->{"show_cursor"}
MC/DC0%0/1
$self->{'rows'} || 0
$self->{'rows'}
Truth table20%1/5
A and B and C and not(D)
A= $row_num == $total_rowsB= $total_rows != ($self->{"rows"} || 0)C= $row =~ /^[\s\x00]*$/sD= not $self->{"show_cursor"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table50%1/2
A
A= $self->{'rows'}
Aresult
11
00
321 and $row =~ /^[\s\x00]*$/s # and it's completely empty
322 and !$self->{show_cursor} # and we're not showing a cursor
323 0 ) { last } # skip it
324  
325 4 foreach my $col_num (0 .. length($row)) {
326 112 my $text = substr($row, $col_num,, 1);
327  
328 112 @this{qw|fg bg bo fo st ul bl rv|} = $vt->attr_unpack(
329 substr($att, $col_num * 2, 2)
330 );
331  
332 112 ▶ if ($y == $row_num and $x == $col_num + 1 and $self->{show_cursor}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y == $row_num and $x == $col_num + 1 and $self->{'show_cursor'}) { } 4 108 112
elsif ($text eq "\000") { } 0 108 108
Condition66%4/6
$y == $row_num and $x == $col_num + 1 and $self->{"show_cursor"}
!ll&&!rl&&r
10804
$y == $row_num and $x == $col_num + 1
!ll&&!rl&&r
01084
MC/DC33%1/3
$y == $row_num and $x == $col_num + 1 and $self->{"show_cursor"}
$y == $row_num $x == $col_num + 1 $self->{"show_cursor"}
Truth table50%2/4
A and B and C
A= $y == $row_numB= $x == $col_num + 1C= $self->{"show_cursor"}
ABCresult
0XX0
10X0
1100
1111
333 # this block is the cursor
334 4 @this{qw|fg bg bo bl|} = (@this{qw|bg fg bl bo|});
335 4 ▶ $text = ' ' if $text eq '\000';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "\\000" 0 4 4
336 }
337 elsif ($text eq "\000") {
338 0 ▶ next unless $self->{fill_cols};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fill_cols"} 0 0 0
339 }
340  
341 $out .= $self->diff_attr(\%prev, \%this) . (
342 ($text eq ' ' or $text eq "\000") ? '&nbsp;':
343 112 ▶ $self->{html_entity} ? encode_entities($text)
Branch: 2 branches
BranchTrueFalseTotal
$text eq ' ' || $text eq "\000" ? : 19 93 112
$self->{'html_entity'} ? : 0 93 93
Condition66%2/3
$text eq " " or $text eq "\0"
l!l&&r!l&&!r
19093
MC/DC50%1/2
$text eq " " or $text eq "\0"
$text eq " " $text eq "\0"
Truth table66%2/3
A or B
A= $text eq " "B= $text eq "\0"
ABresult
1X1
011
000
344 : encode_entities($text, '<>"&')
345 );
346  
347 112 %prev = %this;
348 }
349  
350 4 $out .= "<br>";
351 }
352  
353 4 return "$out</span>";
354 }
355  
356 112 ▶sub diff_attr {
Subroutine diff_attr: called CC=6 SCAR=17.9
Pod undocumented
357 112 my ($self, $prev, $this) = _obj_args(@_);
358 112 my $out = '';
359  
360 # skip if the attributes remain unchanged
361 112 return if %{$prev} and not scalar (grep {
362 864 ($_->[0] ne $_->[1])
363 } map {
364 864 [ $prev->{$_}, $this->{$_} ]
365 112 ▶ } keys %{$this}
Branch: 1 branch
BranchTrueFalseTotal
if %{$prev;} and not scalar grep({$_->[0] ne $_->[1];} map({[$prev->{$_}, $this->{$_}];} keys %{$this;})) 104 8 112
Condition100%3/3
%{$prev;} and not scalar grep({$_->[0] ne $_->[1];} map({[$prev->{$_}, $this->{$_}];} keys %{$this;}))
!ll&&!rl&&r
44104
MC/DC100%2/2
%{$prev;} and not scalar grep({$_->[0] ne $_->[1];} map({[$prev->{$_}, $this->{$_}];} keys %{$this;}))
%{$prev;} not scalar grep({$_->[0] ne $_->[1];} map({[$prev->{$_}, $this->{$_}];} keys %{$this;}))
Truth table100%3/3
A and not(B)
A= %{$prev;}B= not scalar grep({$_->[0] ne $_->[1];} map({[$prev->{$_}, $this->{$_}];} keys %{$this;}))
ABresult
0X0
100
111
366 );
367  
368 # bold, faint, standout, underline, blink and reverse
369 my ($fg, $bg, $bo, $fo, $st, $ul, $bl, $rv)
370 8 = @{$this}{qw|fg bg bo fo st ul bl rv|};
371  
372 8 ▶ ($fg, $bg) = ($bg, $fg) if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 8 8
373  
374 8 ▶ $out .= "</span>" if %{$prev};
Branch: 1 branch
BranchTrueFalseTotal
if %{$prev;} 4 4 8
375 8 $out .= "<span style='";
376 8 $out .= "color: $Color[$fg + $bo * 8]; ";
377 8 $out .= "background: $Color[$bg + $bl * 8]; ";
378 8 ▶ $out .= "text-decoration: underline; " if $ul;
Branch: 1 branch
BranchTrueFalseTotal
if $ul 0 8 8
379 8 $out .= "'>";
380  
381 8 return $out;
382 }
383  
384 3 1;
385  
386 __END__
↑ Summary