| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use HTML::ListScraper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use HTML::ListScraper::Interactive qw(format_tags canonicalize_tags); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Fatal qw(open close); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use vars qw(*EXPORT *IMPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub show_core; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | my $whole; | |||||||||||||||||||||||||||
| 16 | my $shapeless; | ||||||||||||||||||||||||||||
| 17 | 6 | my $min_count = 1; | |||||||||||||||||||||||||||
| 18 | 6 | my @core_values = qw(item list all); | |||||||||||||||||||||||||||
| 19 | 6 | my $core; | |||||||||||||||||||||||||||
| 20 | 6 | my @detail_values = qw(none tags text attributes all); | |||||||||||||||||||||||||||
| 21 | 6 | my $detail = 'tags'; | |||||||||||||||||||||||||||
| 22 | 6 | my $export; | |||||||||||||||||||||||||||
| 23 | my $import; | ||||||||||||||||||||||||||||
| 24 | 6 | my $acquire; | |||||||||||||||||||||||||||
| 25 | 6 | my $ignore; | |||||||||||||||||||||||||||
| 26 | 6 | GetOptions('whole' => \$whole, | |||||||||||||||||||||||||||
| 27 | 'shapeless' => \$shapeless, | ||||||||||||||||||||||||||||
| 28 | 'min-count=i' => \$min_count, | ||||||||||||||||||||||||||||
| 29 | 'core=s' => \$core, | ||||||||||||||||||||||||||||
| 30 | 'detail=s' => \$detail, | ||||||||||||||||||||||||||||
| 31 | 'export=s' => \$export, | ||||||||||||||||||||||||||||
| 32 | 'import=s' => \$import, | ||||||||||||||||||||||||||||
| 33 | 'acquire=s' => \$acquire, | ||||||||||||||||||||||||||||
| 34 | 'ignore=s' => \$ignore | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 6 | my $argc = scalar(@ARGV); | |||||||||||||||||||||||||||
| 38 | 6 | ▶ | if (($argc < 1) || ($argc > 2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$argc < 1 or $argc > 2
MC/DC0%0/2
$argc < 1 or $argc > 2
$argc < 1
$argc > 2
Truth table33%1/3
A or B
A= $argc < 1B= $argc > 2
| |||||||||||||||||||||||||||||
| 39 | 0 | die "usage: $0 [ options ] filename\n"; | |||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # binmode(STDOUT, ":utf8"); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 6 | my $scraper = HTML::ListScraper->new( api_version => 3, | |||||||||||||||||||||||||||
| 45 | marked_sections => 1 ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 6 | my @ignore; | |||||||||||||||||||||||||||
| 48 | 6 | ▶ | if (!$ignore) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 6 | @ignore = qw(b i em strong); | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 0 | @ignore = map { lc $_; } split /\s*,\s*/, $ignore; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 6 | ▶ | if (@ignore) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 6 | $scraper->ignore_tags(@ignore); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 6 | ▶ | if ($shapeless) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $scraper->shapeless(1); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 6 | ▶ | if ($min_count > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | $scraper->min_count($min_count); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 6 | my $name = $ARGV[0]; | |||||||||||||||||||||||||||
| 67 | 6 | ▶ | if ($acquire || ($name =~ /^http:\/\//)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$acquire or $name =~ m[^http://]
MC/DC0%0/2
$acquire or $name =~ m[^http://]
$acquire
$name =~ m[^http://]
Truth table33%1/3
A or B
A= $acquireB= $name =~ m[^http://]
| |||||||||||||||||||||||||||||
| 68 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 69 | 0 | my $resp = $ua->get($name); | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | if (!$resp->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | die $resp->status_line; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my $raw = $resp->content; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($acquire) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | open(EXPORT, ">$acquire"); | |||||||||||||||||||||||||||
| 77 | 0 | print EXPORT $raw; | |||||||||||||||||||||||||||
| 78 | 0 | close(EXPORT); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $html = $resp->decoded_content; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | if (!$html) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | warn "cannot decode incoming HTML: using raw bytes\n"; | |||||||||||||||||||||||||||
| 84 | 0 | $html = $raw; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | $scraper->parse($html); | |||||||||||||||||||||||||||
| 88 | 0 | $scraper->eof; | |||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||
| 90 | 6 | $scraper->parse_file($name); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 6 | ▶ | if ($whole) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | my $tags = $scraper->get_all_tags(); | |||||||||||||||||||||||||||
| 95 | 0 | print "---|\n", format_tags($scraper, $tags, { index => 1 }); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 6 | ▶ | if ($import && !$core) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$import and not $core
MC/DC0%0/2
$import and not $core
$import
not $core
Truth table66%2/3
A and not(B)
A= $importB= not $core
| |||||||||||||||||||||||||||||
| 99 | 0 | $core = 'l'; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 6 | ▶ | if ($core) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 6 | my %core_values; | |||||||||||||||||||||||||||
| 104 | 6 | foreach my $lv (@core_values) { | |||||||||||||||||||||||||||
| 105 | 18 | my $sv = substr($lv, 0, 1); | |||||||||||||||||||||||||||
| 106 | 18 | $core_values{$sv} = 1; | |||||||||||||||||||||||||||
| 107 | 18 | ▶ | if ($core eq $lv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 3 | $core = $sv; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 6 | ▶ | if (!exists($core_values{$core})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | my $cv = join ', ', @core_values; | |||||||||||||||||||||||||||
| 114 | 0 | die "invalid --core value; use one of [ $cv ]\n"; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 6 | show_core(); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | ▶ | if ($export) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | my @seq = $scraper->get_sequences; | |||||||||||||||||||||||||||
| 122 | 0 | my $seq = shift @seq; | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ($seq) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | open(EXPORT, ">$export"); | |||||||||||||||||||||||||||
| 125 | 0 | my @inst = $seq->instances; | |||||||||||||||||||||||||||
| 126 | 0 | my $inst = shift @inst; | |||||||||||||||||||||||||||
| 127 | 0 | my @tags = format_tags($scraper, [ $inst->tags ]); | |||||||||||||||||||||||||||
| 128 | 0 | print EXPORT @tags; | |||||||||||||||||||||||||||
| 129 | 0 | close(EXPORT); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 6 | ▶ | sub show_core { | ||||||||||||||||||||||||||
|
Subroutine
show_core: called CC=14 SCAR=26.7
| |||||||||||||||||||||||||||||
| 134 | 6 | my %detail_values; | |||||||||||||||||||||||||||
| 135 | 6 | foreach my $lv (@detail_values) { | |||||||||||||||||||||||||||
| 136 | 30 | $detail_values{$lv} = 1; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 6 | ▶ | if (!exists($detail_values{$detail})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | my $dv = join ', ', @detail_values; | |||||||||||||||||||||||||||
| 141 | 0 | die "invalid --detail value; use one of [ $dv ]\n"; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 6 | my @sequences; | |||||||||||||||||||||||||||
| 145 | 6 | ▶ | if (!$import) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 4 | @sequences = $scraper->find_sequences; | |||||||||||||||||||||||||||
| 147 | } else { | ||||||||||||||||||||||||||||
| 148 | 2 | open(IMPORT, $import); | |||||||||||||||||||||||||||
| 149 | 2 | my @import = <IMPORT>; | |||||||||||||||||||||||||||
| 150 | 2 | close(IMPORT); | |||||||||||||||||||||||||||
| 151 | 2 | my @tags = canonicalize_tags(@import); | |||||||||||||||||||||||||||
| 152 | 2 | my $s = $scraper->find_known_sequence(@tags); | |||||||||||||||||||||||||||
| 153 | 2 | ▶ | if ($s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 2 | push @sequences, $s; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 6 | foreach my $seq (@sequences) { | |||||||||||||||||||||||||||
| 159 | 6 | print "---\n"; | |||||||||||||||||||||||||||
| 160 | 6 | print "len: ", $seq->len, "\n"; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 6 | my $inst_count = scalar($seq->instances); | |||||||||||||||||||||||||||
| 163 | 6 | print "count: $inst_count\n"; | |||||||||||||||||||||||||||
| 164 | 6 | ▶ | if ($inst_count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 6 | print "seq:\n"; | |||||||||||||||||||||||||||
| 166 | 6 | foreach my $inst ($seq->instances) { | |||||||||||||||||||||||||||
| 167 | 62 | print " - ", $inst->match, ": ", $inst->start, "\n"; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 62 | ▶ | if ($detail ne 'none') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 50 | ▶ | if (defined($inst->score)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 14 | print " score: ", $inst->score, "\n"; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 50 | ▶ | my $incl_text = ($detail eq 'text') || ($detail eq 'all'); | ||||||||||||||||||||||||||
|
Condition66%2/3
$detail eq 'text' || $detail eq 'all'
MC/DC50%1/2
$detail eq 'text' || $detail eq 'all'
$detail eq 'text'
$detail eq 'all'
Truth table66%2/3
A || B
A= $detail eq 'text'B= $detail eq 'all'
| |||||||||||||||||||||||||||||
| 175 | 50 | ▶ | my $incl_attr = ($detail eq 'attributes') || | ||||||||||||||||||||||||||
|
Condition100%3/3
$detail eq 'attributes' || $detail eq 'all'
MC/DC100%2/2
$detail eq 'attributes' || $detail eq 'all'
$detail eq 'attributes'
$detail eq 'all'
Truth table100%3/3
A || B
A= $detail eq 'attributes'B= $detail eq 'all'
| |||||||||||||||||||||||||||||
| 176 | ($detail eq 'all'); | ||||||||||||||||||||||||||||
| 177 | 50 | my $indent = ' ' x 4; | |||||||||||||||||||||||||||
| 178 | my @scalar = map { | ||||||||||||||||||||||||||||
| 179 | 50 | "$indent$_"; | |||||||||||||||||||||||||||
| 180 | } format_tags($scraper, [ $inst->tags ], | ||||||||||||||||||||||||||||
| 181 | { text => $incl_text, attr => $incl_attr }); | ||||||||||||||||||||||||||||
| 182 | 50 | print " inst: |\n", @scalar; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 62 | ▶ | if ($core eq 'i') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | return; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 6 | ▶ | if ($core eq 'l') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 1 | return; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | __END__ | ||||||||||||||||||||||||||||