Given an HTML::Element, some match text and a formatting tag this code runs through and wraps matching text in the given formatting tags.

For example:

use strict; use warnings; use HTML::TreeBuilder; my $root = HTML::TreeBuilder->new (); $root->parse_file (*DATA); formatText ($root, 'bold', 'b'); formatText ($root, 'wrap selected text in formatting', 'i'); print $root->as_HTML(); __DATA__ <p>This test script demonstrates a way to run through the <em>text</em +> in a chunk of HTML and wrap selected text in formatting tags. </p> <p>Note that the text to be formatted must not contain any tags in the + original or the text will not match and that no checks are made for silly nesti +ng of formatting - re-formatting bold text won't make it double <i>bold</i> +but will double the bold tags.</p>

Prints

<html><head></head><body><p>This test script demonstrates a way to run + through the <em>text</em> in a chunk of HTML and <i>wrap selected te +xt in formatting</i> tags. <p>Note that the text to be formatted must + not contain any tags in the original or the text will not match and +that no checks are made for silly nesting of formatting - re-formatti +ng <b>bold</b> text won't make it double <i><b>bold</b></i> but will +double the <b>bold</b> tags.</body></html>
sub formatText { my ($node, $target, $tag) = @_; my @children = $node->detach_content (); my @newChildren; for (@children) { if (ref) { formatText ($_, $target, $tag); push @newChildren, $_; next; } my @fragments = split /($target)/, $_; while (@fragments) { push @newChildren, shift @fragments; last if ! @fragments; my $element = HTML::Element->new ($tag); $element->push_content (shift @fragments); push @newChildren, $element; } } $node->push_content (@newChildren) if @newChildren; }

In reply to Format key words in HTML text by GrandFather

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.