Subversion Repositories sysadmin_scripts

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

use strict;
use warnings;
use Data::Dumper; # only used for debugging
# define the version number
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
use version;
our $VERSION = version->declare("v3.3.0");

# see https://perldoc.perl.org/Getopt/Long.html
use Getopt::Long;
# allow -vvn (ie, --verbose --verbose --dryrun)
Getopt::Long::Configure ("bundling");

# simple display if --help is passed
sub help {
   use File::Basename;
   print basename($0) . " $VERSION\n";
   print <<END
$0 [options] [inputfile]

Reads tsv file from filename specifid (or STDIN if not), emitting
a fully formed html page (unless --nohtml specified) containing
a table. The first line of the file is assumed to be headers, the 
remainder are <td>, with style='headername' included

Options:
   --title            - A header to be created using <h1>
   --nohtml           - Do NOT create <html><body> blocks
   --css filename     - Create tag in <head> to include css file      
   --outfile filename - The output file (default to STDOUT)
   --delimiter char   - the delimiter for the file (default tab)
   --encapsulate char - an encapsulation character (default none)
   --version          - display version and exit
   --help             - This page
END
}



# handle any command line parameters that may have been passed in
my $version = 0; # just used to determine if we should display the version
my $help = 0; # also if we want help
my $file = ''; # input file
my $outfile = ''; # output file
my $encapsulate = '';
my $delimiter = "\t";
my $noHTML = 0;
my $cssFile = '';
my $title = '';
my $h2 = '';

GetOptions (
            'title|t=s'       => \$title,
            'outfile|o=s'     => \$outfile,
            'encapsulate|e=s' => \$encapsulate,
            'delimiter|d=s'   => \$delimiter,
            'css|c=s'         => \$cssFile,
            'nohtml|n'        => \$noHTML,
            'h2|2=s'            => \$h2,
            'help|h'          => \$help,
            'version|v'       => \$version,
            ) or die "Error parsing command line\n";

                  
if ( $help ) { &help() ; exit; }
if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; }

my $line = <>;
chomp $line;
$line =~ s/_/ /g; # change all underscores to spaces
$line =~ s/\b(\w)/\U$1/g; # capitalize the first letter in each word
my @headers = split( $delimiter, $line ); # put into array

my @out;
# now, make this the table header
push @out, '<th>' . join( '</th><th>', @headers ) . '</th>';
# now, we'll make it all lower case, and replace spaces with underscores, for css
$line = lc $line;
$line =~ s/ /_/g;
@headers = split( $delimiter, $line ); # put back into array

while ( $line = <> ) {
   chomp $line;
   my @fields = split( $delimiter, $line );
   my $lineOut = '';
   for ( my $i = 0; $i < @headers; $i++ ) {
      $lineOut .= "<td class='$headers[$i]'>$fields[$i]</td>";
   }
   push @out, $lineOut;
}

# add the header line at the top


print "<html>\n<head>\n" . 
      ( $cssFile ? "<link rel='stylesheet' href='$cssFile'>\n" : '' ) .
      "</head><body>\n" .
      ( $title ? "<h1>$title</h1>\n" : '' ) .
      ( $h2 ? "<h2>$h2</h2>\n" : '' )
      unless $noHTML;
print "<table>\n<tr>" . join( "</tr>\n<tr>", @out) . "</tr>\n</table>\n";
print "</body>\n</html>\n" unless $noHTML;

1;