sub ComputeTableOfContents()

in d3d/archive/images/d3d11/postproc.pl [194:308]


    sub ComputeTableOfContents
    {
        # First, compute the long table of contents into $TOCLong
        #
        $TOCLong = "\n";
        $TOCLong .= "<A id=\"$tocLong\"></A>\n";
        $DefinedLinkDestinations{$tocLong} = $UseUnlabeledLink;
        $TOCLong .= "<H1>$tocLong</H1><p>(<a href=\"#ShortTableOfContents\">back to top</a>)</p>\n";
        $prevlevel = 1;
        for ($i=1;$i<$HTMLParse::HeaderCount;$i++)
        {
            $level = $HTMLParse::HeaderList[$i][0];
            if ($level < 1) { $prevlevel = $level; next; }
            if ( ($level > $prevlevel) && ($level > 1) )
            {
                for ($j = $prevlevel; $j < $level; $j++) { $TOCLong.= "<DIR>\n"; }
            }
            elsif ($level < $prevlevel)
            {
                for ($j = $level; $j < $prevlevel; $j++) { $TOCLong.= "</DIR>\n"; }
            }
            $TOCLong .= "<LI><A href=\"#".$HTMLParse::HeaderList[$i][1]."\">".$HTMLParse::HeaderList[$i][1]."</A></LI>\n";

            $prevlevel = $level;
        }
        
        # Second, compute the short table of contents into $TOCShort
        #
        $TOCShort = "\n";
        $TOCShort .= "<A id=\"$tocShort\"></A>\n";
        $DefinedLinkDestinations{$tocShort} = $UseUnlabeledLink;
        $TOCShort .= "<H1>$tocShort</H1>\n";
        $CurrChapter = 1;
        for ($i=1;$i<$HTMLParse::HeaderCount;$i++)
        {

            $level = $HTMLParse::HeaderList[$i][0];
            if ($level ne 1) { next; }
            $TOCShort .= "<LI><A href=\"#".$HTMLParse::HeaderList[$i][1]."\">".$HTMLParse::HeaderList[$i][1]."</A></LI>\n";
            $TOCChapter[$CurrChapter] = "<hr><p><a id=\"Chapter".$CurrChapter."Contents\"><b>Chapter Contents</b></a><br><br>(<a href=\"#ShortTableOfContents\">back to top</a>)<br><br>\n";
            $TOCEntriesForCurrentChapter = 0;
            $prevsublevel = 2;
            for ($j=$i+1;$j<$HTMLParse::HeaderCount;$j++)
            {
                $sublevel = $HTMLParse::HeaderList[$j][0];
                if( $sublevel eq 1 ) { last; }  # Stop at the next chapter
                if( $sublevel ge 3 ) { next; }  # Skip 3rd level headings and lower in the chapter TOC

                if ($sublevel > $prevsublevel) 
                {
                    for ($k = $prevsublevel; $k < $sublevel; $k++) { $TOCChapter[$CurrChapter].= "<DIR>\n"; }
                }
                elsif ($sublevel < $prevsublevel)
                {
                    for ($k = $sublevel; $k < $prevsublevel; $k++) { $TOCChapter[$CurrChapter].= "</DIR>\n"; }
                }
                $prevsublevel = $sublevel;

                $TOCChapter[$CurrChapter] .= "<A href=\"#".$HTMLParse::HeaderList[$j][1]."\">".$HTMLParse::HeaderList[$j][1]."</A><br>\n";
                $TOCEntriesForCurrentChapter++;
            }
            for ($k = $prevsublevel; $k > 1; $k--) { $TOCChapter[$CurrChapter].= "</DIR>\n"; }
            $TOCChapter[$CurrChapter] .= "<br></p><hr>\n";
            if( $TOCEntriesForCurrentChapter < 3 )
            {
                $TOCChapter[$CurrChapter] = ""; # Don't bother printing a chapter TOC if there are less than 3 things to list.
            }
            
            $CurrChapter++;
        }
        $TOCShort .= "<br><br>";

        # Third, compute table of contents for each H2 section into $TOCH2
        #
        $CurrChapter = 0;
        $CurrH2 = 1;
        for ($i=1;$i<$HTMLParse::HeaderCount;$i++)
        {

            $level = $HTMLParse::HeaderList[$i][0];
            if ($level eq 1) { $CurrChapter++; $CurrH2 = 1; next; }
            if ($level ne 2) { next; }

            $flatIndex = $CurrH2 * $maxChapters + $CurrChapter;
            $TOCSection[$flatIndex] = "<hr><p><b>Section Contents</b><br><br>(<a href=\"#Chapter".$CurrChapter."Contents\">back to chapter</a>)<br><br>\n";
            $TOCEntriesForCurrentSection = 0;
            $prevsublevel = 3;
            for ($j=$i+1;$j<$HTMLParse::HeaderCount;$j++)
            {
                $sublevel = $HTMLParse::HeaderList[$j][0];
                if( $sublevel le 2 ) { last; }  # Stop at the next section/chapter

                if ($sublevel > $prevsublevel) 
                {
                    for ($k = $prevsublevel; $k < $sublevel; $k++) { $TOCSection[$flatIndex].= "<DIR>\n"; }
                }
                elsif ($sublevel < $prevsublevel)
                {
                    for ($k = $sublevel; $k < $prevsublevel; $k++) { $TOCSection[$flatIndex].= "</DIR>\n"; }
                }
                $prevsublevel = $sublevel;

                $TOCSection[$flatIndex] .= "<A href=\"#".$HTMLParse::HeaderList[$j][1]."\">".$HTMLParse::HeaderList[$j][1]."</A><br>\n";
                $TOCEntriesForCurrentSection++;
            }
            for ($k = $prevsublevel; $k > 2; $k--) { $TOCSection[$flatIndex].= "</DIR>\n"; }
            $TOCSection[$flatIndex] .= "</p>\n";
            if( $TOCEntriesForCurrentSection < 3 )
            {
                $TOCSection[$flatIndex] = ""; # Don't bother printing a chapter TOC if there are less than 3 things to list.
            }
            
            $CurrH2++;
        }
    }