chiark / gitweb /
nailing-cargo: Properly handle renamed packages
[nailing-cargo.git] / markdown-toc-filter
1 #!/usr/bin/perl -wn
2
3 use strict;
4 use autodie;
5
6 our (@doc, @toc);
7 our $last;
8
9 sub scanline () {
10   if (m/^-+$/ || m/^=+$/) {
11     my $lev = $& =~ m/^=/ ? '  ' : '    ';
12     my $href = $last;
13     $href =~ y/ A-Z/-a-z/;
14     $href =~ y/-._a-z//cd;
15     my $text = $last;
16     $text = 'Introduction' if $. == 2;
17     push @toc, "${lev}* [$text](#$href)\n";
18   }
19   $last = $_;
20   chomp $last;
21 }
22
23 if (1..(m/^[A-Z]/ && m/table of contents/i)) {
24   # before TOC
25   scanline();
26   print;
27 } elsif (m/^\w/..0) {
28   # after TOC
29   push @doc, $_;
30   scanline();
31 } else {
32   # in TOC
33   print if m/^===|^---/;
34   scanline();
35 }
36
37 END {
38   print <<END, @toc, "\n";
39
40 <!-- TOC autogenerated by $0, do not edit -->
41
42 END
43   print @doc;
44 }