Skip to content

Instantly share code, notes, and snippets.

@luigidifraia
Created October 6, 2018 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luigidifraia/1329cae81f3b2306d325fe17e8c64e42 to your computer and use it in GitHub Desktop.
Save luigidifraia/1329cae81f3b2306d325fe17e8c64e42 to your computer and use it in GitHub Desktop.
Convert TAPClean's text report to HTML
#!/usr/bin/env perl
use strict;
use File::Basename;
my $tag_closed = ">";
my $tag_bold = "<b>";
my $tag_bold_closed = "</b>";
my $tag_font = "<font class=";
my $tag_font_closed = "</font>";
my %foccurrence;
my %ftotoccurrence;
my $time_stamp = sub
{
# create time stamp in format yy/mm/dd hh:mm:ss [module name]
my ($year,$month,$day,$hour,$minute,$second) = (localtime)[5,4,3,2,1,0];
my $module_name = basename($0);
return (sprintf "%02d/%02d/%02d %02d:%02d:%02d [%s] ",
$year % 100, $month + 1, $day, $hour, $minute, $second, $module_name);
};
sub printmsg (@)
{
print &$time_stamp, @_;
}
sub printmsgdie (@)
{
printmsg "@_\n";
printmsg "ERROR Aborted.\n";
die;
}
sub load_file ($)
{
my ($file) = @_;
if ( ! -f $file ) { printmsgdie "ERROR Cannot find file $file"; }
my @data = ();
open IN, "<$file";
while ( my $in = <IN> )
{
chomp $in;
next if $in =~ /---------------------------------/; # skip separators
next if $in =~ /^\s*$/; # skip blank lines
push @data, $in;
}
close IN;
return @data;
}
sub process_header (@)
{
my @data = @_;
my $in;
if ( $in = shift @data )
{
print REPORT "<h2>$in</h2>\n";
}
print REPORT "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\">\n";
HEADER_LOOP: while ( ( $in = shift @data ) and ( $in !~ /FILE FREQUENCY TABLE/ ) )
{
$in =~ s/PASS/${tag_font}ok${tag_closed}PASS${tag_font_closed}/;
$in =~ s/FAIL/${tag_font}hot${tag_closed}FAIL${tag_font_closed}/;
$in =~ /(.+?)\s+:\s+(.+)/;
next if $1 =~ /^$/;
print REPORT "<tr>\n <td>$1</td>\n <td>$2</td></tr>\n";
}
print REPORT "</table>\n";
print REPORT "<br>\n<hr>\n";
print REPORT "<h2>$in</h2>\n";
return @data;
}
sub process_file_table (@)
{
my @data = @_;
my $in;
FILE_TABLE_LOOP: while ( ( $in = shift @data ) and ( $in !~ /FILE DATABASE/ ) )
{
$in =~ /(.+)\s\((\d+)\)/;
my ($file,$freq) = ($1,$2);
#print "$file:";
print REPORT "$file:";
$file =~ s/\s+//g;
$foccurrence{$file} = 1;
$ftotoccurrence{$file} = $freq;
for (my $i = 1; $i <= $freq; $i++)
{
#print " $i";
print REPORT " <a href=\"\#$file$i\">$i</a>";
}
#print "\n";
print REPORT "<br>\n";
}
print REPORT "<br>\n<hr>\n";
print REPORT "<h2>$in</h2>\n";
return @data;
}
sub process_file_db (@)
{
my @data = @_;
my ($in,$seqno);
FILE_DB_LOOP: while ( ( $in = shift @data ) and ( $in !~ /PULSE FREQUENCY TABLE/ ) )
{
#next if $in =~ /^Seq. no./;
if ( $in =~ /^Seq\. no\.: (\d+)/ )
{
$seqno = $1;
}
if ( $in =~ /^File Type/ )
{
print REPORT "<a name=\"";
# generate a valid html anchor
$in =~ /^File Type:\s+(.+)/;
my $file = $1;
$file =~ s/\s+//g; # remove spaces
print REPORT "$file$foccurrence{$file}\"><hr></a>\n";
if ( $file =~ "UNRECOGNIZED" )
{
print REPORT "<div class=\"fileunrec\">[$seqno] $in [$foccurrence{$file} of $ftotoccurrence{$file}]</div>";
}
else
{
if ( $file =~ "PAUSE" )
{
print REPORT "<div class=\"filepause\">[$seqno] $in [$foccurrence{$file} of $ftotoccurrence{$file}]</div>";
}
else
{
print REPORT "<div class=\"filetitle\">[$seqno] $in [$foccurrence{$file} of $ftotoccurrence{$file}]</div>";
}
}
$foccurrence{$file} += 1;
}
else
{
# remove unuseful info in pause blocks
$in =~ s/-> \$0000 -> \$0000//g;
# use bold for LA, EA and SZ information
$in =~ s/(\w+):\s+\$(\w+)\s+(\w+):\s+\$(\w+)\s+(\w+):\s+(\d+)/$tag_bold$1$tag_bold_closed: \$$2 $tag_bold$3$tag_bold_closed: \$$4 $tag_bold$5$tag_bold_closed: $6/g;
print REPORT "$in<br>\n"
}
}
print REPORT "<br>\n<hr>\n";
print REPORT "<h2>$in</h2>\n";
return @data;
}
sub process_pulse_freq (@)
{
my @data = @_;
print REPORT "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\">\n";
PULSE_FREQ_LOOP: while ( my $in = shift @data )
{
$in =~ /(.+)\s+\((\d+)\)/;
next if $1 =~ /^$/;
print REPORT "<tr>\n <td>$1</td>\n <td>$2</td></tr>\n";
}
print REPORT "</table>\n";
print REPORT "<br>\n<hr>\n";
}
sub init_report_file
{
open REPORT,">tcreport.html";
print REPORT <<HTMLTEMPLATE;
<html>
<header>
<title>HTML report</title>
<STYLE type='text/css'>
A{color: #7F007F; text-decoration: none}
A:HOVER{text-decoration: underline}
TD{color: #000000;}
.filetitle{color: #0000CC; font-weight: bold}
.fileunrec{color: #F4B50C; font-weight: bold}
.filepause{color: #6699AA; font-weight: bold}
.hot{color: #FF0000; font-weight: bold}
.ok{color: #45B826; font-weight: bold}
</STYLE>
</header>
<body>
HTMLTEMPLATE
}
sub finish_report_file
{
print REPORT "<center>Report converted by ".basename($0)." - (C) 2007 Luigi Di Fraia</center>\n";
print REPORT <<HTMLTEMPLATE;
</body>
</html>
HTMLTEMPLATE
close REPORT;
}
sub convert_main ()
{
my @report_loaded = load_file "tcreport.txt";
printmsg " INFO Started processing.\n";
&init_report_file;
@report_loaded = process_header @report_loaded;
@report_loaded = process_file_table @report_loaded;
@report_loaded = process_file_db @report_loaded;
process_pulse_freq @report_loaded;
&finish_report_file;
printmsg " INFO Done.\n";
}
&convert_main;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment