<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#
#  Html2Html.pm
#
#  $Id: Html2Html.pm,v 1.7 1998/03/20 11:26:44 cg Exp $
#
#  Convert parsed linuxdoc-sgml to html.
# 	- Split files; match references, generate TOC and navigation
# 	aids, etc.
#
#  Rules based on html2html.l
#
package SGMLTools::Html2Html;

use FileHandle;
use SGMLTools::Lang;

# Externally visible variables
$html2html = {};

# Initialize: set splitlevel, extension, images, language, and filename
# Usage: &amp;{$html2html-&gt;{init}}(split,ext,img,filename,filenum,label);
#       split level:	 0 - super page mode
#        		 1 - big page mode
#        		 2 - small page mode
$html2html-&gt;{init} = sub {
    $splitlevel = shift;
    SWITCH: {
        $super_page_mode = 0, $big_page_mode = 1, last SWITCH
            if ($splitlevel == 1);
        $super_page_mode = 0, $big_page_mode = 0, last SWITCH
            if ($splitlevel == 2);
    }
    $fileext = shift;
    $use_imgs = shift;
    $firstname = shift;
    $filecount = 1 + shift;
    $lprec = shift;
    $nextlabel = Xlat ("Next");
    $prevlabel = Xlat ("Previous");
    $toclabel = Xlat ("Contents");
};

# Package variables
$big_page_mode = 0;             # '-2' subsection splitting
$super_page_mode = 1;		# One page vs. page/section
$chapter_mode = 0;              # &lt;article&gt; vs. &lt;report&gt;
$current = "";                  # State of section/subsection/etc.
$filenum = 1;                   # Current output file number
$filecount = 1;
$firstname = "$$";              # Base name for file
$headbuf = "";                  # Buffer for URL's
$fileext = "html";       	# "html" vs. "htm" for 8.3
$in_appendix = 0;               # n.n vs. a.n section numbers
$in_section_list = 0;           # List of sections flag
$language = "";                 # Default English; use '-Lname'
# $lprec{label}                 # Label record
$nextlabel = "";		# Link string
$outfh = STDOUT;		# Output filehandle
$outname = "";			# Output file name
$prevlabel = "";		# Link string
$refname = "";                  # Ref string
$sectname = "";                 # Section name
$secnr = 0;                     # Section count
$ssectname = "";                # Subsection name
$ssecnr = 0;                    # Subsection count
$skipnewline = 0;               # Flag to ignore new line
$toclabel = "";			# Link string
$titlename = "";                # Title of document
$use_imgs = 0;                  # '-img' pictorial links
$urlname = "";                  # Name for url links

# Ruleset
$html2html-&gt;{rules} = {};		# Individual parsing rules

