Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To do this with <a href="http://search.cpan.org/perldoc?HTML%3a%3aTreeBuilder" rel="nofollow">HTML::TreeBuilder</a>, you would read the file, modify the tree, and write it out (to the same file, or a different file). This is fairly complex, because you're trying to convert part of a text node into a tag, and because you have comments that can't move.</p> <p>A common idiom with HTML-Tree is to use a recursive function that modifies the tree:</p> <pre><code>use strict; use warnings; use 5.008; use File::Slurp 'read_file'; use HTML::TreeBuilder; sub replace_keyword { my $elt = shift; return if $elt-&gt;is_empty; $elt-&gt;normalize_content; # Make sure text is contiguous my $content = $elt-&gt;content_array_ref; for (my $i = 0; $i &lt; @$content; ++$i) { if (ref $content-&gt;[$i]) { # It's a child element, process it recursively: replace_keyword($content-&gt;[$i]) unless $content-&gt;[$i]-&gt;tag eq 'a'; # Don't descend into &lt;a&gt; } else { # It's text: if ($content-&gt;[$i] =~ /here/) { # your keyword or regexp here $elt-&gt;splice_content( $i, 1, # Replace this text element with... substr($content-&gt;[$i], 0, $-[0]), # the pre-match text # A hyperlink with the keyword itself: [ a =&gt; { href =&gt; 'http://example.com' }, substr($content-&gt;[$i], $-[0], $+[0] - $-[0]) ], substr($content-&gt;[$i], $+[0]) # the post-match text ); } # end if text contains keyword } # end else text } # end for $i in content index } # end replace_keyword my $content = read_file('foo.shtml'); # Wrap the SHTML fragment so the comments don't move: my $html = HTML::TreeBuilder-&gt;new; $html-&gt;store_comments(1); $html-&gt;parse("&lt;html&gt;&lt;body&gt;$content&lt;/body&gt;&lt;/html&gt;"); my $body = $html-&gt;look_down(qw(_tag body)); replace_keyword($body); # Now strip the wrapper to get the SHTML fragment back: $content = $body-&gt;as_HTML; $content =~ s!^&lt;body&gt;\n?!!; $content =~ s!&lt;/body&gt;\s*\z!!; print STDOUT $content; # Replace STDOUT with a suitable filehandle </code></pre> <p>The output from <code>as_HTML</code> will be syntactically correct HTML, but not necessarily nicely-formatted HTML for people to view the source of. You can use <a href="http://search.cpan.org/perldoc?HTML%3a%3aPrettyPrinter" rel="nofollow">HTML::PrettyPrinter</a> to write out the file if you want that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload