package HTML::BBCode; use strict; use warnings; sub new { my $class = shift; my $self = {}; bless($self, $class); $self->_init(@_); return $self; } sub _init { my $self = shift; my %attr = @_; my %tags = ( b => '%s', i => '%s', u => '%s', center => '
%s
', img => 'Bild', color => '%s', size => '%s' ); my %allowedTags = (); if (ref($attr{allowedTags}) eq 'ARRAY') { $allowedTags{$_} = $tags{$_} for @{$attr{allowedTags}}; } else { %allowedTags = %tags; } $self->{escape_html} = 1 if $attr{escape_html}; $self->{allowedTags} = \%allowedTags; } sub parse { my ($self, $text) = @_; $text = escape_html($text) if $self->{escape_html}; $text =~ s/(?/g; $text =~ s/[\n\r]+(.*)[\n\r]+/\n

$1<\/p>\n/g; for (keys %{$self->{allowedTags}}) { $text =~ s{ \[$_\](.+?)\[/$_\] } { _replace($self,$_,$1) }egsx; $text =~ s{ \[($_)=([a-zA-Z_0-9\#]+)\](.+?)\[/$_\] } { _replace($self,$1,$2,$3) }egsx; } return $text; } sub _replace { my $self = shift; return sprintf($self->{allowedTags}->{shift(@_)}, @_); } sub escape_html { my $text = shift; $text =~ s/&/&/gso; $text =~ s//>/gso; return $text; } 1;