########################################################################
# SGMLSPL script produced automatically by the script sgmlspl.pl
#
# Document Type: HACK
# Edited by: 
########################################################################

use strict;

use SGMLS;			# Use the SGMLS package.
use SGMLS::Output;		# Use stack-based output.

our $title = "";
our $url = "";
our $linktext = "";
our $listlevel = 0;
our @listtypes=();   # So $listtypes[$listlevel] is the type of the innermost
                     #    current list
our %htlist = ();    # So $htlist{$type} is the element name of the HTML list
$htlist{"MENU"} = "dl";
$htlist{"ENUMERATE"} = "ol";
$htlist{"ITEMIZE"} = "ul";
$htlist{"DESCRIPTION"} = "dl";

our @itemlabels = ();
our @itembodies = ();
#
# Document Handlers.
#
sgml('start', sub {
    $listlevel = 0;
});
sgml('end', sub {
    if ($listlevel != 0){
	print STDERR "***** WARNING: list level mismatch\n";
    };
});

#
# Element Handlers.
#

# Element: HACK
sgml('<HACK>', sub{
    my $os = "<!DOCTYPE HTML PUBLIC ";
    $os = $os . '"-//W3C//DTD HTML 4.01 Transitional//EN">';
    $os = $os . "\n<html>\n";
    output($os);
});
sgml('</HACK>', "</html>\n");

# Element: HEAD
sgml('<HEAD>', sub{
    $title = "";
});
sgml('</HEAD>', sub{
    my $os = "";
    my $parentname = $_[0]->parent->name;
    if($parentname eq "HACK"){
	$os = $os . "<head>\n<title>" . $title . "</title>\n</head>\n";
    }
    elsif($parentname eq "SECTION"){
	$os = $os . "<h2>" . $title . "</h2>\n";
    }
    else{
	print STDERR "*** WARNING: element HEAD found in bad location\n";
    };
    output($os);
});

# Element: TITLE
sgml('<TITLE>', sub{
    push_output('string');
});
sgml('</TITLE>', sub{
    $title = pop_output;
});

# Element: ACRO
sgml('<ACRO>', sub{
    push_output('string');
});
sgml('</ACRO>', sub{
    my $os = "";
    my $pos = pop_output;
    if($_[0]->within("BODY") ne ""){
	$os = "<abbr>" . $pos . "</abbr>";
    }
    else{
	$os = $pos;
    };
    output($os);
});

# Will also need this type of handling for BOLD, EMPH, and CODE
# Write a function to perform the if block
#
#  Example call from </ACRO> handler: onlyTagInBody($_[0], "abbr", $pos)
#
sub onlyTagInBody {
    my($elt, $tag, $string) = @_;
    my $rs;
    if($elt->within("BODY") ne ""){
	$rs = "<" . $tag . ">" . $string . "</" . $tag . ">";
    }
    else{
	$rs = $string;
    };
    return $rs;
};

# Element: BODY
sgml('<BODY>', sub{
    my $os = "<body>\n";
    $os = $os . "<h1>" . $title . "</h1>\n";
    output($os);
});
sgml('</BODY>', "</body>\n");

# Element: PARA
sgml('<PARA>', "<p>\n");
sgml('</PARA>', "</p>\n");

# Element: BOLD
sgml('<BOLD>', sub{
    push_output('string');
});
sgml('</BOLD>', sub{
    my $os = "";
    my $pos = pop_output;
    $os = onlyTagInBody($_[0], "strong", $pos);
    output($os);
});

# Element: BR
sgml('<BR>', "<br>");
sgml('</BR>', "");

# Element: SECTION
sgml('<SECTION>', sub{
    $title = "";
    my $os = "<div>\n";
    output($os);
});
sgml('</SECTION>', "</div>\n");

# Element: SECBODY
sgml('<SECBODY>', "");
sgml('</SECBODY>', "");

# Element: EMPH
sgml('<EMPH>', sub{
    push_output('string');
});
sgml('</EMPH>', sub{
    my $os = "";
    my $pos = pop_output;
    $os = onlyTagInBody($_[0], "em", $pos);
    output($os);
});

# Element: CODE
sgml('<CODE>', sub{
    push_output('string');
});
sgml('</CODE>', sub{
    my $os = "";
    my $pos = pop_output;
    $os = onlyTagInBody($_[0], "code", $pos);
    output($os);
});

