| 1 | package WebSource::Parser; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use XML::LibXML; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use HTML::TreeBuilder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | { | ||||||||||
| 8 | package MyTreeBuilder; | ||||||||||
| 9 | 2 | our @ISA = ('HTML::TreeBuilder'); | |||||||||
| 10 | |||||||||||
| 11 | 0 | ▶ | sub start { | ||||||||
|
Subroutine
start: not called CC=4 SCAR=30.0
| |||||||||||
| 12 | 0 | my ($self,$tag,$attr,$attrseq,$origtext) =@_; | |||||||||
| 13 | 0 | my %nattr; | |||||||||
| 14 | my @naseq; | ||||||||||
| 15 | # Clean up attributes | ||||||||||
| 16 | 0 | foreach my $a (@$attrseq) { | |||||||||
| 17 | 0 | ▶ | if($a =~ m#[^\w_:\-]#) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 0 | ▶ | $self->{verbose} and warn "Bad attribute $a detected and removed"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | } else { | ||||||||||
| 20 | 0 | push @naseq, ($a); | |||||||||
| 21 | 0 | $nattr{$a} = $attr->{$a}; | |||||||||
| 22 | } | ||||||||||
| 23 | } | ||||||||||
| 24 | 0 | $self->SUPER::start($tag,\%nattr,\@naseq,$origtext); | |||||||||
| 25 | } | ||||||||||
| 26 | 0 | ▶ | sub text { | ||||||||
|
Subroutine
text: not called CC=6 SCAR=37.4
| |||||||||||
| 27 | 0 | my ($self,$origtext,$iscdata) = @_; | |||||||||
| 28 | 0 | ▶ | if(!$iscdata) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | ▶ | $origtext =~ /Sion/ and print "Text : $origtext\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | ▶ | if($origtext =~ m/\0/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 0 | ▶ | $self->{verbose} and warn "Decected null char\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | $origtext =~ s/\0//g; | |||||||||
| 33 | } | ||||||||||
| 34 | 0 | ▶ | if($origtext =~ m/\&\#[0-9]\;/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | warn "Bad entity detected"; | |||||||||
| 36 | 0 | $origtext =~ s/\&\#[0-9]\;//g; | |||||||||
| 37 | } | ||||||||||
| 38 | } | ||||||||||
| 39 | 0 | $self->SUPER::text($origtext,$iscdata); | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 2 | our @ISA = ("XML::LibXML"); | |||||||||
| 45 | =head1 NAME | ||||||||||
| 46 | |||||||||||
| 47 | WebSource::Parser - A XML/HTML parser extending XML::LibXML | ||||||||||
| 48 | |||||||||||
| 49 | =head1 DESCRIPTION | ||||||||||
| 50 | |||||||||||
| 51 | A simple XML::LibXML extention to be more robust in parsing HTML by | ||||||||||
| 52 | using HTML::TreeBuilder | ||||||||||
| 53 | |||||||||||
| 54 | =head1 SYNOPSIS | ||||||||||
| 55 | |||||||||||
| 56 | my $parser = WebSource::Parser->new; | ||||||||||
| 57 | |||||||||||
| 58 | =head1 METHODS | ||||||||||
| 59 | |||||||||||
| 60 | =over 2 | ||||||||||
| 61 | |||||||||||
| 62 | =item B<< $parser = WebSource::Parser->new; >> | ||||||||||
| 63 | |||||||||||
| 64 | Create a new WebSource::Parser | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 69 | 0 | my $class = shift; | |||||||||
| 70 | 0 | my $self = $class->SUPER::new(verbose => 1, @_); | |||||||||
| 71 | 0 | return $self; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | =item B<< $parser->parse_html_file($file); >> | ||||||||||
| 75 | |||||||||||
| 76 | Parse an html file | ||||||||||
| 77 | |||||||||||
| 78 | =cut | ||||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | sub parse_html_file { | ||||||||
|
Subroutine
parse_html_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 81 | 0 | my $self = shift; | |||||||||
| 82 | 0 | my $file = shift; | |||||||||
| 83 | 0 | my $tb = MyTreeBuilder->new; | |||||||||
| 84 | # $tb->xml_mode(1); | ||||||||||
| 85 | 0 | $tb->parse_file($file); | |||||||||
| 86 | 0 | return $self->SUPER::parse_string($tb->as_XML); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | =item B<< $parser->parse_html_string($string); >> | ||||||||||
| 90 | |||||||||||
| 91 | Parse an html string | ||||||||||
| 92 | |||||||||||
| 93 | =cut | ||||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | sub parse_html_string { | ||||||||
|
Subroutine
parse_html_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 96 | 0 | my $self = shift; | |||||||||
| 97 | 0 | my $string = shift; | |||||||||
| 98 | 0 | my $tb = MyTreeBuilder->new; | |||||||||
| 99 | 0 | $tb->parse($string); | |||||||||
| 100 | 0 | $tb->eof; | |||||||||
| 101 | 0 | return $self->SUPER::parse_string($tb->as_XML); | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | =item B<< $parser->parse_html_string($string); >> | ||||||||||
| 105 | |||||||||||
| 106 | Parse an HTML string chunk and return the corresponding nods | ||||||||||
| 107 | |||||||||||
| 108 | =cut | ||||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | sub parse_html_chunks { | ||||||||
|
Subroutine
parse_html_chunks: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 111 | 0 | my $self = shift; | |||||||||
| 112 | 0 | my $string = shift; | |||||||||
| 113 | 0 | my $tb = MyTreeBuilder->new; | |||||||||
| 114 | 0 | $tb->parse($string); | |||||||||
| 115 | 0 | $tb->eof; | |||||||||
| 116 | 0 | return map { $self->SUPER::parse_string($tb->as_XML) } $tb->guts(); | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | =head1 SEE ALSO | ||||||||||
| 120 | |||||||||||
| 121 | XML::LibXML | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||
| 124 | |||||||||||
| 125 | 2 | 1; | |||||||||