$html2html-&gt;{rules}-&gt;{'^&lt;@@appendix&gt;.*$'} = sub {
    $in_appendix = 1; $secnr = 0; $ssecnr = 0;
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@url&gt;(.*)$'} = sub {
    $skipnewline = 1; $urlname = $1; $headbuf = qq(&lt;A HREF="$1"&gt;);
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@urlnam&gt;(.*)$'} = sub { 
    $headbuf = $headbuf . "$urlname&lt;/A&gt;"; 
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@endurl&gt;.*$'} = sub {
    $skipnewline = -1; $outfh-&gt;print($headbuf); $headbuf = "";
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@title&gt;(.*)$'} = sub {
    $titlename = $1; &amp;heading(STDOUT); print(STDOUT "&lt;H1&gt;$1&lt;/H1&gt;\n\n");
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@head&gt;(.*)$'} = sub { 
    $skipnewline = 1; $headbuf = $1; 
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@part&gt;.*$'} = sub { $current = "PART"; };

$html2html-&gt;{rules}-&gt;{'^&lt;@@endhead&gt;.*$'} = sub {
    SWITCH: {
    $outfh-&gt;print("&lt;H1&gt;$headbuf&lt;/H1&gt;\n\n"), last SWITCH 
        if ($current eq "PART");
    $outfh-&gt;print("&lt;H1&gt;$headbuf&lt;/H1&gt;\n\n"), last SWITCH 
        if ($current eq "CHAPTER");
    $outfh-&gt;print("&lt;H2&gt;$headbuf&lt;/H2&gt;\n\n"), last SWITCH 
        if ($current eq "SECTION");
    $outfh-&gt;print("&lt;H2&gt;$headbuf&lt;/H2&gt;\n\n"), last SWITCH 
        if ($current eq "SUBSECT");
    $outfh-&gt;print("&lt;H3&gt;$headbuf&lt;/H3&gt;\n\n"), last SWITCH;
    }
    $current = ""; $headbuf = ""; $skipnewline = 0;
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@chapt&gt;(.*)$'} = sub {
    $chapter_mode = 1; $skipnewline = 1; $sectname = $1;
    &amp;start_chapter($sectname);
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@sect&gt;(.*)$'} = sub {
    $skipnewline = 1; $ssectname = $1;
    if ($chapter_mode) {
        &amp;start_section($ssectname);
    } else {
        $sectname = $ssectname; &amp;start_chapter($ssectname);
    }
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@ssect&gt;(.*)$'} = sub {
    $skipnewline = 1; $ssectname = $1;
    if (!$chapter_mode) {
        &amp;start_section($ssectname);
    } else {
        $current = ""; $headbuf = $ssectname;
    }
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@endchapt&gt;.*$'} = sub {
    STDOUT-&gt;print("&lt;/UL&gt;\n") if ($in_section_list);
    if ($outfh-&gt;fileno != STDOUT-&gt;fileno) {
        &amp;footing($outfh); $outfh-&gt;close; $outfh = STDOUT;
    }
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@endsect&gt;.*$'} = sub {
    STDOUT-&gt;print("&lt;/UL&gt;\n") if (!$chapter_mode &amp;&amp; $in_section_list);
    if (($outfh-&gt;fileno != STDOUT-&gt;fileno) 
           &amp;&amp; ((!$chapter_mode) || (!$big_page_mode))) {
        &amp;footing($outfh); $outfh-&gt;close; $outfh = STDOUT;
    }
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@endssect&gt;.*$'} = sub {
    if (($outfh-&gt;fileno != STDOUT-&gt;fileno) 
           &amp;&amp; (!$chapter_mode) &amp;&amp; (!big_page_mode)) {
        &amp;footing($outfh); $outfh-&gt;close; $outfh = STDOUT;
    }
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@enddoc&gt;.*$'} = sub { };

$html2html-&gt;{rules}-&gt;{'^&lt;@@label&gt;(.*)$'} = sub {
    if (!defined($lprec-&gt;{$1})) {
        STDERR-&gt;print(qq(html2html: Problem with label "$1"\n)); next;
    }
    if ($skipnewline) {
        $headbuf = sprintf(qq(&lt;A NAME="%s"&gt;&lt;/A&gt; %s), $1, $headbuf);
    } else {
        $outfh-&gt;print(qq(&lt;A NAME="$1"&gt;&lt;/A&gt; ));
    }
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@ref&gt;(.*)$'} = sub {
    my $tmp;

    $refname = $1;
    if (!defined($lprec-&gt;{$1})) {
        STDERR-&gt;print(qq(html2html: Problem with ref "$1"\n));
        $skipnewline++; next;
    }
    SWITCH: {
    $tmp = qq(&lt;A HREF="#$1"&gt;), last SWITCH 
        if ($lprec-&gt;{$1} == $filenum - 1);
    $tmp = qq(&lt;A HREF="$firstname.$fileext#$1"&gt;), last SWITCH
        if ($lprec-&gt;{$1} == 0);
    $tmp = qq(&lt;A HREF="$firstname-$lprec-&gt;{$1}.$fileext#$1"&gt;),
            last SWITCH;
    }
    if ($skipnewline) {
        $headbuf = "$headbuf$tmp";
    } else {
        $headbuf = $tmp;
    }
    $skipnewline++;
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@refnam&gt;.*$'} = sub { 
    $headbuf = "$headbuf$refname&lt;/A&gt;\n"; 
};

$html2html-&gt;{rules}-&gt;{'^&lt;@@endref&gt;.*$'} = sub {
    if ($skipnewline == 1) {
        $outfh-&gt;print($headbuf); $skipnewline = -1;
    } elsif ($skipnewline == 2) {
        $skipnewline--;
    } else {
        STDERR-&gt;print("html2html: Problem with endref\n");
        $skipnewline--;
    }
};

# Default parsing rule
$html2html-&gt;{defaultrule} = sub {
    $skipnewline++ if ($skipnewline &lt; 0);
    if ($skipnewline) {
        chop; $headbuf = "$headbuf$_";
    } else {
        $outfh-&gt;print($_);
    }
};

# Finalize parsing process
$html2html-&gt;{finish} = sub {
    # Finish footers
    if ($outfh-&gt;fileno != STDOUT-&gt;fileno) {
        &amp;footing($outfh);
        $outfh-&gt;close;
    }
    # Finish the TOC; ensure "next" points to the first page.
    &amp;browse_links(STDOUT, 1, 0);
    print STDOUT "&lt;/BODY&gt;\n&lt;/HTML&gt;\n";
};


###################################################################
# Secondary Functions
###################################################################

# Print standard links
sub browse_links {
    my ($outfh, $myfilenum, $top) = @_;

    return if ($super_page_mode);

    $outfh-&gt;print("&lt;HR&gt;\n") unless ($top);

    # NOTE: For pages where a next or prev button isn't appropriate, include
    # the graphic anyway - just don't make it a link. That way, the mouse
    # position of each button is unchanged from page to page.
    # Use the passed myfilenum since filenum may already be incremented

    # Next link (first)
    my $next = $use_imgs  
                  ? qq(&lt;IMG SRC="next.gif" ALT="$nextlabel"&gt;)
                  : qq($nextlabel);
    $next = qq(&lt;A HREF="$firstname-$myfilenum.$fileext"&gt;$next&lt;/A&gt;)
       if ($myfilenum &lt; $filecount);
    $next = join "", $next, "\n";
    $outfh-&gt;print($next);

    # Previous link
    my $prev = $use_imgs  
                  ? qq(&lt;IMG SRC="prev.gif" ALT="$prevlabel"&gt;)
                  : qq($prevlabel);
    $prev = join "", qq(&lt;A HREF="$firstname-), ($myfilenum - 2),
                     qq(.$fileext"&gt;$prev&lt;/A&gt;)
       if ($myfilenum &gt;= 3);
    $prev = join "", $prev, "\n";
    $outfh-&gt;print($prev);

    # Table of contents link
    my $toc = $use_imgs 
                ? qq(&lt;IMG SRC="toc.gif" ALT="$toclabel"&gt;)
                : qq($toclabel);
    $toc = join "", qq(&lt;A HREF="$firstname.$fileext#toc),
                    &amp;section_num($secnr, 0), qq("&gt;$toc&lt;/A&gt;)
       if ($outfh-&gt;fileno != STDOUT-&gt;fileno);
    $toc = join "", $toc, "\n";
    $outfh-&gt;print($toc);

    print($outfh "&lt;HR&gt;\n") if ($top);
}

# Print end-of-file markup
sub footing {
    my $outfh = shift;
    &amp;browse_links($outfh, $filenum, 0);
    $outfh-&gt;print("&lt;/BODY&gt;\n&lt;/HTML&gt;\n");
}

# Print top-of-file markup
sub heading {
    my $outfh = shift; my $match;

    # Emit 3.2 HTML until somebody comes up with a better idea - CdG
    $outfh-&gt;print(
        qq(&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"&gt;\n));
    $outfh-&gt;print("&lt;HTML&gt;\n&lt;HEAD&gt;\n");
    open VERSFILE, "&lt;$main::LibDir/VERSION" or die "Cannot open version file\n";
    $version = &lt;VERSFILE&gt;;
    close VERSFILE;
    chop $version;
    $outfh-&gt;print(
      " &lt;META NAME=\"GENERATOR\" CONTENT=\"SGML-Tools $version\"&gt;\n");

    $outfh-&gt;print(" &lt;TITLE&gt;");
    $match = $titlename;
    $match =~ s/&lt;[^&gt;]*&gt;//g;
    $outfh-&gt;print($match);
    if ($secnr &gt; 0) {
        $match = $sectname;
        $match =~ s/&lt;[^&gt;]*&gt;//g;
        $outfh-&gt;print(": $match");
    }
    if ($ssecnr &gt; 0) {
        $match = $ssectname;
        $match =~ s/&lt;[^&gt;]*&gt;//g;
        $outfh-&gt;print(": $match");
    }
    $outfh-&gt;print("&lt;/TITLE&gt;\n");

    #
    #  &lt;LINK&gt; Information for next, previous, contents, etc...
    #
    $outfh-&gt;print(qq( &lt;LINK HREF="$firstname-$filenum.$fileext" REL=next&gt;),"\n")
       if ($filenum &lt; $filecount);
    my $prev;
    $prev = join "", qq( &lt;LINK HREF="$firstname-), ($filenum - 2),
                     qq(.$fileext" REL=previous&gt;)
	if ($filenum &gt;= 3);
    $outfh-&gt;print($prev,"\n");

    #
    #  Table of contents link
    #
    my $toc ;
    $toc = join "", qq( &lt;LINK HREF="$firstname.$fileext#toc),
                    &amp;section_num($secnr, 0), qq(" REL=contents&gt;)
	if ($outfh-&gt;fileno != STDOUT-&gt;fileno);
    $outfh-&gt;print($toc,"\n");

    $outfh-&gt;print("&lt;/HEAD&gt;\n&lt;BODY&gt;\n");
    &amp;browse_links($outfh, $filenum, 1);
}

# Return the section and subsection as a dotted string
sub section_num {
    my ($sec, $ssec) = @_;
    my $l = "A";

    if ($in_appendix) {
        $sec--;
        while ($sec) { $l++; $sec--; }
        return("$l.$ssec") if ($ssec &gt; 0);
        return("$l");
    } else {
        return("$sec.$ssec") if ($ssec &gt; 0);
        return("$sec");
    }
}

# Create a chapter head; start a new file, etc.
sub start_chapter {
    my $sectname = shift;

    if (!$super_page_mode &amp;&amp; $outfh-&gt;fileno != STDOUT-&gt;fileno) {
        &amp;footing($outfh); $outfh-&gt;close;
    }
    $current = "SECTION"; $secnr++; $ssecnr = 0;
    if ($super_page_mode) {
        $headbuf = sprintf(qq(&lt;A NAME="s%s"&gt;%s. %s&lt;/A&gt;),
                           &amp;section_num($secnr, 0), &amp;section_num($secnr, 0),
                           $sectname);
    } else {
        $outname = "$firstname-$filenum.$fileext"; $filenum++;
        $outfh = new FileHandle "&gt;$outname"
            or die qq(html2html: Fatal: Could not open file "$outname"\n);
        &amp;heading($outfh);
        $headbuf = sprintf(qq(&lt;A NAME="s%s"&gt;%s. %s&lt;/A&gt;),
                           &amp;section_num($secnr, 0), &amp;section_num($secnr, 0),
                           $sectname);
        STDOUT-&gt;printf(
          qq(&lt;P&gt;\n&lt;H2&gt;&lt;A NAME="toc%s"&gt;%s.&lt;/A&gt; &lt;A HREF="%s"&gt;%s&lt;/A&gt;&lt;/H2&gt;\n\n),
               &amp;section_num($secnr, 0), &amp;section_num($secnr, 0),
               $outname, $sectname);
        $in_section_list = 0;
    }
}

# Create a section; start a new file, etc.
sub start_section {
    my $ssectname = shift;
    
    $current = "SUBSECT"; $ssecnr++;
    if ($super_page_mode) {
        $headbuf = sprintf("%s %s", &amp;section_num($secnr, $ssecnr),
                           $ssectname);
    } else {
        if (!$in_section_list) {
            STDOUT-&gt;print("&lt;UL&gt;\n"); $in_section_list = 1;
        }
        if (!$big_page_mode) {
            if ($outfh-&gt;fileno != STDOUT-&gt;fileno) {
                &amp;footing($outfh); $outfh-&gt;close;
            }
            $outname = "$firstname-$filenum.$fileext"; $filenum++;
            $outfh = new FileHandle "&gt;$outname"
                or die qq(html2html: Fatal: Could not open file "$outname"\n);
            heading($outfh);
    
            # Since only one section is on any page, 
            # don't use # so that when we
            # jump to this page, we see the browse 
            # links at the top of the page. 
            $headbuf = sprintf("%s %s", &amp;section_num($secnr, $ssecnr), 
                               $ssectname);
            STDOUT-&gt;printf(qq(&lt;LI&gt;&lt;A HREF="%s"&gt;%s %s&lt;/A&gt;\n), $outname, 
                   &amp;section_num($secnr, $ssecnr), $ssectname);
        } else {
            # Since many sections are on one page, we need to use #
            $headbuf = sprintf(qq(&lt;A NAME="ss%s"&gt;%s %s&lt;/A&gt;\n),
                               &amp;section_num($secnr, $ssecnr),
                               &amp;section_num($secnr, $ssecnr),
                               $ssectname);
            STDOUT-&gt;printf(qq(&lt;LI&gt;&lt;A HREF="%s#ss%s"&gt;%s %s&lt;/A&gt;\n), $outname,
                   &amp;section_num($secnr, $ssecnr), 
                   &amp;section_num($secnr, $ssecnr), 
                   $ssectname);
        }
    }
}

</pre></body></html>