gitolite/contrib/autotoc

35 lines
701 B
Perl
Executable file

#!/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 name=".*"><\/a>\n\n//mg;
my @toc = $doc =~ /^###+ .*/mg;
$doc =~ s/^(###+) (.*)/"<a name=\"" . &make_anchor($2) . "\"><\/a>\n\n$1 $2"/mge;
for (@toc) {
s/^(###+) (.*)/' ' x (length($1)-3) . ' * <a href="#' . &make_anchor($2) . "\">$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;