blib/lib/Statocles/App/Perldoc.pm

filter: Statementstmt 62.1% 97 / 156 Branchbran 33.3% 18 / 54 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 81.2% 13 / 16 Podpod 100% 1 / 1 totaltotal 53.7% 129 / 240 CC 38 SCAR 46.4
CC
38
Cov
53%
_weave_module
306.0
pages
14.0
__ANON__
12.0
CRAP 103.5
« blib/lib/Statocles/Plugin/LinkCheck.pm ↑ Summary blib/lib/Statocles/Command/daemon.pm »
1 package Statocles::App::Perldoc;
2 4 our $VERSION = '0.099';
3 # ABSTRACT: Render documentation for Perl modules
4  
5 4 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Statocles::Page::Plain;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Pod::Simple::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Pod::Simple::XHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 with 'Statocles::App';
11  
12 #pod =attr inc
13 #pod
14 #pod The directories to search for modules. Defaults to @INC.
15 #pod
16 #pod =cut
17  
18 has inc => (
19 is => 'ro',
20 isa => ArrayRef[Path],
21 # We can't check for existence, because @INC might contain nonexistent
22 # directories (I think)
23 3 ▶ default => sub { [ @INC ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 7 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 7 my ( $args ) = @_;
26 7 return [ map { Path::Tiny->new( $_ ) } @$args ];
27 },
28 4 );
29  
30 #pod =attr modules
31 #pod
32 #pod The root modules to find. Required. All child modules will be included. Any module that does
33 #pod not start with one of these strings will not be included.
34 #pod
35 #pod =cut
36  
37 4 has modules => (
38 is => 'ro',
39 isa => ArrayRef[Str],
40 required => 1,
41 );
42  
43 #pod =attr index_module
44 #pod
45 #pod The module to use for the index page. Required.
46 #pod
47 #pod =cut
48  
49 4 has index_module => (
50 is => 'ro',
51 isa => Str,
52 required => 1,
53 );
54  
55 #pod =attr weave
56 #pod
57 #pod If true, run the POD through L<Pod::Weaver> before converting to HTML
58 #pod
59 #pod =cut
60  
61 has weave => (
62 is => 'ro',
63 isa => Bool,
64 7 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 4 );
66  
67 #pod =attr weave_config
68 #pod
69 #pod The path to the Pod::Weaver configuration file
70 #pod
71 #pod =cut
72  
73 has weave_config => (
74 is => 'ro',
75 isa => Path,
76 6 ▶ default => sub { './weaver.ini' },
Subroutine __ANON__: called CC=1 SCAR=0.0
77 4 coerce => Path->coercion,
78 );
79  
80 #pod =attr template_dir
81 #pod
82 #pod The directory (inside the theme directory) to use for this app's templates.
83 #pod Defaults to C<blog>.
84 #pod
85 #pod =cut
86  
87 4 has '+template_dir' => (
88 default => 'perldoc',
89 );
90  
91 #pod =method pages
92 #pod
93 #pod my @pages = $app->pages;
94 #pod
95 #pod Render the requested modules as HTML. Returns an array of L<Statocles::Page> objects.
96 #pod
97 #pod =cut
98  
99 2 ▶sub pages {
Subroutine pages: called CC=14 SCAR=26.4
Pod documented
100 2 my ( $self ) = @_;
101 2 my @dirs = map { "$_" } @{ $self->inc };
102 2 my $pod_base = 'https://metacpan.org/pod/';
103  
104 2 my %modules;
105 2 for my $glob ( @{ $self->modules } ) {
106 %modules = (
107 %modules,
108 7 %{ Pod::Simple::Search->new->inc(0)->limit_re( qr{^$glob} )->survey( @dirs ) },
109 );
110  
111 # Also check for exact matches, for strange extensions
112 7 for my $dir ( @dirs ) {
113 14 my @glob_parts = split /::/, $glob;
114 14 my $path = Path::Tiny->new( $dir, @glob_parts );
115 14 ▶ if ( $path->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->is_file) 1 13 14
116 1 $modules{ $glob } = "$path";
117 }
118 }
119 }
120  
121  
122 #; use Data::Dumper;
123 #; say Dumper \%modules;
124  
125 2 my @pages;
126 2 for my $module ( keys %modules ) {
127  
128 7 my $path = $modules{ $module };
129 #; use Data::Dumper;
130 #; say Dumper $path;
131  
132 # Weave the POD before trying to make HTML
133 7 ▶ my $pod = $self->weave
Branch: 1 branch
BranchTrueFalseTotal
$self->weave ? : 0 7 7
134 ? $self->_weave_module( $path )
135 : Path::Tiny->new( $path )->slurp
136 ;
137  
138 7 my $parser = Pod::Simple::XHTML->new;
139 7 $parser->perldoc_url_prefix( $pod_base );
140 7 $parser->$_('') for qw( html_header html_footer );
141 7 $parser->output_string( \(my $parser_output) );
142 7 $parser->parse_string_document( $pod );
143 #; say $parser_output;
144  
145 7 my $dom = Mojo::DOM->new( $parser_output );
146 7 for my $node ( $dom->find( 'a[href]' )->each ) {
147 10 my $href = $node->attr( 'href' );
148  
149 # Rewrite links for modules that we will be serving locally
150 10 ▶ if ( grep { $href =~ /^$pod_base$_/ } @{ $self->modules } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$href =~ /^$pod_base$_/;} @{$self->modules;}) { } 8 2 10
elsif ($href =~ m[(?:[^:]+:)?//]) { } 2 0 2
151 8 my ( $module, $section ) = $href =~ /^$pod_base([^#]+)(?:\#(.*))?$/;
152 8 my $url = $self->url( $self->_module_href( $module ) );
153 8 ▶ $node->attr( href => $section ? join( "#", $url, $section ) : $url );
Branch: 1 branch
BranchTrueFalseTotal
$section ? : 2 6 8
154 }
155 # Add rel="external" for remaining external links
156 elsif ( $href =~ m{(?:[^:]+:)?//} ) {
157 2 $node->attr( rel => 'external' );
158 }
159  
160 }
161  
162 7 my $source_path = "$module/source.html";
163 7 $source_path =~ s{::}{/}g;
164  
165 7 my ( @parts ) = split m{::}, $module;
166 7 my @crumbtrail;
167 7 for my $i ( 0..$#parts ) {
168 9 my $trail_module = join "::", @parts[0..$i];
169 9 ▶ if ( $modules{ $trail_module } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modules{$trail_module}) { } 9 0 9
170 9 push @crumbtrail, {
171 text => $parts[ $i ],
172 href => $self->url( $self->_module_href( $trail_module ) ),
173 };
174 }
175 else {
176 0 push @crumbtrail, {
177 text => $parts[ $i ],
178 };
179 }
180 }
181  
182 7 my %page_args = (
183 layout => $self->template( 'layout.html' ),
184 template => $self->template( 'pod.html' ),
185 title => $module,
186 content => "$dom",
187 app => $self,
188 path => $self->_module_href( $module ),
189 data => {
190 source_path => $self->url( $source_path ),
191 crumbtrail => \@crumbtrail,
192 },
193 );
194  
195 7 ▶ if ( $module eq $self->index_module ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module eq $self->index_module) { } 2 5 7
196 2 unshift @pages, Statocles::Page::Plain->new( %page_args );
197 2 $self->_highlight_page( $pages[0], 'pre > code' );
198 }
199 else {
200 5 push @pages, Statocles::Page::Plain->new( %page_args );
201 5 $self->_highlight_page( $pages[-1], 'pre > code' );
202 }
203  
204 # Add the source as a text file
205 push @pages, Statocles::Page::Plain->new(
206 path => $source_path,
207 layout => $self->template( 'layout.html' ),
208 template => $self->template( 'source.html' ),
209 title => "$module (source)",
210 content => Path::Tiny->new( $path )->slurp,
211 app => $self,
212 data => {
213 7 doc_path => $self->url( $page_args{path} ),
214 crumbtrail => \@crumbtrail,
215 },
216 );
217 # unable to highlight source as source.html.ep uses <%== %> to escape html.
218 # $self->_highlight_page( $pages[-1], 'pre' );
219 }
220  
221 2 return @pages;
222 }
223  
224 7 ▶sub _highlight_page {
Subroutine _highlight_page: called CC=5 SCAR=16.1
225 7 my ( $self, $page, $sel ) = @_;
226 # highlight only if site-wide highlighting is available
227 7 ▶ return unless my $hl = $page->site->plugins->{highlight};
Branch: 1 branch
BranchTrueFalseTotal
unless my $hl = $page->site->plugins->{"highlight"} 4 3 7
228 # this add the highlight stylesheet to $page->links (template logic)
229 3 $hl->highlight({page => $page}, Perl => '');
230 # $page->dom calls $page->render 'set/making fast' links in the dom.
231 3 my $codes = $page->dom->find($sel);
232 3 ▶ if ($codes->first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($codes->first) { } 2 1 3
233 2 for my $node ($codes->each) {
234 2 ▶ my $parent = $node->tag eq 'code' ? $node->parent : $node;
Branch: 1 branch
BranchTrueFalseTotal
$node->tag eq 'code' ? : 2 0 2
235 2 $parent->replace($hl->highlight({}, Perl => $node->text));
236 }
237 } else {
238 # remove if not used. path from Statocles::Plugin::Highlight#L159
239 $page->dom->find('link[rel=stylesheet][href*=/plugin/highlight/]')
240 1 ▶ ->each(sub { $_->remove });
Subroutine __ANON__: called CC=1 SCAR=0.0
241 }
242  
243 3 return $page;
244 }
245  
246 24 ▶sub _module_href {
Subroutine _module_href: called CC=2 SCAR=6.9
247 24 my ( $self, $module ) = @_;
248 24 ▶ if ( $module eq $self->index_module ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module eq $self->index_module) 8 16 24
249 8 return '/index.html';
250 }
251  
252 16 my $page_url = "$module/index.html";
253 16 $page_url =~ s{::}{/}g;
254 16 return $page_url;
255 }
256  
257 # Run Pod::Weaver on the POD in the given path
258 0 ▶sub _weave_module {
Subroutine _weave_module: not called CC=17 SCAR=57.2
259 0 my ( $self, $path ) = @_;
260  
261 # Oh... My... GOD...
262 0 my %errors;
263 0 ▶ if ( !eval { require Pod::Weaver; 1; } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Pod::Weaver; 1 } }) 0 0 0
264 0 $errors{ 'Pod::Weaver' } = $@;
265 }
266 0 ▶ if ( !eval { require PPI; 1; } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require PPI; 1 } }) 0 0 0
267 0 $errors{ 'PPI' } = $@;
268 }
269 0 ▶ if ( !eval { require Pod::Elemental; 1; } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Pod::Elemental; 1 } }) 0 0 0
270 0 $errors{ 'Pod::Elemental' } = $@;
271 }
272 0 ▶ if ( !eval { require Encode; 1; } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Encode; 1 } }) 0 0 0
273 0 $errors{ 'Encode' } = $@;
274 }
275  
276 # Pod::Weaver 4.014 shipped with a bug that causes problems unless
277 # we have a LEGAL section, which we do not presently allow users to
278 # set. So warn them to upgrade if they have this version
279 0 ▶ if ( defined($Pod::Weaver::VERSION) and $Pod::Weaver::VERSION == 4.014 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Pod::Weaver::VERSION and $Pod::Weaver::VERSION == "4.014") 0 0 0
Condition0%0/3
defined $Pod::Weaver::VERSION and $Pod::Weaver::VERSION == "4.014"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Pod::Weaver::VERSION and $Pod::Weaver::VERSION == "4.014"
defined $Pod::Weaver::VERSION $Pod::Weaver::VERSION == "4.014"
Truth table0%0/3
A and B
A= defined $Pod::Weaver::VERSIONB= $Pod::Weaver::VERSION == "4.014"
ABresult
0X0
100
111
280 0 $errors{ 'Pod::Weaver' } = q{Pod::Weaver version 4.014 has a bug that will cause a fatal error when a LEGAL section isn't available. Please upgrade to version 4.015 or later.};
281 }
282  
283 0 ▶ if ( keys %errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %errors) 0 0 0
284 die "Cannot weave POD: Error loading modules "
285 0 . join( "\n", map { "$_: $errors{$_}" } keys %errors )
286 ;
287 }
288  
289 # Check for a config and give a friendly error message if missing.
290 # The default exception thrown by a missing config is very difficult
291 # to understand out of context
292 0 ▶ if ( !$self->weave_config->parent->child( 'weaver.ini' )->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->weave_config->parent->child("weaver.ini")->is_file) 0 0 0
293 0 die sprintf q{Cannot find Pod::Weaver config in "%s". Missing "weaver.ini" file?},
294 $self->weave_config->parent;
295 }
296  
297 0 my $perl_utf8 = Encode::encode( 'utf-8', Path::Tiny->new( $path )->slurp, Encode::FB_CROAK );
298 0 ▶ my $ppi_document = PPI::Document->new( \$perl_utf8 ) or die PPI::Document->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $ppi_document = "PPI::Document"->new(\$perl_utf8) or die "PPI::Document"->errstr 0 0 0
299  
300 ### Copy/paste from Pod::Elemental::PerlMunger
301 my $code_elems = $ppi_document->find(
302 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
303 return
304 0 ▶ if grep { $_[ 1 ]->isa( "PPI::Token::$_" ) }
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_[1]->isa("PPI::Token::$_");} "Comment", "Pod", "Whitespace", "Separator", "Data", "End" 0 0 0
305 qw(Comment Pod Whitespace Separator Data End);
306 0 return 1;
307 }
308 0 );
309  
310 0 ▶ $code_elems ||= [];
Condition0%0/2
$code_elems ||= []
l!l
00
MC/DC0%0/1
$code_elems ||= []
$code_elems
Truth table0%0/2
A
A= $code_elems
Aresult
11
00
311 0 my @pod_tokens;
312  
313 0 my @queue = $ppi_document->children;
314 0 ▶ while ( my $element = shift @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $element = shift @queue) 0 0 0
315 0 ▶ if ( $element->isa( 'PPI::Token::Pod' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->isa("PPI::Token::Pod")) 0 0 0
316 # save the text for use in building the Pod-only document
317 0 push @pod_tokens, "$element";
318 }
319  
320 0 ▶ if ( blessed $element && $element->isa( 'PPI::Node' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($element) and $element->isa("PPI::Node")) 0 0 0
Condition0%0/3
builtin::blessed($element) and $element->isa("PPI::Node")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($element) and $element->isa("PPI::Node")
builtin::blessed($element) $element->isa("PPI::Node")
Truth table0%0/3
A and B
A= builtin::blessed($element)B= $element->isa("PPI::Node")
ABresult
0X0
100
111
321 # Depth-first keeps the queue size down
322 0 unshift @queue, $element->children;
323 }
324 }
325  
326 ## Check for any problems, like POD inside of heredoc or strings
327 0 ▶ my $finder = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
328 0 my $node = $_[ 1 ];
329 return 0
330 0 ▶ unless grep { $node->isa( $_ ) }
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$node->isa($_);} "PPI::Token::Quote", "PPI::Token::QuoteLike", "PPI::Token::HereDoc" 0 0 0
331 qw( PPI::Token::Quote PPI::Token::QuoteLike PPI::Token::HereDoc );
332 0 ▶ return 1 if $node->content =~ /^=[a-z]/m;
Branch: 1 branch
BranchTrueFalseTotal
if $node->content =~ /^=[a-z]/m 0 0 0
333 0 return 0;
334 0 };
335  
336 0 ▶ if ( $ppi_document->find_first( $finder ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppi_document->find_first($finder)) 0 0 0
337 0 warn "can't invoke Pod::Weaver on '$path': There is POD in string literals";
338 0 return '';
339 }
340  
341 0 my $pod_str = join "\n", @pod_tokens;
342 0 my $pod_document = Pod::Elemental->read_string( $pod_str );
343  
344 ### MUNGE THE POD HERE!
345  
346 0 my $weaved_doc;
347 0 eval {
348 0 my $weaver = Pod::Weaver->new_from_config(
349 { root => $self->weave_config->parent->stringify },
350 );
351 0 $weaved_doc = $weaver->weave_document({
352 pod_document => $pod_document,
353 ppi_document => $ppi_document,
354 });
355 };
356  
357 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
358 0 die sprintf q{Error weaving POD for path "%s": %s}, $path, $@;
359 }
360  
361 ### END MUNGE THE POD
362  
363 0 my $pod_text = $weaved_doc->as_pod_string;
364  
365 #; say $pod_text;
366 0 return $pod_text;
367 }
368  
369 4 1;
370  
371 __END__
« blib/lib/Statocles/Plugin/LinkCheck.pm ↑ Summary blib/lib/Statocles/Command/daemon.pm »