Hi i’m using HTML::TreeBuilder / HTML::Element to clean up some bad HTML produced by programs such as Microsoft Word.
Given the snippet of bad HTML in the example I need to extract the text between mosh="start" and mosh="stop". Note this is an arbitrary attribute set elsewhere in the code.
Also note that this is only an example: the only guarantee are the div where mosh starts and stops. These could also be tables or <p><b>.
The code below achieves this but each line is extracted multiple times because each child also has children.
$MoshText should be
Good Text can be pattern matched Wanted Text More Wanted TextYet More Wanted Text
But after the table
$MoshText is
Good Text can be pattern matched Good Text can be pattern matched Good Text can be pattern matched Good Text can be pattern matched
I then need to split $MoshText on m/matched/ into two strings and remove whatever object the original text was in.
How can I modify the code below to achieve this?
#!/usr/bin/perl
use HTML::TreeBuilder;
use HTML::Element;
my $body =qq(
<body>
<div mosh="start">Div where mosh set to start</div
<div>
<table>
<tr>
<td></td><td</td>
<th>Good Text can be pattern matched</th>
<td></td><td</td>
</tr>
</table
</div>
<p>
<p>
<b>Wanted Text</b>
<br>
<p><b>More Wanted Text</b></p>
<div>
<p><b>Yet More Wanted Text</b></p>
</div>
</p>
<div mosh="stop">Div where mosh set to stop bellow here is not needed</div>
);
my ($MoshText, $Flag);
my @kids = $body->content_list();
while (@kids) {
my $child = shift @kids;
if (ref $child) {
my $Mosh = child->attr("mosh");
if ($Mosh eq "start") {
$Flag = 1;
}
if ($Mosh eq "stop") {
$Flag = 0;
last;
}
if ($Flag == 1) {
my $T = $child->as_trimmed_text;
$MoshText = $MoshText . " " . $T;
}
unshift @kids, $child->content_list;
}
}
print $MoshText . "\n";
EDIT
To clarify what I meant by remove whatever object the original text was in
the table containing “Good Text can be pattern matched” shouldn’t be in a table but a div
I’m amusing is an object so I’d replace this object with a new div object something like
my $new = HTML::Element->new('tag','div');
$new->attr('class', 'MyClass');
$new->push_content('Good Text can be pattern matched');
but how would i now find the table delete and insert $new
Cleaned Output
<div>
Div where mosh set to start
</div>
<div class ='MyClass'>
Good Text can be pattern matched
</div>
<div class ='AnotherClass' >
Wanted Text More Wanted Text Yet More Wanted Text
</div>
<div mosh="stop">Div where mosh set to stop bellow here is not needed</div>
Hope that makes more sence
I think you understand why your code isn’t working. You are printing the text value of all the elements in the HTML, and because an element’s text value includes all of its descendants’ text nodes several pieces of text are appearing more than once.
You need to process the HTML tree recursively, checking the value of the
moshattribute for each element and keeping a flag accordingly (as you already do) and printing text nodes as you come across them only if the flag is set.This program demonstrates. I have shown splitting the string on
matched, but I’m not clear what you mean by remove whatever object the original text was in.output