#!/usr/bin/perl -w
use strict;
# filter gitolite's mkd files through this; it's designed to be idempotent of
# course
sub make_anchor {
# make an anchor out of a section heading
my $sh = shift;
$sh =~ s/\W+/_/g;
$sh =~ s/^_//;
return $sh;
}
undef $/;
my $doc = <>;
$doc =~ s/^<\/a>\n\n//mg;
$doc =~ s/^<\/a>\n\n//mg;
my @toc = $doc =~ /^###+ .*/mg;
$doc =~ s/^(###+) (.*)/"<\/a>\n\n$1 $2"/mge;
for (@toc) {
s/^(###+) (.*)/' ' x (length($1)-3) . ' * $2<\/a>"/e;
}
my $toc = "In this document:\n\n";
$toc .= join("\n", @toc);
$toc .= "\n\n";
$doc =~ s/^In this document:\n\n.*?\n\n/$toc/sm;
print $doc;