sgml('<LINK>', sub{
    $url = "";
    $linktext = "";
});
sgml('</LINK>', sub{
    my $os = "<a href=\"" . $url . "\">" . $linktext . "</a>";
    output($os);
});

sgml('<URL>', sub{
    push_output('string');
});
sgml('</URL>', sub{
    $url = pop_output;
});

sgml('<LINKTEXT>', sub{
    push_output('string');
});
sgml('</LINKTEXT>', sub{
    $linktext = pop_output;
});

#
# Default handlers (uncomment these if needed).  Right now, these are set
# up to gag on any unrecognised elements, sdata, processing-instructions,
# or entities.
#
sgml('start_element', sub{
    print " *** UNKNOWN -" . $_[0]->name . "- ";
});
sgml('end_element', '*** ');
sgml('cdata',sub{
    my $os = $_[0];
    $os =~ s/\&/&amp;/;
    $os =~ s/</&lt;/;
    $os =~ s/>/&gt;/;
    output($os);
});
# sgml('sdata',sub { die "Unknown SDATA: " . $_[0]; });
sgml('re',"\n");
# sgml('pi',sub { die "Unknown processing instruction: " . $_[0]; });
# sgml('entity',sub { die "Unknown external entity: " . $_[0]->name; });
# sgml('start_subdoc',sub { die "Unknown subdoc entity: " . $_[0]->name; });
# sgml('end_subdoc','');
# sgml('conforming','');

# End of previous list "hack2html.pl"
# Skeleton function additions:
# Document Type: HACK

# Element: LIST
sgml('<LIST>', sub{
    $listlevel++;
    $listtypes[$listlevel] = "";
    my $os = "";
    my $type = "";
    my ($elt, $ev) = @_;
    if($elt->attribute("TYPE")->value ne ""){
	$type = $elt->attribute("TYPE")->value;
	$listtypes[$listlevel] = $type;
	$os = "<" . $htlist{$type} . ">";
    }
    else{
	print STDERR "*** WARNING: list at level $listlevel has no type\n";
    };
    output($os);
});
sgml('</LIST>', sub{
    my $os = "";
    my $type = $listtypes[$listlevel];
    $os = "</" . $htlist{$type} . ">";
    output($os);
    $listlevel--;
});

# Element: ITEM
sgml('<ITEM>', sub{
    $itemlabels[$listlevel] = "";
    $itembodies[$listlevel] = "";
    push_output('string');
});
sgml('</ITEM>', sub{
    my $os = "";
    my $pos = pop_output;
    my $type = $listtypes[$listlevel];
    if($type eq "MENU"){
	$os = $os . "<dd>\n";
	if($itemlabels[$listlevel] ne ""){
	    $os = $os . $itemlabels[$listlevel] . ": ";
	};
	$os = $os . $itembodies[$listlevel];
	$os = $os . "</dd>\n";
    }
    elsif($type eq "DESCRIPTION"){
	$os = $os . "<dt>" . $itemlabels[$listlevel] . "</dt>";
	$os = $os . "<dd>\n" . $itembodies[$listlevel] . "</dd>\n";
    }
    elsif($type eq "ENUMERATE"){
	$os = $os . "<li>\n";
	if($itemlabels[$listlevel] ne ""){
	    $os = $os . " " . $itemlabels[$listlevel] . ": ";
	};
	$os = $os . $itembodies[$listlevel];
	$os = $os . "</li>\n";
    }
    elsif($type eq "ITEMIZE"){
	$os = $os . "<li>\n";
	if($itemlabels[$listlevel] ne ""){
	    $os = $os . " " . $itemlabels[$listlevel] . ": ";
	};
	$os = $os . $itembodies[$listlevel];
	$os = $os . "</li>\n";
    }
    else{
	print STDERR "*** WARNING: unknown list type \"$type\"\n";
    };
	output($os);
});

# Element: ITEMLABEL
sgml('<ITEMLABEL>', sub{
    push_output('string');
});
sgml('</ITEMLABEL>', sub{
    my $pos = pop_output;
    if($pos ne ""){
	$itemlabels[$listlevel] = $pos;
    };
});

# Element: ITEMBODY
sgml('<ITEMBODY>', sub{
    push_output('string');
});
sgml('</ITEMBODY>', sub{
    $itembodies[$listlevel] = pop_output;
});

# Element: IPAR
sgml('<IPAR>', "<p>");
sgml('</IPAR>', "</p>");

1;
