
#############
# FUNCTIONS #
#############

my $sect_level = 0;
my $sect_1_count = 0;
my $sect_2_count = 0;
my $sect_3_count = 0;
my $do_print = 1;

my $toc_level = 0;
my $toc_1_count = 0;
my $toc_2_count = 0;

sub handle_start {
    my $p = shift;                       # This is the reference to the parser object.
                                         # We don't use it directly, but I thought
                                         # you'd like to know anyway.
    my $el = shift;                      # ooh! This is the name of the element the
                                         # parser found.

    my %attribs = @_;                    # Everything else that's passed are name/value
                                         # pairs. Since a hash is really an array, we can
                                         # pull them into a hash. neat!

    # Here is where we start identifying each tag, and printing something.
    if ($el =~ /\bfaq\b/i) {             # <faq> is the root tag, so we replace it with 
                                         # <html>
	print "<html>";
    } elsif ($el =~ /\bbook\b/i) {
	print "<head>\n";
	foreach my $attrib (keys(%attribs)) {
	    if ($attrib =~ /\btitle\b/i) {
		print "<title>\n$attribs{$attrib}\n</title>\n";
	    }
	}
	print "</head>\n<body>\n";

    } elsif ($el =~ /\bchapter\b/i) {     # the <chapter> tag
	$sect_level = 0;
	$sect_1_count = 0;
	# we should only do the next stuff if we are sure
	# we are standalone and not part of a book
	print "<head>\n";
	foreach my $attrib (keys(%attribs)) {
	    if ($attrib =~ /\btitle\b/i) {
		print "<title>\n$attribs{$attrib}\n</title>\n";
	    }
	}
	print "</head>\n<body>\n";

    } elsif ($el =~ /\babstract\b/i) {  # the <abstract> tag
	print "<blockquote>\n<em>\n";
    } elsif ($el =~ /\bsect1\b/i) {     # the <sect1> tag
	$sect_level = 1;
	$sect_2_count = 0;
    } elsif ($el =~ /\bsect2\b/i) {     # the <sect2> tag
	$sect_level = 2;
	$sect_3_count = 0;
    } elsif ($el =~ /\bsect3\b/i) {     # the <sect3> tag
	$sect_level = 3;
    } elsif ($el =~ /\btitle\b/i) {      # the <title> tag
	print "<a";
	foreach my $attrib (keys(%attribs)) {
	    if ($attrib =~ /\bname\b/i) {
		print " name = \"$attribs{$attrib}\"";
	    }
	}
	print ">\n";

	if ($sect_level == 0) {
	    print "<h1 align=\"center\">";
	} elsif ($sect_level == 1) {
	    $sect_1_count++;
	    # print "<a name=\"section_$sect_1_count\">\n";
	    print "<h2> $sect_1_count. ";
	} elsif ($sect_level == 2) {
	    $sect_2_count++;
	    # print "<a name=\"subsection_$sect_1_count.$sect_2_count\">\n";
	    print "<h3> $sect_1_count.$sect_2_count ";
	} elsif ($sect_level == 3) {
	    $sect_3_count++;
	    # print "<a name=\"subsection_$sect_1_count.$sect_2_count.$sect_3_count\">\n";
	    print "<h4> $sect_1_count.$sect_2_count.$sect_3_count ";
	}

    ## Table of contents stuff
    } elsif ($el =~ /\btocchap\b/i) {     # the <tocchap> tag
	print "<blockquote>\n";
    } elsif ($el =~ /\btoclevel1\b/i) {     # the <toclevel1> tag
        # use HTML list rather our own
	print "<ol>\n";
    } elsif ($el =~ /\btoclevel2\b/i) {     # the <toclevel2> tag
        # use HTML list rather our own
	print "<ol>\n";
    } elsif ($el =~ /\btocentry\b/i) {      # the <tocentry> tag
	print "<li>";

    ## info stuff
    } elsif ($el =~ /\bauthor\b/i) {     # the <author> tag
	print "<h2 align=\"center\">";
    } elsif ($el =~ /\brevnumber\b/i) {    # the <version number> tag
	print "<h3 align=\"center\">Version ";
    } elsif ($el =~ /\bdate\b/i) {    # the date revised tag
	print "<h3 align=\"center\">";

    ## other stuff
    } elsif ($el =~ /\bpara\b/i) {  # the <para> tag
	print "<p>";
    } elsif ($el =~ /\bgraphic\b/i) {  # the <graphic> tag
	print "<center>\n<img ";
	foreach my $attrib (keys(%attribs)) {
	    if ($attrib =~ /\bfileref\b/i) {
		print " src = \"$attribs{$attrib}\"";
	    } else {
		print " $attrib = \"$attribs{$attrib}\"";
	    }
	}
	print ">\n</center>";

    } elsif ($el =~ /\bprogramlisting\b/i) {  # the <program listing> tag
	print "<pre><code>";
    } elsif ($el =~ /\bfuncsynopsis\b/i) {  # the <funcsynopsis> tag
	print "<pre><code>";
    } elsif ($el =~ /\bitemizedlist\b/i) {
	print "<ul>\n";
    } elsif ($el =~ /\borderedlist\b/i) {
	print "<ol>\n";
    } elsif ($el =~ /\blistitem\b/i) {
	print "<li>";
    } elsif ($el =~ /\bemphasis\b/i) {
	print "<em>";
    } elsif ($el =~ /\bulink\b/i) {
	print "<a ";
	foreach my $attrib (keys(%attribs)) {
	    if ($attrib =~ /\burl\b/i) {
		print " href = \"$attribs{$attrib}\"";
	    } else {
		print " $attrib = \"$attribs{$attrib}\"";
	    }
	}
	print ">";
    } else {                             # for all unrecognized tags, we simply print them.
                                         # I did this so I can easily include HTML tags.
                                         # One problem is that it doesn't print the attributes
	                                 # in order, but that's ok here.
	print "<$el";
	foreach my $attrib (keys(%attribs)) {
	    print " $attrib = \"$attribs{$attrib}\"";
	}
	print ">";
    }
}

