
#!/usr/local/bin/perl
# $Id: doc2.pl,v 1.1.1.1 2003/11/04 23:34:58 mjames Exp $
# $Log: doc2.pl,v $
# Revision 1.1.1.1  2003/11/04 23:34:58  mjames
# Imported into local repositrory
#
# Revision 1.7  2002/09/30 13:26:33  MJAMES
# Files split from one HTML monster into one per command
#
# Revision 1.1.1.1  2002/09/09 15:06:16  mjames
# no message
#
# Revision 1.6  2002/08/06 12:52:44  mjames
# Merge in from latest version
#
# Revision 1.6  2002/03/08 14:56:13  mjames
# Edited all of the commands in the cmdexec1.c and cmdexec2.c files
# so that they can be read by doc2.pl
#
# Revision 1.5  2001/12/20 13:54:19  mjames
# Update version number to 15.1
#
# Revision 1.4  2001/10/31 22:20:03  mjames
# Tidying up problematical comments caused by CVS
# 'intelligent' comment guessing
#
# Revision 1.3  2001/10/02 20:54:45  mjames
# Tidied up the formatted HTML, making sensible crossreference
# links from within document text.
#
# Revision 1.2  2001/10/01 21:56:47  mjames
# Added crossreferences from text
# SO that e.g see fred command' will generate an html link to fred.
#
# Argument 0 is file to index
# Argument is path to output file. 
#
# State variable $i =
#1 = title
#2 = text
#3 = listing
#0 = nothing

# phase 1 : open file and search for all of the headlines

open INFILE,"<${ARGV[0]}";
open INDEXFILE,">${ARGV[1]}/index.htm";

print INDEXFILE "<title>VERTICAL Automatically Generated Documentation</title>";
print INDEXFILE  "<html>\n";
print INDEXFILE  "<h1>Index of commands</h1>\n";
print INDEXFILE  "Items marked as PROGRAM are implemented as separate preprocessors for Vertical.\n" ;
print INDEXFILE  "Call these from a shell command line rather than within Vertical.\n";
print INDEXFILE  "<a name=\"top\"><table>\n";

# loop 1 : index all of the entries

$i=0;
$tag = 0;
$patterns = 0;

#bit mask giving pattern membership
$bit = 1; 
$areacnt = 0;

while ( <INFILE> ) {
# should be more escapes here.
  s/</\&lt;/g;

  if ($i==1 && $tag ==0 ) {
    $targ = $_;
    chomp $targ;
    if ($targ =~ s/([a-zA-Z0-9_ ]+)(.*)/$1/ ) {
      $rest = $2;
      $link = $1;
      $link =~ s/ $//g;
# remember the text of the link for future 
      $links[$pattern] = $link;      
      $targ =~ s/ /_/g;
      $targ =~ s/_$//g;
      $linktxt[$pattern] =  "<tr><td><a href=\"./$targ.htm\">$link</a><td>$rest</tr>\n";
      print INDEXFILE $linktxt;
      $prevpatt = $pattern;
      $pattern++;
      }; 
    $tag = 1;
    $i = 0;
    };
#decode keywords
  if (/\@application +([^\n\r ]+)/) {
    $app_area = $1;
    chomp $app_area;
    $ii=0;
    $app_area;
    while(($ii < @areas) && ($areas[$ii] ne $app_area))
      { $ii++; }
    if( $ii == @areas)
      {
#      print "<!-- found new $app_area = $ii -->\n";
      $areas[$ii]   = $app_area;
      $bitflag[$ii] = $bit;
      $bit = $bit * 2; 
      }
    else
      {
#      print "<!-- found existing $app_area = $ii -->\n";
      }
    $linkflags[$prevpatt] |= $bitflag[$ii];
    $tag = 0;
    }
  if (/\@title/) { 
    $i=1;
    $tag = 0;
    }
  elsif (/\@end/ ) {
    $i=0; 
    };
  };



print INDEXFILE "</table><br><hr><br>";


sort @links;


close INFILE;
open INFILE,"<${ARGV[0]}";
  
