
#!/usr/local/bin/perl
# $Id: docyacc.pl,v 1.1.1.1 2003/11/04 23:34:58 mjames Exp $
# $Log: docyacc.pl,v $
# Revision 1.1.1.1  2003/11/04 23:34:58  mjames
# Imported into local repositrory
#
# Revision 1.4  2002/09/30 13:28:02  MJAMES
# Tidy up
#
# Revision 1.1.1.1  2002/09/09 15:06:16  mjames
# no message
#
# Revision 1.3  2001/10/31 22:20:03  mjames
# Tidying up problematical comments caused by CVS
# 'intelligent' comment guessing
#
# Revision 1.2  2001/10/07 20:51:31  mjames
# Modified Perl command to generate better formed HTML
#
# Revision 1.1  2001/10/02 20:47:41  mjames
# Yacc documentation generator : produces HTML 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]}";


print "<html>\n";
print "<title>Index of ${ARGV[0]}</title>\n";
print "<h1>Index of ${ARGV[0]}</h1>\n";
print "Key objects are here\n" ;
print "<a name=\"top\"><table>\n";

# loop 1 : index all of the entries

$i=0;
$tag = 0;
$patterns = 0;
while ( <INFILE> ) {
# should be more escapes here.
  s/</\&lt;/g;

  $targ = $_;
  chomp $targ;
  if ($targ =~ s/^([a-zA-Z0-9_]+)\s*\:(.*)/$1/ )
    {
    $rest = $2;
    $link = $1;
    $link =~ s/ $//;
# remember the text of the link for future 
    $links[$pattern++] = $link;      
    $targ =~ s/ /_/g;
    $targ =~ s/_$//g;
    print "<tr><td><a href=\"\#\_$targ\_\">$link</a><td>$rest</tr>\n";
    }; 
  };

print "</table><br><hr><br>";


sort @links;


close INFILE;
open INFILE,"<${ARGV[0]}";

print "<hr><pre>";

while ( <INFILE> ) {

# should be more escapes here.
  s/&/\&ampersand;/g;
  s/</\&lt;/g;

    $line = $_;
    chomp $line;
    $targ = $line;
#detect anchor target
    if ($targ =~ s/^([a-zA-Z0-9_]+)\s*\:(.*)/$1/ ) 
      {
      $patt = $1;
#make spaces into underscores
      $targ =~ s/ /_/g;
#kill trailing underscore
      $targ =~ s/_$//;
      
      $line =~ s/$patt/<a href=\"\#top\">Top of Document<\/a><hr><a name=\"\_$targ\_\">$patt/;
      }; 

#very slow but acceptable, search from longest to shortest string
# more than one link on an input line : repeat search for all strings
   for ($j=$pattern-1 ;$j>= 0;$j--)
      {
      $tail = $line;    
#miss out any already anchored text and get maximal match
# more than one match for the same link on the same line ?

      if ( $tail =~ m/[^>_]$links[$j]\W+/ )
        {
# got match          
        $targ = $links[$j];
        if(length $targ >= 2) 
          {
          $targ =~ s/ /_/g;
          $targ =~ s/_$//g;
          $line =~ s/(.*[^>_])($links[$j])(\W+.*)/$1<a href=\"\#\_$targ\_\">$links[$j]<\/a>$3/;
          $tail = $3;
          }
        } 
      }

# print the output line       
   print "<br>$line\n";
   };



print "</pre>\n";

print "</html>\n";
