sub parse { my ($text,$mode) = @_; my $output = ""; my $stack = []; while (length $text) { if ($text =~ s/^\[(pic|gallery|audio|video|infobox)=//) { 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 my $last = pop @$stack; my ($name, $attr, $content) = @$last; $out = render($mode, $name, $attr, $content); } else { # text if ($text =~ s/^([^\[\]]+)//) { $out = $1; } } if (@$stack > 0) { $stack->[-1]->[2] .= $out; } else { $output .= $out; } } } return $output; }