#!/usr/local/bin/perl5
#
#   Copyright (C) 1996,1997,1998,1999,2000,2001
#   Associated Universities, Inc. Washington DC, USA.
#
#   This program is free software; you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by the Free
#   Software Foundation; either version 2 of the License, or (at your option)
#   any later version.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#   more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   675 Massachusetts Ave, Cambridge, MA 02139, USA.
#
#   Correspondence concerning AIPS++ should be addressed as follows:
#          Internet email: aips2-request@nrao.edu.
#          Postal address: AIPS++ Project Office
#                          National Radio Astronomy Observatory
#                          520 Edgemont Road
#                          Charlottesville, VA 22903-2475 USA
#
#   $Id$
#
use Getopt::Long;
use Cwd;

$/ = "";
$* = 1;
$mtext = "[^}]*";
$etext = "([^\}]*\{[^\}]*\}[^\}]*|[^\}]*)";
$wlabel = "\\subsubsection";
$tbflabel = '\\flushleft\\vspace{.20in}\\textbf';
$theText = "";
$args = "";
$end_section = "\\vspace{.18in}\n\\hrule";
   
   # OK first we make sure we can tokenize everything by adding an endaips2help
   # token after the \\end{ah}
$result = &GetOptions('glish', 'noheaders', 'package=s', 'outline', 'xml');
$pack = $opt_package;

# Note to self here is where the XML header stuff goes

print "\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\n";
print "\<?xml-stylesheet type=\"text/xsl\" href=\"file:\/\/\/Users\/wyoung\/casa\/interfaces\/casa2acsidl.xsl\"?\>\n";
print "\<myps xmlns=\"http:\/\/www.aoc.nrao.edu\/~wyoung\/psetTypes.html\"\n";
print "xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n";
print "xsi:schemaLocation=\"http:\/\/www.aoc.nrao.edu\/~wyoung\/psetTypes.html\n";
print "file:\/\/\/users\/wyoung\/casa\/interfaces\/psetdef.xsd\"\>\n";


foreach $file (@ARGV) {
   &fetch_raw_help($file, 'fh00', $pack);
   foreach $moduleN (split(/beginaips2module/, $theText)){
      $objectText = "";
      foreach $objectN (split(/beginaips2object/, $moduleN)){
         &make_tex_file($objectN);
         &term_object($firstPass);
         $objectText .= $_;
      }
      $isObj = 0;
      &term_module($objectText);
   }
}
print "<\/myps>\n";

sub fetch_raw_help{
   local($filename, $readme, $pack)  = @_;
   $readme++;
   open($readme, $filename);
   while(<$readme>){
      s/\\%/escapedpercent/g;
        # remove any comments that have ah commands or
        # environments in them.
      s/%.*begin.*ah.*//g;
      s/%.*\\ah.*//g;
      s/%%.*//g;
      s/escapedpercent/\\%/g;
      s/(\\begin{ahmodule})/beginaips2module $1/g;
      s/(\\begin{ahtool})/beginaips2object $1/g;
      s/(\\begin{ahobject})/beginaips2object $1/g;
      s/(\\begin{ahfunction})/beginaips2help $1/g;
      s/(\\begin{ahconstructor})/beginaips2help $1/g;
      s/(\\begin{ahrecord})/beginaips2help $1/g;
      s/(\\end{ahrecord})/beginaips2help $1/g;
      s/\\documentclass.*//g;
      s/\\usepackage.*//g;
      s/\\begin{document}//g;
      s/\\end{document}//g;

      if($opt_glish == 1){
        if(/\\ahpackage{($mtext)}/mg){
           $packd = "::d";
           print "help::pkg.$opt_package$packd := '$1';\n";
        }
      } else {
        s/\\ahpackage{($mtext)}/
        \\begin{htmlonly}
        \\htmladdnormallink{Postscript file available}{\\psgzfile}
        \\vspace{.10in}
        \\end{htmlonly}
        $1\\\\
        /g;
      }
      if(/\\input{($mtext)}/){
         foreach $inputfile(split(/\n/, $_)){
            if($inputfile =~ /\\input{(.*)}/){
               &fetch_raw_help($1, $readme, $pack); 
               $inputfile =~ s/\\input{(.*)}/$itext/g;
            }
         }
      }
      if(/\\include{($mtext)}/){
         foreach $inputfile(split(/\n/, $_)){
            if($inputfile =~ /\\input{(.*)}/){
               &fetch_raw_help($1, $readme, $pack); 
               $inputfile =~ s/\\include{(.*)}/$itext/g;
            }
         }
      }
      $theText .= $_;
   }
   close $readme;
   return;
}

