sub toc { my $source = shift; my @parsed; my $h_flag; my $data; my $text_sub = sub { if ($h_flag) { $data->{text} = shift; push @parsed, $data; $data = undef; $h_flag = 0; } }; ## my $end_sub = sub { ## if ($h_flag) { ## $data = undef; ## $h_flag = 0; ## } ## }; my $tag_sub = sub { my $s = shift; my $tag = shift; my $self = shift; if ( $tag =~ m/[hH](\d)/ ) { $data = { level => $1, tag => $tag, text => "" }; $h_flag = 1; } }; # HTML-Parser erzeugen my $p = HTML::Parser->new( api_version => 3, start_h => [ $tag_sub, "text,tagname,self" ], #end_h => [ $end_sub, "text,tagname,self" ], text_h => [ $text_sub, "text,self" ], #process_h => [ $text_sub, "text,self" ], #comment_h => [ $text_sub, "text,self" ], #declaration_h => [ $text_sub, "text,self" ], #default_h => [ $text_sub, "text,self" ], ); $p->empty_element_tags(1); $p->report_tags(qw(h1 h2 h3 h4 h5 h6)); $p->xml_pic(1); $p->utf8_mode(1); $p->case_sensitive(1); $p->parse($source); $p->eof(); return @parsed; } ## end sub toc