Skip to content

Commit 285663f

Browse files
committed
feature: added anchors to section headings.
1 parent a168672 commit 285663f

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

utils/gen-html-index-en.pl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@
3333
$res .= "<ul>\n";
3434
for my $infile (@ARGV) {
3535
(my $base = $infile) =~ s{.*/|\.html$}{}g;
36+
my $id = lc($base);
3637

3738
if ($infile =~ /Foreword(\d+)/) {
3839
my $n = $1;
3940
if ($n eq '01') {
4041
$res .= <<_EOC_;
41-
<li><a href="#$base">Foreword</a></li>
42+
<li><a href="#$id">Foreword</a></li>
4243
_EOC_
4344
} elsif ($n eq '02') {
4445
$res .= <<_EOC_;
45-
<li><a href="#$base">Writing Plan for the Tutorials</a></li>
46+
<li><a href="#$id">Writing Plan for the Tutorials</a></li>
4647
_EOC_
4748
} else {
4849
die "unknown infile: $infile";
@@ -53,15 +54,15 @@
5354
my $n = $nums[$num];
5455
#$infile =~ s{.*/}{}g;
5556
$res .= <<_EOC_;
56-
<li><a href="#$base">Nginx Variables ($n)</a></li>
57+
<li><a href="#$id">Nginx Variables ($n)</a></li>
5758
_EOC_
5859

5960
} elsif ($infile =~ /DirectiveExecOrder(\d+)/) {
6061
my $num = +$1;
6162
my $n = $nums[$num];
6263
#$infile =~ s{.*/}{}g;
6364
$res .= <<_EOC_;
64-
<li><a href="#$base">Nginx Directive Execution Order ($n)</a></li>
65+
<li><a href="#$id">Nginx Directive Execution Order ($n)</a></li>
6566
_EOC_
6667

6768
} else {

utils/wiki2html-en.pl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,25 @@ sub fmt_para {
110110
my ($s, $ctx) = @_;
111111
if ($s =~ /^= (.*?) =$/) {
112112
my $title = $1;
113+
my $id = quote_anchor($base);
113114
return <<"_EOC_";
114-
<h1 id="$base">$title</h1>
115+
<h1 id="$id">$title</h1>
115116
_EOC_
116117
}
117118

118119
if ($s =~ /^== (.*?) ==$/) {
119120
my $title = $1;
121+
my $id = quote_anchor("$base-$title");
120122
return <<"_EOC_";
121-
<h2>$title</h2>
123+
<h2 id="$id">$title</h2>
122124
_EOC_
123125
}
124126

125127
if ($s =~ /^=== (.*?) ===$/) {
126128
my $title = $1;
129+
my $id = quote_anchor("$base-$title");
127130
return <<"_EOC_";
128-
<h2>$title</h2>
131+
<h3 id="$id">$title</h3>
129132
_EOC_
130133
}
131134

@@ -169,7 +172,7 @@ sub fmt_para {
169172
}
170173

171174
$serial = sprintf("%02d", $serial);
172-
my $base = "$serial-${chapter}01";
175+
my $base = lc("$serial-${chapter}01");
173176
$res .= qq{$indent<a href="#$base">$label</a>};
174177

175178
} elsif ($label =~ m/(.*)\((\d{2})\)/) {
@@ -249,3 +252,17 @@ sub usage {
249252
die "Usage: $0 [-o <outfile>] <infile>\n";
250253
}
251254

255+
sub quote_anchor {
256+
my $id = shift;
257+
for ($id) {
258+
s/\$/dollar/g;
259+
s/[^-\w.]/-/g;
260+
s/--+/-/g;
261+
s/^-+|-+$//g;
262+
$_ = lc;
263+
}
264+
265+
$id =~ s/^01-nginxvariables01-/nginx-variables-/;
266+
267+
return $id;
268+
}

0 commit comments

Comments
 (0)