File Coverage

blib/lib/Pod/Weaver/Section/Region.pm
Criterion Covered Total %
statement 42 42 100.0
branch 11 14 78.5
condition 7 9 77.7
subroutine 10 10 100.0
pod 0 1 0.0
total 70 76 92.1


line stmt bran cond sub pod time code
1             package Pod::Weaver::Section::Region 4.019;
2             # ABSTRACT: find a region and put its contents in place where desired
3              
4 6     6   13165 use Moose;
  6         19  
  6         58  
5             with 'Pod::Weaver::Role::Section';
6              
7             # BEGIN BOILERPLATE
8 6     6   43809 use v5.20.0;
  6         24  
9 6     6   53 use warnings;
  6         16  
  6         252  
10 6     6   52 use utf8;
  6         21  
  6         71  
11 6     6   241 no feature 'switch';
  6         19  
  6         828  
12 6     6   59 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  6         24  
  6         61  
13             # END BOILERPLATE
14              
15             #pod =head1 OVERVIEW
16             #pod
17             #pod This section will find and include a located hunk of Pod. In general, it will
18             #pod find a region with the specified name, such as:
19             #pod
20             #pod =begin :myfoo
21             #pod
22             #pod =head1 More Pod Here
23             #pod
24             #pod =end :myfoo
25             #pod
26             #pod In other words, if your configuration include:
27             #pod
28             #pod [Region]
29             #pod region_name = myfoo
30             #pod
31             #pod ...then this weaver will look for "=begin :myfoo" ( and "=for :myfoo" and... ) and include
32             #pod it at the appropriate location in your output.
33             #pod
34             #pod Since you'll probably want to use Region several times, and that will require
35             #pod giving each use a unique name, you can omit C<region_name> if you provide a
36             #pod plugin name, and it will default to the plugin name. In other words, the
37             #pod configuration above could be specified just as:
38             #pod
39             #pod [Region / myfoo]
40             #pod
41             #pod If the C<required> attribute is given, and true, then an exception will be
42             #pod raised if this region can't be found.
43             #pod
44             #pod =cut
45              
46 6     6   862 use Pod::Elemental::Element::Pod5::Region;
  6         25  
  6         288  
47 6     6   41 use Pod::Elemental::Selectors -all;
  6         30  
  6         73  
48 6     6   3731 use Pod::Elemental::Types qw(FormatName);
  6         21  
  6         99  
