]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/toc.pm
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[git.ikiwiki.info.git] / IkiWiki / Plugin / toc.pm
index 070c07b0e96093b4c82c3bb9e6c8751dcb7ed414..5380dd9658d5ecbfa71b335edd7830628c83a88b 100644 (file)
@@ -9,7 +9,7 @@ use HTML::Parser;
 
 sub import { #{{{
        hook(type => "preprocess", id => "toc", call => \&preprocess);
-       hook(type => "format", id => "toc", call => \&format);
+       hook(type => "sanitize", id => "toc", call => \&sanitize);
 } # }}}
 
 my %tocpages;
@@ -33,7 +33,7 @@ sub preprocess (@) { #{{{
        }
 } # }}}
 
-sub format (@) { #{{{
+sub sanitize (@) { #{{{
        my %params=@_;
        my $content=$params{content};
        
@@ -86,17 +86,25 @@ sub format (@) { #{{{
                                }
                                $liststarted=0;
                        }
+                               
+                       $index.=&$indent."</li>\n" unless $liststarted;
+                       $liststarted=0;
+                       $index.=&$indent."<li class=\"L$curlevel\">".
+                               "<a href=\"#$anchor\">";
        
                        $p->handler(text => sub {
                                $page.=join("", @_);
-                               $index.=&$indent."</li>\n" unless $liststarted;
-                               $liststarted=0;
-                               $index.=&$indent."<li class=\"L$curlevel\">".
-                                       "<a href=\"#$anchor\">".
-                                       join("", @_).
-                                       "</a>\n";
-                               $p->handler(text => undef);
+                               $index.=join("", @_);
                        }, "dtext");
+                       $p->handler(end => sub {
+                               my $tagname=shift;
+                               if ($tagname =~ /^h(\d+)$/i) {
+                                       $p->handler(text => undef);
+                                       $p->handler(end => undef);
+                                       $index.="</a>\n";
+                               }
+                               $page.=join("", @_);
+                       }, "tagname, text");
                }
                else {
                        $page.=$text;