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-09-03 13:16:51 +02:00
|
|
|
$doc =~ s/^<a name="_.*"><\/a>\n\n//mg;
|
|
|
|
$doc =~ s/^<a name="AUTO_.*"><\/a>\n\n//mg;
|
2010-05-21 14:23:05 +02:00
|
|
|
|
2010-06-01 02:46:13 +02:00
|
|
|
my @toc = $doc =~ /^###+ .*/mg;
|
2011-01-15 16:39:56 +01:00
|
|
|
$doc =~ s/^(###+) (.*)/"<a name=\"_" . make_anchor($2) . "\"><\/a>\n\n$1 $2"/mge;
|
2010-05-21 14:23:05 +02:00
|
|
|
|
|
|
|
for (@toc) {
|
2011-01-15 16:39:56 +01: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;
|