       1 #!/usr/bin/perl -w
       2 #
       3 # $RCSfile: TextFilesToHTML.pl,v $
       4 # $Date: 2015/02/28 20:46:21 $
       5 # $Revision: 1.41 $
       6 #
       7 # Author: Manish Sud <msud@san.rr.com>
       8 #
       9 # Copyright (C) 2015 Manish Sud. All rights reserved.
      10 #
      11 # This file is part of MayaChemTools.
      12 #
      13 # MayaChemTools is free software; you can redistribute it and/or modify it under
      14 # the terms of the GNU Lesser General Public License as published by the Free
      15 # Software Foundation; either version 3 of the License, or (at your option) any
      16 # later version.
      17 #
      18 # MayaChemTools is distributed in the hope that it will be useful, but without
      19 # any warranty; without even the implied warranty of merchantability of fitness
      20 # for a particular purpose.  See the GNU Lesser General Public License for more
      21 # details.
      22 #
      23 # You should have received a copy of the GNU Lesser General Public License
      24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
      25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
      26 # Boston, MA, 02111-1307, USA.
      27 #
      28
      29 use strict;
      30 use FindBin; use lib "$FindBin::Bin/../lib";
      31 use Getopt::Long;
      32 use File::Basename;
      33 use Text::ParseWords;
      34 use Benchmark;
      35 use FileUtil;
      36 use TextUtil;
      37 use HTMLUtil;
      38
      39 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
      40
      41 # Autoflush STDOUT
      42 $| = 1;
      43
      44 # Starting message...
      45 $ScriptName = basename($0);
      46 print "\n$ScriptName: Starting...\n\n";
      47 $StartTime = new Benchmark;
      48
      49 # Get the options and setup script...
      50 SetupScriptUsage();
      51 if ($Options{help} || @ARGV < 1) {
      52   die GetUsageFromPod("$FindBin::Bin/$ScriptName");
      53 }
      54
      55 my(@TextFilesList);
      56 @TextFilesList = ExpandFileNames(\@ARGV, "csv tsv");
      57
      58 print "Processing options...\n";
      59 my(%OptionsInfo);
      60 ProcessOptions();
      61
      62 print "Checking input text file(s)...\n";
      63 my(%TextFilesInfo);
      64 RetrieveTextFilesInfo();
      65 SetupCoulmnsTablesAndMiscInfo();
      66
      67 # Generate output files...
      68 my($FileIndex);
      69 if (@TextFilesList > 1) {
      70   print "\nProcessing text files...\n";
      71 }
      72 for $FileIndex (0 .. $#TextFilesList) {
      73   if ($TextFilesInfo{FileOkay}[$FileIndex]) {
      74     print "\nProcessing file $TextFilesList[$FileIndex]...\n";
      75     GenerateHTMLTable($FileIndex);
      76   }
      77 }
      78 print "\n$ScriptName:Done...\n\n";
      79
      80 $EndTime = new Benchmark;
      81 $TotalTime = timediff ($EndTime, $StartTime);
      82 print "Total time: ", timestr($TotalTime), "\n";
      83
      84 ###############################################################################
      85
      86 # Generate HTML table(s)...
      87 sub GenerateHTMLTable {
      88   my($Index) = @_;
      89
      90   if ($TextFilesInfo{MultipleHTMLTables}[$Index]) {
      91     GenerateMultipleHTMLTable($Index);
      92   }
      93   else {
      94     GenerateOneHTMLTable($Index);
      95   }
      96 }
      97
      98 # Generate one table...
      99 sub GenerateOneHTMLTable {
     100   my($Index) = @_;
     101   my($TextFile, $TopHTMLDir, $HTMLFile, $Line, $StartRowNum, $EndRowNum, $CSSFile, $CSSFilePath, $CSSRef);
     102
     103   $HTMLFile = $TextFilesInfo{HTMLRoot}[$Index] . ".html";
     104   $TextFile = $TextFilesList[$Index];
     105
     106   # Setup data directories...
     107   ($TopHTMLDir) = SetupDataDirs($Index);
     108
     109   # Setup stylesheet file...
     110   $CSSRef = "";
     111   if ($Options{stylesheet} =~ /^new$/i) {
     112     $CSSFile = $TextFilesInfo{HTMLRoot}[$Index] . ".css"; $CSSRef = ".\/" . "$CSSFile";
     113     $CSSFilePath = "$TopHTMLDir" . "\/" . $CSSFile;
     114     GenerateStyleSheetFile($CSSFilePath);
     115   }
     116   elsif ($Options{stylesheet} =~ /^old$/i) {
     117     $CSSRef = $Options{stylesheetname};
     118   }
     119   # Set HTML file location...
     120   $HTMLFile = "$TopHTMLDir" . "\/" . $HTMLFile;
     121
     122   print "Generating HTML file $HTMLFile...\n";
     123   open HTMLFILE, ">$HTMLFile" or die "Error: Can't open $HTMLFile: $! \n";
     124   open TEXTFILE, "$TextFile" or die "Error: Can't open $TextFile: $! \n";
     125
     126   # Write out HTML page header...
     127   print HTMLFILE SetupHTMLPageHeader($TextFilesInfo{HTMLTitle}[$Index], $CSSRef);
     128   if ($OptionsInfo{TitleDisplay}) {
     129     print HTMLFILE SetupHTMLPageTitle($TextFilesInfo{HTMLTitle}[$Index]);
     130   }
     131   else {
     132     print HTMLFILE SetupHTMLEmptyLines(1);
     133   }
     134
     135   # Start the table...
     136   print HTMLFILE SetupHTMLAlignmentBegin("center");
     137   print HTMLFILE SetupHTMLTableHeader($OptionsInfo{TableBorder}, $OptionsInfo{TableCellPadding}, $OptionsInfo{TableCellSpacing});
     138
     139   WriteColLabels($Index, \*TEXTFILE, \*HTMLFILE);
     140
     141   # Skip the labels and write out all the other rows...
     142   $Line = <TEXTFILE>;
     143   $StartRowNum = 1;
     144   $EndRowNum = $TextFilesInfo{LineCount}[$Index];
     145   WriteRowValues($Index, $StartRowNum, $EndRowNum, \*TEXTFILE, \*HTMLFILE);
     146
     147   # Finish up the table...
     148   print HTMLFILE SetupHTMLTableEnd();
     149   print HTMLFILE SetupHTMLAlignmentEnd("center");
     150
     151   # Write out HTML page end...
     152   print HTMLFILE SetupHTMLPageEnd($OptionsInfo{Footer});
     153
     154   close HTMLFILE;
     155   close TEXTFILE;
     156 }
     157
     158 # Generate multiple tables...
     159 sub GenerateMultipleHTMLTable {
     160   my($Index) = @_;
     161   my($TopHTMLDir, $SubHTMLDir, $TextFile, $HTMLFile, $TableNum, $TableCount, $TableIndex, $TableStartLineNum, $TableEndLineNum, $Line, $InSubHTMLDir, $PrintMsg, $CSSFile, $CSSFilePath, $CSSRef, $NewStyleSheet);
     162
     163   # Open text file and skip over label line...
     164   $TextFile = $TextFilesList[$Index];
     165   open TEXTFILE, "$TextFile" or die "Error: Can't open $TextFile: $! \n";
     166   $Line = <TEXTFILE>;
     167
     168   # Set up data directories to hold various html files...
     169   ($TopHTMLDir, $SubHTMLDir) = SetupDataDirs($Index);
     170
     171   # Create stylesheet file...
     172   $CSSRef = "";
     173   $NewStyleSheet = 0;
     174   if ($Options{stylesheet} =~ /^new$/i) {
     175     $NewStyleSheet = 1;
     176     $CSSFile = $TextFilesInfo{HTMLRoot}[$Index] . ".css";
     177     $CSSFilePath = "$TopHTMLDir" . "\/" . $CSSFile;
     178     GenerateStyleSheetFile($CSSFilePath);
     179   }
     180   elsif ($Options{stylesheet} =~ /^old$/i) {
     181     $CSSRef = $Options{stylesheetname};
     182   }
     183
     184   $PrintMsg = 1;
     185   # Generate HTML files for all the tables...
     186   $TableCount = $TextFilesInfo{TableCount}[$Index];
     187   for $TableNum (1 .. $TableCount) {
     188     $TableIndex = $TableNum - 1;
     189     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$TableIndex];
     190     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$TableIndex];
     191     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$TableIndex];
     192
     193     # Setup file name...
     194     if ($TableNum == 1) {
     195       $HTMLFile = "$TopHTMLDir" . "\/" . $HTMLFile;
     196       print "Generating HTML file $HTMLFile...\n";
     197     }
     198     else {
     199       $HTMLFile = "$SubHTMLDir" . "\/" . $HTMLFile;
     200       if ($PrintMsg) {
     201         $PrintMsg = 0;
     202         if ($TableCount == 2) {
     203           print "Generating HTML file $HTMLFile...\n";
     204         }
     205         else {
     206           print "Generating ", ($TableCount - 1), " other HTML files: $SubHTMLDir\/$TextFilesInfo{HTMLRoot}[$Index]\*.html...\n";
     207         }
     208       }
     209     }
     210     # Setup stylesheet reference...
     211     if ($NewStyleSheet) {
     212       $CSSRef = ($TableNum == 1) ? ".\/" : "..\/";
     213       $CSSRef .= $CSSFile;
     214     }
     215
     216     open HTMLFILE, ">$HTMLFile" or die "Error: Can't open $HTMLFile: $! \n";
     217     # Write out HTML page header...
     218     print HTMLFILE SetupHTMLPageHeader($TextFilesInfo{HTMLTitle}[$Index], $CSSRef);
     219
     220     # Set up the navigation links for this table...
     221     if ($OptionsInfo{NavLinksAtTop}) {
     222       WriteNavigationLinks($Index, $TableNum, \*HTMLFILE);
     223     }
     224     # Setup page title...
     225     if ($OptionsInfo{TitleDisplay}) {
     226       print HTMLFILE SetupHTMLPageTitle($TextFilesInfo{HTMLTitle}[$Index]);
     227     }
     228     else {
     229       print HTMLFILE SetupHTMLEmptyLines(1);
     230     }
     231
     232     # Start the table...
     233     print HTMLFILE SetupHTMLAlignmentBegin("center");
     234     print HTMLFILE SetupHTMLTableHeader($OptionsInfo{TableBorder}, $OptionsInfo{TableCellPadding}, $OptionsInfo{TableCellSpacing});
     235
     236     WriteColLabels($Index, \*TEXTFILE, \*HTMLFILE);
     237
     238     # Write out appropriate row data for this table...
     239     WriteRowValues($Index, $TableStartLineNum, $TableEndLineNum, \*TEXTFILE, \*HTMLFILE);
     240
     241     # Finish up the table...
     242     print HTMLFILE SetupHTMLTableEnd();
     243     print HTMLFILE SetupHTMLAlignmentEnd("center");
     244
     245     # Set up the navigation links for this table...
     246     if ($OptionsInfo{NavLinksAtBottom}) {
     247       print HTMLFILE SetupHTMLEmptyLines(1);
     248       WriteNavigationLinks($Index, $TableNum, \*HTMLFILE);
     249     }
     250
     251     # Write out HTML page end...
     252     print HTMLFILE SetupHTMLPageEnd($OptionsInfo{Footer});
     253     close HTMLFILE;
     254   }
     255   close TEXTFILE;
     256
     257 }
     258
     259 # Create stylesheet file...
     260 sub GenerateStyleSheetFile {
     261   my($CSSFile) = @_;
     262     print "Generating stylesheet file $CSSFile...\n";
     263     open CSSFILE, ">$CSSFile" or die "Error: Can't open $CSSFile: $! \n";
     264     print CSSFILE SetupHTMLStyleSheetTags();
     265     close CSSFILE;
     266 }
     267
     268 # Write out table header using column labels...
     269 sub WriteColLabels {
     270   my($Index, $TextFileRef, $HTMLFileRef) = @_;
     271   my(@ColLabels, $Label);
     272
     273   print $HTMLFileRef $TextFilesInfo{TableRowHeaderTags};
     274
     275   @ColLabels = @{$TextFilesInfo{ColLabels}[$Index]};
     276   for $Label (@ColLabels) {
     277     print $HTMLFileRef SetupHTMLTableRowHeaderValue($Label);
     278   }
     279   print $HTMLFileRef $TextFilesInfo{RowEndTags};
     280 }
     281
     282 #Write out the rows value...
     283 sub WriteRowValues {
     284   my($Index, $StartRowNum, $EndRowNum, $TextFileRef, $HTMLFileRef) = @_;
     285   my($ColNum, $BackgroundColor, $FontColor, $LineCount, $Line, @RowValues, $Value, $InDelim, $LastColNum);
     286
     287   $InDelim = $TextFilesInfo{InDelim}[$Index];
     288   $LastColNum = @{$TextFilesInfo{ColLabels}[$Index]} - 1;
     289
     290   for $LineCount ($StartRowNum .. $EndRowNum) {
     291     $Line = GetTextLine($TextFileRef);
     292
     293     if ($OptionsInfo{ShadeRowsStatus}) {
     294       print $HTMLFileRef ($LineCount % 2) ? $TextFilesInfo{BgFilledOddRowHeaderTags} : $TextFilesInfo{BgFilledEvenRowHeaderTags};
     295     }
     296     else {
     297       print $HTMLFileRef $TextFilesInfo{RowHeaderTags};
     298     }
     299     @RowValues = quotewords($InDelim, 0, $Line);
     300     for $ColNum (0 .. $LastColNum) {
     301       $Value = ($ColNum <= $#RowValues) ? $RowValues[$ColNum] : "";
     302       $BackgroundColor = ""; $FontColor = "";
     303       if ($OptionsInfo{HighlightStatus}) {
     304         if (exists($TextFilesInfo{HightlightColNumMap}[$Index]{$ColNum})) {
     305           ($BackgroundColor, $FontColor) = GetValueHighlightColors($Index, $ColNum, $Value);
     306         }
     307       }
     308       print $HTMLFileRef SetupHTMLTableRowDataValue($Value, $BackgroundColor, $FontColor);
     309     }
     310     print $HTMLFileRef $TextFilesInfo{RowEndTags};
     311   }
     312 }
     313
     314 # Setup navigation link information for each table.
     315 #
     316 # All table sets besides first and last have these links: FirstTable, Previous, Current-1,Current,Current+1,  Next, and LastTable
     317 # First set: Current, Next, and LastTable
     318 # Last set: FirstTable, Previous and Current.
     319 #
     320 sub WriteNavigationLinks {
     321   my($Index, $CurTableNum, $HTMLFileRef) = @_;
     322   my($TableNum, $StartTableNum, $EndTableNum, $TableIndex, $BorderWidth, $CellPadding, $CellSpacing,$HTMLFile, $HTMLRefFile, $RelativeFileDir, $HTMLRefValue, $FirstTableNum, $FirstTableIndex, $LastTableNum, $LastTableIndex, $TableStartLineNum, $TableEndLineNum, $LastLineNum, $BGColor, $LinksOffSet);
     323
     324   $LinksOffSet = 10;
     325
     326   $FirstTableNum = 1; $FirstTableIndex = $FirstTableNum - 1;
     327   $LastTableNum = $TextFilesInfo{TableCount}[$Index]; $LastTableIndex = $LastTableNum - 1;
     328   $LastLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$LastTableIndex];
     329
     330   # Figure out which links to display for a particular table...
     331   $StartTableNum = $CurTableNum - $LinksOffSet + 1;
     332   $StartTableNum = ($StartTableNum < $FirstTableNum) ? $FirstTableNum : $StartTableNum;
     333   if ($CurTableNum < $LinksOffSet) {
     334     $EndTableNum = $LinksOffSet;
     335   }
     336   else {
     337     $EndTableNum = $CurTableNum + $LinksOffSet - 1;
     338   }
     339   $EndTableNum = ($EndTableNum > $LastTableNum) ? $LastTableNum : $EndTableNum;
     340
     341   my($InactiveLinkNumColor, $InactiveLinkFontBold) = ("#8e2323", "1");
     342   my($LinkTextColor, $LinkBGColor, $LinkFontBold) = ("", "", "1");
     343
     344   # Start link table...
     345   $BorderWidth = 0; $CellPadding = 2; $CellSpacing = 2;
     346   print $HTMLFileRef SetupHTMLAlignmentBegin("center");
     347   print $HTMLFileRef SetupHTMLDivBegin("tablenav");
     348   print $HTMLFileRef  SetupHTMLTableHeader($BorderWidth, $CellPadding, $CellSpacing);
     349   print $HTMLFileRef $TextFilesInfo{RowHeaderTags};
     350
     351   if ($OptionsInfo{NavLinksTableInfo} && $OptionsInfo{NavLinksLineInfo}) {
     352     print $HTMLFileRef SetupHTMLTableRowDataValue("Showing table $CurTableNum of $LastTableNum");
     353     print $HTMLFileRef SetupHTMLTableRowDataValue("&nbsp");
     354     print $HTMLFileRef SetupHTMLTableRowDataValue("&nbsp");
     355   }
     356
     357   print $HTMLFileRef SetupHTMLTableRowDataValue("Tables: ");
     358   # Setup a link to first table...
     359   if ($StartTableNum != $FirstTableNum) {
     360     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$FirstTableIndex];
     361     $HTMLRefFile = GetRelativeFileDir($CurTableNum, $FirstTableNum, $FirstTableNum) . $HTMLFile;
     362     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$FirstTableIndex];
     363     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$FirstTableIndex];
     364     $HTMLRefValue = SetupHTMLHRef("First", $HTMLRefFile, "First Table Containing Lines $TableStartLineNum To $TableEndLineNum");
     365     print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue, $LinkBGColor, $LinkTextColor, $LinkFontBold);
     366   }
     367
     368   # Setup link to previous table...
     369   if ($CurTableNum != $FirstTableNum) {
     370     my($PreviousTableNum, $PreviousTableIndex);
     371     $PreviousTableNum = $CurTableNum - 1; $PreviousTableIndex = $PreviousTableNum - 1;
     372     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$PreviousTableIndex];
     373     $HTMLRefFile = GetRelativeFileDir($CurTableNum, $PreviousTableNum, $FirstTableNum) . $HTMLFile;
     374     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$PreviousTableIndex];
     375     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$PreviousTableIndex];
     376     $HTMLRefValue = SetupHTMLHRef("Previous", $HTMLRefFile, "Previous Table Containing Lines $TableStartLineNum To $TableEndLineNum");
     377     print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue, $LinkBGColor, $LinkTextColor, $LinkFontBold);
     378   }
     379
     380   for $TableNum ($StartTableNum .. $EndTableNum) {
     381     $TableIndex = $TableNum - 1;
     382     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$TableIndex];
     383     if ($TableNum == $CurTableNum) {
     384       print $HTMLFileRef SetupHTMLTableRowDataValue($TableNum, $LinkBGColor, $InactiveLinkNumColor, $InactiveLinkFontBold);
     385     }
     386     else {
     387       # Setup the link...
     388       my($RefTitle);
     389       $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$TableIndex];
     390       $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$TableIndex];
     391       $RefTitle = AddNumberSuffix($TableNum) . " Table Containing Lines $TableStartLineNum To $TableEndLineNum";
     392       $HTMLRefFile = GetRelativeFileDir($CurTableNum, $TableNum, $FirstTableNum) . $HTMLFile;
     393       $HTMLRefValue = SetupHTMLHRef($TableNum, $HTMLRefFile, $RefTitle);
     394       print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue);
     395     }
     396   }
     397
     398   # Setup link to next table...
     399   if ($CurTableNum != $LastTableNum) {
     400     my($NextTableNum, $NextTableIndex);
     401     $NextTableNum = $CurTableNum + 1; $NextTableIndex = $NextTableNum - 1;
     402     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$NextTableIndex];
     403     $HTMLRefFile = GetRelativeFileDir($CurTableNum, $NextTableNum, $FirstTableNum) . $HTMLFile;
     404     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$NextTableIndex];
     405     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$NextTableIndex];
     406     $HTMLRefValue = SetupHTMLHRef("Next", $HTMLRefFile, "Next Table Containing Lines $TableStartLineNum To $TableEndLineNum");
     407     print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue, $LinkBGColor, $LinkTextColor, $LinkFontBold);
     408   }
     409
     410   # Setup link to last table...
     411   if ($EndTableNum != $LastTableNum) {
     412     $HTMLFile = ${$TextFilesInfo{TableHTMLFiles}[$Index]}[$LastTableIndex];
     413     $HTMLRefFile = GetRelativeFileDir($CurTableNum, $LastTableNum, $FirstTableNum) . $HTMLFile;
     414     $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$LastTableIndex];
     415     $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$LastTableIndex];
     416     $HTMLRefValue = SetupHTMLHRef("Last", $HTMLRefFile, "Last Table Containing Lines $TableStartLineNum To $TableEndLineNum");
     417     print $HTMLFileRef SetupHTMLTableRowDataValue($HTMLRefValue, $LinkBGColor, $LinkTextColor, $LinkFontBold);
     418   }
     419   # Setup current table info text....
     420   print $HTMLFileRef SetupHTMLTableRowDataValue("&nbsp");
     421   print $HTMLFileRef SetupHTMLTableRowDataValue("&nbsp");
     422   $TableStartLineNum = ${$TextFilesInfo{TableStartLineNum}[$Index]}[$CurTableNum - 1];
     423   $TableEndLineNum = ${$TextFilesInfo{TableEndLineNum}[$Index]}[$CurTableNum - 1];
     424   if ($OptionsInfo{NavLinksLineInfo}) {
     425     print $HTMLFileRef SetupHTMLTableRowDataValue("Showing lines $TableStartLineNum to $TableEndLineNum of $LastLineNum");
     426   }
     427   else {
     428     print $HTMLFileRef SetupHTMLTableRowDataValue("Showing table $CurTableNum of $LastTableNum");
     429   }
     430
     431   print $HTMLFileRef $TextFilesInfo{RowEndTags};
     432   # End link table...
     433   print $HTMLFileRef SetupHTMLTableEnd();
     434   print $HTMLFileRef SetupHTMLDivEnd();
     435   print $HTMLFileRef SetupHTMLAlignmentEnd("center");
     436 }
     437
     438 # Generate relative directory path...
     439 sub GetRelativeFileDir {
     440   my($FromTableNum, $ToTableNum, $FirstTableNum) = @_;
     441   my($RelativeFileDir) = "";
     442
     443   if ($FromTableNum == $FirstTableNum) {
     444     $RelativeFileDir = ($ToTableNum == $FirstTableNum) ? ".\/" : ".\/html\/";
     445   }
     446   else {
     447     $RelativeFileDir = ($ToTableNum == $FirstTableNum) ? "..\/" : ".\/";
     448   }
     449   return $RelativeFileDir;
     450 }
     451
     452 # Based on hightlight stype, return appropriate colors for background or text...
     453 sub GetValueHighlightColors {
     454   my($FileIndex, $ColNum, $Value) = @_;
     455   my($DataType, $Criterion, $CriterionValue, $BgColor, $FontColor, $ValueOk, $Nothing);
     456
     457   $BgColor = ""; $FontColor = "";
     458   $DataType = ${$TextFilesInfo{HightlightDataMap}[$FileIndex]{$ColNum}}[0];
     459   $Criterion = ${$TextFilesInfo{HightlightDataMap}[$FileIndex]{$ColNum}}[1];
     460   $CriterionValue = ${$TextFilesInfo{HightlightDataMap}[$FileIndex]{$ColNum}}[2];
     461
     462   $ValueOk = 0;
     463   if ($DataType =~ /^numeric$/i) {
     464   NUMSWITCH: {
     465       if ($Criterion =~ /^ge$/i) { $ValueOk = ($Value >= $CriterionValue) ? 1 : 0; last NUMSWITCH; }
     466       if ($Criterion =~ /^le$/i) { $ValueOk = ($Value <= $CriterionValue) ? 1 : 0; last NUMSWITCH; }
     467       if ($Criterion =~ /^eq$/i) { $ValueOk = ($Value == $CriterionValue) ? 1 : 0; last NUMSWITCH; }
     468       $Nothing = 1;
     469     }
     470   }
     471   else {
     472   TEXTSWITCH: {
     473       if ($Criterion =~ /^ge$/i) { $ValueOk = ($Value ge $CriterionValue) ? 1 : 0; last TEXTSWITCH; }
     474       if ($Criterion =~ /^le$/i) { $ValueOk = ($Value le $CriterionValue) ? 1 : 0; last TEXTSWITCH; }
     475       if ($Criterion =~ /^eq$/i) { $ValueOk = ($Value eq $CriterionValue) ? 1 : 0; last TEXTSWITCH; }
     476       $Nothing = 1;
     477     }
     478   }
     479   $BgColor = $ValueOk ? $OptionsInfo{ValueOkColor} : $OptionsInfo{ValueNotOkColor};
     480   if ($Options{highlightstyle} =~ /^text$/i) {
     481     $BgColor = "";
     482     $FontColor = $ValueOk ? $OptionsInfo{ValueOkColor} : $OptionsInfo{ValueNotOkColor};
     483   }
     484   return ($BgColor, $FontColor);
     485 }
     486
     487 # Setup columns, tables and other information...
     488 sub SetupCoulmnsTablesAndMiscInfo {
     489   SetupColumnsToHighlightInfo();
     490   SetupMultipleTablesInfo();
     491   SetupHTMLTagsInfo();
     492 }
     493
     494 # Setup columns to highlight information...
     495 sub SetupColumnsToHighlightInfo {
     496   my($ColID, $DataType, $Criterion, $Value, $Index, $ColNum, $ColLabel, $ColIndex);
     497
     498   @{$TextFilesInfo{HightlightColNumMap}} = ();
     499   @{$TextFilesInfo{HightlightDataMap}} = ();
     500
     501   for $Index (0 .. $#TextFilesList) {
     502     %{$TextFilesInfo{HightlightColNumMap}[$Index]} = ();
     503     %{$TextFilesInfo{HightlightDataMap}[$Index]} = ();
     504     if ($TextFilesInfo{FileOkay}[$Index]) {
     505       SPECIFIEDCOLS: for $ColIndex (0 .. $#{$OptionsInfo{SpecifiedColIds}}) {
     506         $ColID = $OptionsInfo{SpecifiedColIds}[$ColIndex];
     507         $DataType = $OptionsInfo{SpecifiedColDataTypes}[$ColIndex];
     508         $Criterion = $OptionsInfo{SpecifiedColCriteria}[$ColIndex];
     509         $Value = $OptionsInfo{SpecifiedColValues}[$ColIndex];
     510         if (!$OptionsInfo{HighlightStatus}) {
     511           next SPECIFIEDCOLS;
     512         }
     513         if ($Options{highlightby} =~ /^colnum$/i) {
     514           $ColNum = $ColID;
     515           if ($ColNum > 0 && $ColNum <= $TextFilesInfo{ColCount}[$Index]) {
     516             $ColNum -= 1;
     517           }
     518           else {
     519             warn "Warning: Ignoring column number, $ColID, specifed in quartet, \"$ColID,$DataType,$Criterion,$Value\", using \"--highlight\" option for $TextFilesList[$Index]: it doesn't exists \n";
     520             next SPECIFIEDCOLS;
     521           }
     522         }
     523         else {
     524           $ColLabel = $ColID;
     525           if (exists($TextFilesInfo{ColLabelToNumMap}[$Index]{$ColLabel})) {
     526             $ColNum = $TextFilesInfo{ColLabelToNumMap}[$Index]{$ColLabel};
     527           } else {
     528             warn "Warning: Ignoring column label, $ColID, specifed in quartet, \"$ColID,$DataType,$Criterion,$Value\", using \"--highlight\" option for $TextFilesList[$Index]: it doesn't exists \n";
     529             next SPECIFIEDCOLS;
     530           }
     531         }
     532         $TextFilesInfo{HightlightColNumMap}[$Index]{$ColNum} = $ColNum;
     533         @{$TextFilesInfo{HightlightDataMap}[$Index]{$ColNum}} =();
     534         push @{$TextFilesInfo{HightlightDataMap}[$Index]{$ColNum}}, ($DataType, $Criterion, $Value);
     535       }
     536     }
     537   }
     538 }
     539
     540 # Setup navigation link information for multiple tables...
     541 sub SetupMultipleTablesInfo {
     542   my($Index, $LinesPerTable);
     543
     544   $LinesPerTable = $Options{numrows};
     545   @{$TextFilesInfo{TableCount}} = ();
     546   @{$TextFilesInfo{TableHTMLFiles}} = ();
     547   @{$TextFilesInfo{TableStartLineNum}} = ();
     548   @{$TextFilesInfo{TableEndLineNum}} = ();
     549
     550   for $Index (0 .. $#TextFilesList) {
     551     $TextFilesInfo{TableCount}[$Index] = 1;
     552     @{$TextFilesInfo{TableHTMLFiles}[$Index]} = ();
     553     @{$TextFilesInfo{TableStartLineNum}[$Index]} = ();
     554     @{$TextFilesInfo{TableEndLineNum}[$Index]} = ();
     555
     556     if ($TextFilesInfo{FileOkay}[$Index]) {
     557       if ($TextFilesInfo{MultipleHTMLTables}[$Index]) {
     558         my($TableIndex, $TotalLines, $TableCount, $TableStartLineNum, $TableEndLineNum, $Name);
     559
     560         $TotalLines = $TextFilesInfo{LineCount}[$Index];
     561         $TableCount = ($TotalLines % $LinesPerTable) ? (int($TotalLines/$LinesPerTable) + 1) : ($TotalLines/$LinesPerTable);
     562         $TextFilesInfo{TableCount}[$Index] = $TableCount;
     563         for $TableIndex (1 .. $TableCount) {
     564           $TableStartLineNum = ($TableIndex - 1) * $LinesPerTable + 1;
     565           $TableEndLineNum = ($TableIndex == $TableCount) ? $TotalLines : ($TableIndex * $LinesPerTable);
     566           push @{$TextFilesInfo{TableStartLineNum}[$Index]}, $TableStartLineNum;
     567           push @{$TextFilesInfo{TableEndLineNum}[$Index]}, $TableEndLineNum;
     568
     569           # Setup HTML file names for all the tables...
     570           $Name = "Lines" . "$TableStartLineNum" . "To" . "$TableEndLineNum";
     571           if ($TableIndex == 1) {
     572             $Name = "";
     573           }
     574           $Name = $TextFilesInfo{HTMLRoot}[$Index] . $Name . ".html";
     575           push @{$TextFilesInfo{TableHTMLFiles}[$Index]}, $Name;
     576         }
     577         #print "$TextFilesList[$Index]: $TableCount -  @{$TextFilesInfo{TableStartLineNum}[$Index]} - @{$TextFilesInfo{TableEndLineNum}[$Index]} -  @{$TextFilesInfo{TableHTMLFiles}[$Index]}\n";
     578       }
     579     }
     580   }
     581 }
     582
     583 # Setup HTML tags information...
     584 sub SetupHTMLTagsInfo {
     585   # Setup row tags...
     586   $TextFilesInfo{RowHeaderTags} = "";
     587   $TextFilesInfo{RowEndTags} = "";
     588   $TextFilesInfo{BgFilledOddRowHeaderTags} = "";
     589   $TextFilesInfo{BgFilledEvenRowHeaderTags} = "";
     590   $TextFilesInfo{TableRowHeaderTags} = "";
     591
     592   $TextFilesInfo{RowHeaderTags} = SetupHTMLTableRowHeader($OptionsInfo{RowHAlignment}, "", $OptionsInfo{RowVAlignment});
     593   $TextFilesInfo{RowEndTags} = SetupHTMLTableRowEnd();
     594
     595   if ($OptionsInfo{ShadeRowsStatus}) {
     596     $TextFilesInfo{BgFilledOddRowHeaderTags} = SetupHTMLTableRowHeader($OptionsInfo{RowHAlignment}, $OptionsInfo{OddRowsShadeColor}, $OptionsInfo{RowVAlignment});
     597     $TextFilesInfo{BgFilledEvenRowHeaderTags} = SetupHTMLTableRowHeader($OptionsInfo{RowHAlignment}, $OptionsInfo{EvenRowsShadeColor}, $OptionsInfo{RowVAlignment});
     598   }
     599
     600   $TextFilesInfo{TableRowHeaderTags} = SetupHTMLTableRowHeader($OptionsInfo{TableHeaderRowHAlignment}, $OptionsInfo{TableHeaderRowColor}, $OptionsInfo{TableHeaderRowVAlignment});
     601
     602 }
     603
     604 #Make sure appropriate mode specific option values are specified...
     605 sub ProcessOptions {
     606
     607   %OptionsInfo = ();
     608
     609   $OptionsInfo{RowHAlignment} = "left"; $OptionsInfo{RowVAlignment} = "middle";
     610   if (exists($Options{align})) {
     611     my (@AlignValues) = split ",", $Options{align};
     612     if (@AlignValues == 2) {
     613       $OptionsInfo{RowHAlignment} = $AlignValues[0];
     614       $OptionsInfo{RowVAlignment} = $AlignValues[1];
     615     }
     616     elsif (@AlignValues == 1) {
     617       $OptionsInfo{RowHAlignment} = $AlignValues[0];
     618     }
     619     else {
     620       die "Error: Invalid number of values, ", scalar(@AlignValues) , ", specified by \"-a --align\" option.\nIt must contain only one or two value.\n";
     621     }
     622     if ($OptionsInfo{RowHAlignment} !~ /^(left|center|right)$/i) {
     623       die "Error: The horizontal alignment value specified, $Options{align}, for option \"-a --align\" is not valid. Allowed values: left, center, or right\n";
     624     }
     625     if ($OptionsInfo{RowVAlignment} !~ /^(top|middle|bottom)$/i) {
     626       die "Error: The horizontal alignment value specified, $Options{align}, for option \"-a --align\" is not valid. Allowed values: top, middle, or bottom\n";
     627     }
     628   }
     629
     630   $OptionsInfo{TableHeaderRowHAlignment} = "center"; $OptionsInfo{TableHeaderRowVAlignment} = "middle";
     631   if (exists($Options{headeralign})) {
     632     my (@AlignValues) = split ",", $Options{headeralign};
     633     if (@AlignValues == 2) {
     634       $OptionsInfo{TableHeaderRowHAlignment} = $AlignValues[0];
     635       $OptionsInfo{TableHeaderRowVAlignment} = $AlignValues[1];
     636     }
     637     elsif (@AlignValues == 1) {
     638       $OptionsInfo{TableHeaderRowHAlignment} = $AlignValues[0];
     639     }
     640     else {
     641       die "Error: Invalid number of values, ", scalar(@AlignValues) , ", specified by \"--headeralign\" option.\nIt must contain only one or two value.\n";
     642     }
     643     if ($OptionsInfo{TableHeaderRowHAlignment} !~ /^(left|center|right)$/i) {
     644       die "Error: The horizontal alignment value specified, $Options{headeralign}, for option \"--headeralign\" is not valid. Allowed values: left, center, or right\n";
     645     }
     646     if ($OptionsInfo{TableHeaderRowVAlignment} !~ /^(top|middle|bottom)$/i) {
     647       die "Error: The horizontal alignment value specified, $Options{headeralign}, for option \"-a --headeralign\" is not valid. Allowed values: top, middle, or bottom\n";
     648     }
     649   }
     650
     651   $OptionsInfo{TitleDisplay} = ($Options{titledisplay} =~ /^yes$/i) ? 1 : 0;
     652
     653   if (exists($Options{border})) {
     654     $OptionsInfo{TableBorder} = $Options{border};
     655   }
     656   else {
     657     $OptionsInfo{TableBorder} = ($Options{mode} =~ /^(plain|highlight)$/i) ? 1 : 0;
     658   }
     659   $OptionsInfo{TableCellPadding} = $Options{cellpadding};
     660   $OptionsInfo{TableCellSpacing} = $Options{cellspacing};
     661   $OptionsInfo{Footer} = $Options{footer} ? $Options{footer} : "";
     662
     663   if ($Options{headercolor}) {
     664     $OptionsInfo{TableHeaderRowColor} = $Options{headercolor};
     665   }
     666   else {
     667     $OptionsInfo{TableHeaderRowColor} = ($Options{mode} =~ /^plain$/i) ? "" : "#ccccff";
     668   }
     669
     670   $OptionsInfo{NavLinksAtBottom} = 1; $OptionsInfo{NavLinksAtTop} = 0;
     671   if ($Options{displaylinks} =~ /^(both|top)$/i) {
     672     $OptionsInfo{NavLinksAtTop} = 1;
     673   }
     674   $OptionsInfo{NavLinksTableInfo} = 1; $OptionsInfo{NavLinksLineInfo} = 0;
     675   if ($Options{displaylinksinfo} =~ /^both$/i) {
     676     $OptionsInfo{NavLinksLineInfo} = 1;
     677     $OptionsInfo{NavLinksTableInfo} = 1;
     678   }
     679   elsif ($Options{displaylinksinfo} =~ /^line$/i) {
     680     $OptionsInfo{NavLinksLineInfo} = 1;
     681     $OptionsInfo{NavLinksTableInfo} = 0;
     682   }
     683
     684   if ($Options{stylesheet} =~ /^old$/i ) {
     685     if (!$Options{stylesheetname}) {
     686       die "Error: No stylesheet name specified using \"--stylesheetname\" option: It is required for \"old\" value of \"-s --stylesheet\" option. \n";
     687     }
     688   }
     689
     690   my(@ColorValues);
     691   $OptionsInfo{OddRowsShadeColor} = ""; $OptionsInfo{EvenRowsShadeColor} = ""; $OptionsInfo{ShadeRowsStatus} = 0;
     692   if ($Options{mode} =~ /^(shade|shadedhighlight)$/i) {
     693     $OptionsInfo{OddRowsShadeColor} = "#ffffff";
     694     $OptionsInfo{EvenRowsShadeColor} = "#e0e0eb";
     695     $OptionsInfo{ShadeRowsStatus} = 1;
     696     if ($Options{shadecolor}) {
     697       # Make sure only two value are specified...
     698       @ColorValues = split ",", $Options{shadecolor};
     699       if (@ColorValues == 2) {
     700         $OptionsInfo{OddRowsShadeColor} = $ColorValues[0];
     701         $OptionsInfo{EvenRowsShadeColor} = $ColorValues[1];
     702       }
     703       else {
     704         die "Error: Invalid number of values, ", scalar(@ColorValues) , ", specified by \"--shadecolor\" option.\nIt must contain only two values.\n";
     705       }
     706     }
     707   }
     708   $OptionsInfo{ValueOkColor} = ""; $OptionsInfo{ValueNotOkColor} = ""; $OptionsInfo{HighlightStatus} = 0;
     709   if ($Options{mode} =~ /^(highlight|shadedhighlight)$/i) {
     710     my($HighlightMode, $HighlightBy);
     711     $HighlightMode = $Options{mode}; $HighlightBy = $Options{highlightby};
     712
     713     $OptionsInfo{HighlightStatus} = 1;
     714     $OptionsInfo{ValueOkColor} = "#0fff0f";
     715     $OptionsInfo{ValueNotOkColor} = "#ff0f0f";
     716     if ($Options{highlightstyle} =~ /^text$/i) {
     717       $OptionsInfo{ValueOkColor} = "#0fbb0f";
     718       $OptionsInfo{ValueNotOkColor} = "#ff0f0f";
     719     }
     720     if ($Options{highlightcolor}) {
     721       # Make sure two values are specified...
     722       @ColorValues = split ",", $Options{highlightcolor};
     723       if (@ColorValues == 2) {
     724         $OptionsInfo{ValueOkColor} = $ColorValues[0];
     725         $OptionsInfo{ValueNotOkColor} = $ColorValues[1];
     726       }
     727       else {
     728         die "Error: Invalid number of values, ", scalar(@ColorValues), ", specified by \"--highlightcolor\" option.\nIt must contain only two value for $HighlightMode value specified using \"-m --mode\" option.\n";
     729       }
     730     }
     731     if (!$Options{highlight}) {
     732       die "Error: Specify columns to be highlighted using \"--hightlight\" option\n";
     733     }
     734     # Retrieve quartet values from "hightlight" option...
     735     my(@HighlightValueQuartets);
     736
     737     @HighlightValueQuartets = ();
     738     @HighlightValueQuartets = split ",", $Options{highlight};
     739     if ((@HighlightValueQuartets % 4)) {
     740       die "Error: Quartets not found in values specified using \"--highlight\" option for $HighlightMode \"-m --mode\"\n";
     741     }
     742     # Process quartets...
     743     my($Index, $Col, $DataType, $Criterion, $Value);
     744
     745     @{$OptionsInfo{SpecifiedColIds}} = ();
     746     @{$OptionsInfo{SpecifiedColDataTypes}} = ();
     747     @{$OptionsInfo{SpecifiedColCriteria}} = ();
     748     @{$OptionsInfo{SpecifiedColValues}} = ();
     749     for ($Index = 0; $Index < @HighlightValueQuartets; $Index = $Index + 4) {
     750       $Col = $HighlightValueQuartets[$Index];
     751       $DataType = $HighlightValueQuartets[$Index + 1];
     752       $Criterion = $HighlightValueQuartets[$Index + 2];
     753       $Value = $HighlightValueQuartets[$Index + 3];
     754       if ($Options{highlightby} =~ /^colnum$/i ) {
     755         if (!IsPositiveInteger($Col)) {
     756           die "Error: Invalid column id, $Col, specified in quartet, \"$Col,$DataType,$Criterion,$Value\", using \"--hightlight\" option: It must be an integer value > 0 for $HighlightMode \"-m --mode\" and $HighlightBy \"--highlightby\" option values.\n";
     757         }
     758       }
     759       if ($DataType !~ /^(numeric|text)$/i) {
     760         die "Error: Invalid column data type, $DataType, specified in quartet, \"$Col,$DataType,$Criterion,$Value\", using \"--hightlight\" option: Valid values: numeric or text\n";
     761       }
     762       if ($Criterion !~ /^(eq|le|ge)$/i) {
     763         die "Error: Invalid criterion value, $Criterion, specified in quartet, \"$Col,$DataType,$Criterion,$Value\", using \"--hightlight\" option: Valid values: le, ge, or eq\n";
     764       }
     765       if ($DataType =~ /^numeric$/i) {
     766         if (!IsFloat($Value)) {
     767           die "Error: Invalid criterion value, $Value, specified in quartet, \"$Col,$DataType,$Criterion,$Value\", using \"--hightlight\" option: numeric value required for numeric data type\n";
     768         }
     769       }
     770       push @{$OptionsInfo{SpecifiedColIds}}, $Col;
     771       push @{$OptionsInfo{SpecifiedColDataTypes}}, $DataType;
     772       push @{$OptionsInfo{SpecifiedColCriteria}}, $Criterion;
     773       push @{$OptionsInfo{SpecifiedColValues}}, $Value;
     774     }
     775   }
     776 }
     777
     778 # Retrieve information about input text files...
     779 sub RetrieveTextFilesInfo {
     780   my($LineCount, $TextFile, $FileDir, $FileName, $HTMLFile, $CSSFile, $HTMLRoot, $HTMLTitle, $FileExt, $Index, $ColIndex, $ColNum, $ColLabel, $LinesCount, $InDelim, $Line, @LineWords, @ColLabels, $TopHTMLDir);
     781
     782   %TextFilesInfo = ();
     783
     784   @{$TextFilesInfo{FileOkay}} = ();
     785   @{$TextFilesInfo{ColCount}} = ();
     786   @{$TextFilesInfo{ColLabels}} = ();
     787   @{$TextFilesInfo{ColLabelToNumMap}} = ();
     788   @{$TextFilesInfo{LineCount}} = ();
     789   @{$TextFilesInfo{InDelim}} = ();
     790
     791   @{$TextFilesInfo{HTMLRoot}} = ();
     792   @{$TextFilesInfo{HTMLTitle}} = ();
     793   @{$TextFilesInfo{MultipleHTMLTables}} = ();
     794
     795   @{$TextFilesInfo{TopHTMLDir}} = ();
     796   @{$TextFilesInfo{SubHTMLDir}} = ();
     797
     798   FILELIST: for $Index (0 .. $#TextFilesList) {
     799     $TextFile = $TextFilesList[$Index];
     800
     801     $TextFilesInfo{FileOkay}[$Index] = 0;
     802     $TextFilesInfo{ColCount}[$Index] = 0;
     803     $TextFilesInfo{LineCount}[$Index] = 0;
     804     $TextFilesInfo{InDelim}[$Index] = "";
     805     $TextFilesInfo{HTMLRoot}[$Index] = "";
     806     $TextFilesInfo{HTMLTitle}[$Index] = "";
     807     $TextFilesInfo{MultipleHTMLTables}[$Index] = 0;
     808
     809     @{$TextFilesInfo{ColLabels}[$Index]} = ();
     810     %{$TextFilesInfo{ColLabelToNumMap}[$Index]} = ();
     811
     812     if (!(-e $TextFile)) {
     813       warn "Warning: Ignoring file $TextFile: It doesn't exist\n";
     814       next FILELIST;
     815     }
     816     if (!CheckFileType($TextFile, "csv tsv")) {
     817       warn "Warning: Ignoring file $TextFile: It's not a csv or tsv file\n";
     818       next FILELIST;
     819     }
     820     ($FileDir, $FileName, $FileExt) = ParseFileName($TextFile);
     821     if ($FileExt =~ /^tsv$/i) {
     822       $InDelim = "\t";
     823     }
     824     else {
     825       $InDelim = "\,";
     826       if ($Options{indelim} !~ /^(comma|semicolon)$/i) {
     827         warn "Warning: Ignoring file $TextFile: The value specified, $Options{indelim}, for option \"--indelim\" is not valid for csv files\n";
     828         next FILELIST;
     829       }
     830       if ($Options{indelim} =~ /^semicolon$/i) {
     831         $InDelim = "\;";
     832       }
     833     }
     834
     835     if (!open TEXTFILE, "$TextFile") {
     836       warn "Warning: Ignoring file $TextFile: Couldn't open it: $! \n";
     837       next FILELIST;
     838     }
     839
     840     $Line = GetTextLine(\*TEXTFILE);
     841     @ColLabels = quotewords($InDelim, 0, $Line);
     842     $LineCount = 0;
     843     while (<TEXTFILE>) {
     844       $LineCount++;
     845     }
     846     close TEXTFILE;
     847
     848     $FileDir = ""; $FileName = ""; $FileExt = "";
     849     ($FileDir, $FileName, $FileExt) = ParseFileName($TextFile);
     850     $HTMLRoot = $FileName;
     851     if ($Options{root} && (@TextFilesList == 1)) {
     852       my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($Options{root});
     853       if ($RootFileName && $RootFileExt) {
     854         $HTMLRoot = $RootFileName;
     855       }
     856       else {
     857         $HTMLRoot = $Options{root};
     858       }
     859     }
     860     $HTMLTitle = $HTMLRoot;
     861     if ($Options{title} && (@TextFilesList == 1)) {
     862       $HTMLTitle = $Options{title};
     863     }
     864     $HTMLFile = lc($HTMLRoot) . "-html";
     865     if (!$Options{overwrite}) {
     866       if (-d $HTMLFile) {
     867         warn "Warning: Ignoring file $TextFile: The directory $HTMLFile already exists\n";
     868         next FILELIST;
     869       }
     870     }
     871
     872     $TextFilesInfo{FileOkay}[$Index] = 1;
     873     $TextFilesInfo{InDelim}[$Index] = $InDelim;
     874     $TextFilesInfo{HTMLRoot}[$Index] = "$HTMLRoot";
     875     $TextFilesInfo{HTMLTitle}[$Index] = "$HTMLTitle";
     876
     877     $TextFilesInfo{ColCount}[$Index] = @ColLabels;
     878     push @{$TextFilesInfo{ColLabels}[$Index]}, @ColLabels;
     879     for $ColNum (0 .. $#ColLabels) {
     880       $ColLabel = $ColLabels[$ColNum];
     881       $TextFilesInfo{ColLabelToNumMap}[$Index]{$ColLabel} = $ColNum;
     882     }
     883     $TextFilesInfo{LineCount}[$Index] = $LineCount;
     884
     885     if ($Options{numrows} == 0 || $LineCount <= $Options{numrows}) {
     886       $TextFilesInfo{MultipleHTMLTables}[$Index] = 0;
     887     }
     888     else {
     889       $TextFilesInfo{MultipleHTMLTables}[$Index] = 1;
     890     }
     891     # Setup HTML data directories paths...
     892     $TopHTMLDir = lc($TextFilesInfo{HTMLRoot}[$Index]) . "-html";
     893     $TextFilesInfo{TopHTMLDir}[$Index] = "$TopHTMLDir";
     894     $TextFilesInfo{SubHTMLDir}[$Index] = "$TopHTMLDir\/html";
     895   }
     896 }
     897
     898 # Setup various data directories to hold HTML and other related files...
     899 sub SetupDataDirs {
     900   my($Index) = @_;
     901   my($TopHTMLDir, $SubHTMLDir, $CreateTopHTMLDir, $CreateSubHTMLDir);
     902
     903   $TopHTMLDir = $TextFilesInfo{TopHTMLDir}[$Index];
     904   $SubHTMLDir = $TextFilesInfo{SubHTMLDir}[$Index];
     905
     906   # Clean up existing directories...
     907   if (-d $TopHTMLDir) {
     908     unlink "<$TopHTMLDir/*.html>";
     909     unlink "<$TopHTMLDir/*.css>";
     910   }
     911   if (-d $SubHTMLDir) {
     912     unlink "<$SubHTMLDir/*.html>";
     913   }
     914   # What directories need to be created...
     915   $CreateTopHTMLDir = (-d $TopHTMLDir) ? 0 : 1;
     916
     917   $CreateSubHTMLDir = 0;
     918   if ($TextFilesInfo{MultipleHTMLTables}[$Index]) {
     919     $CreateSubHTMLDir = (-d $SubHTMLDir) ? 0 : 1;
     920   }
     921
     922   # Create appropriate directories...
     923   if ($CreateTopHTMLDir) {
     924     mkdir $TopHTMLDir or die "Couldn't mkdir $TopHTMLDir: $! \n";
     925   }
     926   if ($CreateSubHTMLDir) {
     927     mkdir $SubHTMLDir or die "Error: Couldn't mkdir $SubHTMLDir: $! \n";
     928   }
     929   else {
     930     unlink <$SubHTMLDir/*.html>;
     931   }
     932   return ($TopHTMLDir, $SubHTMLDir);
     933 }
     934
     935 # Setup script usage  and retrieve command line arguments specified using various options...
     936 sub SetupScriptUsage {
     937
     938   # Retrieve all the options...
     939   %Options = ();
     940   $Options{indelim} = "comma";
     941   $Options{numrows} = 50;
     942
     943   $Options{mode} = "shade";
     944   $Options{highlightby} = "colnum";
     945   $Options{highlightstyle} = "background";
     946
     947   $Options{cellpadding} = 2;
     948   $Options{cellspacing} = 1;
     949
     950   $Options{displaylinks} = "both";
     951   $Options{displaylinksinfo} = "both";
     952   $Options{stylesheet} = "new";
     953
     954   $Options{titledisplay} = "yes";
     955
     956   if (!GetOptions(\%Options, "align|a=s", "border|b=i", "cellpadding=i", "cellspacing=i", "color|c=s", "footer=s", "displaylinks|d=s", "displaylinksinfo=s", "help|h", "headeralign=s", "headercolor=s", "highlight=s", "highlightby=s", "highlightcolor=s", "highlightstyle=s", "indelim=s", "mode|m=s", "numrows|n=i", "overwrite|o", "root|r=s", "shadecolor=s", "stylesheet=s", "stylesheetname=s", "title|t=s", "titledisplay=s", "workingdir|w=s")) {
     957     die "\nTo get a list of valid options and their values, use \"$ScriptName -h\" or\n\"perl -S $ScriptName -h\" command and try again...\n";
     958   }
     959
     960   if ($Options{workingdir}) {
     961     if (! -d $Options{workingdir}) {
     962       die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
     963     }
     964     chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
     965   }
     966   if ($Options{displaylinks} !~ /^(top|bottom|both)$/i) {
     967     die "Error: The value specified, $Options{displaylinks}, for option \"-d --displaylinks\" is not valid. Allowed values: top, bottom, or both\n";
     968   }
     969   if ($Options{displaylinksinfo} !~ /^(line|table|both)$/i) {
     970     die "Error: The value specified, $Options{displaylinksinfo}, for option \"--displaylinksinfo\" is not valid. Allowed values: line, table, or both\n";
     971   }
     972   if ($Options{indelim} !~ /^(comma|semicolon)$/i) {
     973     die "Error: The value specified, $Options{indelim}, for option \"--indelim\" is not valid. Allowed values: comma or semicolon\n";
     974   }
     975   if ($Options{highlightby} !~ /^(colnum|collabel)$/i) {
     976     die "Error: The value specified, $Options{highlightby}, for option \"--highlightby\" is not valid. Allowed values: colnum or collabel\n";
     977   }
     978   if ($Options{highlightstyle} !~ /^(background|text)$/i) {
     979     die "Error: The value specified, $Options{highlightstyle}, for option \"--highlightstyle\" is not valid. Allowed values: background or text\n";
     980   }
     981   if ($Options{mode} !~ /^(plain|shade|highlight|shadedhighlight)$/i) {
     982     die "Error: The value specified, $Options{mode}, for option \"-m --mode\" is not valid. Allowed values: plain, shade, hightlight, or shadedhighlight\n";
     983   }
     984   if ($Options{stylesheet} !~ /^(old|new|none)$/i) {
     985     die "Error: The value specified, $Options{stylesheet}, for option \"-s --stylesheet\" is not valid. Allowed values: old, new, or none\n";
     986   }
     987   if ($Options{numrows} < 0) {
     988     die "Error: The value specified, $Options{numrows},  for option \"-n --numrows\" is not valid. Allowed values: >= 0 \n";
     989   }
     990   if ($Options{titledisplay} !~ /^(yes|no)$/i) {
     991     die "Error: The value specified, $Options{titledisplay}, for option \"--titledisplay\" is not valid. Allowed values: yes or no\n";
     992   }
     993   if (exists($Options{border})) {
     994     if ($Options{border} < 0) {
     995       die "Error: The value specified, $Options{border},  for option \"--border\" is not valid. Allowed values: >= 0 \n";
     996     }
     997   }
     998   if ($Options{cellpadding} < 0) {
     999     die "Error: The value specified, $Options{cellpadding},  for option \"--cellpadding\" is not valid. Allowed values: >= 0 \n";
    1000   }
    1001   if ($Options{cellspacing} < 0) {
    1002     die "Error: The value specified, $Options{cellspacing},  for option \"--cellspacing\" is not valid. Allowed values: >= 0 \n";
    1003   }
    1004 }
    1005