#!/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; }