tshabet has asked for the wisdom of the Perl Monks concerning the following question:
Since I'm turning this stuff into XML, I'm using the fantastic Text::Balanced module to balance the braces so that the above can be turned into{Curl is all about {braces}}
OK, so far so good, right? Now I'm having no problems achieveing this conversion. The problem comes when I have something like<Curl> is all about <braces/></Curl>
which should become{Curl is all about {code {braces}}}
So, as you see, the code tag works in much the same way as HTML. Still cool, right? It gets a little more complicated. The code<Curl> is all about {braces}</Curl>
should become{Curl is all about {code {braces}{escape {braces}}}}
OK, that's as complicated as it gets, aside from the fact that there are a few other tags that act the same as "code," but that's no biggie. Anyway, here's the code I implemented to hopefully make this work:<Curl> is all about {braces}<braces/></Curl>
So this code (in my mind anyway) slurps up entire blocks of balanced code and then looks within for a "code"ish tag, looks within that for an "escape" tag, makes the conversions, then sort of backs out of the sub-block and does the necessary replacements. Hopefully my code is easier to follow than that last sentence :-)while($next = (extract_bracketed($text, '{}', '[^{}]*' ))[0]) #this is + general. { $holder = $next; if($bext = (extract_bracketed($next, '{}', '(?s).*?(?=\{ctext|\{co +de|\{example|\{pre)' ))[0]) #this handles "code" and the like. { $bolder = $bext; while($cext = (extract_bracketed($bext, '{}', '(?s).*?(?=\{escape) +' ))[0]) #this is for escaped "code" and the like. { $colder = $cext; $cext =~ s/\{([^ \s|\}]*?)\}/<$1\/>/gix; $cext =~ s/\{([\w|-]*)(.*)\}/<$1>$2<\/$1>/osi; $bext =~ s/$colder/$cext/sgi; } $bext =~ s/\{pre(.*)\}/\<pre\>$1<\/pre>/gosix; $bext =~ s/\{ctext(.*)\}/\<ctext\>$1<\/ctext>/gosix; $bext =~ s/\{code(.*)\}/\<code\>$1<\/code>/gosix; $bext =~ s/\{example(.*)\}/\<example\>$1<\/example>/gosix; $bext =~ s/\}/ebrac/g; $bext =~ s/\{/obrac/g; $next =~ s/$bolder/$bext/sgi; } $next =~ s/\{([^ \s|\}]*?)\}/<$1\/>/gix; $next =~ s/\{([\w|-]*)(.*)\}/<$1>$2<\/$1>/osi; $text =~ s/$holder/$next/sgi; }
|
|---|