sub term_module{
   local($theRawText) = @_;
   $_ = $theRawText;
#
   $objs_count = %objs;
   $objItems = "";
   if($objs_count > 0){
      @keys = sort (keys %objs);
      for($i=0;$i<@objsid;$i++){
          #$key = $objsid[$i];
          $key = $keys[$i];
          $objItems .= "\\htmlref{$key}{$mname:$key} & $objs{$key}\\\\\n";
          delete $objs{$key};
       }
#       s/\\ahobjs{}/
#\\flushleft
#\\textbf{Tools}\\\\
#\\begin{longtable}{ll}
#$objItems
#\\end{longtable}/g;
   } else {
       s/\\ahobjs{}//g;
   }

   $funs_count = %funcs;
   local $funItems = "";
   if($funs_count > 0){
   @keys = sort (keys %funcs);
#      foreach $key (keys %funcs) {
     for ($i=0; $i<@funcsid; $i++){
        $key = $keys[$i];
        # print $i, $key;
        $funItems .= "\\htmlref{$key}{$mname:$key} & $funcs{$key}\\\\\n";
        delete $funcs{$key};
      }
#      s/\\ahfuncs{}/
#\\textbf{Functions}
#\\begin{longtable}{ll}
#$funItems
#\\end{longtable}/g;
    }

#
   s/\\ahobjs{}//g;
   s/\\ahfuncs{}//g;
   s/\\ahmethods{}//g;
   if($opt_glish == 0 && $opt_outline == 0){
      print;
   }
   if($opt_outline != 1){
      print "\n";
   }
   return;
}

sub term_object{
   local($theRawText) = @_;
   $_ = $theRawText;
   $wlabel = "\\subsubsection";
   local $methodItems = "";
   local $constructorItems = "";

   if($has_methods == 1){
     
   @keys = sort (keys %{$methods{$oname}});
     for($i=0;$i<@{$methodsid{$oname}};$i++) {
        $key = $keys[$i];
        # print "Your are here ", $i, " ", $key, "\n";
        $methodItems .= "\\htmlref{$key}{$mname:$oname.$key.function} & $methods{$oname}{$key}\\\\\n";
     }
   @ckeys = sort (keys %{$constructors{$oname}});
     for($i=0;$i<@{$constructorsid{$oname}};$i++) {
        $key = $ckeys[$i];
        # print "Your are here ", $i, " ", $key, "\n";
        $constructorItems .= "\\htmlref{$key}{$mname:$oname.$key.constructor} & $constructors{$oname}{$key}\\\\\n";
     }
     #s/\\ahmethods{}/
   #\\flushleft
   #\\textbf{Constructors}\\\\
   #\\begin{longtable}{ll}
   #$constructorItems
   #\\end{longtable}
   #\\textbf{Functions}\\\\
   #\\begin{longtable}{ll}
   #$methodItems
   #\\end{longtable}/g;
     #s/\\ahfuncs{}/
   #\\flushleft
   #\\textbf{Constructors}\\\\
   #\\begin{longtable}{ll}
   #$constructorItems
   #\\end{longtable}
   #\\textbf{Functions}\\\\
   #\\begin{longtable}{ll}
   #$methodItems
   #\\end{longtable}/g;
   }
   $has_methods = 0;
   return;
}

