-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathmarkdown-toc.pl
executable file
·199 lines (154 loc) · 3.6 KB
/
markdown-toc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env perl
use v5.10.1;
use strict;
use warnings;
use Getopt::Std qw/ getopts /;
use File::Copy qw(move);
sub usage ($);
my %opts;
getopts 'hf', \%opts
or usage 1;
if ($opts{h}) {
usage 0;
}
my $as_first_sec = $opts{f};
my $infile = shift or
die "usage: $0 <infile>\n";
my $name;
if ($infile =~ m{([^/]+)\.wiki$}) {
$name = $1;
if ($name !~ /^[A-Z]/) {
undef $name;
} else {
warn "Using name $name...\n";
}
}
open my $in, $infile or
die "cannot open $infile for reading: $!\n";
my $s = do { local $/; <$in> };
close $in;
my $preamble = '';
my @sections;
my $section;
my $verbatim;
my $toc = '';
while (1) {
if ($s =~ /\G^\`\`\`.*/gmc) {
$verbatim = !$verbatim;
if (!$section) {
$preamble .= $&;
#die "Bad line: $&";
next;
}
$section->[1] .= $&;
next;
}
if ($verbatim && $s =~ /\G.*\n/gmc) {
if (!$section) {
$preamble .= $&;
#die "Bad line: $&";
next;
}
$section->[1] .= $&;
next;
}
if ($s =~ /\G^(\S[^\n]*)\n([-=]){2,}\n/gsmc) {
my ($title, $bar) = ($1, $2);
warn "$title\n";
my $anchor = gen_anchor($title);
my $indent;
if ($bar eq '=') {
$indent = "";
} else {
$indent = " ";
}
$bar = $bar x length($title);
if ($title ne 'Table of Contents') {
$toc .= "$indent* [$title](#$anchor)\n";
}
$section = [$title, "$title\n$bar\n"];
push @sections, $section;
} elsif ($s =~ /\G^(\#{1,})\s*(\S[^\n]*)\n/gsmc) {
my ($bar, $title) = ($1, $2);
warn "$title\n";
my $anchor = gen_anchor($title);
my $indent = " " x (length($bar) - 1);
if ($title ne 'Table of Contents') {
$toc .= "$indent* [$title](#$anchor)\n";
}
$section = [$title, $&];
push @sections, $section;
} elsif ($s =~ /\G[^\n]*\n/gsmc) {
if (!$section) {
$preamble .= $&;
#die "Bad line: $&";
next;
}
$section->[1] .= $&;
#warn $&;
} else {
last;
}
}
my $outfile = "$infile.new";
open my $out, ">$outfile"
or die "Cannot open $outfile for writing: $!\n";
my $i = 0;
print $out $preamble;
my $wrote_toc;
for my $sec (@sections) {
my ($title, $src) = @$sec;
if ($title =~ /^(?:\# )?Table of Contents$/) {
# skip this section
next;
}
if ($as_first_sec) {
if (!$wrote_toc) {
print $out "# Table of Contents\n\n$toc\n";
$wrote_toc = 1;
}
print $out $src;
next;
}
print $out $src;
if (++$i > 3 && $src !~ /Back to TOC/sm) {
if ($src =~ /\w\n.*?\n.*?\w/s) {
if ($src !~ /\n\n$/s) {
print $out "\n";
}
print $out "[Back to TOC](#table-of-contents)\n\n";
}
}
if (!$wrote_toc) {
print $out "# Table of Contents\n\n$toc\n";
$wrote_toc = 1;
}
}
close $out;
move($outfile, $infile);
sub gen_anchor {
my $link = shift;
$link =~ s/[^-\w_ ]//g;
$link =~ s/ /-/g;
lc($link);
}
sub gen_toc ($) {
my $s = shift;
return $toc;
}
sub usage ($) {
my $rc = shift;
my $msg = <<_EOC_;
Usage: $0 [-h] [-f] MD_FILE
Options:
-f Insert TOC before the first section title
(by default, inserting before the 2nd)
-h Print this help.
_EOC_
if ($rc == 0) {
print $msg;
exit 0;
}
warn $msg;
exit $rc;
}