File Coverage

blib/lib/Pod/Elemental/Selectors.pm
Criterion Covered Total %
statement 33 33 100.0
branch 12 14 85.7
condition 9 12 75.0
subroutine 12 12 100.0
pod 4 4 100.0
total 70 75 93.3


line stmt bran cond sub pod time code
1 12     12   65974 use strict;
  12         40  
  12         438  
2 12     12   75 use warnings;
  12         29  
  12         956  
3             package Pod::Elemental::Selectors;
4             # ABSTRACT: predicates for selecting elements
5             $Pod::Elemental::Selectors::VERSION = '0.103005';
6             #pod =head1 OVERVIEW
7             #pod
8             #pod Pod::Elemental::Selectors provides a number of routines to check for
9             #pod Pod::Elemental paragraphs with given qualities.
10             #pod
11             #pod =head1 SELECTORS
12             #pod
13             #pod Selectors are predicates: they examine paragraphs and return either true or
14             #pod false. All the selectors have (by default) names like: C<s_whatever>. They
15             #pod expect zero or more parameters to affect the selection. If these parameters
16             #pod are given, but no paragraph, a callback will be returned that will expect a
17             #pod paragraph. If a paragraph is given, the selector will return immediately.
18             #pod
19             #pod For example, the C<s_command> selector expects a parameter that can be the name
20             #pod of the command desired. Both of the following uses are valid:
21             #pod
22             #pod # create and use a callback:
23             #pod
24             #pod my $selector = s_command('head1');
25             #pod my @headers = grep { $selector->($_) } @paragraphs;
26             #pod
27             #pod # just check a paragraph right now:
28             #pod
29             #pod if ( s_command('head1', $paragraph) ) { ... }
30             #pod
31             #pod The selectors can be imported individually or as the C<-all> group, and can be
32             #pod renamed with L<Sub::Exporter> features. (Selectors cannot I<yet> be curried by
33             #pod Sub::Exporter.)
34             #pod
35             #pod =cut
36              
37 12     12   86 use List::Util 1.33 'any';
  12         356  
  12         1593  
38              
39 12         168 use Sub::Exporter -setup => {
40             exports => [ qw(s_blank s_flat s_node s_command) ],
41 12     12   655 };
  12         12188  
