The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

#!/usr/bin/perl
$VERSION = '0.02';
=head1 NAME
ansi2html - Convert ANSI sequence to HTML
=head1 SYNOPSIS
B<ansi2html> S<[ B<-p> B<-f> B<-c> ]> S<[ I<infile> ]> S<[ I<outfile> ]>
=head1 DESCRIPTION
This script takes one input file containing text with ANSI sequences,
and outputs the converted HTML code into another file.
If I<outfile> is omitted, it defaults to STDOUT. If I<infile> is omitted,
it defaults to STDIN.
Note that this script will automatically set the C<fill_cols> option
to 1, filling empty columns with space characters.
If the B<-p> option is specified, it will print HTML header and footers.
If the B<-f> option is specified columns will not be filled.
If the B<-c> option is specified the cursor will be shown.
=cut
use strict;
my %args; getopts('pcf', \%args);
my $h = HTML::FromANSI->new(
fill_cols => !$args{f},
show_cursor => $args{c},
);
my ($infile, $outfile) = @ARGV;
open IN, "<", $infile or die "cannot read $infile: $!" if $infile;
open OUT, ">", $outfile or die "cannot write $outfile: $!" if $outfile;
select OUT if $outfile;
$h->add_text($infile ? <IN> : <STDIN>);
if ($args{p}) {
my $title = 'ansi2html' . ($infile ? " - $infile" : '');
print "<HTML><HEAD><TITLE>$title</TITLE></HEAD><BODY>"
. $h->html
. "</BODY></HTML>";
}
else {
print $h->html;
}
exit;
=head1 SEE ALSO
L<HTML::FromANSI>
=head1 AUTHORS
Audrey Tang E<lt>audreyt@audreyt.orgE<gt>
=head1 COPYRIGHT
Copyright 2001, 2002 by Audrey Tang E<lt>audreyt@audreyt.orgE<gt>.
Copyright 2007 by Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut