Thread CMS Tags parsen (29 answers)
Opened by cbxk1xg at 2010-11-26 16:27

pq
 2010-11-26 20:16
#143101 #143101
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
hier mal ein quick&dirty-parser:
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
sub render {
    my ($tag, $attr, $content) = @_;
    # hier entsprechend GetPic, GetAudio etc. implementieren
}
sub parse {
    my ($text) = @_;
    my $output = "";
    my $stack = [];
    while (length $text) {
        if ($text =~ s/^\[(pic|audio|infobox)=//) {
            my $name = $1;
            # tag
            my $attr = "";
            if ($text =~ s/^([^\[\]]+)//) {
                $attr = $1;
            }
            push @$stack, [$name, $attr, ""];
        }
        else {
            my $out;
            if ($text =~ s/^\]//) {
                # closing tag
                my $last = pop @$stack;
                my ($name, $attr, $content) = @$last;
                $out = render($name, $attr, $content);
            }
            else {
                # text
                if ($text =~ s/^([^\[\]]+)//) {
                    $out = $1;
                }
            }
            if (@$stack > 0) {
                $stack->[-1]->[2] .= $out;
            }
            else {
                $output .= $out;
            }
        }
    }
    return $output;
}

my $output = parse($data);


edit: variable $tree in $stack umbenannt
Last edited: 2010-11-26 20:18:37 +0100 (CET)
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread CMS Tags parsen