package Parse::Paragraph; use 5.006000; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not export # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. # This allows declaration use Parse::Paragraph ':all'; # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01'; # Preloaded methods go here. sub new { my $class = shift; my %options = @_; my $self = {}; $self->{'noparagraph'} = $options{'noparagraph'} || ["img","video", "audio"]; bless($self, $class); return $self; }; sub parse { my $self = shift; my ($content) = @_; # Füge zur Sicherheit 2 Newlines am Ende hinzu, damit auch der letzte # Absatz in jedem Fall mit
abgeschlossen werden kann $content = "$content\n\n"; # Der erste Absatz soll nur durch ein öffnendesersetzt werden # Um diese einmalige Aktion zu implementieren, benötigen wir die Variable # $first my $first = 1; # get the array with all elements where no substitution shall be done my @noparse = @{ $self->{'noparagraph'} }; # Die Elemente, die von sich aus schon Block Elemente sind, und für die daher # eine Umschließung mit
..
nicht erforderlich ist, sollen durch # 3 vorangestellte ### abgesichert werden. Eine Leerzeile, nach der ### kommt # wird nicht durch \nersetzt foreach my $noparse (@noparse) { $content =~ s/\[$noparse\]/###\[$noparse\]/g; } # Wenn kein geschütztes Blockelement am Anfang des Inhalts steht # beginne mit einem öffnenden
$content = "
$content" unless ($content =~ m/^###/s); $first = 0 unless ($content =~ m/^###/s); # Nun ersetze alle mehrfachen Zeilenumbrüche mit # lediglich einem öffnendem
, wenn zum ersten Mal eine Ersetzung durchgeführt wird # Ansonsten mit
\n# Keine Ersetzung darf jedoch durchgeführt werden, wenn das erste Zeichen # nach den Leerzeilen zu den nicht zu parsenden Elementen gehört (sprich mit ### # gesichert wurde). Dies wird durch (?!###) erreicht if ($first == 1) { $content =~ s/\n{2,}(?!###)/\n\
/; $first = 0; } $content =~ s/\n{2,}(?!###)/\<\/p\>\n\
/g;
# Ersetze dann alle einfachen Zeilenumbrüche mit
\n, es sei denn auf den
# einfachen Zeilenumbruch folgt ein gesichertes Element
$content =~ s/\n{1}(?!###|
)/
\n/g;
# Lösche falls vorhanden ein überflüssiges
am Ende $content =~ s/
$//;
# Mache die Sicherung der nicht zu parsenden Elemente mittels ### rückgängig
foreach my $noparse (@noparse) {
$content =~ s/###\[$noparse\]/\[$noparse\]/g;
}
return $content
}
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
Parse::Paragraph - Perl extension for blah blah blah
=head1 SYNOPSIS
use Parse::Paragraph;
blah blah blah
=head1 DESCRIPTION
Stub documentation for Parse::Paragraph, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.
Blah blah blah.
=head2 EXPORT
None by default.
=head1 SEE ALSO
Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.
If you have a mailing list set up for your module, mention it here.
If you have a web site set up for your module, mention it here.
=head1 AUTHOR
Maximilian Lika, E