# Make Index - HTMLファイルのhタグから目次を生成する
# Copyright (C) Takashi Utsunomiya. All Rights Reserved.
# http://uttsu.com/
# 2002.08.07-
# Usage: perl mkindex.pl file
# 2003.03.20 ver 1.02
# - から に変更
# ver 1.01 2002.10.19
# ver 1.00 2002.08.07
# ■init
use strict;
my $file = $ARGV[0];
main();
exit;
# ■main
sub main {
open(IN, $file) or die;
my @in = ;
close(IN);
my (@body, @head, @index, $index, $anchor, $while_old_index);
foreach (@in) {
if ($while_old_index) {
$while_old_index = 0 if (/^$/);
next;
}
if (/^目次<\/h2>/) {
$while_old_index = 1;
next;
}
if (/^(.*)(<\/h\d>)/i) {
if ($index < $1) {
push(@head, "\t" x ($1-2) . "\n");
$index[$1] = 0;
} elsif ($index > $1) {
push(@head, "\t" x ($index-2) . "
\n");
}
$index = $1;
$index[$1-2]++;
$anchor = '';
for (my $i=0; $i<$1-1; $i++) {
$anchor .= ($i == 0 ? '' : '_') . $index[$i];
}
push(@head, "\t" x ($index-2) . "$3\n");
push(@body, "$3$4\n");
} else {
push(@body, $_);
}
}
unshift(@head, "目次
\n");
push(@head, "\t" x ($index-2) . "\n") if ($index > 2);
push(@head, "\n\n");
open(OUT, "> $file\.bak") or die;
print OUT @in;
close(OUT);
open(OUT, "> $file") or die;
my $head = 1;
foreach (@body) {
if ($head && /^