Weaver/Section/Collect.pm

filter: Statementstmt 100% 71 / 71 Branchbran 87.5% 7 / 8 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 2 totaltotal 93.6% 103 / 110 CC 9 SCAR 11.3
CC
9
Cov
97%
transform_document
7.0
weave_section
3.0
BEGIN
1.0
CRAP 3.1
« Weaver/Plugin/EnsurePod5.pm ↑ Summary Weaver/Section/GenerateSection.pm »
1 package Pod::Weaver::Section::Collect 4.022;
2 # ABSTRACT: a section that gathers up specific commands
3  
4 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 with 'Pod::Weaver::Role::Section',
6 'Pod::Weaver::Role::Transformer';
7  
8 # BEGIN BOILERPLATE
9 5 ▶use v5.22.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶no feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
Subroutine BEGIN: called CC=1 SCAR=0.0
14 # END BOILERPLATE
15  
16 #pod =head1 OVERVIEW
17 #pod
18 #pod Given the configuration:
19 #pod
20 #pod [Collect / METHODS]
21 #pod command = method
22 #pod
23 #pod This plugin will start off by gathering and nesting any C<=method> commands
24 #pod found in the C<pod_document>. Those commands, along with their nestable
25 #pod content, will be collected under a C<=head1 METHODS> header and placed in the
26 #pod correct location in the output stream. Their order will be preserved as it was
27 #pod in the source document.
28 #pod
29 #pod =cut
30  
31 5 ▶use Pod::Elemental::Element::Pod5::Region;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use Pod::Elemental::Selectors -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use List::Util 1.33 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 #pod =attr command
36 #pod
37 #pod The command that will be collected (e.g. C<attr> or C<method>).
38 #pod (required)
39 #pod
40 #pod =attr new_command
41 #pod
42 #pod The command to be used in the output instead of the collected command.
43 #pod (default: C<head2>)
44 #pod
45 #pod =attr header_command
46 #pod
47 #pod The section command for the section to be added.
48 #pod (default: C<head1>)
49 #pod
50 #pod =attr header
51 #pod
52 #pod The title of the section to be added.
53 #pod (default: the plugin name)
54 #pod
55 #pod =cut
56  
57 5 has command => (
58 is => 'ro',
59 isa => 'Str',
60 required => 1,
61 );
62  
63 5 has new_command => (
64 is => 'ro',
65 isa => 'Str',
66 required => 1,
67 default => 'head2',
68 );
69  
70 5 has header_command => (
71 is => 'ro',
72 isa => 'Str',
73 required => 1,
74 default => 'head1',
75 );
76  
77 has header => (
78 is => 'ro',
79 isa => 'Str',
80 lazy => 1,
81 required => 1,
82 7 ▶ default => sub { $_[0]->plugin_name },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 5 );
84  
85 5 ▶use Pod::Elemental::Transformer::Gatherer;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 5 ▶use Pod::Elemental::Transformer::Nester;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 5 has __used_container => (is => 'rw');
89  
90 20 ▶sub transform_document {
Subroutine transform_document: called CC=7 SCAR=19.5
Pod undocumented
91 20 my ($self, $document) = @_;
92  
93 20 my $command = $self->command;
94 20 my $selector = s_command($command);
95  
96 20 my $children = $document->children;
97 20 ▶ unless (any { $selector->($_) } @$children) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { &$selector($_); } , @$children)) 13 7 20
98 13 $self->log_debug("no $command commands in pod to collect");
99 13 return;
100 }
101  
102 7 $self->log_debug("transforming $command commands into standard pod");
103  
104 7 my $nester = Pod::Elemental::Transformer::Nester->new({
105 top_selector => $selector,
106 content_selectors => [
107 s_command([ qw(head3 head4 over item back) ]),
108 s_flat,
109 ],
110 });
111  
112 # try and find array position of suitable host
113 my ( $container_id ) = grep {
114 7 my $c = $children->[$_];
115 55 ▶ $c->isa("Pod::Elemental::Element::Nested")
Condition83%5/6
$c->isa('Pod::Elemental::Element::Nested') && $c->command eq $self->header_command && $c->content eq $self->header
!ll&&!rl&&r
28216
$c->isa("Pod::Elemental::Element::Nested") and $c->command eq $self->header_command
!ll&&!rl&&r
28027
MC/DC66%2/3
$c->isa('Pod::Elemental::Element::Nested') && $c->command eq $self->header_command && $c->content eq $self->header
$c->isa("Pod::Elemental::Element::Nested") $c->command eq $self->header_command $c->content eq $self->header
Truth table75%3/4
A and B && C
A= $c->isa("Pod::Elemental::Element::Nested")B= $c->command eq $self->header_commandC= $c->content eq $self->header
ABCresult
0XX0
10X0
1100
1111
116 and $c->command eq $self->header_command and $c->content eq $self->header;
117 } 0 .. $#$children;
118  
119 7 ▶ my $container = $container_id
Branch: 1 branch
BranchTrueFalseTotal
$container_id ? : 6 1 7
120 ? splice @$children, $container_id, 1 # excise host
121 : Pod::Elemental::Element::Nested->new({ # synthesize new host
122 command => $self->header_command,
123 content => $self->header,
124 });
125  
126 7 $self->__used_container($container);
127  
128 7 my $gatherer = Pod::Elemental::Transformer::Gatherer->new({
129 gather_selector => $selector,
130 container => $container,
131 });
132  
133 7 $nester->transform_node($document);
134 7 my @children = $container->children->@*; # rescue children
135 7 $gatherer->transform_node($document); # insert host at position of first adopt-child and inject it with adopt-children
136 7 foreach my $child ($container->children->@*) {
137 7 ▶ $child->command( $self->new_command ) if $child->command eq $command;
Branch: 1 branch
BranchTrueFalseTotal
if $child->command eq $command 7 0 7
138 }
139 7 unshift $container->children->@*, @children; # give original children back to host
140 }
141  
142 20 ▶sub weave_section {
Subroutine weave_section: called CC=3 SCAR=11.0
Pod undocumented
143 20 my ($self, $document, $input) = @_;
144  
145 20 ▶ return unless $self->__used_container;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->__used_container 13 7 20
146  
147 7 my $in_node = $input->{pod_document}->children;
148  
149 my @found = grep {
150 7 my ($i, $para) = ($_, $in_node->[$_]);
151 21 ▶ ($para == $self->__used_container)
Condition66%2/3
$para == $self->__used_container && @{$self->__used_container->children}
!ll&&!rl&&r
1407
MC/DC50%1/2
$para == $self->__used_container && @{$self->__used_container->children}
$para == $self->__used_container @{$self->__used_container->children}
Truth table66%2/3
A && B
A= $para == $self->__used_containerB= @{$self->__used_container->children}
ABresult
0X0
100
111
152 && $self->__used_container->children->@*;
153 } (0 .. $#$in_node);
154  
155 7 push $document->children->@*, map { splice @$in_node, $_, 1 } reverse @found;
156 }
157  
158 5 __PACKAGE__->meta->make_immutable;
159 5 1;
160  
161 __END__
« Weaver/Plugin/EnsurePod5.pm ↑ Summary Weaver/Section/GenerateSection.pm »