#! /usr/bin/perl # # Copyright (c) 2005-2006 Motoyuki Kasahara # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of the project nor the names of its contributors # may be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # # html-toc -- make `table of contents' of HTML files. # # Usage: # html-index [option...] input-file... # # `html-toc' reads HTML files, and generates `table of contents' (TOC) # of the HTML files. The TOC is created from

...

tags and # tag in the HTML files. Since `html-toc' doesn't parse # HTML precisely, the tags must be the following form: # # heading # # where `?' is 1..6. Note that and above must be in the same # line. # # `html-toc' outputs TOC to standard out by default. # # Options: # -o file specify output file. # -h do not output file name in . # -m level minimum target heading level # (default: h1) # -M level maximum target heading level # (default: h6) require 5.005; use Getopt::Std; # # Usage # my $usage = "Usage: $0 [option...] input-file...\n"; # # Variables # my $out_file = '-'; my @preamble = (); my $fragment_only = 0; my $min_level = 1; my $max_level = 6; # # Parse command line arguments. # my %options; getopts('o:hm:M:', \%options) or die $usage; die $usage if (@ARGV == 0); $fragment_only = 1 if (defined($options{h}) || @ARGV == 1); $out_file = $options{o} if (defined($options{o})); if (defined($options{m})) { $options{m} =~ s/^h//; $min_level = $options{m}; } if (defined($options{M})) { $options{M} =~ s/^h//; $max_level = $options{M}; } # # Read an HTML file. # $current_level = $min_level; if ($out_file eq '-') { $out_file = 'stdout'; open(OUT_FILE, ">& STDOUT"); } else { if (!open(OUT_FILE, "> $out_file")) { die "$0: failed to open the file, $!: $out_file\n"; } } print OUT_FILE "\n"; } while ($current_level < $level) { print OUT_FILE ' ' x ($current_level - $min_level + 1); print OUT_FILE "\n"; } print OUT_FILE "\n"; close(OUT_FILE);