| 1 | package HTML::Extract::CPANModules; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | our $DATE = '2016-03-07'; # DATE | |||||||||||||||||||
| 4 | 1 | our $VERSION = '0.04'; # VERSION | |||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use 5.010001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | our @EXPORT_OK = qw(extract_cpan_modules_from_html); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | our %SPEC; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | $SPEC{extract_cpan_modules_from_html} = { | ||||||||||||||||||||
| 16 | 1 | v => 1.1, | |||||||||||||||||||
| 17 | summary => 'Extract CPAN module names from an HTML document', | ||||||||||||||||||||
| 18 | args => { | ||||||||||||||||||||
| 19 | html => { | ||||||||||||||||||||
| 20 | schema => 'str*', | ||||||||||||||||||||
| 21 | req => 1, | ||||||||||||||||||||
| 22 | tags => ['category:input'], | ||||||||||||||||||||
| 23 | pos => 0, | ||||||||||||||||||||
| 24 | cmdline_src => 'stdin_or_files', | ||||||||||||||||||||
| 25 | }, | ||||||||||||||||||||
| 26 | from_text => { | ||||||||||||||||||||
| 27 | schema => 'bool', | ||||||||||||||||||||
| 28 | default => 0, | ||||||||||||||||||||
| 29 | description => <<'_', | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | If set to true, will try to extract things that look like a Perl module name | ||||||||||||||||||||
| 32 | from text in HTML, e.g.: `Foo::Bar`, `Baz::Qux::2048` (basically, anything that | ||||||||||||||||||||
| 33 | looks like a package name). This means that single words (package without a | ||||||||||||||||||||
| 34 | double colon and a subpackage) won't be picked up. | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | _ | ||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||
| 38 | from_links => { | ||||||||||||||||||||
| 39 | schema => 'bool', | ||||||||||||||||||||
| 40 | default => 1, | ||||||||||||||||||||
| 41 | description => <<'_', | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | If set to true (the default), will try to extract module names from URLs. Some | ||||||||||||||||||||
| 44 | URLs are recognized, e.g.: | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | https://metacpan.org/pod/Foo::Bar | ||||||||||||||||||||
| 47 | https://search.cpan.org/~user/Foo-Bar-1.23/lib/Foo/Bar.pm | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | and so on. Currently, the CPAN module `CPAN::Module::FromURL` is used to | ||||||||||||||||||||
| 50 | recognize the URLs. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | _ | ||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||
| 55 | result_naked => 1, | ||||||||||||||||||||
| 56 | result => { | ||||||||||||||||||||
| 57 | schema => ['array*', of=>'str*'], | ||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||
| 60 | 0 | ▶ | sub extract_cpan_modules_from_html { | ||||||||||||||||||
|
Subroutine
extract_cpan_modules_from_html: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 61 | 0 | require DOM::Tiny; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | my %args = @_; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | my %mods; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | my $dom = DOM::Tiny->new($args{html}); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | if ($args{from_links} // 1) { | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$args{"from_links"} // 1
MC/DC0%0/1
$args{"from_links"} // 1
$args{"from_links"}
Truth table0%0/2
A
A= $args{"from_links"}
| |||||||||||||||||||||
| 70 | 0 | require CPAN::Module::FromURL; | |||||||||||||||||||
| 71 | 0 | for my $link ($dom->find('a[href]')->each) { | |||||||||||||||||||
| 72 | #say $link; | ||||||||||||||||||||
| 73 | 0 | my $url = $link->attr->{href}; | |||||||||||||||||||
| 74 | 0 | my $m = CPAN::Module::FromURL::extract_cpan_module_from_url($url); | |||||||||||||||||||
| 75 | 0 | ▶ | next unless $m; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | $mods{$m}++; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | ▶ | if ($args{from_text}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | for my $el ($dom->find('*')->each) { | |||||||||||||||||||
| 82 | 0 | my $text = $el->text; | |||||||||||||||||||
| 83 | 0 | ▶ | while ($text =~ /\b([A-Za-z_][A-Za-z_0-9]*(?:::[A-Za-z_0-9]+)+)\b/g) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 0 | $mods{$1}++; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | [sort keys %mods]; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 1 | 1; | |||||||||||||||||||
| 93 | # ABSTRACT: Extract CPAN module names from an HTML document | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||