Kind of hackish... but as the start handler is being called before the text handler, you could store the tag name in a global variable, which you can then use in the text method to decorate the content as desired:

... my $tagname; sub start { my ($self, $tag, $attr, $attrseq, $origtext) = @_; $tagname = $tag; # store tag name for later use if ($tag =~ /^span$/i && $attr->{'class'} =~ /^main-content$/i +) { # set if we find <span class="main-content" $content_flag = 1; } } sub text { my ($self, $text) = @_; # If we're in <H1>...</H1> or my $tagged_text = $text !~ /^\s*$/ ? "<$tagname>$text</$tagnam +e>" : $text; if ($content_flag) { $main_content .= $tagged_text; } } ...

Output:

<bold_text> Here's the body 1 </bold_text> <p> para1 </p> <p> para2 </p>

(Might need some further tweaking (like the $text !~ /^\s*$/ ? ...) to handle edge cases — but you get the idea.)


In reply to Re: Parsing HTML with tags intact by Anonyrnous Monk
in thread Parsing HTML with tags intact by mr_p

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.