@@ -194,9 +194,20 @@ sub _prepare_html ($c, $src, $url_perl_version, $module, $function = undef, $var
194
194
}
195
195
}
196
196
197
+ # Insert permalinks
198
+ my $linkable = ' h1, h2, h3, h4' ;
199
+ $linkable .= ' , dt' unless $module eq ' search' ;
200
+ for my $e ($dom -> find($linkable )-> each ) {
201
+ my $link = Mojo::URL-> new-> fragment($e -> {id });
202
+ my $permalink = $c -> link_to(' #' => $link , class => ' permalink' );
203
+ $e -> content($permalink . $e -> content);
204
+ }
205
+
197
206
return $dom ;
198
207
}
199
208
209
+ my %toc_level = (h1 => 1, h2 => 2, h3 => 3, h4 => 4);
210
+
200
211
sub _render_html ($c , $dom ) {
201
212
my $module = $c -> stash(' module' );
202
213
# Try to find a title
@@ -208,19 +219,16 @@ sub _render_html ($c, $dom) {
208
219
$title = $p -> all_text;
209
220
});
210
221
211
- # Rewrite headers
212
- my %level = (h1 => 1, h2 => 2, h3 => 3, h4 => 4);
213
- my $linkable = ' h1, h2, h3, h4' ;
214
- $linkable .= ' , dt' unless $c -> stash(' module' ) eq ' search' ;
215
- my (@toc , $parent );
216
- for my $e ($dom -> find($linkable )-> each ) {
217
- my $link = Mojo::URL-> new-> fragment($e -> {id });
218
- my $text = $e -> all_text;
219
- unless ($module eq ' index' or $e -> tag eq ' dt' ) {
220
- # Add to table of contents
222
+ # Assemble table of contents
223
+ my @toc ;
224
+ unless ($module eq ' index' ) {
225
+ my $parent ;
226
+ for my $e ($dom -> find(' h1, h2, h3, h4' )-> each ) {
227
+ my $link = Mojo::URL-> new-> fragment($e -> {id });
228
+ my $text = $e -> all_text =~ s / ^#// r ;
221
229
my $entry = {tag => $e -> tag, text => $text , link => $link };
222
230
$parent = $parent -> {parent } until !defined $parent
223
- or $level {$e -> tag} > $level {$parent -> {tag }};
231
+ or $toc_level {$e -> tag} > $toc_level {$parent -> {tag }};
224
232
if (defined $parent ) {
225
233
weaken($entry -> {parent } = $parent );
226
234
push @{$parent -> {contents }}, $entry ;
@@ -229,8 +237,6 @@ sub _render_html ($c, $dom) {
229
237
}
230
238
$parent = $entry ;
231
239
}
232
- my $permalink = $c -> link_to(' #' => $link , class => ' permalink' );
233
- $e -> content($permalink . $e -> content);
234
240
}
235
241
236
242
# Combine everything to a proper response
0 commit comments