49              
50             #pod =attr required
51             #pod
52             #pod A boolean value specifying whether this region is required to be present or not. Defaults
53             #pod to false.
54             #pod
55             #pod If it's enabled and the region can't be found an exception will be raised.
56             #pod
57             #pod =cut
58              
59             has required => (
60             is => 'ro',
61             isa => 'Bool',
62             default => 0,
63             );
64              
65             #pod =attr region_name
66             #pod
67             #pod The name of this region. Defaults to the plugin name.
68             #pod
69             #pod =cut
70              
71             has region_name => (
72             is => 'ro',
73             isa => FormatName,
74             lazy => 1,
75             required => 1,
76             default => sub { $_[0]->plugin_name },
77             );
78              
79             #pod =attr allow_nonpod
80             #pod
81             #pod A boolean value specifying whether nonpod regions are allowed or not. Defaults to false.
82             #pod
83             #pod C<nonpod> regions are regions I<without> a C<:> prefix as explained in
84             #pod L<< perlpodspec|perlpodspec/About Data Paragraphs and "=begin/=end" Regions >>
85             #pod
86             #pod # region_name = myregion
87             #pod # is_pod = false
88             #pod =begin myregion
89             #pod
90             #pod # region_name = myregion
91             #pod # is_pod = true
92             #pod =begin :myregion
93             #pod
94             #pod =cut
95              
96             has allow_nonpod => (
97             is => 'ro',
98             isa => 'Bool',
99             default => 0,
100             );
101              
102             #pod =attr flatten
103             #pod
104             #pod A boolean value specifying whether the region's contents should be flattened or not. Defaults to true.
105             #pod
106             #pod #unflattened
107             #pod =begin :myregion
108             #pod
109             #pod =head1
110             #pod
111             #pod =end :myregion
112             #pod
113             #pod #flattened
114             #pod =head1
115             #pod
116             #pod =cut
117              
118             has flatten => (
119             is => 'ro',
120             isa => 'Bool',
121             default => 1,
122             );
123              
124             sub weave_section {
125 26     26 0 87 my ($self, $document, $input) = @_;
126              
127 26         50 my @to_insert;
128              
129 26         680 my $idc = $input->{pod_document}->children;
130 26         304 IDX: for (my $i = 0; $i < @$idc; $i++) {
131 98 50       566 next unless my $para = $idc->[ $i ];
132 98 100 100     1440 next unless $para->isa('Pod::Elemental::Element::Pod5::Region')
133             and $para->format_name eq $self->region_name;
134 24 50 66     761 next if !$self->allow_nonpod and !$para->is_pod;
135              
136 24 100       945 if ( $self->flatten ) {
137 21         611 push @to_insert, $para->children->@*;
138             } else {
139 3         10 push @to_insert, $para;
140             }
141              
142 24         246 splice @$idc, $i, 1;
143              
144 24         727 redo IDX;
145             }
146              
147             confess "Couldn't find required Region for " . $self->region_name . " in file "
148 26 50 66     861 . (defined $input->{filename} ? $input->{filename} : '') if $self->required and not @to_insert;
    100          
149              
150 25 100       736 my $verb = $self->flatten ? 'flattening' : 'inserting';
151 25         718 $self->log_debug($verb . q{ } . $self->region_name . ' into pod');
152 25         1341 push $document->children->@*, @to_insert;
153             }
154              
155             __PACKAGE__->meta->make_immutable;
156             1;
157              
158             __END__
159              
160             =pod
161              
162             =encoding UTF-8
163              
164             =head1 NAME
165              
166             Pod::Weaver::Section::Region - find a region and put its contents in place where desired
167              
168             =head1 VERSION
169              
170             version 4.019
171              
172             =head1 OVERVIEW
173              
174             This section will find and include a located hunk of Pod. In general, it will
175             find a region with the specified name, such as:
176              
177             =begin :myfoo
178              
179             =head1 More Pod Here
180              
181             =end :myfoo
182              
183             In other words, if your configuration include:
184              
185             [Region]
186             region_name = myfoo
187              
188             ...then this weaver will look for "=begin :myfoo" ( and "=for :myfoo" and... ) and include
189             it at the appropriate location in your output.
190              
191             Since you'll probably want to use Region several times, and that will require
192             giving each use a unique name, you can omit C<region_name> if you provide a
193             plugin name, and it will default to the plugin name. In other words, the
194             configuration above could be specified just as:
195              
196             [Region / myfoo]
197              
198             If the C<required> attribute is given, and true, then an exception will be
199             raised if this region can't be found.
200              
201             =head1 PERL VERSION
202              
203             This module should work on any version of perl still receiving updates from
204             the Perl 5 Porters. This means it should work on any version of perl released
205             in the last two to three years. (That is, if the most recently released
206             version is v5.40, then this module should work on both v5.40 and v5.38.)
207              
208             Although it may work on older versions of perl, no guarantee is made that the
209             minimum required version will not be increased. The version may be increased
210             for any reason, and there is no promise that patches will be accepted to lower
211             the minimum required perl.
212              
213             =head1 ATTRIBUTES
214              
215             =head2 required
216              
217             A boolean value specifying whether this region is required to be present or not. Defaults
218             to false.
219              
220             If it's enabled and the region can't be found an exception will be raised.
221              
222             =head2 region_name
223              
224             The name of this region. Defaults to the plugin name.
225              
226             =head2 allow_nonpod
227              
228             A boolean value specifying whether nonpod regions are allowed or not. Defaults to false.
229              
230             C<nonpod> regions are regions I<without> a C<:> prefix as explained in
231             L<< perlpodspec|perlpodspec/About Data Paragraphs and "=begin/=end" Regions >>
232              
233             # region_name = myregion
234             # is_pod = false
235             =begin myregion
236              
237             # region_name = myregion
238             # is_pod = true
239             =begin :myregion
240              
241             =head2 flatten
242              
243             A boolean value specifying whether the region's contents should be flattened or not. Defaults to true.
244              
245             #unflattened
246             =begin :myregion
247              
248             =head1
249              
250             =end :myregion
251              
252             #flattened
253             =head1
254              
255             =head1 AUTHOR
256              
257             Ricardo SIGNES <cpan@semiotic.systems>
258              
259             =head1 COPYRIGHT AND LICENSE
260              
261             This software is copyright (c) 2023 by Ricardo SIGNES.
262              
263             This is free software; you can redistribute it and/or modify it under
264             the same terms as the Perl 5 programming language system itself.
265              
266             =cut