Elemental/Transformer/Nester.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 78.5% 11 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 83.9% 68 / 81 CC 13 SCAR 20.3
CC
13
Cov
88%
transform_node
11.8
_is_containable
3.0
BEGIN
1.0
CRAP 7.6
« Elemental/Document.pm ↑ Summary Elemental/Transformer/Pod5.pm »
1 package Pod::Elemental::Transformer::Nester 0.103006;
2 # ABSTRACT: group the document into sections
3  
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 with 'Pod::Elemental::Transformer';
6  
7 #pod =head1 OVERVIEW
8 #pod
9 #pod The Nester transformer is meant to find potential container elements and make
10 #pod them into actual containers. It works by being told what elements may be made
11 #pod into containers and what subsequent elements they should allow to be stuffed
12 #pod into them.
13 #pod
14 #pod For example, given the following nester:
15 #pod
16 #pod use Pod::Elemental::Selectors qw(s_command s_flat);
17 #pod
18 #pod my $nester = Pod::Elemental::Transformer::Nester->new({
19 #pod top_selector => s_command('head1'),
20 #pod content_selectors => [
21 #pod s_command([ qw(head2 head3 head4) ]),
22 #pod s_flat,
23 #pod ],
24 #pod });
25 #pod
26 #pod ..then when we apply the transformation:
27 #pod
28 #pod $nester->transform_node($document);
29 #pod
30 #pod ...the nester will find all C<=head1> elements in the top-level of the
31 #pod document. It will ensure that they are represented by objects that perform the
32 #pod Pod::Elemental::Node role, and then it will move all subsequent elements
33 #pod matching the C<content_selectors> into the container.
34 #pod
35 #pod So, if we start with this input:
36 #pod
37 #pod =head1 Header
38 #pod =head2 Subheader
39 #pod Pod5::Ordinary <some content>
40 #pod =head1 New Header
41 #pod
42 #pod The nester will convert its structure to look like this:
43 #pod
44 #pod =head1 Header
45 #pod =head2 Subheader
46 #pod Pod5::Ordinary <some content>
47 #pod =head1 New Header
48 #pod
49 #pod Once an element is reached that does not pass the content selectors, the
50 #pod nesting ceases until the next potential container.
51 #pod
52 #pod =cut
53  
54 2 ▶use MooseX::Types::Moose qw(ArrayRef CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 2 ▶use Pod::Elemental::Element::Nested;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use Pod::Elemental::Selectors -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 #pod =attr top_selector
62 #pod
63 #pod This attribute must be a coderef (presumably made from
64 #pod Pod::Elemental::Selectors) that will test elements in the transformed node and
65 #pod return true if the element is a potential new container.
66 #pod
67 #pod =cut
68  
69 2 has top_selector => (
70 is => 'ro',
71 isa => CodeRef,
72 required => 1,
73 );
74  
75 #pod =attr content_selectors
76 #pod
77 #pod This attribute must be an arrayref of coderefs (again presumably made from
78 #pod Pod::Elemental::Selectors) that will test whether paragraphs subsequent to the
79 #pod top-level container may be moved under the container.
80 #pod
81 #pod =cut
82  
83 2 has content_selectors => (
84 is => 'ro',
85 isa => ArrayRef[ CodeRef ],
86 required => 1,
87 );
88  
89 25 ▶sub _is_containable {
Subroutine _is_containable: called CC=3 SCAR=11.0
90 25 my ($self, $para) = @_;
91  
92 25 for my $sel (@{ $self->content_selectors }) {
93 44 ▶ return 1 if $sel->($para);
Branch: 1 branch
BranchTrueFalseTotal
if &$sel($para) 21 23 44
94 }
95  
96 4 return;
97 }
98  
99 2 ▶sub transform_node {
Subroutine transform_node: called CC=11 SCAR=24.7
Pod undocumented
100 2 my ($self, $node) = @_;
101  
102 # We used to say (length -2) because "if we're already at the last element,
103 # we can't nest anything -- there's nothing subsequent to the potential
104 # top-level element to nest!" -- my (rjbs's) reasoning in 2009.
105 #
106 # This was an unneeded optimization, and therefore stupid. Worse, it was a
107 # bug. It meant that a nestable element that was the last element in a
108 # sequence wouldn't be upgraded to a Nested element, so later munging could
109 # barf. In fact, that's what happened in [rt.cpan.org #69189]
110 # -- rjbs, 2012-05-04
111 2 PASS: for my $i (0 .. @{ $node->children }- 1) {
112 10 ▶ last PASS if $i >= @{ $node->children };
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @{$node->children;} 2 8 10
113  
114 8 my $para = $node->children->[ $i ];
115 8 ▶ next unless $self->top_selector->($para);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->top_selector->($para) 2 6 8
116  
117 6 ▶ if (s_command(undef, $para) and not s_node($para)) {
Branch: 1 branch
BranchTrueFalseTotal
if (s_command(undef, $para) and not s_node($para)) 6 0 6
Condition33%1/3
s_command(undef, $para) and not s_node($para)
!ll&&!rl&&r
006
MC/DC0%0/2
s_command(undef, $para) and not s_node($para)
s_command(undef, $para) not s_node($para)
Truth table33%1/3
A and not(B)
A= s_command(undef, $para)B= not s_node($para)
ABresult
0X0
100
111
118 6 $para = $node->children->[ $i ] = Pod::Elemental::Element::Nested->new({
119 command => $para->command,
120 content => $para->content,
121 });
122 }
123  
124 6 ▶ if (! s_node($para) or @{ $para->children }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not s_node($para) or @{$para->children;}) 0 6 6
Condition33%1/3
not s_node($para) or @{$para->children;}
l!l&&r!l&&!r
006
MC/DC0%0/2
not s_node($para) or @{$para->children;}
not s_node($para) @{$para->children;}
Truth table33%1/3
not(A) or B
A= not s_node($para)B= @{$para->children;}
ABresult
1X1
011
000
125 0 confess "can't use $para as the top of a nesting";
126 }
127  
128 6 my @to_nest;
129 6 NEST: for my $j ($i+1 .. @{ $node->children } - 1) {
130 25 ▶ last unless $self->_is_containable($node->children->[ $j ]);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_containable($node->children->[$j]) 4 21 25
131 21 push @to_nest, $j;
132 }
133  
134 6 ▶ if (@to_nest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@to_nest) 6 0 6
135 my @to_nest_elem =
136 6 splice @{ $node->children }, $to_nest[0], scalar(@to_nest);
137  
138 6 push @{ $para->children }, @to_nest_elem;
139 6 next PASS;
140 }
141 }
142  
143 2 return $node;
144 }
145  
146 2 __PACKAGE__->meta->make_immutable;
147  
148 2 1;
149  
150 __END__
« Elemental/Document.pm ↑ Summary Elemental/Transformer/Pod5.pm »