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   67317 use strict;
  12         35  
  12         370  
2 12     12   75 use warnings;
  12         26  
  12         737  
3             package Pod::Elemental::Selectors 0.103006;
4             # ABSTRACT: predicates for selecting elements
5              
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   71 use List::Util 1.33 'any';
  12         200  
  12         1027  
38              
39 12         140 use Sub::Exporter -setup => {
40             exports => [ qw(s_blank s_flat s_node s_command) ],
41 12     12   701 };
  12         13903  
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   329 my $para = shift;
56 267   100     2637 return $para && $para->isa('Pod::Elemental::Element::Generic::Blank');
57 267     267 1 663 };
58              
59 267 50       570 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   33 my $para = shift;
76 27   66     77 return $para && $para->does('Pod::Elemental::Flat');
77 4     4 1 1922 };
78              
79 4 100       21 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   18 my $para = shift;
96 12   66     40 return $para && $para->does('Pod::Elemental::Node');
97 12     12 1 38 };
98              
99 12 50       44 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 1044 my $command = shift;
121              
122             my $code = sub {
123 693     693   15248 my $para = shift;
124 693 100 66     1806 return unless $para && $para->does('Pod::Elemental::Command');
125 249 100       11304 return 1 unless defined $command;
126              
127 239 100       480 my $alts = ref $command ? $command : [ $command ];
128 239         932 return any { $para->command eq $_ } @$alts;
  464         13391  
129 471         1240 };
130              
131 471 100       1126 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.103006
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 PERL VERSION
156              
157             This library should run on perls released even a long time ago. It should work
158             on any version of perl released in the last five years.
159              
160             Although it may work on older versions of perl, no guarantee is made that the
161             minimum required version will not be increased. The version may be increased
162             for any reason, and there is no promise that patches will be accepted to lower
163             the minimum required perl.
164              
165             =head1 SELECTORS
166              
167             Selectors are predicates: they examine paragraphs and return either true or
168             false. All the selectors have (by default) names like: C<s_whatever>. They
169             expect zero or more parameters to affect the selection. If these parameters
170             are given, but no paragraph, a callback will be returned that will expect a
171             paragraph. If a paragraph is given, the selector will return immediately.
172              
173             For example, the C<s_command> selector expects a parameter that can be the name
174             of the command desired. Both of the following uses are valid:
175              
176             # create and use a callback:
177              
178             my $selector = s_command('head1');
179             my @headers = grep { $selector->($_) } @paragraphs;
180              
181             # just check a paragraph right now:
182              
183             if ( s_command('head1', $paragraph) ) { ... }
184              
185             The selectors can be imported individually or as the C<-all> group, and can be
186             renamed with L<Sub::Exporter> features. (Selectors cannot I<yet> be curried by
187             Sub::Exporter.)
188              
189             =head2 s_blank
190              
191             my $callback = s_blank;
192              
193             if( s_blank($para) ) { ... }
194              
195             C<s_blank> tests whether a paragraph is a Generic::Blank element.
196              
197             =head2 s_flat
198              
199             my $callback = s_flat;
200              
201             if( s_flat($para) ) { ... }
202              
203             C<s_flat> tests whether a paragraph does Pod::Elemental::Flat -- in other
204             words, is content-only.
205              
206             =head2 s_node
207              
208             my $callback = s_node;
209              
210             if( s_node($para) ) { ... }
211              
212             C<s_node> tests whether a paragraph does Pod::Elemental::Node -- in other
213             words, whether it may have children.
214              
215             =head2 s_command
216              
217             my $callback = s_command;
218             my $callback = s_command( $command_name);
219             my $callback = s_command(\@command_names);
220              
221             if( s_command(undef, \$para) ) { ... }
222              
223             if( s_command( $command_name, \$para) ) { ... }
224             if( s_command(\@command_names, \$para) ) { ... }
225              
226             C<s_command> tests whether a paragraph does Pod::Elemental::Command. If a
227             command name (or a reference to an array of command names) is given, the tested
228             paragraph's command must match one of the given command names.
229              
230             =head1 AUTHOR
231              
232             Ricardo SIGNES <cpan@semiotic.systems>
233              
234             =head1 COPYRIGHT AND LICENSE
235              
236             This software is copyright (c) 2022 by Ricardo SIGNES.
237              
238             This is free software; you can redistribute it and/or modify it under
239             the same terms as the Perl 5 programming language system itself.
240              
241             =cut