Thread Board-rewrite: allgemeine Ideen
(44 answers)
Opened by jan10001 at 2003-08-25 00:08
Habe gerade mal einen Scanner fuer tags geschrieben, in denen nicht mehr weitere Ersetzungen vorgenommen werden (z.B. perl, code, perldoc) und die die Form haben
Taugt der was? oder habe ich was uebersehen? Code (perl): (dl
)
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 #! /usr/bin/perl use warnings; use strict; use vars qw(@TagsBlocked); @TagsBlocked = qw(perle codex perldocy); $/ = undef; my $posting = <DATA>; &ParseText($posting); # ------------------------------------------------------------ sub ParseText { my ($text) = @_; my @scanned = (); foreach my $tag (@TagsBlocked) { # @scanned yet empty, start with $text unless (scalar (@scanned)) { @scanned = &PostFilterBlocked($text, $tag); } # unless # @scanned filled, start scanning the rest else { my @scanned2 = (); # temporary variable foreach (@scanned) { # skip already scanned parts push(@scanned2, $_), next if ref($_); # scan only if scalar push (@scanned2, &PostFilterBlocked($_, $tag)); } # foreach @scanned = @scanned2; } # else } # foreach use Data::Dumper; print " ", Dumper(\@scanned); } # ParseText # ------------------------------------------------------------ sub PostFilterBlocked { my ($text, $tag) = @_; my $startLength = length("[$tag]"); my $endLength = length("[/$tag]"); my @scanned = (); my $startPos = index($text, "[$tag]"); my $lastPos = 0; print " Scanning for tag [$tag]"; while ($startPos != -1) { print " $startPos"; # add leading part to @scanned my $string = substr($text, $lastPos, $startPos-$lastPos); push (@scanned, $string) if length $string; # check if [$tag]...[/$tag]-block found $lastPos = index($text, "[/$tag]", $startPos+$startLength); print "-$lastPos"; unless ($lastPos == -1) { # if found # push reference to @scanned: { $tag => $string } my $string = substr($text, $startPos+$startLength, $lastPos-$startPos-$startLength); push (@scanned, {$tag => $string}) if length $string; } # if else { # if not found # push rest of $text to @scanned (as string) my $string = substr($text, $startPos, -1); # if last line was in @TagsBlocked, add new array element if (ref $scanned[-1]) { push (@scanned, $string) if length $string; } # if not, append as text to the last element else { $scanned[-1] .= $string; } # else last; # and exit while } # else # (re-)initialize next search position $lastPos += $endLength; # search for next [tag] $startPos = index($text, "[$tag]", $lastPos); } # while # care for rest of $text if ($lastPos != -1) { my $string = substr($text, $lastPos, -1); push (@scanned, $string) if length($string); } # if return @scanned; } # PostFilterBlocked # ------------------------------------------------------------ Hallo Leute, das ist mein erstes Posting: [perldocy]CGI[/perldocy] [perle] #! /usr/bin/perl use warnings; use strict; use CGI (); my $cgi = CGI->new(); [/perle] und das ist was anderes: [perldocy]CGI[/perldocy] [perle] #! /usr/bin/perl use warnings; use strict; use CGI (); my $cgi = CGI->new(); my $string = "[perldocy]CGI[/perldocy]"; [/perle] normaltext [codex] #! /usr/bin/perl use warnings; use strict; use CGI (); my $cgi = CGI->new(); [/codex] [perle] this is perl without finishing tag [codex] #! /usr/bin/perl [/codex] Habe perle, codex und perldocy genommen, damit mir das forum nicht dazwischenpfuscht... <edit>hmmm, irgendwie mag [perl] diesen code nicht... \ n wird durch einen Zeilenumbruch ersetzt, die formatierung ist auch nicht das wahre... wenn jemand den code haben will, einfach eine mail schicken an martin@fabiani.net oder eine nachricht uebers forum\n\n <!--EDIT|Strat|1061898246--> perl -le "s::*erlco'unaty.'.dk':e,y;*kn:ai;penmic;;print"
http://www.fabiani.net/ |