Subversion Repositories sysadmin_scripts

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
133 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
use Data::Dumper; # only used for debugging
6
# define the version number
7
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
8
use version;
9
our $VERSION = version->declare("v3.3.0");
10
 
11
# see https://perldoc.perl.org/Getopt/Long.html
12
use Getopt::Long;
13
# allow -vvn (ie, --verbose --verbose --dryrun)
14
Getopt::Long::Configure ("bundling");
15
 
16
# simple display if --help is passed
17
sub help {
18
   use File::Basename;
19
   print basename($0) . " $VERSION\n";
20
   print <<END
21
$0 [options] [inputfile]
22
 
23
Reads tsv file from filename specifid (or STDIN if not), emitting
24
a fully formed html page (unless --nohtml specified) containing
25
a table. The first line of the file is assumed to be headers, the 
26
remainder are <td>, with style='headername' included
27
 
28
Options:
29
   --title            - A header to be created using <h1>
30
   --nohtml           - Do NOT create <html><body> blocks
31
   --css filename     - Create tag in <head> to include css file      
32
   --outfile filename - The output file (default to STDOUT)
33
   --delimiter char   - the delimiter for the file (default tab)
34
   --encapsulate char - an encapsulation character (default none)
35
   --version          - display version and exit
36
   --help             - This page
37
END
38
}
39
 
40
 
41
 
42
# handle any command line parameters that may have been passed in
43
my $version = 0; # just used to determine if we should display the version
44
my $help = 0; # also if we want help
45
my $file = ''; # input file
46
my $outfile = ''; # output file
47
my $encapsulate = '';
48
my $delimiter = "\t";
49
my $noHTML = 0;
50
my $cssFile = '';
51
my $title = '';
52
my $h2 = '';
53
 
54
GetOptions (
55
            'title|t=s'       => \$title,
56
            'outfile|o=s'     => \$outfile,
57
            'encapsulate|e=s' => \$encapsulate,
58
            'delimiter|d=s'   => \$delimiter,
59
            'css|c=s'         => \$cssFile,
60
            'nohtml|n'        => \$noHTML,
61
            'h2|2=s'            => \$h2,
62
            'help|h'          => \$help,
63
            'version|v'       => \$version,
64
            ) or die "Error parsing command line\n";
65
 
66
 
67
if ( $help ) { &help() ; exit; }
68
if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; }
69
 
70
my $line = <>;
71
chomp $line;
72
$line =~ s/_/ /g; # change all underscores to spaces
73
$line =~ s/\b(\w)/\U$1/g; # capitalize the first letter in each word
74
my @headers = split( $delimiter, $line ); # put into array
75
 
76
my @out;
77
# now, make this the table header
78
push @out, '<th>' . join( '</th><th>', @headers ) . '</th>';
79
# now, we'll make it all lower case, and replace spaces with underscores, for css
80
$line = lc $line;
81
$line =~ s/ /_/g;
82
@headers = split( $delimiter, $line ); # put back into array
83
 
84
while ( $line = <> ) {
85
   chomp $line;
86
   my @fields = split( $delimiter, $line );
87
   my $lineOut = '';
88
   for ( my $i = 0; $i < @headers; $i++ ) {
89
      $lineOut .= "<td class='$headers[$i]'>$fields[$i]</td>";
90
   }
91
   push @out, $lineOut;
92
}
93
 
94
# add the header line at the top
95
 
96
 
97
print "<html>\n<head>\n" . 
98
      ( $cssFile ? "<link rel='stylesheet' href='$cssFile'>\n" : '' ) .
99
      "</head><body>\n" .
100
      ( $title ? "<h1>$title</h1>\n" : '' ) .
101
      ( $h2 ? "<h2>$h2</h2>\n" : '' )
102
      unless $noHTML;
103
print "<table>\n<tr>" . join( "</tr>\n<tr>", @out) . "</tr>\n</table>\n";
104
print "</body>\n</html>\n" unless $noHTML;
105
 
106
1;