Elemental/Transformer/Pod5.pm

filter: Statementstmt 100% 134 / 134 Branchbran 94.8% 55 / 58 Conditioncond 94.7% 18 / 19 MC/DCmcdc 91.6% 11 / 12 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 1 totaltotal 97.5% 239 / 245 CC 42 SCAR 19.3
CC
42
Cov
98%
_collect_runs
14.0
_collect_regions
9.0
__extract_region
8.0
CRAP 6.9
« Elemental/Transformer/Nester.pm ↑ Summary Elemental/Element/Pod5/Region.pm »
1 package Pod::Elemental::Transformer::Pod5 0.103006;
2 # ABSTRACT: the default, minimal semantics of Perl5's pod element hierarchy
3  
4 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 with 'Pod::Elemental::Transformer';
6  
7 #pod =head1 SYNOPSIS
8 #pod
9 #pod Pod::Elemental::Transformer::Pod5->new->transform_node($pod_elem_document);
10 #pod
11 #pod ...and that's it.
12 #pod
13 #pod =head1 OVERVIEW
14 #pod
15 #pod The Pod5 transformer is meant to be used to convert the result of a "stock"
16 #pod Pod::Elemental::Document into something simpler to work with. It assumes that
17 #pod the document conforms more or less to the convetions laid out in L<perlpod> and
18 #pod L<perlpodspec>. It is not very strict, and makes very few assumptions,
19 #pod described here:
20 #pod
21 #pod =over 4
22 #pod
23 #pod =item * =begin/=end and =for enclose or produce regions within the document
24 #pod
25 #pod =item * regions are associated with format names; format names that begin with a colon enclose more pod-like content
26 #pod
27 #pod =item * regions nest strictly; all inner regions must end before outer regions
28 #pod
29 #pod =item * paragraphs in non-pod regions are "data" paragraphs
30 #pod
31 #pod =item * non-data paragraphs that start with spaces are "verbatim" paragraphs
32 #pod
33 #pod =item * groups of data or verbatim paragraphs can be consolidated
34 #pod
35 #pod =back
36 #pod
37 #pod Further, all elements are replaced with equivalent elements that perform the
38 #pod L<Pod::Elemental::Autoblank> role, so all "blank" events can be removed form
39 #pod the tree and ignored.
40 #pod
41 #pod =head1 CONFIGURATION
42 #pod
43 #pod None. For now, it just does the same thing every time with no configuration or
44 #pod options.
45 #pod
46 #pod =cut
47  
48 11 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 11 ▶use Pod::Elemental::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 11 ▶use Pod::Elemental::Element::Pod5::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 11 ▶use Pod::Elemental::Element::Pod5::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 11 ▶use Pod::Elemental::Element::Pod5::Nonpod;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 11 ▶use Pod::Elemental::Element::Pod5::Ordinary;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 11 ▶use Pod::Elemental::Element::Pod5::Verbatim;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 11 ▶use Pod::Elemental::Element::Pod5::Region;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 11 ▶use Pod::Elemental::Selectors -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 629 ▶sub _gen_class { "Pod::Elemental::Element::Generic::$_[1]" }
Subroutine _gen_class: called CC=1 SCAR=0.0
61 881 ▶sub _class { "Pod::Elemental::Element::Pod5::$_[1]" }
Subroutine _class: called CC=1 SCAR=0.0
62  
63 40 ▶sub _region_para_parts {
Subroutine _region_para_parts: called CC=4 SCAR=13.9
64 40 my ($self, $para) = @_;
65  
66 40 my ($colon, $target, $content, $nl) = $para->content =~ m/
67 \A
68 (:)?
69 (\S+)
70 (?:\s+(.+))?
71 (\s+)\z
72 /xsm;
73  
74 40 ▶ confess("=begin cannot be parsed") unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 40 40
75  
76 40 ▶ $colon ||= '';
Condition100%2/2
$colon ||= ""
l!l
931
MC/DC100%1/1
$colon ||= ""
$colon
Truth table100%2/2
A
A= $colon
Aresult
11
00
77 40 ▶ $content ||= '';
Condition100%2/2
$content ||= ""
l!l
238
MC/DC100%1/1
$content ||= ""
$content
Truth table100%2/2
A
A= $content
Aresult
11
00
78  
79 40 return ($colon, $target, "$content$nl");
80 }
81  
82 13 ▶sub __extract_region {
Subroutine __extract_region: called CC=8 SCAR=20.8
83 13 my ($self, $name, $in_paras) = @_;
84  
85 13 my %nest = ($name => 1);
86 13 my @region_paras;
87  
88 13 ▶ REGION_PARA: while (my $region_para = shift @$in_paras) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $region_para = shift @$in_paras) 157 0 157
89 157 ▶ if (s_command([ qw(begin end) ], $region_para)) {
Branch: 1 branch
BranchTrueFalseTotal
if (s_command(["begin", "end"], $region_para)) 25 132 157
90 25 my ($r_colon, $r_target) = $self->_region_para_parts($region_para);
91  
92 25 for ($nest{ "$r_colon$r_target" }) {
93 25 ▶ $_ += $region_para->command eq 'begin' ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$region_para->command eq 'begin' ? : 6 19 25
94  
95 25 ▶ confess("=end $r_colon$r_target without matching begin") if $_ < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 0 0 25 25
96  
97 25 ▶ last REGION_PARA if !$_ and "$r_colon$r_target" eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if not $_ and "$r_colon$r_target" eq $name 13 12 25
Condition100%3/3
not $_ and "$r_colon$r_target" eq $name
!ll&&!rl&&r
8413
MC/DC100%2/2
not $_ and "$r_colon$r_target" eq $name
not $_ "$r_colon$r_target" eq $name
Truth table100%3/3
not(A) and B
A= not $_B= "$r_colon$r_target" eq $name
ABresult
0X0
100
111
98 }
99 }
100  
101 144 push @region_paras, $region_para;
102 };
103  
104 13 return \@region_paras;
105 }
106  
107 12 ▶sub _upgrade_nonpod {
Subroutine _upgrade_nonpod: called CC=2 SCAR=6.9
108 12 my ($self, $in_paras) = @_;
109  
110 @$in_paras = map {
111 12 ▶ $_->isa( $self->_gen_class('Nonpod') )
Branch: 1 branch
BranchTrueFalseTotal
$_->isa($self->_gen_class('Nonpod')) ? : 4 229 233
112 ? $self->_class('Nonpod')->new({
113 content => $_->content,
114 })
115 : $_
116 } @$in_paras;
117 }
118  
119 25 ▶sub _collect_regions {
Subroutine _collect_regions: called CC=9 SCAR=22.0
120 25 my ($self, $in_paras) = @_;
121  
122 25 my @out_paras;
123  
124 25 my $s_region = s_command([ qw(begin for) ]);
125 25 my $region_class = $self->_class('Region');
126  
127 25 ▶ PARA: while (my $para = shift @{ $in_paras }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $para = shift @{$in_paras;}) 195 25 220
128 195 ▶ push(@out_paras, $para), next PARA unless $s_region->($para);
Branch: 1 branch
BranchTrueFalseTotal
unless &$s_region($para) 180 15 195
129  
130 15 ▶ if ($para->command eq 'for') {
Branch: 1 branch
BranchTrueFalseTotal
if ($para->command eq "for") 2 13 15
131 # factor out (for vertical space if nothing else) -- rjbs, 2009-10-20
132 2 my ($colon, $target, $content) = $self->_region_para_parts($para);
133  
134 2 ▶ my $region = $region_class->new({
Branch: 1 branch
BranchTrueFalseTotal
$colon ? : 1 1 2
135 children => [
136 $self->_gen_class('Text')->new({ content => $content }),
137 ],
138 format_name => $target,
139 is_pod => $colon ? 1 : 0,
140 content => "\n",
141 });
142  
143 2 push @out_paras, $region;
144 2 next PARA;
145 }
146  
147 13 my ($colon, $target, $content) = $self->_region_para_parts($para);
148  
149 13 my $region_paras = $self->__extract_region("$colon$target", $in_paras);
150  
151 13 ▶ shift @$region_paras while s_blank($region_paras->[0]);
Branch: 1 branch
BranchTrueFalseTotal
s_blank($region_paras->[0]) and do { shift @$region_paras } 13 13 26
152 13 ▶ pop @$region_paras while @$region_paras && s_blank($region_paras->[-1]);
Branch: 1 branch
BranchTrueFalseTotal
@$region_paras and s_blank($region_paras->[-1]) and do { pop @$region_paras } 12 13 25
Condition100%3/3
@$region_paras and s_blank($region_paras->[-1])
!ll&&!rl&&r
11212
MC/DC100%2/2
@$region_paras and s_blank($region_paras->[-1])
@$region_paras s_blank($region_paras->[-1])
Truth table100%3/3
A and B
A= @$region_parasB= s_blank($region_paras->[-1])
ABresult
0X0
100
111
153  
154 13 ▶ my $region = $region_class->new({
Branch: 1 branch
BranchTrueFalseTotal
$colon ? : 4 9 13
155 children => $self->_collect_regions($region_paras),
156 format_name => $target,
157 is_pod => $colon ? 1 : 0,
158 content => $content,
159 });
160  
161 13 push @out_paras, $region;
162 }
163  
164 25 @$in_paras = @out_paras;
165  
166 25 return $in_paras;
167 }
168  
169 12 ▶sub _strip_markers {
Subroutine _strip_markers: called CC=3 SCAR=11.0
170 12 my ($self, $in_paras) = @_;
171  
172 12 @$in_paras = grep { ! s_command([ qw(cut pod) ], $_) } @$in_paras;
173 12 ▶ shift @$in_paras while @$in_paras and s_blank($in_paras->[0]);
Branch: 1 branch
BranchTrueFalseTotal
@$in_paras and s_blank($in_paras->[0]) and do { shift @$in_paras } 9 12 21
Condition66%2/3
@$in_paras and s_blank($in_paras->[0])
!ll&&!rl&&r
0129
MC/DC50%1/2
@$in_paras and s_blank($in_paras->[0])
@$in_paras s_blank($in_paras->[0])
Truth table66%2/3
A and B
A= @$in_parasB= s_blank($in_paras->[0])
ABresult
0X0
100
111
174 }
175  
176 27 ▶sub _autotype_paras {
Subroutine _autotype_paras: called CC=6 SCAR=17.9
177 27 my ($self, $paras, $is_pod) = @_;
178  
179 @$paras = map {
180 27 my $elem = $_;
181 197 ▶ if ($elem->isa( $self->_gen_class('Text') )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa($self->_gen_class("Text"))) 48 149 197
182 48 ▶ my $class = $is_pod
Branch: 2 branches
BranchTrueFalseTotal
$is_pod ? : 34 14 48
$elem->content =~ /\A\s/ ? : 4 30 34
183 ? $elem->content =~ /\A\s/
184 ? $self->_class('Verbatim')
185 : $self->_class('Ordinary')
186 : $self->_class('Data');
187  
188 48 $elem = $class->new({ content => $elem->content });
189 }
190  
191 197 ▶ if ($elem->isa( $self->_class('Region') )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa($self->_class("Region"))) 15 182 197
192 15 $self->_autotype_paras( $elem->children, $elem->is_pod );
193 }
194  
195 197 ▶ if ($elem->isa( $self->_gen_class('Command') )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa($self->_gen_class("Command"))) 40 157 197
196 40 $elem = $self->_class('Command')->new({
197 command => $elem->command,
198 content => $elem->content,
199 });
200 }
201  
202 197 $elem;
203  
204 } @$paras;
205 }
206  
207 145 ▶sub __text_class {
Subroutine __text_class: called CC=3 SCAR=11.0
208 145 my ($self, $para) = @_;
209  
210 145 for my $type (qw(Verbatim Data)) {
211 286 my $class = $self->_class($type);
212 286 ▶ return $class if $para->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $para->isa($class) 10 276 286
213 }
214  
215 135 return;
216 }
217  
218 27 ▶sub _collect_runs {
Subroutine _collect_runs: called CC=14 SCAR=26.4
219 27 my ($self, $paras) = @_;
220  
221 $self->_collect_runs($_->children)
222 27 foreach grep { $_->isa( $self->_class('Region') ) } @$paras;
223  
224 27 PASS: for my $start (0 .. $#$paras) {
225 171 ▶ last PASS if $#$paras - $start < 2; # we need X..Blank..X at minimum
Branch: 1 branch
BranchTrueFalseTotal
if $paras->$#* - $start < 2 26 145 171
226  
227 145 my $class = $self->__text_class( $paras->[ $start ] );
228 145 ▶ next PASS unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 135 10 145
229  
230 10 my @to_collect = ($start);
231 10 NEXT: for my $next ($start+1 .. $#$paras) {
232 26 ▶ if ($paras->[ $next ]->isa($class) or s_blank($paras->[ $next ])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paras->[$next]->isa($class) or s_blank($paras->[$next])) 19 7 26
Condition100%3/3
$paras->[$next]->isa($class) or s_blank($paras->[$next])
l!l&&r!l&&!r
6137
MC/DC100%2/2
$paras->[$next]->isa($class) or s_blank($paras->[$next])
$paras->[$next]->isa($class) s_blank($paras->[$next])
Truth table100%3/3
A or B
A= $paras->[$next]->isa($class)B= s_blank($paras->[$next])
ABresult
1X1
011
000
233 19 push @to_collect, $next;
234 19 next NEXT;
235 }
236  
237 7 last NEXT;
238 }
239  
240 10 ▶ pop @to_collect while s_blank($paras->[ $to_collect[ -1 ] ]);
Branch: 1 branch
BranchTrueFalseTotal
s_blank($$paras[$to_collect[-1]]) and do { pop @to_collect } 7 10 17
241  
242 10 ▶ next PASS unless @to_collect >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @to_collect >= 3 6 4 10
243  
244 my $new_content = join(qq{\n},
245 4 map { $_ = $_->content; chomp; $_ } @$paras[@to_collect]
246 );
247  
248 4 splice @$paras, $start, scalar(@to_collect), $class->new({
249 content => $new_content,
250 });
251  
252 4 redo PASS;
253 }
254  
255 27 my @out;
256 27 ▶ PASS: for (my $i = 0; $i < @$paras; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$paras) 101 27 128
257 101 my $this = $paras->[$i];
258 101 push @out, $this;
259  
260 101 ▶ while ($paras->[$i+1] and s_blank($paras->[$i+1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$paras[$i + 1] and s_blank($$paras[$i + 1])) 84 101 185
Condition100%3/3
$$paras[$i + 1] and s_blank($$paras[$i + 1])
!ll&&!rl&&r
267584
MC/DC100%2/2
$$paras[$i + 1] and s_blank($$paras[$i + 1])
$$paras[$i + 1] s_blank($$paras[$i + 1])
Truth table100%3/3
A and B
A= $$paras[$i + 1]B= s_blank($$paras[$i + 1])
ABresult
0X0
100
111
261 84 $i++;
262 84 ▶ next unless $this->isa( $self->_class('Data') );
Branch: 1 branch
BranchTrueFalseTotal
unless $this->isa($self->_class("Data")) 81 3 84
263 3 $this->content( $this->content . $paras->[$i]->content );
264 }
265 }
266  
267 # @out = grep { not s_blank($_) } @$paras;
268  
269 # I really don't feel bad about rewriting in place by the time we get here.
270 # These are private methods, and I know the consequence of calling them.
271 # Nobody else should be. So there. -- rjbs, 2009-10-17
272 27 @$paras = @out;
273 27 return \@out;
274 }
275  
276 12 ▶sub transform_node {
Subroutine transform_node: called CC=1 SCAR=0.0
Pod undocumented
277 12 my ($self, $node) = @_;
278  
279 12 $self->_strip_markers($node->children);
280 12 $self->_upgrade_nonpod($node->children);
281 12 $self->_collect_regions($node->children);
282 12 $self->_autotype_paras($node->children, 1);
283 12 $self->_collect_runs($node->children);
284  
285 12 return $node;
286 }
287  
288 11 __PACKAGE__->meta->make_immutable;
289  
290 11 1;
291  
292 __END__
« Elemental/Transformer/Nester.pm ↑ Summary Elemental/Element/Pod5/Region.pm »