42              
43             #pod =head2 s_blank
44             #pod
45             #pod my $callback = s_blank;
46             #pod
47             #pod if( s_blank($para) ) { ... }
48             #pod
49             #pod C<s_blank> tests whether a paragraph is a Generic::Blank element.
50             #pod
51             #pod =cut
52              
53             sub s_blank {
54             my $code = sub {
55 267     267   401 my $para = shift;
56 267   100     3046 return $para && $para->isa('Pod::Elemental::Element::Generic::Blank');
57 267     267 1 800 };
58              
59 267 50       680 return @_ ? $code->(@_) : $code;
60             }
61              
62             #pod =head2 s_flat
63             #pod
64             #pod my $callback = s_flat;
65             #pod
66             #pod if( s_flat($para) ) { ... }
67             #pod
68             #pod C<s_flat> tests whether a paragraph does Pod::Elemental::Flat -- in other
69             #pod words, is content-only.
70             #pod
71             #pod =cut
72              
73             sub s_flat {
74             my $code = sub {
75 27     27   45 my $para = shift;
76 27   66     103 return $para && $para->does('Pod::Elemental::Flat');
77 4     4 1 1960 };
78              
79 4 100       28 return @_ ? $code->(@_) : $code;
80             }
81              
82             #pod =head2 s_node
83             #pod
84             #pod my $callback = s_node;
85             #pod
86             #pod if( s_node($para) ) { ... }
87             #pod
88             #pod C<s_node> tests whether a paragraph does Pod::Elemental::Node -- in other
89             #pod words, whether it may have children.
90             #pod
91             #pod =cut
92              
93             sub s_node {
94             my $code = sub {
95 12     12   21 my $para = shift;
96 12   66     76 return $para && $para->does('Pod::Elemental::Node');
97 12     12 1 49 };
98              
99 12 50       36 return @_ ? $code->(@_) : $code;
100             }
101              
102             #pod =head2 s_command
103             #pod
104             #pod my $callback = s_command;
105             #pod my $callback = s_command( $command_name);
106             #pod my $callback = s_command(\@command_names);
107             #pod
108             #pod if( s_command(undef, \$para) ) { ... }
109             #pod
110             #pod if( s_command( $command_name, \$para) ) { ... }
111             #pod if( s_command(\@command_names, \$para) ) { ... }
112             #pod
113             #pod C<s_command> tests whether a paragraph does Pod::Elemental::Command. If a
114             #pod command name (or a reference to an array of command names) is given, the tested
115             #pod paragraph's command must match one of the given command names.
116             #pod
117             #pod =cut
118              
119             sub s_command {
120 471     471 1 1305 my $command = shift;
121              
122             my $code = sub {
123 693     693   11800 my $para = shift;
124 693 100 66     2170 return unless $para && $para->does('Pod::Elemental::Command');
125 249 100       13128 return 1 unless defined $command;
126              
127 239 100       544 my $alts = ref $command ? $command : [ $command ];
128 239         987 return any { $para->command eq $_ } @$alts;
  464         15965  
129 471         1393 };
130              
131 471 100       1394 return @_ ? $code->(@_) : $code;
132             }
133              
134             1;
135              
136             __END__
137              
138             =pod
139              
140             =encoding UTF-8
141              
142             =head1 NAME
143              
144             Pod::Elemental::Selectors - predicates for selecting elements
145              
146             =head1 VERSION
147              
148             version 0.103005
149              
150             =head1 OVERVIEW
151              
152             Pod::Elemental::Selectors provides a number of routines to check for
153             Pod::Elemental paragraphs with given qualities.
154              
155             =head1 SELECTORS
156              
157             Selectors are predicates: they examine paragraphs and return either true or
158             false. All the selectors have (by default) names like: C<s_whatever>. They
159             expect zero or more parameters to affect the selection. If these parameters
160             are given, but no paragraph, a callback will be returned that will expect a
161             paragraph. If a paragraph is given, the selector will return immediately.
162              
163             For example, the C<s_command> selector expects a parameter that can be the name
164             of the command desired. Both of the following uses are valid:
165              
166             # create and use a callback:
167              
168             my $selector = s_command('head1');
169             my @headers = grep { $selector->($_) } @paragraphs;
170              
171             # just check a paragraph right now:
172              
173             if ( s_command('head1', $paragraph) ) { ... }
174              
175             The selectors can be imported individually or as the C<-all> group, and can be
176             renamed with L<Sub::Exporter> features. (Selectors cannot I<yet> be curried by
177             Sub::Exporter.)
178              
179             =head2 s_blank
180              
181             my $callback = s_blank;
182              
183             if( s_blank($para) ) { ... }
184              
185             C<s_blank> tests whether a paragraph is a Generic::Blank element.
186              
187             =head2 s_flat
188              
189             my $callback = s_flat;
190              
191             if( s_flat($para) ) { ... }
192              
193             C<s_flat> tests whether a paragraph does Pod::Elemental::Flat -- in other
194             words, is content-only.
195              
196             =head2 s_node
197              
198             my $callback = s_node;
199              
200             if( s_node($para) ) { ... }
201              
202             C<s_node> tests whether a paragraph does Pod::Elemental::Node -- in other
203             words, whether it may have children.
204              
205             =head2 s_command
206              
207             my $callback = s_command;
208             my $callback = s_command( $command_name);
209             my $callback = s_command(\@command_names);
210              
211             if( s_command(undef, \$para) ) { ... }
212              
213             if( s_command( $command_name, \$para) ) { ... }
214             if( s_command(\@command_names, \$para) ) { ... }
215              
216             C<s_command> tests whether a paragraph does Pod::Elemental::Command. If a
217             command name (or a reference to an array of command names) is given, the tested
218             paragraph's command must match one of the given command names.
219              
220             =head1 AUTHOR
221              
222             Ricardo SIGNES <rjbs@cpan.org>
223              
224             =head1 COPYRIGHT AND LICENSE
225              
226             This software is copyright (c) 2020 by Ricardo SIGNES.
227              
228             This is free software; you can redistribute it and/or modify it under
229             the same terms as the Perl 5 programming language system itself.
230              
231             =cut