| 1 | package EPublisher::Source::Plugin::MetaCPAN; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Get POD from distributions via MetaCPAN | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use MetaCPAN::API; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use EPublisher::Source::Base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use EPublisher::Utils::PPI qw(extract_pod_from_code); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | our @ISA = qw( EPublisher::Source::Base ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | our $VERSION = 0.24; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # implementing the interface to EPublisher::Source::Base | ||||||||||||||||||||||||||||
| 21 | 3 | ▶ | sub load_source{ | ||||||||||||||||||||||||||
|
Subroutine
load_source: called CC=17 SCAR=32.7
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3 | $self->publisher->debug( '100: start ' . __PACKAGE__ ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 3 | my $options = $self->_config; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | return '' unless $options->{module}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 3 | my $module = $options->{module}; # the name of the CPAN-module | |||||||||||||||||||||||||||
| 31 | 3 | my $dont_merge_release = $options->{onlythis}; | |||||||||||||||||||||||||||
| 32 | 3 | my $mcpan = MetaCPAN::API->new; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # metacpan does not handle ".pm" in dist names | ||||||||||||||||||||||||||||
| 35 | 3 | my $release_name_metacpan = $module; | |||||||||||||||||||||||||||
| 36 | 3 | $release_name_metacpan =~ s/\.pm\z//; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # fetching the requested module from metacpan | ||||||||||||||||||||||||||||
| 39 | 3 | $self->publisher->debug( "103: fetch release $module ($release_name_metacpan)" ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # if just the one and only POD from the modules name and not the entire | ||||||||||||||||||||||||||||
| 42 | # release is wanted, we just fetch it and return | ||||||||||||||||||||||||||||
| 43 | 3 | ▶ | if ($dont_merge_release) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my $result; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | eval { | ||||||||||||||||||||||||||||
| 48 | 0 | $result = $mcpan->pod( | |||||||||||||||||||||||||||
| 49 | module => $release_name_metacpan, | ||||||||||||||||||||||||||||
| 50 | 'content-type' => 'text/x-pod', | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | 0 | 1; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $self->publisher->debug( | |||||||||||||||||||||||||||
| 55 | "103: Can't retrieve pod for $release_name_metacpan" | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | 0 | return; | |||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my @pod = (); | |||||||||||||||||||||||||||
| 61 | 0 | my $info = { pod => $result, filename => '', title => $module }; | |||||||||||||||||||||||||||
| 62 | 0 | push (@pod, $info); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # EXIT! | ||||||||||||||||||||||||||||
| 65 | 0 | return @pod; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | # ELSE we go on and build the entire release... | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # if there is a wrong module-name we write a debug-message and return | ||||||||||||||||||||||||||||
| 70 | # an empty array | ||||||||||||||||||||||||||||
| 71 | 3 | my $module_result; | |||||||||||||||||||||||||||
| 72 | eval { | ||||||||||||||||||||||||||||
| 73 | 3 | $module_result = | |||||||||||||||||||||||||||
| 74 | $mcpan->fetch( 'release/' . $release_name_metacpan ); | ||||||||||||||||||||||||||||
| 75 | 2 | 1; | |||||||||||||||||||||||||||
| 76 | 3 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 1 | $self->publisher->debug( | |||||||||||||||||||||||||||
| 78 | "103: release $release_name_metacpan does not exist" | ||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | 1 | return; | |||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # if we reached here the module-call was probably fine... | ||||||||||||||||||||||||||||
| 84 | # so we print out what we have got | ||||||||||||||||||||||||||||
| 85 | 2 | $self->publisher->debug( | |||||||||||||||||||||||||||
| 86 | "103: fetch result: " . Dumper $module_result | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # get the manifest with module-author and modulename-moduleversion | ||||||||||||||||||||||||||||
| 90 | 2 | $self->publisher->debug( '103: get MANIFEST' ); | |||||||||||||||||||||||||||
| 91 | 2 | my $manifest; | |||||||||||||||||||||||||||
| 92 | eval { | ||||||||||||||||||||||||||||
| 93 | $manifest = $mcpan->source( | ||||||||||||||||||||||||||||
| 94 | author => $module_result->{author}, | ||||||||||||||||||||||||||||
| 95 | release => $module_result->{name}, | ||||||||||||||||||||||||||||
| 96 | 2 | path => 'MANIFEST', | |||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | 2 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | $self->publisher->debug( | |||||||||||||||||||||||||||
| 100 | "103: Cannot get MANIFEST", | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | 0 | return; | |||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # make a list from all possible POD-files in the lib directory | ||||||||||||||||||||||||||||
| 106 | 2 | my @files = split /\n/, $manifest; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | #$self->publisher->debug( "103: files from manifest: " . join ', ', @files ); | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # some MANIFESTS (like POD::Parser) have comments after the filenames, | ||||||||||||||||||||||||||||
| 111 | # so we match against an optional \s instead of \z | ||||||||||||||||||||||||||||
| 112 | # the manifest, in POD::Parser in looks e.g. like this: | ||||||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||||||
| 114 | # lib/Pod/Usage.pm -- The Pod::Usage module source | ||||||||||||||||||||||||||||
| 115 | # lib/Pod/Checker.pm -- The Pod::Checker module source | ||||||||||||||||||||||||||||
| 116 | # lib/Pod/Find.pm -- The Pod::Find module source | ||||||||||||||||||||||||||||
| 117 | my @pod_files = grep{ | ||||||||||||||||||||||||||||
| 118 | 2 | ▶ | /^.*\.p(?:od|m|l)(?:\s|$)/ # all POD everywhere | ||||||||||||||||||||||||||
|
Condition100%3/3
/^.*\.p(?:od|m|l)(?:\s|$)/ && !m[^(?:example/|x?t/|inc/)]
MC/DC100%2/2
/^.*\.p(?:od|m|l)(?:\s|$)/ && !m[^(?:example/|x?t/|inc/)]
/^.*\.p(?:od|m|l)(?:\s|$)/
!m[^(?:example/|x?t/|inc/)]
Truth table100%3/3
A && not(B)
A= /^.*\.p(?:od|m|l)(?:\s|$)/B= !m[^(?:example/|x?t/|inc/)]
| |||||||||||||||||||||||||||||
| 119 | and not | ||||||||||||||||||||||||||||
| 120 | /^(?:example\/|x?t\/|inc\/)/ # but not in example/ or t/ or xt/ or inc/ | ||||||||||||||||||||||||||||
| 121 | }@files; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # especially in App::* dists the most important documentation | ||||||||||||||||||||||||||||
| 124 | # is often in the scripts | ||||||||||||||||||||||||||||
| 125 | push @pod_files, grep { | ||||||||||||||||||||||||||||
| 126 | 2 | ▶ | /^bin\// | ||||||||||||||||||||||||||
|
Condition33%1/3
m[^bin/] && !/^.*\.p(?:od|m|l)(?:\s|$)/
MC/DC0%0/2
m[^bin/] && !/^.*\.p(?:od|m|l)(?:\s|$)/
m[^bin/]
!/^.*\.p(?:od|m|l)(?:\s|$)/
Truth table33%1/3
A && not(B)
A= m[^bin/]B= !/^.*\.p(?:od|m|l)(?:\s|$)/
| |||||||||||||||||||||||||||||
| 127 | and not | ||||||||||||||||||||||||||||
| 128 | /^.*\.p(?:od|m|l)(?:\s|$)/ | ||||||||||||||||||||||||||||
| 129 | }@files; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # here whe store POD if we find some later on | ||||||||||||||||||||||||||||
| 132 | 2 | my @pod; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # look for POD | ||||||||||||||||||||||||||||
| 135 | 2 | for my $file ( @pod_files ) { | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # we match the filename again, in case there are comments in | ||||||||||||||||||||||||||||
| 138 | # the manifest, in POD::Parser in looks e.g. like this: | ||||||||||||||||||||||||||||
| 139 | # | ||||||||||||||||||||||||||||
| 140 | # lib/Pod/Usage.pm -- The Pod::Usage module source | ||||||||||||||||||||||||||||
| 141 | # lib/Pod/Checker.pm -- The Pod::Checker module source | ||||||||||||||||||||||||||||
| 142 | # lib/Pod/Find.pm -- The Pod::Find module source | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 15 | my ($path) = split /\s/, $file; | |||||||||||||||||||||||||||
| 145 | 15 | ▶ | next if $path !~ m{ \. p(?:od|m|l) \z }x && $path !~ m{ \A bin/ }x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$path =~ / \. p(?:od|m|l) \z /x or $path =~ m[ \A bin/ ]x
MC/DC0%0/2
$path =~ / \. p(?:od|m|l) \z /x or $path =~ m[ \A bin/ ]x
$path =~ / \. p(?:od|m|l) \z /x
$path =~ m[ \A bin/ ]x
Truth table33%1/3
A or B
A= $path =~ / \. p(?:od|m|l) \z /xB= $path =~ m[ \A bin/ ]x
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 15 | $file = $path; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # the call below ($mcpan->pod()) fails if there is no POD in a | ||||||||||||||||||||||||||||
| 150 | # module so this is why I filter all the modules. I check if they | ||||||||||||||||||||||||||||
| 151 | # have any line BEGINNING with '=head1' ore similar | ||||||||||||||||||||||||||||
| 152 | 15 | my $source; | |||||||||||||||||||||||||||
| 153 | eval { | ||||||||||||||||||||||||||||
| 154 | $source = $mcpan->source( | ||||||||||||||||||||||||||||
| 155 | author => $module_result->{author}, | ||||||||||||||||||||||||||||
| 156 | release => $module_result->{name}, | ||||||||||||||||||||||||||||
| 157 | 15 | path => $file, | |||||||||||||||||||||||||||
| 158 | ); | ||||||||||||||||||||||||||||
| 159 | 15 | 1; | |||||||||||||||||||||||||||
| 160 | 15 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | $self->publisher->debug( | |||||||||||||||||||||||||||
| 162 | "103: Cannot get source for $file", | ||||||||||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||||||||||
| 164 | 0 | return; | |||||||||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 15 | $self->publisher->debug( "103: source of $file found" ); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # The Moose-Project made me write this filtering Regex, because | ||||||||||||||||||||||||||||
| 170 | # they have .pm's without POD, and also with nonsense POD which | ||||||||||||||||||||||||||||
| 171 | # still fails if you call $mcpan->pod | ||||||||||||||||||||||||||||
| 172 | 15 | my $pod_src; | |||||||||||||||||||||||||||
| 173 | 15 | ▶ | if ( $source =~ m{ ^=head[1234] }xim ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | eval { | ||||||||||||||||||||||||||||
| 176 | $pod_src = $mcpan->pod( | ||||||||||||||||||||||||||||
| 177 | author => $module_result->{author}, | ||||||||||||||||||||||||||||
| 178 | release => $module_result->{name}, | ||||||||||||||||||||||||||||
| 179 | 13 | path => $file, | |||||||||||||||||||||||||||
| 180 | 'content-type' => 'text/x-pod', | ||||||||||||||||||||||||||||
| 181 | ); | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 13 | 1; | |||||||||||||||||||||||||||
| 184 | 0 | ▶ | } or do{ $self->publisher->debug( $@ ); next; }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 13 | ▶ | if (!$pod_src) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | $self->publisher->debug( "103: empty pod handle" ); | |||||||||||||||||||||||||||
| 188 | 0 | next; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 13 | ▶ | if ( $pod_src =~ m/ \A ({.*) /xs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | $self->publisher->debug( "103: error message: $1" ); | |||||||||||||||||||||||||||
| 193 | 0 | next; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | else { | ||||||||||||||||||||||||||||
| 196 | 13 | $self->publisher->debug( "103: got pod" ); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # metacpan always provides utf-8 encoded data, so we have to decode it | ||||||||||||||||||||||||||||
| 200 | # otherwise the target plugins may produce garbage | ||||||||||||||||||||||||||||
| 201 | 13 | $pod_src = decode( 'utf-8', $pod_src ); | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | else { | ||||||||||||||||||||||||||||
| 205 | # if there is no head we consider this POD unvalid | ||||||||||||||||||||||||||||
| 206 | 2 | next; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # check if $result is always only the Pod | ||||||||||||||||||||||||||||
| 210 | #push @pod, extract_pod_from_code( $result ); | ||||||||||||||||||||||||||||
| 211 | 13 | my $filename = basename $file; | |||||||||||||||||||||||||||
| 212 | 13 | my $title = $file; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 13 | $title =~ s{^(?:lib|bin)/}{}; | |||||||||||||||||||||||||||
| 215 | 13 | $title =~ s{\.p(?:m|od|l)\z}{}; | |||||||||||||||||||||||||||
| 216 | 13 | $title =~ s{/}{::}g; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 13 | my $info = { pod => $pod_src, filename => $filename, title => $title }; | |||||||||||||||||||||||||||
| 219 | 13 | push @pod, $info; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # make some nice debug output for what is in $info | ||||||||||||||||||||||||||||
| 222 | 13 | my $pod_short; | |||||||||||||||||||||||||||
| 223 | 13 | ▶ | if ($pod_src =~ m/(.{50})/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 13 | $pod_short = $1 . '[...]'; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | else { | ||||||||||||||||||||||||||||
| 227 | 0 | $pod_short = $pod_src; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | 13 | $self->publisher->debug( | |||||||||||||||||||||||||||
| 230 | "103: passed info: " | ||||||||||||||||||||||||||||
| 231 | . "filename => $filename, " | ||||||||||||||||||||||||||||
| 232 | . "title => $title, " | ||||||||||||||||||||||||||||
| 233 | . "pod => $pod_short" | ||||||||||||||||||||||||||||
| 234 | ); | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # voila | ||||||||||||||||||||||||||||
| 238 | 2 | return @pod; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 4 | 1; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | __END__ | ||||||||||||||||||||||||||||