Facing problem with HTML::Parser

Shivani Palle <[email protected]> Thu, 11 Aug 2016 11:14:44 +0530
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <CAH0Myt8GNrMXqHB4ge-FnB+qkpzL4K_5SAVx=eBcU=QqaN5hMA@mail.gmail.com>
--001a11471ac25374100539c543e5
Content-Type: text/plain; charset=UTF-8

Hi,


I am facing one issue while using HTML::Parser. Please help me.

*Issue:*

I am using HTML::Parser to parse all the HTML files through out the
directories to get hard coded strings from the html files(text between the
tags).

the code is like this:

 #!/usr/bin/perl -w
package Example;
require HTML::Parser;
@Example::ISA = qw(HTML::Parser);
use File::Find;
use File::Basename;

#my @files = glob("*.thtml");
find({ wanted => \&process_file, no_chdir => 1 },
"/mnt/src/xxx/git/xxx-ive-rdv/");

#foreach $file (@files){
sub process_file {
   if (-f $_) {
       if ($_ =~ m/(.thtml)$/i) {
   #my($file, $dir, $ext) = fileparse($_);
   my $file = $_;
    #step1: Parsing the html file and storing the parsed content in another
file
    my $parser = Example->new;
    $parser->ignore_elements(qw(script)); #ignoring script elements
    $parser->parse_file($file);
    print  $parser->{TEXT};

    sub text
    {
        my ($self,$text) = @_;
        $self->{TEXT} .= $text."\n";
    }
    open(my $fh, '>', 'parserOutput.txt');
    print $fh  $parser->{TEXT};
    close $fh;
   }
  }
}



*Failing case*:

It is breaking some lines in to two lines.
For example, I have the following line.

*Before Parsing:*
<label for="chkInstallAgent">Install Agent for this role</label>

*After Parsing*:
Install Agent for this
role

There is no tag in "Install Agent for this role". But still it is breaking
in to two lines.
Can you please help me with it.



Thanks,
Sivani

--001a11471ac25374100539c543e5
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Hi,</div><div><br></div><div><br></div><div>I am faci=
ng one issue while using HTML::Parser. Please help me.</div><div><br></div>=
<div><i><b>Issue:</b></i></div><div><br></div><div>I am using HTML::Parser =
to parse all the HTML files through out the directories to get hard coded s=
trings from the html files(text between the tags).</div><div><br></div><div=
>the code is like this:</div><div><br></div><div>=C2=A0#!/usr/bin/perl -w</=
div><div>package Example;</div><div>require HTML::Parser;</div><div>@Exampl=
e::ISA =3D qw(HTML::Parser);</div><div>use File::Find;</div><div>use File::=
Basename;</div><div><br></div><div>#my @files =3D glob(&quot;*.thtml&quot;)=
;</div><div>find({ wanted =3D&gt; \&amp;process_file, no_chdir =3D&gt; 1 },=
 &quot;/mnt/src/xxx/git/xxx-ive-rdv/&quot;);</div><div><br></div><div>#fore=
ach $file (@files){</div><div>sub process_file {</div><div>=C2=A0 =C2=A0if =
(-f $_) {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0if ($_ =3D~ m/(.thtml)$/i) {=
</div><div>=C2=A0 =C2=A0#my($file, $dir, $ext) =3D fileparse($_);</div><div=
>=C2=A0 =C2=A0my $file =3D $_;=C2=A0</div><div>=C2=A0 =C2=A0 #step1: Parsin=
g the html file and storing the parsed content in another file</div><div>=
=C2=A0 =C2=A0 my $parser =3D Example-&gt;new;</div><div>=C2=A0 =C2=A0 $pars=
er-&gt;ignore_elements(qw(script)); #ignoring script elements</div><div>=C2=
=A0 =C2=A0 $parser-&gt;parse_file($file);</div><div>=C2=A0 =C2=A0 print =C2=
=A0$parser-&gt;{TEXT};</div><div><br></div><div>=C2=A0 =C2=A0 sub text</div=
><div>=C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 my ($self,$text=
) =3D @_;</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 $self-&gt;{TEXT} .=3D $text=
.&quot;\n&quot;;</div><div>=C2=A0 =C2=A0 }</div><div>=C2=A0 =C2=A0 open(my =
$fh, &#39;&gt;&#39;, &#39;parserOutput.txt&#39;);</div><div>=C2=A0 =C2=A0 p=
rint $fh =C2=A0$parser-&gt;{TEXT};</div><div>=C2=A0 =C2=A0 close $fh;</div>=
<div>=C2=A0 =C2=A0}</div><div>=C2=A0 }</div><div>}</div><div><br></div><div=
><br></div><div><br></div><div><b><i>Failing case</i></b>:</div><div><br></=
div><div>It is breaking some lines in to two lines.</div><div>For example, =
I have the following line.</div><div><br></div><div><b>Before Parsing:</b><=
/div><div>&lt;label for=3D&quot;chkInstallAgent&quot;&gt;Install Agent for =
this role&lt;/label&gt;</div><div><br></div><div><b>After Parsing</b>:</div=
><div>Install Agent for this</div><div>role</div><div><br></div><div>There =
is no tag in &quot;Install Agent for this role&quot;. But still it is break=
ing in to two lines.</div><div>Can you please help me with it.</div><div><b=
r></div><div><br></div><div><br></div><div>Thanks,</div><div>Sivani</div><d=
iv><br></div></div>

--001a11471ac25374100539c543e5--