Thread [HTML::Parser] Überschriften parsen und als Liste darstellen - nur wie? (6 answers)
Opened by GwenDragon at 2023-12-20 16:08

GwenDragon
 2023-12-20 16:08
#195704 #195704
User since
2005-01-17
14607 Artikel
Admin1
[Homepage]
user image
Ich bekomme es nicht hin, bei Überschriften den Text zu bekommen und als verschachtelte Liste (egal ob ul oder ol) für ein Inhaltsverzeichnis zu generieren.
Der Parse für die Hx-Elemente selbst ist ja simple, aber dann den eingeschlossenen text zu bekommen, und das Ganze als Liste auszugeben.
Den HTML::Parser verstehe ich einfach nicht.

Code (perl): (dl )
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
package toc;

use strict;
use warnings;
use 5.020;
use HTML::Parser;

use vars qw( $toc );

sub toc {
    my $source = shift;

    my @parsed  = ();
        
    my $tag_sub = sub {
        my $s = shift;
        my $tag = shift;
        if ( $tag =~ m/[hH](\d)/ ) {
                push @parsed, "level $1: $s";
        }
    };
    my $text_sub = sub { 
        push @parsed, shift; 
    };

    # HTML-Parser erzeugen
    my $p = HTML::Parser->new(
        api_version   => 3,
        start_h       => [ $tag_sub,  "text,tagname" ],
        end_h         => [ $tag_sub,  "text,tagname" ],
        #process_h     => [ $text_sub, "text" ],
        #comment_h     => [ $text_sub, "text" ],
        #declaration_h => [ $text_sub, "text" ],
        #default_h     => [ $text_sub, "text" ],
    );
    $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 "\n\n<!--\n" . join("\n",@parsed) . "\n-->\n\n";
}

sub story {
    my ( $story_ref ) = @_;

    $toc = toc::toc($$story_ref);       
        $$story_ref =~ s/\Q{{{%%TOC%%}}}\E/Inhalt:\n$toc/;

    return 1;
}

1;

package main;

#use toc;

my $DATA = join "",<DATA>;

toc::story(\$DATA);

say $toc::toc;

__DATA__

{{{%%TOC%%}}}

<h1>Test 1</H1>

<p>Test</p>

<h2>Test 2</H2>

<p>Test 123</p>

<h2>Test 3</H2>

<p>Test</p>

<h1>Test 4</H1>

<p>Test</p>

<h1>Test 5</H1>

<p>Test</p>


ergibt:
Code: (dl )
1
2
3
4
5
6
7
<!--
level 1: <h1>
level 2: <h2>
level 2: <h2>
level 1: <h1>
level 1: <h1>
-->

View full thread [HTML::Parser] Überschriften parsen und als Liste darstellen - nur wie?