#!/usr/bin/env perl 

use strict;
use warnings;
use Spreadsheet::HTML;

use Safe;
use Pod::Usage;
use Getopt::Long;

eval "use HTML::Display";
our $NO_DISPLAY = $@;

my $generator = Spreadsheet::HTML->new;
my $method    = shift if $generator->can( $ARGV[0] || '' );
$method ||= 'generate';

GetOptions (
    'param=s'   => \my %params,
    'display'   => \my $display,
    'help'      => \my $help,
    'man'       => \my $man,
);
pod2usage( -verbose => 0 ) if $help;
pod2usage( -verbose => 2 ) if $man;

my %args;
my $safe = Safe->new;
for (keys %params) {
    next if $_ eq 'preset';
    $args{$_} = $safe->reval( $params{$_} );
}
$args{$_} = $params{$_} for qw( file indent art map preset );
$args{encodes} = undef if exists $params{encodes} and $params{encodes} eq 'undef';

if ($display and not $NO_DISPLAY) {
    HTML::Display::display( $generator->$method( %args ) );
} else {
    print $generator->$method( %args ), $/;
}

__END__
=head1 NAME

mktable - generate an HTML table.

=head1 SYNOPSIS

mktable method [options]

 Options:
   --param          parameters, multiple allowed
   --display        display results via HTML::Display
   --help           list usage
   --man            print man page

Example:

  mktable portrait --param file=data.xls > out.html

  # portrait is default generation method
  mktable --param file=data.xls --param preserve=1 > out.html

  # display output to browser with HTML::Display
  mktable landscape --param data=[[a..d],[1..4],[5..8]] --display

  mktable conway --param data=[1..300] --param wrap=20 --param matrix=1 --display

  mktable sudoku --display

  # banner requires you to install and configure Text::FIGlet
  mktable banner --param text="'hello world'" --param animate=1 --display

=head1 OPTIONS

=over 8

=item B<--param>

The params. See perldoc L<Spreadsheet::HTML> for more documentation.

=item B<--display>

Display results to your browser via L<HTML::Display>.

=item B<--help>

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=back

=cut