# here we handle all non-tag strings. All we need to do here is print whatever is passed
sub handle_char {
    my ($p, $data) = @_;
    my $tag = $p -> current_element;
    # The headings stuff later needs to have all stuff in section title
    # on one line, so we stuff around here in order to do that - gross
    #if ($tag =~ /\btitle\b/) {
	#print "Title tag\n";
	#if ($data eq "\n") {
	    # print "empty\n";
	#} else {
	 #   print $data;
	#}
    #} else {
	#print $data;
    #}
    print $data;
}

# here we handle ending tags.
sub handle_end {
    my $p = shift;
    my $el = shift;
    my %atrribs = @_;

    if ($el =~ /\bfaq\b/i) {
	print "</html>";
    } elsif ($el =~ /\bbook\b/i) {

    } elsif ($el =~ /\btitle\b/i) {     # the <title> tag
	if ($sect_level == 0) {
	    print "</h1>";
	} elsif ($sect_level == 1) {
	    print "</h2>\n";
	    # print "</a>\n";
	} elsif ($sect_level == 2) {
	    print "</h3>\n";
	    # print "</a>\n";
	} elsif ($sect_level == 3) {
	    print "</h4>\n";
	    # print "</a>\n";
	}
	print "</a>";
    } elsif ($el =~ /\btocchap\b/i) {     # the <tocchap> tag
	print "</blockquote>\n";
    } elsif ($el =~ /\btocentry\b/i) {      # the <tocentry> tag
	print "</li>";
    } elsif ($el =~ /\btoclevel1\b/i) {     # the <toclevel1> tag
	print "</ol>\n";
    } elsif ($el =~ /\btoclevel2\b/i) {     # the <toclevel2> tag
	print "</ol>\n";
    } elsif ($el =~ /\bauthor\b/i) {        # the <author> tag
	print "</h2>\n";
    } elsif ($el =~ /\brevnumber\b/i) {        # the <revnumber> tag
	print "</h3>\n";
    } elsif ($el =~ /\bdate\b/i) {        # the <date> tag
	print "</h3>\n";
     } elsif ($el =~ /\bpara\b/i) {        # the <para> tag
	print "</p>\n";
    } elsif ($el =~ /\bprogramlisting\b/i) {  # the <programlisting> tag
	print "</code></pre>\n";
    } elsif ($el =~ /\bfuncsynopsis\b/i) {  # the <funcsynopsis> tag
	print "</code></pre>\n";
     } elsif ($el =~ /\bitemizedlist\b/i) {  # the <itemizedlist> tag
	print "</ul>\n";
     } elsif ($el =~ /\borderedlist\b/i) {  # the <orderelist> tag
	print "</ol>\n";
     } elsif ($el =~ /\blistitem\b/i) {  # the <listitem> tag
	print "</li>\n";
    } elsif ($el =~ /\bemphasis\b/i) {  # the <emphasis> tag
	print "</em>";
    } elsif ($el =~ /\bsect1\b/i) {         # the <sect1> tag

    } elsif ($el =~ /\bsect2\b/i) {         # the <sect2> tag

    } elsif ($el =~ /\bsect3\b/i) {         # the <sect3> tag

    } elsif ($el =~ /\bchapter\b/i) {        # the <chapter> tag

    } elsif ($el =~ /\babstract\b/i) {        # the <abstract> tag
	print "</em>\n</blockquote>\n";
    } elsif ($el =~ /\bulink\b/i) {        # the <ulink> tag
	print "</a>";
    } else {
	print "</$el>";
    }
}

# here we handle document type.
sub handle_doc_type {
    my $p = shift;
    my $name = shift;

    print "<html>\n";
#<head>\n\
#</head>\n\
#<body>\n";
}

sub handle_final {
    print "</body>\n</html>\n";
}

sub handle_extern_ent {
    my  ($p, $base, $sysid, $pubid) = @_;
    open(INPUT, $sysid) or return "can't open external file $sysid";
    my $extern = "";
    while (<INPUT>) {
	$extern .= $_;
    }
    close INPUT;
    return $extern;
}

sub handle_proc {
    my ($p, $target, $data) = @_;
    my $anchor;
    my $heading;
    my $in_name = "";

    $data =~ s/\"//g;
    # print "opening $data\n";
    open(INPUT, $data) or print "cant open $data\n";
    if ($target =~ /program/) {
	# sanitize awkward characters
	while (<INPUT>) {
	    $_ =~ s/\&/\&amp;/g;
	    $_ =~ s/</\&lt;/g;
	    $_ =~ s/>/\&gt;/g;
	    print $_;
	}
    }
    close(INPUT);
}
