| 1 | # XML::Driver::HTML | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright (c) 2000 Michael Koehne <kraehe@copyleft.de> | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # XML::Driver::HTML is free software. You can use and redistribute | ||||||||||||||||||||||||||||
| 6 | # this copy under terms of the GNU General Public License. | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | no warnings 'utf8' ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package XML::Driver::HTML; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use HTML::TreeBuilder; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use vars qw($VERSION $METHODS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 18 | 1 | $METHODS = { | |||||||||||||||||||||||||||
| 19 | start_document => 1, | ||||||||||||||||||||||||||||
| 20 | end_document => 1, | ||||||||||||||||||||||||||||
| 21 | start_element => 1, | ||||||||||||||||||||||||||||
| 22 | end_element => 1, | ||||||||||||||||||||||||||||
| 23 | characters => 1, | ||||||||||||||||||||||||||||
| 24 | comment => 1 | ||||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | $HTML::TreeBuilder::Debug = 0; # default debug level | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 1 | my $proto = shift; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | bless($self, $class); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=16 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $args = ($#_ == 0) ? { %{ (shift) } } : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 3 | my $file; | |||||||||||||||||||||||||||
| 41 | 3 | my $result = undef; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | $self->{'Source'} = $args->{'Source'} if $args->{'Source'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 3 | ▶ | $self->{'Handler'} = $args->{'Handler'} if $args->{'Handler'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 3 | ▶ | die "no Source defined" unless $self->{'Source'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | die "no Handler defined" unless $self->{'Handler'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | my $h = HTML::TreeBuilder->new; | |||||||||||||||||||||||||||
| 50 | 3 | $h->ignore_unknown(1); | |||||||||||||||||||||||||||
| 51 | 3 | $h->warn(0); | |||||||||||||||||||||||||||
| 52 | 3 | $h->{'_store_comments'}=1; | |||||||||||||||||||||||||||
| 53 | 3 | $h->{'_store_declarations'}=1; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 3 | ▶ | if ($self->{'Source'}{'ByteStream'}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 56 | 1 | $h->parse_file($self->{'Source'}{'ByteStream'}); | |||||||||||||||||||||||||||
| 57 | } elsif ($self->{'Source'}{'String'}) { | ||||||||||||||||||||||||||||
| 58 | 1 | $h->parse($self->{'Source'}{'String'}); | |||||||||||||||||||||||||||
| 59 | 1 | $h->eof(); | |||||||||||||||||||||||||||
| 60 | } elsif ($self->{'Source'}{'SystemId'}) { | ||||||||||||||||||||||||||||
| 61 | 1 | $h->parse_file($self->{'Source'}{'SystemId'}); | |||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 0 | die "no Source defined"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3 | $self->{'Methods'} = {}; | |||||||||||||||||||||||||||
| 67 | 3 | foreach (keys %$METHODS) { | |||||||||||||||||||||||||||
| 68 | 18 | ▶ | $self->{'Methods'}{$_} = 1 if $self->{'Handler'}->can($_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | delete $self->{'Recode'}; | |||||||||||||||||||||||||||
| 72 | 3 | ▶ | $self->{'Recode'} = 1 if lc($self->{'Source'}{'Encoding'}) eq "iso-8859-1"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | $self->{'Recode'} = 1 if lc($self->{'Source'}{'Encoding'}) eq "iso88591"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 3 | ▶ | $self->{'Recode'} = 1 if lc($self->{'Source'}{'Encoding'}) eq "latin1"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | $self->{'Handler'}->start_document() | ||||||||||||||||||||||||||||
| 77 | 3 | ▶ | if $self->{'Methods'}{'start_document'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 3 | $self->dumptree($h); | |||||||||||||||||||||||||||
| 79 | $result = $self->{'Handler'}->end_document() | ||||||||||||||||||||||||||||
| 80 | 3 | ▶ | if $self->{'Methods'}{'end_document'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | $h = $h->delete(); # nuke it! | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 3 | return $result; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 165 | ▶ | sub dumptree { | ||||||||||||||||||||||||||
|
Subroutine
dumptree: called CC=18 SCAR=29.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 165 | my ($self,$element) = @_; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 165 | my $tag = $element->{'_tag'}; | |||||||||||||||||||||||||||
| 91 | 165 | my $cont = $element->{'_content'}; | |||||||||||||||||||||||||||
| 92 | 165 | my $attr = {}; | |||||||||||||||||||||||||||
| 93 | 165 | my $value; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 165 | ▶ | return if $tag eq "style"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 164 | ▶ | if ($tag) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 164 | ▶ | if ($self->{'Recode'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 45 | foreach (keys %$element) { | |||||||||||||||||||||||||||
| 100 | 187 | ▶ | if ($_ !~ '^_') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 42 | $value = $element->{$_}; | |||||||||||||||||||||||||||
| 102 | 42 | $value =~ | |||||||||||||||||||||||||||
| 103 | 0 | s/([\x80-\xFF])/chr(0xC0|ord($1)>>6).chr(0x80|ord($1)&0x3F)/eg; | |||||||||||||||||||||||||||
| 104 | 42 | $attr->{$_} = $value; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | 119 | foreach (keys %$element) { | |||||||||||||||||||||||||||
| 109 | 425 | ▶ | $attr->{$_} = $element->{$_} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | if $_ !~ '^_'; | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 164 | ▶ | if ($tag !~ /^\~/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | $self->{'Handler'}->start_element( { | ||||||||||||||||||||||||||||
| 116 | 'Name' => $tag, 'Attributes' => $attr | ||||||||||||||||||||||||||||
| 117 | } ) | ||||||||||||||||||||||||||||
| 118 | 161 | ▶ | if $self->{'Methods'}{'start_element'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 161 | foreach (@$cont) { | |||||||||||||||||||||||||||
| 121 | 315 | ▶ | if (ref $_ eq 'HTML::Element') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 162 | $self->dumptree($_) | |||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | 10 | s/([\x80-\xFF])/chr(0xC0|ord($1)>>6).chr(0x80|ord($1)&0x3F)/eg | |||||||||||||||||||||||||||
| 125 | 153 | ▶ | if $self->{'Recode'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | $self->{'Handler'}->characters( { 'Data' => $_ } ) | ||||||||||||||||||||||||||||
| 127 | 153 | ▶ | if ($self->{'Methods'}{'characters'} && (lc($_) !~ "^[ \t]*<!")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"Methods"}{"characters"} and not lc($_) =~ /^[ ]*<!/
MC/DC0%0/2
$self->{"Methods"}{"characters"} and not lc($_) =~ /^[ ]*<!/
$self->{"Methods"}{"characters"}
not lc($_) =~ /^[ ]*<!/
Truth table33%1/3
A and not(B)
A= $self->{"Methods"}{"characters"}B= not lc($_) =~ /^[ ]*<!/
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | $self->{'Handler'}->end_element( { 'Name' => $tag } ) | ||||||||||||||||||||||||||||
| 132 | 161 | ▶ | if $self->{'Methods'}{'end_element'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 164 | ▶ | if ($tag eq "~comment") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | $self->{'Handler'}->comment( { 'Data' => $attr->{'text'} } ) | ||||||||||||||||||||||||||||
| 136 | 3 | ▶ | if $self->{'Methods'}{'comment'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | 1; | |||||||||||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||||||||||