#!/usr/bin/perl -T use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); print "Content-type: text; charset=utf-8\n\n"; my $teststring = qq|[pic w=100px h=50px style=test] some text [infobox headline=Hello world! content=[pic w=50 h=50 style=left] hier ist mein text.] [gallery path=[host]myfolder/ style=cool navi=standard] [host]|; print "

input

\n$teststring"; print "

output

\n"; print &GetTags ($teststring); sub GetKeyValue { my ($tag, $attr) = @_; my %KeyVal = $attr =~ m[(\S+)\s*=\s*(\S+)]g; my $testkeyval = ""; my $totalkey = keys %KeyVal; my $count= 0; my $whitespace = " "; for my $key (sort keys %KeyVal) { if ($totalkey == $count) {$whitespace = ""}; $testkeyval .= "$key=\"$KeyVal{$key}\"$whitespace"; $count++; } if ($tag eq "pic") { return "{pic $testkeyval /}"; } if ($tag eq "gallery") { return "{gallery $testkeyval /}"; } if ($tag eq "infobox") { return "{infbox $testkeyval /}"; } if ($tag eq "host") { return "http://www.example.com/"; } } sub GetTags { my ($text) = @_; my $output = ""; my $stack = []; while (length $text) { if (($text =~ s/^\[(pic|gallery|infobox)\s//si) # tag mit attr || ($text =~ s/^\[(host)//si)) # tag ohne attr { my $name = $1; # tag my $attr = ""; if ($text =~ s/^([^\[\]]+)//) { $attr = $1; } push @$stack, [$name, $attr]; } else { my $out; if ($text =~ s/^\[//) { $out = "["; } if ($text =~ s/^\]//) { # closing tag if (@$stack) { my $last = pop @$stack; my ($name, $attr) = @$last; $out = &GetKeyValue($name, $attr); } } else { # text if ($text =~ s/^([^\[\]]+)//) { $out = $1; } } if (@$stack > 0) { $stack->[-1]->[2] .= $out; } else { $output .= $out; } } } return $output; }