Leser: 30
1
2
[pic=Pfad/bild.jpg#310#left#meta] Etwas Text
im Dokument. [audio=Pfad/datei.mp3#left#meta]
1 2 3 4 5 6 7 8
use strict; use warnings; for my $line (@FILE) { $line =~ s-\[pic\=(.*?)\]-my $PicTag = &GetPic( "$1" )-sige; $line =~ s-\[audio\=(.*?)\]-my $AudioTag = &GetAudio( "$1" )-sige; $line =~ s-\[infobox\=(.*?)\]-my $InfoBoxTag = &CreateInfoBox( "$1" )-sige; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
sub render { my ($tag, $attr, $content) = @_; # hier entsprechend GetPic, GetAudio etc. implementieren } sub parse { my ($text) = @_; my $output = ""; my $stack = []; while (length $text) { if ($text =~ s/^\[(pic|audio|infobox)=//) { my $name = $1; # tag my $attr = ""; if ($text =~ s/^([^\[\]]+)//) { $attr = $1; } push @$stack, [$name, $attr, ""]; } else { my $out; if ($text =~ s/^\]//) { # closing tag my $last = pop @$stack; my ($name, $attr, $content) = @$last; $out = render($name, $attr, $content); } else { # text if ($text =~ s/^([^\[\]]+)//) { $out = $1; } } if (@$stack > 0) { $stack->[-1]->[2] .= $out; } else { $output .= $out; } } } return $output; } my $output = parse($data);
$text =~ s/^\[(pic|audio|infobox)=//;
$text =~ s/^([^\[\]]+)//;
2010-11-27T16:33:28 cbxk1xgMit dieser RegEx wird nach einem Wort oder dem anderen Wort gesucht, eine Backreferenz gebildet und der String inkl. dem = ersetzt. Richtig?
Code: (dl )$text =~ s/^\[(pic|audio|infobox)=//;
QuoteMit dieser RegEx guckst Du nach einer möglichen Verschachtelung, die auch mehrmals vorkommen kann. Außerdem erzeugst Du eine Backreferenz die später $attr getauft wird. Richtig?Code: (dl )$text =~ s/^([^\[\]]+)//;
[pic=Pfad/bild.jpg#310#left#meta]
QuoteDann nimmst Du alles und packst es wieder in den stack, damit es erneut mit der ersten RexEx bearbeitet wird.
QuoteDas verstehe ich soweit alles. Aber was mache ich mit $attr in der render sub?
[pic=Pfad/bild.jpg#310#left#meta]
Pfad/bild.jpg#310#left
die "tag=$tag\n attr=$attr\n content=$content\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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; }
Can't use an undefined value as an ARRAY reference at /var/www/index.pl line 2841
my ($name, $attr, $content) = @$last;
1 2 3
if ($tag eq "pic"){...machwas...} if ($tag eq "gallery"){...machwas...} else {return;}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#!/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 "<h2>input</h2>\n$teststring"; print "<h2>output</h2>\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; }
1
2
3
4
5
6
<h2>input</h2>
[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]<h2>output</h2>
{pic h="50px" style="test" w="100px" /} some text
{infbox headline="Hello" /} {gallery /} http://www.example.com/
[b]fett[/b]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#!/usr/bin/perl -T use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use Parse::BBCode; print "Content-type: text; charset=utf-8\n\n"; my $teststring = "[b]bold[/b] [host] [pic src=/mfolder/myimage.jpg 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]"; my $p = Parse::BBCode->new({ direct_attributes => 0, #close_open_tags => 1, tags => { b => '<b>%{parse}s</b>', pic => {output => '%{pic}s', class => 'block', single => 1 }, gallery => {output => '%{gallery}s', class => 'block', single => 1 }, infobox => {output => '%{infobox}s', class => 'block', single => 1 }, host => {output => 'http://www.example.com/', class => 'block', single => 1 }, }, escapes => { pic => sub { my ($parser, $attr, $content, $attribute_fallback) = @_; # Hier will ich die key/vals an eine Sub übergeben. return "{pic $parser, $attr, $content, $attribute_fallback}"; }, gallery => sub { my ($parser, $tag, $text) = @_; return "{gallery $text/}"; }, infobox => sub { my ($parser, $tag, $text) = @_; return "{infobox $text/}"; }, }, } ); my $parsed = $p->render($teststring); print "<h2>input</h2>\n"; print $teststring; print "<h2>output</h2>\n"; print $parsed;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#!/usr/bin/perl -T use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use Parse::BBCode; print "Content-type: text; charset=utf-8\n\n"; my $teststring = "[b]bold[/b] [host] [pic src=/mfolder/myimage.jpg w=100px h=50px style=test] some text [infobox headline=Hello world!][pic w=50 h=50 style=left]hier ist mein text.[/infobox] [gallery style=cool navi=standard]my/path/[/gallery] [host]"; my $p = Parse::BBCode->new({ direct_attributes => 0, #close_open_tags => 1, tags => { b => '<b>%{parse}s</b>', pic => {output => '%{src}A,%{style}A,%{w}A,%{h}A,%a,%s,%{parse}s', class => 'block', single => 1 }, gallery => {output => '%{gallery}a', class => 'block', single => 0 }, infobox => {output => '%{infobox}s', class => 'block', single => 0 }, host => {output => 'http://www.example.com/', class => 'block', single => 1 }, }, escapes => { pic => sub { my ($w, $h) = @_; # Hier will ich die key/vals an eine Sub übergeben. return "{pic $w, $h}"; }, gallery => sub { my ($parser, $tag, $text) = @_; return "{gallery $text/}"; }, infobox => sub { my ($parser, $tag, $text) = @_; return "{infobox $text/}"; }, }, } ); my $parsed = $p->render($teststring); print "<h2>input</h2>\n"; print $teststring; print "<h2>output</h2>\n"; print $parsed;