package Parse::Paragraph; use 5.006000; use strict; use warnings; use Data::Dumper; 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"]; $self->{'linebreaks'} = $options{'linebreaks'}; 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"; # Formatiere Windows Zeilenumbrüche in Linux Zeilenumbrüche um $content =~ s/\r\n/\n/g; # Der erste Absatz soll nur durch ein öffnendes

ersetzt 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

\n

ersetzt foreach my $noparse (@noparse) { $content =~ s/\[$noparse\]/###\[$noparse\]/g; } # Wenn kein geschütztes Blockelement am Anfang des Inhalts steht # beginne mit einem öffnenden

$content = "[p] $content" unless ($content =~ m/^###/s); $content =~ s/^\[p\](.*?)\n\n/\[p\]$1\[\/p\]\n/ unless ($content =~ m/^###/s); $first = 0 unless ($content =~ m/^###/s); # Nach mindestens 2 aufeinander folgenden Zeilenumbrüchen (\n\n) # soll ein öffneneds [p] eingesetzt werden, sofern kein gesichertes # Zeichen folgt $content =~ s/\n{2,}(?!###)/\n\n\[p\]/sg; # Ausgehend davon wird das schließende [/p] nach dem nächsten doppelten # Zeilenumbruch (\n\n) eingesetzt $content =~ s/\[p\](.*?)\n\n/[p\]$1\[\/p\]\n/sg; # Zuletzt werden alle vor einem [p] Leerzeichen zusammengezogen, damit # das Parsen von den einfachen Zeilenumbrüchen funktioniert $content =~ s/\n\n\[p\]/\n\[p\]/sg; # Ersetze dann alle einfachen Zeilenumbrüche mit
\n, es sei denn auf den # einfachen Zeilenumbruch folgt ein gesichertes Element oder die Eigenschaft # "linebreaks" ist deaktiviert $content =~ s/\n{1}(?!###|\[p\])/\[br\]\n/g if ($self->{'linebreaks'} == 1); # Lösche falls vorhanden ein überflüssiges

am Ende $content =~ s/\[p\]$//; # 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, Emaximilian@E =head1 COPYRIGHT AND LICENSE Copyright (C) 2017 by Maximilian Lika This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.22.2 or, at your option, any later version of Perl 5 you may have available. =cut