It's easy enough to change it to a single function...

use strict; use warnings; use Text::Balanced qw( extract_bracketed ); use HTML::Entities qw( encode_entities ); my $default_allowed_tags = do { my @tags = qw( A ABBR ACRONYM B BIG CITE CODE DFN EM I KBD Q SAMP SMALL SPAN STRONG SUB SUP TT VAR ); qr/${\( join "|", map quotemeta, @tags )}/; }; sub parse_markup { my ($text, $tags) = @_; $tags ||= $default_allowed_tags; my ($before, $match) = ($text =~ m{ \A # start of string (.*?) # leading text ($before) ( # either... \<\!-- # the start of a comment | # or... $tags\< # a tag ) }xsm) or do { my @return = split /\|/, $text; $return[0] = encode_entities($return[0]); return @return; }; # strip $before from $text substr($text, 0, length($before)) = ''; # If the first thing that needed to be handled was a comment if ($match eq '<!--') { # Strip it out $text =~ s/\<\!--(.+?)--\>//g; # Handle the rest via recursion return join "", $before, parse_markup($text, $tags); } chop(my $found_tag = lc $match); substr($text, 0, length($found_tag)) = ''; my ($got, $remainder) = extract_bracketed($text, q/<"'>/); $got = substr($got, 1, length($got) - 2); my ($markup, @attrs) = parse_markup($got, $tags); my ($more_markup, @more_attrs) = parse_markup($remainder, $tags); defined($_) or $_='' for $markup, $more_markup; join("", $before, (@attrs ? "<$found_tag @attrs>" : "<$found_tag>"), $markup, "</$found_tag>", $more_markup, ), @more_attrs; } print for parse_markup(<<'TEXT'); Anyone who watches the Syfy channel knows that on Monday nights they aired three television series I<A<EurSUP<e>ka|href="Movies_by_series.pl?series=EWA#EUReKA">|class="t +itle">, I<A<Warehouse & 13|href="Movies_by_series.pl?series=EWA#Warehouse_13"> +>, and I<A<Alphas|href="Movies_by_series.pl?series=EWA#Alphas">>. Some might not be aware that these three series have formed a crossove +r cosmology which I call A<EWA|href="Movies_by_series.pl?series=EWA"> <!-- This is a long string. --> TEXT

But personally I prefer the OO version because it means I can have a $parser object, which I can pass around. Code that needs to process some markup gets given the markup, and given the parser, and simply throws the markup at the parser. This makes it easy to switch in a different parser if required (say, one that generated stricter XHTML, or one that processed Markdown).

c

In reply to Re^3: RFC: Is there a better way to use Text::Balanced? by tobyink
in thread RFC: Is there a better way to use Text::Balanced? by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.