chiark / gitweb /
README.md: wip TOC
[nailing-cargo.git] / update-readme-toc
diff --git a/update-readme-toc b/update-readme-toc
new file mode 100755 (executable)
index 0000000..d5d5852
--- /dev/null
@@ -0,0 +1,31 @@
+#!/usr/bin/perl -wni~
+
+use strict;
+use autodie;
+
+our (@doc, @toc);
+our $last;
+
+if (1..m/^Table of Contents/) {
+  # before TOC
+  print;
+} elsif (m/^\w/..0) {
+  push @doc, $_;
+  # after TOC
+  if (m/^-+$/ || m/^=+$/) {
+    my $lev = $& =~ m/^-/ ? '  ' : '    ';
+    my $href = $last;
+    $href =~ y/ A-Z-/-a-z/d;
+    push @toc, "${lev}* [$last](#$href)\n";
+  }
+  $last = $_;
+  chomp $last;
+} else {
+  # in TOC
+  print if m/^===/;
+}
+
+END {
+  print @toc;
+  print @doc;
+}