#!/usr/bin/perl -- use strict; use warnings; use Test::More qw' no_plan '; use Regexp::Common qw/ balanced /; use Data::Dump qw/ dd pp /; sub TRACE; sub DEBUG; *TRACE = *DEBUG = sub { print STDERR @_,"\n" }; #~ *TRACE = *DEBUG = sub { }; our $allowed = join '|', qw[ ABBR ACRONYM B BIG CITE CODE DFN EM I KBD SAMP SMALL SPAN STRONG SUB SUP TT VAR ]; { my $in = 'SPAN|class="span_class"> some more text B.'; my $out = shabba($in); my $wanted = 'text italic text some more text bold text.'; is( $out, $wanted , 'shabba'); } { my $in = 'SPAN text>|class="span_class"> some more text B.'; my $out = shabba($in); my $wanted = 'text italic and bold text some more text bold text.'; is( $out, $wanted , 'shabba'); } exit( 0 ); sub shabba { local $_ = $_[0]; my $dent = $_[1] || 0; pos = 0; my $ret = ""; SHABBALOOP: while( length > pos ){ m{\G(\s+)}gcsx and do { $ret .= $1; next SHABBALOOP; }; m{ \G( $allowed )( $RE{balanced}{-parens=>'<>'} ) }gcsx and do { TRACE "# $dent allowed<> { $1 ( $2 ) }"; $ret .= shabba_allowed( "$1" , "$2" , $dent ); next SHABBALOOP; }; #~ confusion :) #~ \G(\w+\b) #~ fail #~ \G([^<]+)(?!:$allowed)\b #~ \w+\b #~ \G([^<]+?)(?!:$allowed\<)\b #~ fail #~ \G([^<]+?)(?!:$allowed\<) #~ fail #~ \G([^<]+)(?!:$allowed\<) #~ fail \G([^<]+)(?!:\<) #~ inch #~ \G([^<]+?)(?!:\<) #~ \G([^<]+?\b[^<]) #~ \G([^<]+?[^<]) #~ FAIL #~ \G([^<]+[^<]) #~ \G([^<]+[^<]\b) #~ 2same#~ \G( (?!:$allowed\<) .+ ) #~ 2same#~ \G( .+(?!:$allowed\<) ) m{ \G([^<]+\s) }gcmx and do { TRACE "# $dent text { $1 }"; $ret .= shabba_text( "$1" ); next SHABBALOOP; };;; m{ \G([\<\>]) }gcmx and do { TRACE "## $dent error-stray<> { $1 } at pos(@{[pos]})"; last SHABBALOOP; };;; m{ \G(\S) }gcmx and do { TRACE "# $dent inch-forward { $1 }"; $ret .= shabba_text( "$1" ); next SHABBALOOP; };;; } $ret; } #~ sub shabba_allowed { join'',@_ } #~ confusion :) #~ use Text::Balanced qw' :ALL '; #~ dd([ extract_multiple( $stuff,[\&extract_bracketed, ],)]); #~ my $extract_allowed = gen_extract_tagged(qw/$allowed');dd([ extract_multiple( $stuff,[ $extract_allowed , $extract_allowed , ],)]); #~ #~ 0 and $stuff =~ s{ #~ ( $allowed )( $RE{balanced}{-parens=>'<>'} ) #~ | #~ (.) #~ }{ #~ if( defined $2 ){ #~ $ret .= $2; #~ } else { #~ $ret .= shabba_allowed( "$1" ); #~ } #~ ""; #~ }gsex; sub shabba_allowed { my( $tag , $stuff, $dent ) = @_; $stuff = $1 if $stuff =~ m{^<(.*)>$}gs; my $ret = ""; $ret .= "<\L$tag\E" if $tag; $stuff =~ s{\|([^<>]+)$}{ $ret .= " $1"; ## shabba_allowed_atts($tag,$1); ""; }gsex if defined $stuff ; $ret .= ">" if $tag; if( defined $stuff and length $stuff and $stuff =~ m{[<>]}g ){ $ret .= shabba( $stuff , $dent+1) ; ## recurse } else { $ret .= $stuff; } $ret .= "" if $tag; $ret; } sub shabba_text { join'',@_ } __END__ $ prove -vb lady.alena.balanced.podlike.pl lady.alena.balanced.podlike.pl .. # 0 allowed<> { SPAN ( |class="span_class"> ) } # 1 text { text } # 1 allowed<> { I ( ) } # 0 text { some more text } # 0 allowed<> { B ( ) } # 0 inch-forward { . } # 0 allowed<> { SPAN ( text>|class="span_class"> ) } ok 1 - shabba # 1 text { text } # 1 allowed<> { I ( text> ) } # 2 text { italic } # 2 allowed<> { B ( ) } # 2 inch-forward { t } # 2 inch-forward { e } # 2 inch-forward { x } # 2 inch-forward { t } # 0 text { some more text } # 0 allowed<> { B ( ) } # 0 inch-forward { . } ok 2 - shabba 1..2 ok All tests successful. Files=1, Tests=2, 0 wallclock secs ( 0.11 usr + 0.02 sys = 0.12 CPU) Result: PASS