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