#!/usr/bin/perl -w

use strict ;
use warnings ;
use Carp ;

=head1 NAME 

 $> ansi_to_html - transforms a text file with embeded color tags to HTML or ANSI

=head1 USAGE

 $> script file_name


Use B<AE<lt>ansi_color on_ansi_colorE<gt>> tags where you want the color to change, IE:

 display_help() A<bright_green>unless @ARGV ;A<bright_white>
 
 use A<bright_red>Data::TreeDumperA<bright_white> ;
 use Text::Colorizer  A<yellow>qw() ;

displays as (you'll see the colorized text in the html documentation not in the man page):

=begin html

<pre style ="font-family: monospace; background-color: #000 ;">
<span style = ' color:#fff; '>display_help() </span><span style = ' color:#0f0; '>unless @ARGV ;</span><span style = ' color:#fff; '>

use </span><span style = ' color:#f00; '>Data::TreeDumper</span><span style = ' color:#fff; '> ;
use Text::Colorizer  </span><span style = ' color:#880; '>qw() ;
</span>
</pre>

=end html

The start color is B<bright_white> rendered on a black background.

=head1 OPTIONS

 format|f                          ANSI or HTML

=head1 EXIT STATUS


=head1 AUTHOR

	Nadim ibn hamouda el Khemir
	CPAN ID: NKH
	mailto: nkh@cpan.org

=cut

#------------------------------------------------------------------------------------------------------------------------

use Getopt::Long ;
use English qw( -no_match_vars ) ;
use File::Slurp ;

our $VERSION = '0.01' ;

#------------------------------------------------------------------------------------------------------------------------

display_help() unless 
	GetOptions
		(
		'format|f=s' => \ my $format,
		) ;
	
display_help() unless @ARGV ;

my $tag_regex = qr/(A<[^[]+?>)/ ;

my @chunks =  split $tag_regex , read_file(shift @ARGV) ;
unshift @chunks, 'bright_white' unless $chunks[0] =~ $tag_regex  ;
@chunks = map {s/^A<(.*)>$/$1/; $_} @chunks ;

use Data::TreeDumper ;
@chunks % 2 and die DumpTree \@chunks, 'text and color chunks' ;

use Text::Colorizer  qw() ;
my $c= Text::Colorizer->new(FORMAT => defined $format ? $format : 'ANSI') ;
print $c->color(@chunks) ;

#------------------------------------------------------------------------------------------------------------------------

sub display_help
{

#~ =head2 display_help()

#~ I<Arguments> - None

#~ I<Returns> - Nothing

#~ I<Exceptions> - exits with status code B<1>

#~ =cut

my ($this_script) = ($PROGRAM_NAME =~m/(.*)/sxm ) ;

print {*STDERR} `perldoc $this_script`  or croak 'Error: Can\'t display help!' ; ## no critic (InputOutput::ProhibitBacktickOperators)
exit(1) ;
}
