File Coverage

blib/lib/Pod/Elemental/Selectors.pm
Criterion Covered Total %
statement 12 33 36.3
branch 0 14 0.0
condition 0 12 0.0
subroutine 4 12 33.3
pod 4 4 100.0
total 20 75 26.6


line stmt bran cond sub pod time code
1 1     1   15954 use strict;
  1         2  
  1         37  
2 1     1   4 use warnings;
  1         1  
  1         63  
3             package Pod::Elemental::Selectors;
4             # ABSTRACT: predicates for selecting elements
5             $Pod::Elemental::Selectors::VERSION = '0.103004';
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 1     1   4 use List::Util 1.33 'any';
  1         29  
  1         110  
38              
39 1         7 use Sub::Exporter -setup => {
40             exports => [ qw(s_blank s_flat s_node s_command) ],
41 1     1   594 };
  1         9929  
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 0     0     my $para = shift;
56 0   0       return $para && $para->isa('Pod::Elemental::Element::Generic::Blank');
57 0     0 1   };
58              
59 0 0         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 0     0     my $para = shift;
76 0   0       return $para && $para->does('Pod::Elemental::Flat');
77 0     0 1   };
78              
79 0 0         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 0     0     my $para = shift;
96 0   0       return $para && $para->does('Pod::Elemental::Node');
97 0     0 1   };
98              
99 0 0         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 0     0 1   my $command = shift;
121              
122             my $code = sub {
123 0     0     my $para = shift;
124 0 0 0       return unless $para && $para->does('Pod::Elemental::Command');
125 0 0         return 1 unless defined $command;
126              
127 0 0         my $alts = ref $command ? $command : [ $command ];
128 0           return any { $para->command eq $_ } @$alts;
  0            
129 0           };
130              
131 0 0         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.103004
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              
180             =head2 s_blank
181              
182             my $callback = s_blank;
183              
184             if( s_blank($para) ) { ... }
185              
186             C<s_blank> tests whether a paragraph is a Generic::Blank element.
187              
188              
189             =head2 s_flat
190              
191             my $callback = s_flat;
192              
193             if( s_flat($para) ) { ... }
194              
195             C<s_flat> tests whether a paragraph does Pod::Elemental::Flat -- in other
196             words, is content-only.
197              
198              
199             =head2 s_node
200              
201             my $callback = s_node;
202              
203             if( s_node($para) ) { ... }
204              
205             C<s_node> tests whether a paragraph does Pod::Elemental::Node -- in other
206             words, whether it may have children.
207              
208              
209             =head2 s_command
210              
211             my $callback = s_command;
212             my $callback = s_command( $command_name);
213             my $callback = s_command(\@command_names);
214              
215             if( s_command(undef, \$para) ) { ... }
216              
217             if( s_command( $command_name, \$para) ) { ... }
218             if( s_command(\@command_names, \$para) ) { ... }
219              
220             C<s_command> tests whether a paragraph does Pod::Elemental::Command. If a
221             command name (or a reference to an array of command names) is given, the tested
222             paragraph's command must match one of the given command names.
223              
224             =head1 AUTHOR
225              
226             Ricardo SIGNES <rjbs@cpan.org>
227              
228             =head1 COPYRIGHT AND LICENSE
229              
230             This software is copyright (c) 2014 by Ricardo SIGNES.
231              
232             This is free software; you can redistribute it and/or modify it under
233             the same terms as the Perl 5 programming language system itself.
234              
235             =cut