sub make_tex_file{
   local($theRawText) = @_;   
   # Now we split the the text into several pieces we can handle
   
   $firstPass = "";
   foreach $thing (split(/beginaips2help/, $theRawText)){
      $_ = $thing;
      if(/\\begin{ahmodule}\s*{($mtext)}\s*{($mtext)}/){
         $isObj = 0;
         $needsSynopsis = 0;
         $terminateObject = 0;
         $mname = $1;
         $miname = $1;
         $miname =~ s/\\_/_/g;
         $wlabel = "\\subsection";
         $has_methods = 0;
         $iname = $mname;
         $dum = $2;
         $iname = $miname;
         undef @funcsid;
         undef @objsid;
         $psfile = "../$miname-Module.ps.gz";
         if($opt_glish == 1){
            print "$pack.$miname := [=];\n";
            print "$pack.$miname.objs := [=];\n";
            print "$pack.$miname.funs := [=];\n";
            $argroot ='';
            $dum =~ s/[\n\']/ /g;
            $dum =~ s/\\aipspp/AIPS++/g;
            print "$pack.$miname.d := '$dum';\n";}
          if($opt_outline == 1){
            print "\\>\\htmlref{$mname}{$mname}\\\\\n";
          }
      }
      if(/\\begin{ahrecord}\s*{($mtext)}/){
        $recname = "$1.";
      }
      if(/\\end{ahrecord}/){
        $recname = '';
      }
      if(/\\begin{ahaipsrc}/){
         $needsSynopsis = 0;
      }
      if(/\\begin{ahtool}\s*{($mtext)}\s*{($mtext)}/){
         $oname = $1;
         $needsSynopsis = 0;
         $terminateObject = 0;
         $objs{$1} = $2;
         push @objsid, $1;
         $isObj = 1;
         $wlabel = "\\subsubsection";
         $has_methods = 0;
         $iname = $1;
         $dum = $2;
         $iname =~ s/\\_/_/g;
         $oiname = $iname;
         $psfile = "../$oiname-Tool.ps.gz";
         if($opt_glish == 1){
            $oiname =~ s$/$_or_$g;
            print "$pack.$miname.objs.$oiname := [=];\n";
            print "$pack.$miname.objs.$oiname.m := [=];\n";
            $argroot ='';
            $argroot = "$pack.$miname.objs.$oiname.c";
            # $argroot =~ s$/$_or_$g;
            print "$argroot := [=];\n";
            $dum =~ s/[\n\']/ /g;
            $dum =~ s/\\aipspp/AIPS++/g;
            print "$pack.$miname.objs.$oiname.d := '$dum';\n";}
         #if($opt_outline == 1){
           #print "\\>\\>\\htmlref{$oname}{$mname:$oname}\\\\\n";
         #}
         undef @methodsid{$oname};
         undef @constructorsid{$oname};
         undef @constructors{$oname};
         undef @methods{$oname};
      }
      if(/\\begin{ahobject}\s*{($mtext)}\s*{($mtext)}/){
         $oname = $1;
         $needsSynopsis = 0;
         $terminateObject = 0;
         $objs{$1} = $2;
         push @objsid, $1;
         $isObj = 1;
         $wlabel = "\\subsubsection";
         $has_methods = 0;
         $iname = $1;
         $dum = $2;
         $iname =~ s/\\_/_/g;
         $oiname = $iname;
         $psfile = "../$oiname-Tool.ps.gz";
         if($opt_glish == 1){
            $oiname =~ s$/$_or_$g;
            print "$pack.$miname.objs.$oiname := [=];\n";
            print "$pack.$miname.objs.$oiname.m := [=];\n";
            $argroot ='';
            $argroot = "$pack.$miname.objs.$oiname.c";
            #$argroot =~ s$/$_or_$g;
            print "$argroot := [=];\n";
            $dum =~ s/[\n\']/ /g;
            $dum =~ s/\\aipspp/AIPS++/g;
            print "$pack.$miname.objs.$oiname.d := '$dum';\n";}
         #if($opt_outline == 1){
           #print "\\>\\>\\htmlref{$oname}{$mname:$oname}\\\\\n";
         #}
         undef @methodsid{$oname};
         undef @constructorsid{$oname};
         undef @constructors{$oname};
         undef @methods{$oname};
      }
      if(/\\begin{ahconstructor}\s*{($mtext)}\s*{($mtext)}/){
         $args = "";
         $needsSynopsis = 1;
         $constructors{$oname}{$1} = $2;
         push @{$constructorsid{$oname}}, $1;
         # print "Adding ", $1, @{$methodsid{$oname}}, "\n";
         $wlabel = "\\paragraph";
         $fname = $1;
         $iname = $1;
         $dum = $2;
         $iname =~ s/\\_/_/g;
         $argroot ='';
         $argroot = "$pack.$mname.objs.$oname.c.$iname";
         if($opt_glish == 1){
            $argroot = "$pack.$miname.objs.$oiname.c.$iname";
            print "$pack.$miname.objs.$oiname.c.$iname := [=];\n";
            $dum =~ s/[\n\']/ /g;
            $dum =~ s/\\aipspp/AIPS++/g;
            print "$pack.$miname.objs.$oiname.c.$iname.d := '$dum';\n";}
         #if($opt_outline == 1){
           #print "\\>\\>\\>\\htmlref{$fname}{$mname:$oname.$fname.constructor}\\\\\n";
         #}
      }
      $recIndent = "";
      if(/\\begin{ahrecord}/){
         $recIndent = "\\hspace{0.25in}";
      }
      if(/\\begin{ahfunction}\s*{($mtext)}\s*\{($etext)\}/){
         $needsSynopsis = 1;
         $args = "";
         $fname = $1;
         $iname = $1;
         $dum = $2;
         $iname =~ s/\\_/_/g;
         $argroot ='';
         if($isObj == 1){
            $has_methods = 1;
            $methods{$oname}{$fname} = $dum;
            push @{$methodsid{$oname}}, $fname;
            # print "Adding ", $fname, @{$methodsid{$oname}},"\n";
            $wlabel = "\\paragraph";
            $argroot = "$pack.$mname.objs.$oname.m.$iname";
            if($opt_glish == 1){
               $argroot = "$pack.$miname.objs.$oiname.m.$iname";
               print "$argroot := [=];\n";
               $dum =~ s/[\n\']/ /g;
               $dum =~ s/\\aipspp/AIPS++/g;
               print "$argroot.d := '$dum';\n"; }
             #if($opt_outline == 1){
                #print "\\>\\>\\>\\htmlref{$fname}{$mname:$oname.$fname.function}\\\\\n";
             #}
         }else{
            push @funcsid, $fname;
            $funcs{$fname} = $dum;
            $wlabel = "\\subsubsection";
            $argroot = "$pack.$mname.funs.$iname";
            if($opt_glish == 1){
               $argroot = "$pack.$miname.funs.$iname";
               print "$argroot := [=];\n";
               print "$argroot.a := [=];\n";
               $dum =~ s/[\n\']/ /g;
               $dum =~ s/\\aipspp/AIPS++/g;
               print "$argroot.d := '$dum';\n";}
             #if($opt_outline == 1){
                #print "\\>\\>\\htmlref{$fname}{$mname:$fname.function}\\\\\n";
             #}
         }
      } 
   
     # Build an argument list from addarg, used for synopsis
   
     $hasArgs = 0;
     $dtext = ".*[\s]*.*";
     s/&/\&amp\;/g;
     s/</\&lt\;/g;
     s/>/\&gt\;/g;

      # Changed dtext to mtext in the hopes everything will appear in the
      # synopsis now

     while(/\\ahaddarg[^\{]*{($mtext)}\s*{($mtext)}\s*{($mtext)}\s*{($mtext)}/mg){
       $args .= ($1.", ");
       $hasArgs = 1;
    foreach $inout ("in", "out", "inout", ""){
     s/\\ahaddarg\[($inout)\]{($mtext)}\s*{($mtext)}\s*{\s*}\s*{\s*}/
     <param xsi:type=\"unknown\" direction=\"$inout\" name=\"$2\">
     <description>$3<\/description>
     <\/param>/g;

     s/\\ahaddarg\[($inout)\]{($mtext)}\s*{($mtext)}\s*{}\s*{($mtext)}/
     <param xsi:type=\"unknown\" direction=\"$inout\" name=\"$2\">
     <description>$3<\/description>
     <choice>$4<\/choice>
     <\/param>/g;
   
     s/\\ahaddarg\[($inout)\]{($mtext)}\s*{($mtext)}\s*{($mtext)}\s*{}/
     <param xsi:type=\"unknown\" direction=\"$inout\" name=\"$2\">
     <description>$3<\/description>
     <value>$4<\/value>
     <\/param>/g;
   
     s/\\ahaddarg\[($inout)\]{($mtext)}\s*{($mtext)}\s*{($mtext)}\s*{($mtext)}/
     <param xsi:type=\"unknown\" direction=\"$inout\" name=\"$2\">
     <description>$3<\/description>
     <value>$5<\/value>
     <choice>$4<\/choice>
     <\/param>/g;
   }
     }
     if($hasArgs == 1){
        $args =~ s/, $//g;
     }

   
   # substitutions go here
   #  labels must use iname's otherwise things get confused

      if($isObj == 1){
         s/\\end{ahfunction}/<\/method>/g;
      } else {
         s/\\end{ahfunction}/<\/task>/g;
      }
   
      if($isObj == 1){
         s/\\begin{ahfunction}\s*{($mtext)}\s*{($mtext)}/
   <method type=\"function"\ name=\"$1\">
   <shortdescription>$2<\/shortdescription>
   /sg;
      } else {
         s/\\begin{ahfunction}\s*{($mtext)}\s*\{($etext)\}/
   <task name=\"$1\">
   <shortdescription>$2<\/shortdescription>
   /sg;
      } 

      if(/\\end{ahobject}/ || /\\end{ahtool}/|| /\\end{ahmodule}/){
         $isObj = 0;
      }
      s/\\end{ahobject}/<\/tool>/g;
      s/\\end{ahtool}/<\/tool>/g;
   
# Here we start the tool
      s/\\begin{ahobject}\s*{($mtext)}\s*{($mtext)}/
        <tool name=\"$1\" module=\"$mname\">
        <shortdescription>$2<\/shortdescription>
       /g;
      s/\\begin{ahtool}\s*{($mtext)}\s*{($mtext)}/
        <tool>
        <name>$1<\/name>
        <shortdescription>$2<\/shortdescription>
       /g;

      s/\\end{ahconstructor}/<\/method>/g;
      s/\\begin{ahconstructor}\s*{($mtext)}\s*{($mtext)}/
   <method type=\"constructor"\ name=\"$1\">
   <shortdescription>$2<\/shortdescription>
   /sg;
  
     s/\\end{ahmodule}/<\/module>/g;
   
      my $dir = cwd() . '/' . $file;
      $dir =~ s|/+|/|g;
      my @dirs = split (/\//, $dir);
      if ($#dirs > 2) {
	$dir = $dirs[$#dirs - 3];     #get package
      }
      my $name = $dirs[$#dirs];       #get tool name (is .help file)
      $name =~ s|\.[^.]+$||;          #remove extension

     s/\\begin{ahmodule}\s*{($mtext)}\s*{($mtext)}/
     <module name=\"$1\">
     <shortdescription>$2<\/shortdescription>/g;
   
     s/\\ahinclude{($mtext)}/include \"$1\"/g;
   
     s/\\ahcategory{($mtext)}//g;
     s/\\end{ahdescription}/<\/description>/g;
     s/\\begin{ahdescription}/<description>/g;
     s/\\end{ahrecord}/<\/record>/g;
     s/\\begin{ahrecord}{($mtext)}{($mtext)}/
       <record>
       <name>$1<\/name>
       <shortdescription>$2<\/shortdescription>/g;

     s/\\end{ahdata}/<\/publicdata>/g;
     s/\\begin{ahdata}/<publicdata>/g;
     s/\\ahdatum{($mtext)}\s*{($mtext)}\s*{}\s*{}/
       <param xsi:type=\"unknown\" name=\"$recname$1\">
       <name>$recname$1<\/name>
       <description>$2<\/description>
       <\/param>/g;
     s/\\ahdatum{($mtext)}\s*{($mtext)}\s*{($mtext)}\s*{}/
       <param xsi:type=\"unknown\" name=\"$recname$1\">
       <description>$2<\/description>
       <value>$3<\/value>
       <\/param>/g;
     s/\\ahdatum{($mtext)}\s*{($mtext)}\s*{}\s*{($mtext)}/
       <param xsi:type=\"unknown\" name=\"$recname$1\">
       <description>$2<\/description>
       <choice>$3<\/choice>
       <\/param>/g;
     s/\\ahdatum{($mtext)}\s*{($mtext)}\s*{($mtext)}\s*{($mtext)}/
       <param xsi:type=\"unknown\" name=\"$recname$1\">
       <description>$2<\/description>
       <value>$4<\/value>
       <choice>$3<\/choice>
       <\/param>/g;

     s/\\end{ahaipsrc}/<\/casarc>/g;
   
     s/\\begin{ahaipsrc}/<casarc>/g;
   
#
     s/\\end{ahargs}/<\/input>/g;
   
     s/\\begin{ahargs}/<input>/g;

     s/\\end{ahargslong}/<\/input>/g;
     s/\\begin{ahargslong}/<\/input>/g;
   
     s/\\ahaddarg{($mtext)}\s*{($mtext)}\s*{\s*}\s*{\s*}/
     <param xsi:type=\"unknown\" name=\"$1\">
     <name>$1<\/name>
     <description>$2<\/description>
     <\/param>/g;

     s/\\ahaddarg{($mtext)}\s*{($mtext)}\s*{}\s*{($mtext)}/
     <param xsi:type=\"unknown\" name=\"$1\">
     <description>$2<\/description>
     <choice>$3<\/choice>
     <\/param>/g;
   
     s/\\ahaddarg{($mtext)}\s*{($mtext)}\s*{($mtext)}\s*{}/
     <param xsi:type=\"unknown\" name=\"$1\">
     <description>$2<\/description>
     <value>$3<\/value>
     <\/param>/g;
   
     s/\\ahaddarg{($mtext)}\s*{($mtext)}\s*{($mtext)}\s*{($mtext)}/
     <param xsi:type=\"unknown\" name=\"$1\">
     <description>$2<\/description>
     <value>$4<\/value>
     <choice>$3<\/choice>
     <\/param>/g;
   
# input/output option   
    if( $hasArgs == 1) {
    foreach $inout ("in", "out", "inout", ""){
     s/\\ahaddarg\[($inout)\]{($mtext)}\s*{($mtext)}\s*{\s*}\s*{\s*}/
     <param xsi:type=\"unknown\" direction=\"$inout\" name=\"$2\">
     <description>$3<\/description>
     <\/param>/g;

     s/\\ahaddarg\[($inout)\]{($mtext)}\s*{($mtext)}\s*{}\s*{($mtext)}/
     <param xsi:type=\"unknown\" direction=\"$inout\" name=\"$2\">
     <description>$3<\/description>
     <choice>$4<\/choice>
     <\/param>/g;
   
     s/\\ahaddarg\[($inout)\]{($mtext)}\s*{($mtext)}\s*{($mtext)}\s*{}/
     <param xsi:type=\"unknown\" direction=\"$inout\" name=\"$2\">
     <description>$3<\/description>
     <value>$4<\/value>
     <\/param>/g;
   
     s/\\ahaddarg\[($inout)\]{($mtext)}\s*{($mtext)}\s*{($mtext)}\s*{($mtext)}/
     <param xsi:type=\"unknown\" direction=\"$inout\" name=\"$2\">
     <description>$3<\/description>
     <value>$5<\/value>
     <choice>$4<\/choice>
     <\/param>/g;
   }
   }
   
   
     s/\\ahsynopsis{($mtext)}/<synopsis>$1($args)<\/synopsis>/g;

     s/\\ahreturns{($mtext)}/<returns xsi:type=\"unknown\">$1<\/returns>/g;
   
     s/\\end{ahexample}/<\/example>/g;
     s/\\begin{ahexample}/<example>/g;
     s/\\end{ahseealso}/<\/seealso>/g;
     s/\\begin{ahseealso}/<seealso>/g;
     s/\\ahkeyword{($mtext)}\s*{($mtext)}/<keyword>$1<\/keyword>/g;
     s/\\ahkeyword{($mtext)}\s*{}/<keyword>$1<\/keyword>/g;
     s/\\ahkeyword{($mtext)}/<keyword>$1<\/keyword>/g;
     while(/\\ahlink{($mtext)}\s*{($mtext)}/){
        $thelabel = $2;
        if(($thelabel =~ m/function$/) || ($thelabel =~ m/constructor$/)){
           s/\\ahlink{($mtext)}\s*{($mtext)}/<link anchor=\"$2\">$1<\/link>/;
        } else {
           if( $thelabel =~ m/[:\.]/){
              @lfields = split(/[:\.]/, $thelabel);
              $nfields = @lfields;
              if($nfields > 2){
                 if( $lfields[$nfields-2] eq $lfields[$nfields-1] ){
                    s/\\ahlink{($mtext)}\s*{($mtext)}/<link anchor=\"$2.constructor\">$1<\/link>/;
                 } else {
                    s/\\ahlink{($mtext)}\s*{($mtext)}/<link anchor=\"$2.function\">$1<\/link>/;
                 }
              } else {
                 s/\\ahlink{($mtext)}\s*{($mtext)}/<link anchor=\"$2\">$1<\/link>/;
              }
           } else {
              s/\\ahlink{($mtext)}\s*{($mtext)}/<link anchor=\"$2\">$1<\/link>/;
           }
        }
     }
     s/\\end{ahcomments}/<\/comments>/g;
     s/\\begin{ahcomments}/<comments>/g;

     $firstPass .= $_;
   }

   return;
}
