blib/lib/Pod/Query.pm

filter: Statementstmt 88.3% 335 / 379 Branchbran 69.3% 147 / 212 Conditioncond 69.1% 56 / 81 MC/DCmcdc 46.0% 23 / 50 Subroutinesub 94.7% 36 / 38 Podpod 100% 7 / 7 totaltotal 78.7% 604 / 767 CC 156 SCAR 30.4
CC
156
Cov
80%
_find
66.3
_invert
22.4
_render
19.7
CRAP 21.0
↑ Summary
1 package Pod::Query;
2  
3 7 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Pod::Text();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Pod::LOL();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use File::Spec::Functions qw( catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Text::ParseWords qw( parse_line );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Term::Size::Any qw( chars );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Pod::Query - Query pod documents
16  
17 =cut
18  
19 7 our $VERSION = '0.39';
20 7 our $DEBUG_LOL_DUMP = 0;
21 7 our $DEBUG_STRUCT_OVER = 0;
22 7 our $DEBUG_TREE = 0;
23 7 our $DEBUG_TREE_DUMP = 0;
24 7 our $DEBUG_FIND_CONDITIONS = 0;
25 7 our $DEBUG_FIND_AFTER_DEFAULTS = 0;
26 4 our $DEBUG_PRE_FIND_DUMP = 0;
27 4 our $DEBUG_FIND = 0;
28 4 our $DEBUG_FIND_DUMP = 0;
29 4 our $DEBUG_INVERT = 0;
30 4 our $DEBUG_RENDER = 0;
31 4 our $MOCK_ROOT = 0;
32  
33 =head1 SYNOPSIS
34  
35 Query POD information from a file
36  
37 % perl -MPod::Query -E 'say for Pod::Query->new("ojo")->find("head1[0]")'
38  
39 NAME
40 ojo - Fun one-liners with Mojo
41  
42 % perl -MPod::Query -E 'say Pod::Query->new("ojo")->find("head1[0]/Para[0]")'
43  
44 ojo - Fun one-liners with Mojo
45  
46 % perl -MPod::Query -E 'say Pod::Query->new(shift)->find("head1[0]/Para[0]")' my.pod
47  
48 Find Methods:
49  
50 find_title;
51 find_method;
52 find_method_summary;
53 find_events;
54 find($query_sting);
55 find(@query_structs);
56  
57 =head1 DESCRIPTION
58  
59 This module takes a class name, extracts the POD
60 and provides methods to query specific information.
61  
62 =head1 SUBROUTINES/METHODS
63  
64 =cut
65  
66 #
67 # Method maker
68 #
69  
70 =head2 _has
71  
72 Generates class accessor methods (like Mojo::Base::attr)
73  
74 =cut
75  
76 7 ▶sub _has {
Subroutine _has: called CC=2 SCAR=6.9
77 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
78 7 for my $attr ( @_ ) {
79 155 ▶ *$attr = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
80 155 ▶ return $_[0]{$attr} if @_ == 1; # Get: return $self-<{$attr}
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 128 27 155
81 27 $_[0]{$attr} = $_[1]; # Set: $self->{$attr} = $val
82 27 $_[0]; # return $self
83 28 };
84 }
85 }
86  
87 =head2 path
88  
89 Path to the pod class file
90  
91 =head2 lol
92  
93 List of lists (LOL) structure of the pod file.
94 Result of Pod::LOL.
95  
96 =head2 tree
97  
98 An hierarchy is added to the lol to create a
99 tree like structure of the pod file.
100  
101 =head2 class_is_path
102  
103 Flag to indicate if the class is really a path to the file.
104  
105 =cut
106  
107 7 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
108 7 _has qw(
109 path
110 lol
111 tree
112 class_is_path
113 );
114 }
115  
116 #
117 # Debug
118 #
119  
120 0 ▶sub _dumper {
Subroutine _dumper: not called CC=2 SCAR=17.9
121 0 require Data::Dumper;
122 0 my $data = Data::Dumper
123 ->new( [@_] )
124 ->Indent( 1 )
125 ->Sortkeys( 1 )
126 ->Terse( 1 )
127 ->Useqq( 1 )
128 ->Dump;
129 0 ▶ return $data if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
130 0 say $data;
131 }
132  
133 =head2 new
134  
135 Create a new object.
136 Return value is cached (based on the class of the pod file).
137  
138 use Pod::Query;
139 my $pod = Pod::Query->new('Pod::LOL', PATH_ONLY=0);
140  
141 PATH_ONLY can be used to determine the path to the pod
142 document without having to do much unnecessary work.
143  
144 =cut
145  
146 9 ▶sub new {
Subroutine new: called CC=8 SCAR=23.6
Pod documented
147 9 my ( $class, $pod_class, $path_only ) = @_;
148 9 ▶ $path_only //= 0;
Condition50%1/2
$path_only //= 0
l!l
09
MC/DC0%0/1
$path_only //= 0
$path_only
Truth table50%1/2
A
A= $path_only
Aresult
11
00
149 9 state %CACHE;
150  
151 9 my $cached;
152 9 ▶ return $cached if $cached = $CACHE{$pod_class};
Branch: 1 branch
BranchTrueFalseTotal
if $cached = $CACHE{$pod_class} 0 9 9
153  
154 9 my $s = bless {
155 pod_class => $pod_class,
156 lol => [],
157 tree => [],
158 }, $class;
159  
160 9 $s->path( $s->_class_to_path( $pod_class ) );
161  
162 9 ▶ return $s if $path_only or not $s->path;
Branch: 1 branch
BranchTrueFalseTotal
if $path_only or not $s->path 0 9 9
Condition33%1/3
$path_only or not $s->path
l!l&&r!l&&!r
009
MC/DC0%0/2
$path_only or not $s->path
$path_only not $s->path
Truth table33%1/3
A or not(B)
A= $path_onlyB= not $s->path
ABresult
1X1
011
000
163  
164 9 ▶ my $lol = $MOCK_ROOT ? _mock_root() : Pod::LOL->new_root( $s->path );
Branch: 1 branch
BranchTrueFalseTotal
$Pod::Query::MOCK_ROOT ? : 9 0 9
165 9 $lol = _flatten_for_tags( $lol );
166 9 ▶ if ( $DEBUG_LOL_DUMP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_LOL_DUMP) 0 9 9
167 0 say "DEBUG_LOL_DUMP: " . _dumper $lol;
168 0 exit;
169 }
170  
171 9 $s->lol( $lol );
172 9 $s->tree( _lol_to_tree( $lol ) );
173 9 ▶ if ( $DEBUG_TREE_DUMP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_TREE_DUMP) 0 9 9
174 0 say "DEBUG_TREE_DUMP: " . _dumper $s->tree();
175 0 exit;
176 }
177  
178 9 $CACHE{$pod_class} = $s;
179  
180 9 $s;
181 }
182  
183 =head2 _class_to_path
184  
185 Given a class name, returns the path to the pod file.
186 Return value is cached (based on the class of the pod file).
187  
188 If the class is not found in INC, it will be checked whether
189 the input is an existing file path.
190  
191 Returns an empty string if there are any errors.
192  
193 =cut
194  
195 4 ▶sub _class_to_path {
Subroutine _class_to_path: called CC=13 SCAR=26.6
196 4 my ( $s, $pod_class ) = @_;
197 4 state %CACHE;
198 4 my $path;
199  
200 4 ▶ return $path if $path = $CACHE{$pod_class};
Branch: 1 branch
BranchTrueFalseTotal
if $path = $CACHE{$pod_class} 0 4 4
201  
202 4 my $partial_path = catfile( split /::/, $pod_class ) . '.pm';
203  
204 # Shortcut for files already used.
205 4 $path = $INC{$partial_path};
206 4 ▶ return $CACHE{$pod_class} = $path if $path and -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path and -f $path 0 4 4
Condition33%1/3
$path and -f $path
!ll&&!rl&&r
400
MC/DC0%0/2
$path and -f $path
$path -f $path
Truth table33%1/3
A and B
A= $pathB= -f $path
ABresult
0X0
100
111
207  
208 # Otherwise find it ourselves.
209 4 for ( @INC ) {
210 20 $path = catfile( $_, $partial_path );
211 20 ▶ return $CACHE{$pod_class} = $path if $path and -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path and -f $path 2 18 20
Condition66%2/3
$path and -f $path
!ll&&!rl&&r
0182
MC/DC50%1/2
$path and -f $path
$path -f $path
Truth table66%2/3
A and B
A= $pathB= -f $path
ABresult
0X0
100
111
212 }
213  
214 # Check for it in PATH also.
215 # Maybe pod_class is the path.
216 2 for ( "", split /:/, $ENV{PATH} ) {
217  
218 # Absolute path or current folder means class is path.
219 12 ▶ $path = ( $_ and $_ ne "." ) ? catfile( $_, $pod_class ) : $pod_class;
Branch: 1 branch
BranchTrueFalseTotal
$_ && $_ ne '.' ? : 10 2 12
Condition66%2/3
$_ and $_ ne "."
!ll&&!rl&&r
2010
MC/DC50%1/2
$_ and $_ ne "."
$_ $_ ne "."
Truth table66%2/3
A and B
A= $_B= $_ ne "."
ABresult
0X0
100
111
220 12 ▶ if ( $path and -f $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and -f $path) 1 11 12
Condition66%2/3
$path and -f $path
!ll&&!rl&&r
0111
MC/DC50%1/2
$path and -f $path
$path -f $path
Truth table66%2/3
A and B
A= $pathB= -f $path
ABresult
0X0
100
111
221 1 ▶ $s->class_is_path( 1 ) if ref $s;
Branch: 1 branch
BranchTrueFalseTotal
if ref $s 0 1 1
222 1 return $CACHE{$pod_class} = $path;
223 }
224 }
225  
226 1 return "";
227 }
228  
229 =head2 _mock_root
230  
231 For debugging and/or testing.
232 Builds a sample object (overwrite this in a test file).
233  
234 =cut
235  
236 0 ▶sub _mock_root { }
Subroutine _mock_root: not called CC=1 SCAR=0.0
237  
238 =head2 _flatten_for_tags
239  
240 Removes for tags from the lol and flattens
241 out the inner tags to be on the same level as the for
242 tag was.
243  
244 =cut
245  
246 9 ▶sub _flatten_for_tags {
Subroutine _flatten_for_tags: called CC=4 SCAR=14.1
247 9 my ( $lol ) = @_;
248 9 my @flat;
249  
250 9 for ( @$lol ) {
251 322 my ( $tag, @data ) = @$_;
252 322 ▶ $tag //= '';
Condition50%1/2
$tag //= ""
l!l
3220
MC/DC0%0/1
$tag //= ""
$tag
Truth table50%1/2
A
A= $tag
Aresult
11
00
253  
254 322 ▶ push @flat, ( $tag eq "for" ) ? @data : $_;
Branch: 1 branch
BranchTrueFalseTotal
$tag eq 'for' ? : 0 322 322
255 }
256  
257 9 \@flat;
258 }
259  
260 =head2 _lol_to_tree
261  
262 Generates a tree from a Pod::LOL object.
263 The structure of the tree is based on the N (level) in "=headN".
264  
265 This example pod:
266  
267 =head1 FUNCTIONS
268  
269 =Para Description of Functions
270  
271 =head2 Function1
272  
273 =Para Description of Function1
274  
275 =head1 AUTHOR
276  
277 =cut
278  
279 This will be grouped as:
280  
281 =head1 FUNCTIONS
282 =Para Description of Functions
283 =head2 Function1
284 =Para Description of Function1
285 =head1 AUTHOR
286  
287 In summary:
288  
289 =over 2
290  
291 =item *
292  
293 Non "head" tags are always grouped "below".
294  
295 =item *
296  
297 HeadN tags with a higher N with also be grouped below.
298  
299 =item *
300  
301 HeadN tags with the same or lower N will be grouped higher.
302  
303 =back
304  
305 =cut
306  
307 14 ▶sub _lol_to_tree {
Subroutine _lol_to_tree: called CC=9 SCAR=22.0
308 14 my ( $lol ) = @_;
309 14 my ( $is_in, $is_out );
310 14 my %heads_table = __PACKAGE__->_define_heads_regex_table();
311 14 my $is_head = qr/ ^ head (\d) $ /x;
312 14 my $node = {};
313 14 my @tree;
314  
315 107 ▶ my $push = sub { # push to tree.
Subroutine __ANON__: called CC=4 SCAR=13.9
316 107 ▶ return if not %$node;
Branch: 1 branch
BranchTrueFalseTotal
unless %$node 14 93 107
317 93 my $kids = $node->{kids};
318 $node->{kids} = _lol_to_tree( $kids )
319 93 ▶ if ref( $kids ) && first { $_->{tag} =~ /$is_head/ } @$kids;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ref $kids and &first(sub { $_->{"tag"} =~ /$is_head/u; } , @$kids) 5 88 93
Condition100%3/3
ref $kids and &first(sub { $_->{"tag"} =~ /$is_head/u; } , @$kids)
!ll&&!rl&&r
6825
MC/DC100%2/2
ref $kids and &first(sub { $_->{"tag"} =~ /$is_head/u; } , @$kids)
ref $kids &first(sub { $_->{"tag"} =~ /$is_head/u; } , @$kids)
Truth table100%3/3
A and B
A= ref $kidsB= &first(sub { $_->{"tag"} =~ /$is_head/u; } , @$kids)
ABresult
0X0
100
111
320 93 push @tree, $node;
321 93 $node = {};
322 14 };
323  
324 14 ▶ say "\n_ROOT_TO_TREE()" if $DEBUG_TREE;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_TREE 0 14 14
325  
326 14 for ( @$lol ) {
327 545 ▶ say "\n_=", _dumper $_ if $DEBUG_TREE;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_TREE 0 545 545
328  
329 545 my $leaf = _make_leaf( $_ );
330 545 ▶ say "\nleaf=", _dumper $leaf if $DEBUG_TREE;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_TREE 0 545 545
331  
332 # Outer tag.
333 545 ▶ if ( not $is_in or $leaf->{tag} =~ /$is_out/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_in or $leaf->{'tag'} =~ /$is_out/u) { } 93 452 545
Condition100%3/3
not $is_in or $leaf->{"tag"} =~ /$is_out/u
l!l&&r!l&&!r
1875452
MC/DC100%2/2
not $is_in or $leaf->{"tag"} =~ /$is_out/u
not $is_in $leaf->{"tag"} =~ /$is_out/u
Truth table100%3/3
not(A) or B
A= not $is_inB= $leaf->{"tag"} =~ /$is_out/u
ABresult
1X1
011
000
334 93 $push->();
335 93 $node = $leaf;
336 93 ▶ if ( $leaf->{tag} =~ /$is_head/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leaf->{"tag"} =~ /$is_head/u) 88 5 93
337 88 ( $is_in, $is_out ) = @{$heads_table{$1}};
338 }
339 }
340 else {
341 452 push @{$node->{kids}}, $leaf;
342 452 ▶ say "node: ", _dumper $node if $DEBUG_TREE;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_TREE 0 452 452
343 }
344 }
345  
346 14 $push->();
347  
348 14 \@tree;
349 }
350  
351 =head2 _define_heads_regex_table
352  
353 Generates the regexes for head elements inside
354 and outside the current head.
355  
356 =cut
357  
358 14 ▶sub _define_heads_regex_table {
Subroutine _define_heads_regex_table: called CC=1 SCAR=0.0
359 map {
360 14 my $inner = join "", $_ + 1 .. 5; # num=2, inner=345
361 56 my $outer = join "", 0 .. $_; # num=2, outer=012
362  
363 56 $_ => [ map { qr/ ^ head ([$_]) $ /x } $inner, $outer ]
364 } 1 .. 4;
365 }
366  
367 =head2 _make_leaf
368  
369 Creates a new node (aka leaf).
370  
371 =cut
372  
373 545 ▶sub _make_leaf {
Subroutine _make_leaf: called CC=3 SCAR=11.0
374 545 my ( $node ) = @_;
375 545 ▶ return $node if ref $node eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq ref {} 223 322 545
376  
377 322 my ( $tag, @text ) = @$node;
378 322 my $leaf = { tag => $tag };
379  
380 322 ▶ if ( $tag =~ / ^ over- /x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ / ^ over- /ux) { } 7 315 322
381 7 $leaf->{kids} = _structure_over( \@text );
382 7 $leaf->{text} = "";
383 }
384 else {
385 315 $leaf->{text} = join "", @text;
386 }
387  
388 322 $leaf;
389 }
390  
391 =head2 _structure_over
392  
393 Restructures the text for an "over-text" element to be under it.
394 Also, "item-text" will be the first element of each group.
395  
396 =cut
397  
398 7 ▶sub _structure_over {
Subroutine _structure_over: called CC=4 SCAR=14.0
399 7 my ( $text_list ) = @_;
400 7 my @struct;
401 my @nodes;
402  
403 74 ▶ my $push = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
404 74 ▶ return if not @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 7 67 74
405  
406 # First is the parent node.
407 67 my $item_text = shift @nodes;
408  
409 # Treat the rest of the tags as kids.
410 67 ▶ push @struct,
Branch: 1 branch
BranchTrueFalseTotal
@nodes ? : 63 4 67
411 { %$item_text, @nodes ? ( kids => [ splice @nodes ] ) : (), };
412 7 };
413  
414 7 for ( @$text_list ) {
415 242 my ( $tag, @text ) = @$_;
416 242 ▶ $push->() if $tag =~ / ^ item- /x;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ / ^ item- /ux 67 175 242
417 242 push @nodes,
418 {
419 tag => $tag,
420 text => join( "", @text ),
421 };
422 }
423  
424 7 $push->();
425  
426 7 ▶ if ( $DEBUG_STRUCT_OVER ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_STRUCT_OVER) 0 7 7
427 0 say "DEBUG_STRUCT_OVER-IN: " . _dumper $text_list;
428 0 say "DEBUG_STRUCT_OVER-OUT: " . _dumper \@struct;
429 }
430  
431 7 \@struct;
432 }
433  
434 =head2 find_title
435  
436 Extracts the title information.
437  
438 =cut
439  
440 9 ▶sub find_title {
Subroutine find_title: called CC=1 SCAR=0.0
Pod documented
441 9 my ( $s ) = @_;
442 9 scalar $s->find( 'head1=NAME[0]/Para[0]' );
443 }
444  
445 =head2 find_method
446  
447 Extracts the complete method information.
448  
449 =cut
450  
451 10 ▶sub find_method {
Subroutine find_method: called CC=2 SCAR=7.1
Pod documented
452 10 my ( $s, $method ) = @_;
453 10 ▶ my $m = $s->_clean_method_name( $method ) or return "";
Branch: 1 branch
BranchTrueFalseTotal
my $m = $s->_clean_method_name($method) or return "" 10 0 10
454  
455 10 $s->find( sprintf '~head=~^%s\b.*$[0]**', $m );
456 }
457  
458 =head2 find_method_summary
459  
460 Extracts the method summary.
461  
462 =cut
463  
464 10 ▶sub find_method_summary {
Subroutine find_method_summary: called CC=2 SCAR=7.1
Pod documented
465 10 my ( $s, $method ) = @_;
466 10 ▶ my $m = $s->_clean_method_name( $method ) or return "";
Branch: 1 branch
BranchTrueFalseTotal
my $m = $s->_clean_method_name($method) or return "" 10 0 10
467  
468 10 scalar $s->find( sprintf '~head=~^%s\b.*$[0]/~(Data|Para)[0]', $m );
469 }
470  
471 =head2 _clean_method_name
472  
473 Returns a method name without any possible parenthesis.
474  
475 =cut
476  
477 20 ▶sub _clean_method_name {
Subroutine _clean_method_name: called CC=4 SCAR=14.2
478 20 my ( $s, $name ) = @_;
479 20 my $safe_start = qr/ ^ [\w_] /x;
480 20 my $safe_end = qr/ [\w_()] $ /x;
481 20 ▶ return if $name !~ $safe_start;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$safe_start/u 0 20 20
482 20 ▶ return if $name !~ $safe_end;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$safe_end/u 0 20 20
483  
484 20 my $clean = quotemeta( $name =~ s/[^a-zA-Z0-9_]+//gr );
485 20 ▶ return if $clean !~ $safe_start;
Branch: 1 branch
BranchTrueFalseTotal
unless $clean =~ /$safe_start/u 0 20 20
486  
487 20 $clean;
488 }
489  
490 =head2 find_events
491  
492 Extracts a list of events with a description.
493  
494 Returns a list of key value pairs.
495  
496 =cut
497  
498 6 ▶sub find_events {
Subroutine find_events: called CC=1 SCAR=0.0
Pod documented
499 6 my ( $s ) = @_;
500 6 $s->find( '~head=EVENTS[0]/~head*/(Para)[0]' );
501 }
502  
503 =head2 find
504  
505 Generic extraction command.
506  
507 Note: This function is Scalar/List context sensitive!
508  
509 $query->find($condition)
510  
511 Where condtion is a string as described in L</"_query_string_to_struct">
512  
513 $query->find(@conditions)
514  
515 Where each condition can contain:
516  
517 {
518 tag => "TAG_NAME", # Find all matching tags.
519 text => "TEXT_NAME", # Find all matching texts.
520 keep => 1, # Capture the text.
521 keep_all => 1, # Capture entire section.
522 nth => 0, # Use only the nth match.
523 nth_in_group => 0, # Use only the nth matching group.
524 }
525  
526 Return contents of entire head section:
527  
528 find (
529 {tag => "head", text => "a", keep_all => 1},
530 )
531  
532 Results:
533  
534 [
535 " my \$app = a('/hel...",
536 {text => "Create a route with ...", wrap => 1},
537 " \$ perl -Mojo -E ...",
538 ]
539  
540 =cut
541  
542 123 ▶sub find {
Subroutine find: called CC=11 SCAR=26.8
Pod documented
543 123 my ( $s, @raw_conditions ) = @_;
544  
545 123 my $find_conditions;
546  
547 # If the find condition is a single string.
548 123 ▶ if ( @raw_conditions == 1 and not ref $raw_conditions[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@raw_conditions == 1 and not ref $raw_conditions[0]) { } 123 0 123
Condition33%1/3
@raw_conditions == 1 and not ref $raw_conditions[0]
!ll&&!rl&&r
00123
MC/DC0%0/2
@raw_conditions == 1 and not ref $raw_conditions[0]
@raw_conditions == 1 not ref $raw_conditions[0]
Truth table33%1/3
A and not(B)
A= @raw_conditions == 1B= not ref $raw_conditions[0]
ABresult
0X0
100
111
549 123 $find_conditions = $s->_query_string_to_struct( $raw_conditions[0] );
550 }
551 else {
552 0 $find_conditions = \@raw_conditions;
553 }
554 123 ▶ say "DEBUG_FIND_CONDITIONS: " . _dumper $find_conditions
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND_CONDITIONS 0 123 123
555 if $DEBUG_FIND_CONDITIONS;
556  
557 123 _check_conditions( $find_conditions );
558 119 _set_condition_defaults( $find_conditions );
559 119 ▶ say "DEBUG_FIND_AFTER_DEFAULTS " . _dumper $find_conditions
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND_AFTER_DEFAULTS 0 119 119
560 if $DEBUG_FIND_AFTER_DEFAULTS;
561  
562 119 my @tree = @{$s->tree};
563 119 my $kept_all;
564 119 ▶ if ( $DEBUG_PRE_FIND_DUMP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_PRE_FIND_DUMP) 0 119 119
565 0 say "DEBUG_PRE_FIND_DUMP: " . _dumper \@tree;
566 0 exit;
567 }
568  
569 119 for ( @$find_conditions ) {
570 206 @tree = _find( $_, @tree );
571 206 ▶ if ( $_->{keep_all} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"keep_all"}) 18 188 206
572 18 $kept_all++;
573 18 last;
574 }
575 }
576 119 ▶ if ( $DEBUG_FIND_DUMP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_FIND_DUMP) 0 119 119
577 0 say "DEBUG_FIND_DUMP: " . _dumper \@tree;
578 0 ▶ exit if $DEBUG_FIND_DUMP > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND_DUMP > 1 0 0 0
579 }
580  
581 119 ▶ if ( not $kept_all ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kept_all) 101 18 119
582 101 @tree = _invert( @tree );
583 }
584  
585 119 _render( $kept_all, @tree );
586 }
587  
588 =head2 _query_string_to_struct
589  
590 Convert a pod query string into a structure based on these rules:
591  
592 1. Split string by '/'.
593 Each piece is a separate list of conditions.
594  
595 2. Remove an optional '*' or '**' from the last condition.
596 Keep is set if we have '*'.
597 Keep all is set if we have '**'.
598  
599 3. Remove an optional [N] from the last condition.
600 (Where N is a decimal).
601 Set nth base on 'N'.
602 Set nth_in_group if previous word is surrounded by ():
603 (WORD)[N]
604  
605 4. Double and single quotes are removed from the ends (if matching).
606  
607 5. Split each list of conditions by "=".
608 First word is the tag.
609 Second word is the text (if any).
610 If either starts with a tilde, then the word:
611 - is treated like a pattern.
612 - is case Insensitive.
613  
614 Precedence:
615 If quoted and ~, left wins:
616 ~"head1" => qr/"head"/,
617 "~head1" => qr/head/,
618  
619 =cut
620  
621 196 ▶sub _query_string_to_struct {
Subroutine _query_string_to_struct: called CC=13 SCAR=25.7
622 196 my ( $s, $query_string ) = @_;
623 196 my $is_nth = qr/ \[ (-?\d+) \] $ /x;
624 196 my $is_nth_in_group = qr/ ^ \( (.+) \) $is_nth /x;
625 196 my $is_keep = qr/ \* $ /x;
626 196 my $is_keep_all = qr/ \* \* $ /x;
627  
628 my @query_struct =
629 map {
630 316 my @condition = parse_line( '=', "1", $_ );
631 316 my $set = {};
632  
633 # Set flags based on last condition.
634 316 for ( $condition[-1] ) {
635 316 ▶ if ( s/$is_keep_all// ) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/$is_keep_all//u) { } 25 291 316
elsif (s/$is_keep//u) { } 28 263 291
636 25 $set->{keep_all}++;
637 }
638 elsif ( s/$is_keep// ) {
639 28 $set->{keep}++;
640 }
641  
642 316 ▶ if ( s/$is_nth_in_group// ) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/$is_nth_in_group//u) { } 30 286 316
elsif (s/$is_nth//u) { } 205 81 286
643 30 $_ = $1;
644 30 $set->{nth_in_group} = $2;
645 }
646 elsif ( s/$is_nth// ) {
647 205 $set->{nth} = $1;
648 }
649 }
650  
651 # Remove outer quotes (if any).
652 316 for ( @condition ) {
653 435 for my $quote ( qw/ " ' / ) {
654 866 ▶ if ( $quote eq substr( $_, 0, 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($quote eq substr($_, 0, 1) and $quote eq substr($_, -1)) 12 854 866
Condition66%2/3
$quote eq substr($_, 0, 1) and $quote eq substr($_, -1)
!ll&&!rl&&r
854012
MC/DC50%1/2
$quote eq substr($_, 0, 1) and $quote eq substr($_, -1)
$quote eq substr($_, 0, 1) $quote eq substr($_, -1)
Truth table66%2/3
A and B
A= $quote eq substr($_, 0, 1)B= $quote eq substr($_, -1)
ABresult
0X0
100
111
655 and $quote eq substr( $_, -1 ) )
656 {
657 12 $_ = substr( $_, 1, -1 ); # Strip first and last characters.
658 12 last; # Skip multi quoting.
659 }
660 }
661 }
662  
663 # Regex or literal.
664 316 for ( qw/ tag text / ) {
665 632 ▶ last if not @condition;
Branch: 1 branch
BranchTrueFalseTotal
unless @condition 197 435 632
666 435 my $cond = shift @condition;
667 435 ▶ $set->{$_} = ( $cond =~ s/^~// ) ? qr/$cond/i : $cond;
Branch: 1 branch
BranchTrueFalseTotal
$cond =~ s/^~//u ? : 193 242 435
668 }
669  
670 316 $set;
671 }
672 196 grep { $_ } # Skip trailing and leading slashes.
673 parse_line( '/', 1, $query_string );
674  
675 196 \@query_struct;
676 }
677  
678 =head2 _check_conditions
679  
680 Check if queries are valid.
681  
682 =cut
683  
684 123 ▶sub _check_conditions {
Subroutine _check_conditions: called CC=10 SCAR=23.7
685 123 my ( $sections ) = @_;
686  
687 123 my $error_message = <<'ERROR';
688  
689 Invalid input: expecting a hash reference!
690  
691 Syntax:
692  
693 $pod->find( 'QUERY' ) # As explained in _query_string_to_struct().
694  
695 # OR:
696  
697 $pod->find(
698 # section1
699 {
700 tag => "TAG", # Search to look for.
701 text => "TEXT", # Text of the tag to find.
702 keep => 1, # Must only be in last section.
703 keep_all => 1, # Keep this tag and sub tags.
704 nth => 0, # Stop searching after find so many matches.
705 nth_in_group => 0, # Nth only in the current group.
706 },
707 # ...
708 # conditionN
709 );
710 ERROR
711  
712 die "$error_message"
713 if not $sections
714 or not @$sections
715 123 ▶ or grep { ref() ne ref {} } @$sections;
Branch: 1 branch
BranchTrueFalseTotal
if not $sections && @$sections or grep {ref $_ ne ref {};} @$sections 4 119 123
Condition66%4/6
not $sections && @$sections or grep {ref $_ ne ref {};} @$sections
l!l&&r!l&&!r
40119
$sections && @$sections
!ll&&!rl&&r
04119
MC/DC33%1/3
not $sections && @$sections or grep {ref $_ ne ref {};} @$sections
$sections @$sections grep {ref $_ ne ref {};} @$sections
Truth table50%2/4
not(A && B) or C
A= $sectionsB= @$sectionsC= grep {ref $_ ne ref {};} @$sections
ABCresult
0XX1
10X1
1111
1100
716  
717 # keep_all should only be in the last section
718 119 my $last = $#$sections;
719 119 ▶ while ( my ( $n, $section ) = each @$sections ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$sections) 206 119 325
720 die "Error: keep_all is not in last query!\n"
721 206 ▶ if $section->{keep_all} and $n < $last;
Branch: 1 branch
BranchTrueFalseTotal
if $section->{"keep_all"} and $n < $last 0 206 206
Condition66%2/3
$section->{"keep_all"} and $n < $last
!ll&&!rl&&r
188180
MC/DC0%0/2
$section->{"keep_all"} and $n < $last
$section->{"keep_all"} $n < $last
Truth table66%2/3
A and B
A= $section->{"keep_all"}B= $n < $last
ABresult
0X0
100
111
722 }
723  
724 # Cannot use both nth and nth_in_group (makes no sense, plus may cause errors)
725 119 ▶ while ( my ( $n, $section ) = each @$sections ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$sections) 206 119 325
726 die "Error: nth and nth_in_group are exclusive!\n"
727 if defined $section->{nth}
728 206 ▶ and defined $section->{nth_in_group};
Branch: 1 branch
BranchTrueFalseTotal
if defined $section->{"nth"} and defined $section->{"nth_in_group"} 0 206 206
Condition66%2/3
defined $section->{"nth"} and defined $section->{"nth_in_group"}
!ll&&!rl&&r
661400
MC/DC0%0/2
defined $section->{"nth"} and defined $section->{"nth_in_group"}
defined $section->{"nth"} defined $section->{"nth_in_group"}
Truth table66%2/3
A and B
A= defined $section->{"nth"}B= defined $section->{"nth_in_group"}
ABresult
0X0
100
111
729 }
730 }
731  
732 =head2 _set_condition_defaults
733  
734 Assigns default query options.
735  
736 =cut
737  
738 119 ▶sub _set_condition_defaults {
Subroutine _set_condition_defaults: called CC=15 SCAR=27.1
739 119 my ( $conditions ) = @_;
740 119 for my $condition ( @$conditions ) {
741  
742 # Text Options
743 206 for ( qw/ tag text / ) {
744 412 ▶ if ( defined $condition->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $condition->{$_}) { } 283 129 412
745 283 ▶ if ( ref $condition->{$_} ne ref qr// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $condition->{$_} ne ref qr//u) 155 128 283
746 155 $condition->{$_} = qr/^$condition->{$_}$/;
747 }
748 }
749 else {
750 129 $condition->{$_} = qr//;
751 }
752 }
753  
754 # Bit Options
755 206 for ( qw/ keep keep_all / ) {
756 412 ▶ if ( defined $condition->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $condition->{$_}) { } 36 376 412
757 36 $condition->{$_} = !!$condition->{$_};
758 }
759 else {
760 376 $condition->{$_} = 0;
761 }
762 }
763  
764 # Range Options
765 206 my $is_digit = qr/ ^ -?\d+ $ /x;
766 206 for ( qw/ nth nth_in_group / ) {
767 412 my $v = $condition->{$_};
768 412 ▶ if ( defined $v and $v =~ /$is_digit/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $v =~ /$is_digit/u) 156 256 412
Condition66%2/3
defined $v and $v =~ /$is_digit/u
!ll&&!rl&&r
2560156
MC/DC50%1/2
defined $v and $v =~ /$is_digit/u
defined $v $v =~ /$is_digit/u
Truth table66%2/3
A and B
A= defined $vB= $v =~ /$is_digit/u
ABresult
0X0
100
111
769 156 ▶ $v ||= "0 but true";
Condition100%2/2
$v ||= "0 but true"
l!l
10146
MC/DC100%1/1
$v ||= "0 but true"
$v
Truth table100%2/2
A
A= $v
Aresult
11
00
770 156 ▶ my $end = ( $v >= 0 ) ? "pos" : "neg"; # Set negative or
Branch: 1 branch
BranchTrueFalseTotal
$v >= 0 ? : 152 4 156
771 156 my $name = "_${_}_$end"; # positive form.
772 156 $condition->{$name} = $v;
773 }
774 }
775  
776 }
777  
778 # Last condition should be keep or keep_all.
779 # (otherwise, why even query for it?)
780 119 for ( $conditions->[-1] ) {
781 119 ▶ if ( not $_->{keep} || $_->{keep_all} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_->{"keep"} or $_->{"keep_all"}) 93 26 119
Condition100%3/3
$_->{"keep"} or $_->{"keep_all"}
l!l&&r!l&&!r
81893
MC/DC100%2/2
$_->{"keep"} or $_->{"keep_all"}
$_->{"keep"} $_->{"keep_all"}
Truth table100%3/3
A or B
A= $_->{"keep"}B= $_->{"keep_all"}
ABresult
1X1
011
000
782 93 $_->{keep} = 1;
783 }
784 }
785 }
786  
787 =head2 _find
788  
789 Lower level find command.
790  
791 =cut
792  
793 206 ▶sub _find {
Subroutine _find: called CC=35 SCAR=41.9
794 206 my ( $need, @groups ) = @_;
795 206 ▶ if ( $DEBUG_FIND ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_FIND) 0 206 206
796 0 say "\n_FIND()";
797 0 say "need: ", _dumper $need;
798 0 say "groups: ", _dumper \@groups;
799 }
800  
801 206 my $nth_p = $need->{_nth_pos}; # Simplify code by already
802 206 my $nth_n = $need->{_nth_neg}; # knowing if neg or pos.
803 206 my $nth_in_group_p = $need->{_nth_grou_pos}; # Set in _set_section_defaults.
804 206 my $nth_in_group_n = $need->{_nth_grou_neg};
805 206 my @found;
806  
807 GROUP:
808 206 for my $group ( @groups ) {
809 562 my @tries = ( $group ); # Assume single group to process.
810 562 ▶ my @prev = @{ $group->{prev} // [] };
Condition100%2/2
$group->{"prev"} // []
l!l
81481
MC/DC100%1/1
$group->{"prev"} // []
$group->{"prev"}
Truth table100%2/2
A
A= $group->{"prev"}
Aresult
11
00
811 562 my $locked_prev = 0;
812 562 my @found_in_group;
813 562 ▶ if ( $DEBUG_FIND ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_FIND) 0 562 562
814 0 say "\nprev: ", _dumper \@prev;
815 0 say "group: ", _dumper $group;
816 }
817  
818 TRY:
819 562 ▶ while ( my $try = shift @tries ) { # Can add to this queue if a sub tag.
Branch: 1 branch
BranchTrueFalseTotal
if (my $try = shift @tries) 4834 442 5276
820 4834 ▶ say "\nTrying: try=", _dumper $try if $DEBUG_FIND;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 4834 4834
821  
822 4834 ▶ if ( defined $try->{text} ) { # over-text has no text (only kids).
Branch: 1 branch
BranchTrueFalseTotal
if (defined $try->{"text"}) 4834 0 4834
823 4834 ▶ if ( $DEBUG_FIND ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_FIND) { } 0 4834 4834
elsif ($try->{'tag'} =~ /$need->{'tag'}/u and $try->{'text'} =~ /$need->{'text'}/u and not defined $try->{'keep'}) { } 546 8556 9102
824 0 say "text=$try->{text}";
825 0 say "next->{tag}=$need->{tag}";
826 0 say "next->{text}=$need->{text}";
827 }
828  
829 ▶ elsif (
Condition100%6/6
$try->{"tag"} =~ /$need->{"tag"}/u and $try->{"text"} =~ /$need->{"text"}/u and not defined $try->{"keep"}
!ll&&!rl&&r
426820546
$try->{"tag"} =~ /$need->{"tag"}/u and $try->{"text"} =~ /$need->{"text"}/u
!ll&&!rl&&r
3294974566
MC/DC100%3/3
$try->{"tag"} =~ /$need->{"tag"}/u and $try->{"text"} =~ /$need->{"text"}/u and not defined $try->{"keep"}
$try->{"tag"} =~ /$need->{"tag"}/u $try->{"text"} =~ /$need->{"text"}/u not defined $try->{"keep"}
Truth table100%4/4
A and B and not(C)
A= $try->{"tag"} =~ /$need->{"tag"}/uB= $try->{"text"} =~ /$need->{"text"}/uC= not defined $try->{"keep"}
ABCresult
0XX0
10X0
1100
1111
830 $try->{tag} =~ /$need->{tag}/
831 and $try->{text} =~ /$need->{text}/
832 and not defined $try->{keep} # Already found the node.
833 # Since nodes are checked again
834 # on next call to _find.
835 )
836 {
837 546 ▶ say "Found: tag=$try->{tag}, text=$try->{text}"
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 546 546
838 if $DEBUG_FIND;
839 push @found_in_group, {
840 %$try, # Copy current search options.
841 prev => \@prev, # Need this for the inversion step.
842 keep => $need->{keep}, # Remember for later.
843 546 };
844  
845 # Specific match (positive)
846 546 ▶ say "nth_p:$nth_p and found_in_group:"
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 546 546
847 . _dumper \@found_in_group
848 if $DEBUG_FIND;
849 546 ▶ if ( $nth_p and @found + @found_in_group > $nth_p ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nth_p and @found + @found_in_group > $nth_p) { } 120 426 546
elsif ($nth_in_group_p and @found_in_group > $nth_in_group_p) { } 0 852 852
Condition100%3/3
$nth_p and @found + @found_in_group > $nth_p
!ll&&!rl&&r
4224120
MC/DC100%2/2
$nth_p and @found + @found_in_group > $nth_p
$nth_p @found + @found_in_group > $nth_p
Truth table100%3/3
A and B
A= $nth_pB= @found + @found_in_group > $nth_p
ABresult
0X0
100
111
850 120 ▶ say "ENFORCING: nth=$nth_p" if $DEBUG_FIND;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 120 120
851 120 @found = $found_in_group[-1];
852 120 last GROUP;
853 }
854  
855 # Specific group match (positive)
856 ▶ elsif ( $nth_in_group_p
Condition33%1/3
$nth_in_group_p and @found_in_group > $nth_in_group_p
!ll&&!rl&&r
42600
MC/DC0%0/2
$nth_in_group_p and @found_in_group > $nth_in_group_p
$nth_in_group_p @found_in_group > $nth_in_group_p
Truth table33%1/3
A and B
A= $nth_in_group_pB= @found_in_group > $nth_in_group_p
ABresult
0X0
100
111
857 and @found_in_group > $nth_in_group_p )
858 {
859 0 ▶ say "ENFORCING: nth_in_group=$nth_in_group_p"
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 0 0
860 if $DEBUG_FIND;
861 0 @found_in_group = $found_in_group[-1];
862 0 last TRY;
863 }
864  
865 426 next TRY;
866 }
867 }
868  
869 4288 ▶ if ( $try->{kids} and not @found_in_group ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try->{"kids"} and not @found_in_group) 1227 3061 4288
Condition100%3/3
$try->{"kids"} and not @found_in_group
!ll&&!rl&&r
29131481227
MC/DC100%2/2
$try->{"kids"} and not @found_in_group
$try->{"kids"} not @found_in_group
Truth table100%3/3
A and not(B)
A= $try->{"kids"}B= not @found_in_group
ABresult
0X0
100
111
870 1227 ▶ say "Got kids and nothing yet in queue" if $DEBUG_FIND;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 1227 1227
871 1227 unshift @tries, @{$try->{kids}}; # Process kids tags.
872 1227 ▶ if ( $try->{keep} and not $locked_prev++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try->{"keep"} and not $locked_prev++) 8 1219 1227
Condition66%2/3
$try->{"keep"} and not $locked_prev++
!ll&&!rl&&r
121908
MC/DC50%1/2
$try->{"keep"} and not $locked_prev++
$try->{"keep"} not $locked_prev++
Truth table66%2/3
A and not(B)
A= $try->{"keep"}B= not $locked_prev++
ABresult
0X0
100
111
873 unshift @prev,
874 {
875 8 map { $_ => $try->{$_} }
876 qw/tag text keep/
877 };
878 8 ▶ say "prev changed: ", _dumper \@prev if $DEBUG_FIND;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 8 8
879 }
880 1227 ▶ say "locked_prev: $locked_prev" if $DEBUG_FIND;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 1227 1227
881 }
882 }
883  
884 # Specific group match (negative)
885 442 ▶ if ( $nth_in_group_n and @found_in_group >= abs $nth_in_group_n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nth_in_group_n and @found_in_group >= abs $nth_in_group_n) 0 442 442
Condition33%1/3
$nth_in_group_n and @found_in_group >= abs $nth_in_group_n
!ll&&!rl&&r
44200
MC/DC0%0/2
$nth_in_group_n and @found_in_group >= abs $nth_in_group_n
$nth_in_group_n @found_in_group >= abs $nth_in_group_n
Truth table33%1/3
A and B
A= $nth_in_group_nB= @found_in_group >= abs $nth_in_group_n
ABresult
0X0
100
111
886 0 ▶ say "ENFORCING: nth_in_group_n=$nth_in_group_n" if $DEBUG_FIND;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 0 0
887 0 @found_in_group = $found_in_group[$nth_in_group_n];
888 }
889  
890 442 ▶ push @found, splice @found_in_group if @found_in_group;
Branch: 1 branch
BranchTrueFalseTotal
if @found_in_group 186 256 442
891 }
892  
893 # Specific match (negative)
894 206 ▶ if ( $nth_n and @found >= abs $nth_n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nth_n and @found >= abs $nth_n) 4 202 206
Condition66%2/3
$nth_n and @found >= abs $nth_n
!ll&&!rl&&r
20204
MC/DC50%1/2
$nth_n and @found >= abs $nth_n
$nth_n @found >= abs $nth_n
Truth table66%2/3
A and B
A= $nth_nB= @found >= abs $nth_n
ABresult
0X0
100
111
895 4 ▶ say "ENFORCING: nth=$nth_n" if $DEBUG_FIND;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 4 4
896 4 @found = $found[$nth_n];
897 }
898  
899 206 ▶ say "found: ", _dumper \@found if $DEBUG_FIND;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_FIND 0 206 206
900  
901 206 @found;
902 }
903  
904 =head2 _invert
905  
906 Previous elements are inside of the child
907 (due to the way the tree is created).
908  
909 This method walks through each child and puts
910 the parent in its place.
911  
912 =cut
913  
914 101 ▶sub _invert {
Subroutine _invert: called CC=11 SCAR=31.1
915 101 my ( @groups ) = @_;
916 101 ▶ if ( $DEBUG_INVERT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_INVERT) 0 101 101
917 0 say "\n_INVERT()";
918 0 say "groups: ", _dumper \@groups;
919 }
920  
921 101 my @tree;
922 my %navi;
923  
924 101 for my $group ( @groups ) {
925 push @tree, {
926 379 map { $_ => $group->{$_} }
927 qw/ tag text keep kids /
928 };
929 379 ▶ if ( $DEBUG_INVERT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_INVERT) 0 379 379
930 0 say "\nInverting: group=", _dumper $group;
931 0 say "tree: ", _dumper \@tree;
932 }
933  
934 379 ▶ my $prevs = $group->{prev} // [];
Condition50%1/2
$group->{"prev"} // []
l!l
3790
MC/DC0%0/1
$group->{"prev"} // []
$group->{"prev"}
Truth table50%1/2
A
A= $group->{"prev"}
Aresult
11
00
935 379 for my $prev ( @$prevs ) {
936 8 my $prev_node = $navi{$prev};
937 8 ▶ if ( $DEBUG_INVERT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_INVERT) 0 8 8
938 0 say "prev: ", _dumper $prev;
939 0 say "prev_node: ", _dumper $prev_node;
940 }
941 8 ▶ if ( $prev_node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_node) { } 0 8 8
942 0 push @$prev_node, pop @tree;
943 0 ▶ if ( $DEBUG_INVERT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_INVERT) 0 0 0
944 0 say "FOUND: prev_node=", _dumper $prev_node;
945 }
946 0 last;
947 }
948 else {
949 8 $prev_node = $navi{$prev} = [ $tree[-1] ];
950 8 $tree[-1] = { %$prev, kids => $prev_node };
951 8 ▶ if ( $DEBUG_INVERT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_INVERT) 0 8 8
952 0 say "NEW: prev_node=", _dumper $prev_node;
953 }
954 }
955 }
956  
957 379 ▶ say "tree end: ", _dumper \@tree if $DEBUG_INVERT;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_INVERT 0 379 379
958 }
959  
960 101 @tree;
961 }
962  
963 =head2 _render
964  
965 Transforms a tree of found nodes in a simple list
966 or a string depending on context.
967  
968 Pod::Text formatter is used for C<Para> tags when C<keep_all> is set.
969  
970 =cut
971  
972 119 ▶sub _render {
Subroutine _render: called CC=17 SCAR=29.8
973 119 my ( $kept_all, @tree ) = @_;
974 119 ▶ if ( $DEBUG_RENDER ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_RENDER) 0 119 119
975 0 say "\n_RENDER()";
976 0 say "tree: ", _dumper \@tree;
977 0 say "kept_all: ", _dumper $kept_all;
978 }
979  
980 119 my $formatter = Pod::Text->new( width => get_term_width(), );
981 119 $formatter->{MARGIN} = 2;
982  
983 119 my @lines;
984 my $n;
985  
986 119 for my $group ( @tree ) {
987 419 my @tries = ( $group );
988 419 ▶ say "\ngroup: ", _dumper $group if $DEBUG_RENDER;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_RENDER 0 419 419
989  
990 419 ▶ while ( my $try = shift @tries ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $try = shift @tries) 2839 419 3258
991 2839 ▶ say "\nTrying: try=", _dumper $try if $DEBUG_RENDER;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_RENDER 0 2839 2839
992  
993 2839 my $_text = $try->{text};
994 2839 ▶ say "_text=$_text" if $DEBUG_RENDER;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_RENDER 0 2839 2839
995  
996 2839 ▶ if ( $kept_all ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kept_all) { } 548 2291 2839
elsif ($try->{'keep'}) { } 387 1904 2291
997 548 ▶ $_text .= ":" if ++$n == 1; # Only for the first line.
Branch: 1 branch
BranchTrueFalseTotal
if ++$n == 1 18 530 548
998 548 ▶ if ( $try->{tag} eq "Para" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try->{"tag"} eq "Para") 164 384 548
999 164 ▶ say "USING FORMATTER" if $DEBUG_RENDER;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_RENDER 0 164 164
1000 164 $_text = $formatter->reformat( $_text );
1001 }
1002 548 push @lines, $_text, "";
1003 }
1004 elsif ( $try->{keep} ) {
1005 387 ▶ say "keeping" if $DEBUG_RENDER;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_RENDER 0 387 387
1006 387 push @lines, $_text;
1007 }
1008  
1009 2839 ▶ if ( $try->{kids} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try->{"kids"}) 738 2101 2839
1010 738 unshift @tries, @{$try->{kids}};
1011 738 ▶ if ( $DEBUG_RENDER ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Query::DEBUG_RENDER) 0 738 738
1012 0 say "Got kids";
1013 0 say "tries: ", _dumper \@tries;
1014 }
1015 }
1016 }
1017 }
1018  
1019 119 ▶ say "lines: ", _dumper \@lines if $DEBUG_RENDER;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Query::DEBUG_RENDER 0 119 119
1020  
1021 119 ▶ return @lines if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 50 69 119
1022 69 join "\n", @lines;
1023 }
1024  
1025 =head2 get_term_width
1026  
1027 Determines, caches and returns the terminal width.
1028  
1029 =head3 Error: Unable to get Terminal Size
1030  
1031 If terminal width cannot be detected, 80 will be assumed.
1032  
1033 =cut
1034  
1035 6 ▶sub get_term_width {
Subroutine get_term_width: called CC=3 SCAR=11.0
Pod documented
1036 9 state $term_width;
1037  
1038 9 ▶ if ( not $term_width ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term_width) 1 5 6
1039 4 $term_width = eval { chars() };
1040 4 ▶ $term_width ||= 80; # Safe default.
Condition50%1/2
$term_width ||= 80
l!l
01
MC/DC0%0/1
$term_width ||= 80
$term_width
Truth table50%1/2
A
A= $term_width
Aresult
11
00
1041 4 $term_width--; # Padding.
1042 }
1043  
1044 9 $term_width;
1045 }
1046  
1047 =head1 SEE ALSO
1048  
1049 L<App::Pod>
1050  
1051 L<Pod::LOL>
1052  
1053 L<Pod::Text>
1054  
1055  
1056 =head1 AUTHOR
1057  
1058 Tim Potapov, C<< <tim.potapov[AT]gmail.com> >>
1059  
1060 =head1 BUGS
1061  
1062 Please report any bugs or feature requests to L<https://github.com/poti1/pod-query/issues>.
1063  
1064 =head1 CAVEAT
1065  
1066 Nothing to report.
1067  
1068 =head1 SUPPORT
1069  
1070 You can find documentation for this module with the perldoc command.
1071  
1072 perldoc Pod::Query
1073  
1074  
1075 You can also look for information at:
1076  
1077 L<https://metacpan.org/pod/Pod::Query>
1078 L<https://github.com/poti1/pod-query>
1079  
1080  
1081 =head1 ACKNOWLEDGEMENTS
1082  
1083 TBD
1084  
1085 =head1 LICENSE AND COPYRIGHT
1086  
1087 This software is Copyright (c) 2022 by Tim Potapov.
1088  
1089 This is free software, licensed under:
1090  
1091 The Artistic License 2.0 (GPL Compatible)
1092  
1093  
1094 =cut
1095  
1096 4 1; # End of Pod::Query
↑ Summary