while ( <INFILE> ) {

# should be more escapes here.
  s/&/\&amp;/g;
  s/</\&lt;/g;

  if ($i==1 && $tag ==0 ) {
    $targ = $_;
    chomp $targ;
    if ($targ =~ s/([a-zA-Z0-9_ ]+).*/$1/ ) {
#make spaces into underscores
     $targ =~ s/ /_/g;
#kill trailing underscore
     $targ =~ s/_$//;
      if (<OUTFILE>) 
        {
        print OUTFILE "</html>";
        }
      open  OUTFILE,">${ARGV[1]}/$targ.htm";
      print OUTFILE "<title>$targ</title>\n";
      print OUTFILE "<html><h2>";
      }; 
    $tag = 1;
    };

  if (/\@title/) { 
    $i=1;
    $tag = 0;
    #print OUTFILE "<h2>\n";
    }
  elsif (/\@application +([^\n\r ]+)/) {
    $tag = 0;
    if ($i==1) { 
      print OUTFILE "</h2><hr>\n"; 
      $i=0;
      };
    if ($i==3) {  
      print OUTFILE "</pre>\n";  
      };
    if ($i!=4)
      {
      $i = 4;
      print OUTFILE "<ul><font size=-1>\n"
      }
    printf OUTFILE "<li>used with : <a href=\".\/index.htm#index_$1\">$1</a><font size=+0>\n";
    }
# create a list of links to the indexes
  elsif (/\@list_index([^\n\r ]*)/) {
    printf INDEXFILE ("<h2>List of Indexes</h2><ul>\n");
    $ii=0;
    while($ii < @areas) 
      { 
      print INDEXFILE "<li><a href=\".\/index.htm#index_$areas[$ii]\">$areas[$ii]</a></li>\n";
      $ii++; 
      }
    printf INDEXFILE "<font size=+0></ul><br>\n";
    }
# we create an index of links to relevant code
  elsif (/\@index +([^\n\r ]+)/) {
    if ($i==1) { 
      print INDEXFILE "</h2><hr>\n"; 
      $i=0;
      };
    if ($i==3) {  
      print OUTFILE "</pre>\n";
      $i = 0;  
      };
    if ($i==4)
      {
      $i = 0;
      print INDEXFILE "<font size=+0></ul><br>\n"
      }
    print INDEXFILE "<a name=\"index_$1\"><h3>Index $1</h3>\n<table>\n";
# locate application area
    $app_area = $1;
    chomp $app_area;
    $ii=0;
    while(($ii < @areas) && ($areas[$ii] ne $app_area))
      { $ii++; }
# if we have found the area
    if( $ii != @areas)
      {
#      print "<!-- found existing $app_area = $ii -->\n";
      $flag = $bitflag[$ii];
      for ($i =0; $i< @linkflags; $i++) 
        {
#        printf("flag %d\n",$flag & $linkflags[$i]);
        if ( $flag & $linkflags[$i]) 
          {   
          print INDEXFILE "$linktxt[$i]\n";
          }

        }
      }
    printf INDEXFILE "</table>\n";
     


    }
  elsif (/\@text/ ) {
    if ($i==1) { 
      print OUTFILE "</h2><hr>\n"; 
      };
    if ($i==3) {  
      print OUTFILE "</pre>\n";  
      };
    if ($i==4)
      {
      $i = 0;
      print OUTFILE "<font size=+0></ul><br>\n"
      }
    $i=2; 
    }
  elsif (/\@listing/ ) {
    if ($i==1) { 
      print OUTFILE "</h1><pre>\n"; 
      };
    if ($i==2) {  
      print OUTFILE "<pre>\n";  
      };
    if ($i==4)
      {
      $i = 0;
      print OUTFILE "<font size=+0></ul><br>\n"
      }
    $i=3; 
    }
  elsif (/\@end/ ) {
    if ($i==1) { 
      print OUTFILE "</h1>\n"; 
      };
    
    if ($i==3) {  
      print OUTFILE "</pre>\n";  
      };
    if ($i==4)
      {
      $i = 0;
      print OUTFILE "<font size=+0></ul><br>\n"
      }
    print OUTFILE "<br><a href=\".\/index.htm#top\">Vertical Index</a>\n<hr>\n";
    $i=0; 
    }
  elsif (/\@break/ ) {
    if ($i==1 || $i==2 ) { 
      print OUTFILE "<br>\n"; 
      };
    if ($i==3) {  
      print OUTFILE "</pre><br><pre>\n";  
      };
    if ($i==4)
      {
      $i = 0;
      print OUTFILE "<font size=+0></ul><br>\n"
      }
    }
  else {
    if ($i) {
#very slow but acceptable, search from longest to shortest string
# more than one link on an input line : repeat search for all strings
      $line = $_;
      chomp $line;
# only put up links in $i==2 normal text
      for ($j = $pattern-1 ;$i ==2  && $j>=0  ;$j--)
        {    
# same link more than once on same input line.
        $tail = $line;
#dont match stuff already either _link_name or >link_name
# make sure that what follows is not part of a word
        if ( $tail =~ m/\W$links[$j]\W/i )
          {
# got match          
            $targ = $links[$j];
            if(length $targ >= 2) 
              {
              $targ =~ s/ /_/g;
              $targ =~ s/_$//g;
              $line =~ s/(.*\W)($links[$j])(\W.*)/$1<a href=\".\/$targ.htm\">$links[$j]<\/a>$3/i;
#recycle tail to see if any more .
              $tail = $3;

              }
          }; 
        }

# print the output line       
      print OUTFILE $line;
      };
    };
  };





print INDEXFILE "</html>\n";
