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.018;
2             # ABSTRACT: find a region and put its contents in place where desired
3              
4 6     6   12510 use Moose;
  6         17  
  6         51  
5             with 'Pod::Weaver::Role::Section';
6              
7             # BEGIN BOILERPLATE
8 6     6   40471 use v5.20.0;
  6         21  
9 6     6   44 use warnings;
  6         13  
  6         223  
10 6     6   38 use utf8;
  6         15  
  6         52  
11 6     6   222 no feature 'switch';
  6         16  
  6         728  
12 6     6   44 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  6         11  
  6         72  
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   801 use Pod::Elemental::Element::Pod5::Region;
  6         29  
  6         279  
47 6     6   47 use Pod::Elemental::Selectors -all;
  6         28  
  6         69  
48 6     6   3633 use Pod::Elemental::Types qw(FormatName);
  6         15  
  6         87  
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 83 my ($self, $document, $input) = @_;
126              
127 26         54 my @to_insert;
128              
129 26         684 my $idc = $input->{pod_document}->children;
130 26         266 IDX: for (my $i = 0; $i < @$idc; $i++) {
131 98 50       480 next unless my $para = $idc->[ $i ];
132 98 100 100     1448 next unless $para->isa('Pod::Elemental::Element::Pod5::Region')
133             and $para->format_name eq $self->region_name;
134 24 50 66     722 next if !$self->allow_nonpod and !$para->is_pod;
135              
136 24 100       839 if ( $self->flatten ) {
137 21         596 push @to_insert, $para->children->@*;
138             } else {
139 3         8 push @to_insert, $para;
140             }
141              
142 24         198 splice @$idc, $i, 1;
143              
144 24         680 redo IDX;
145             }
146              
147             confess "Couldn't find required Region for " . $self->region_name . " in file "
148 26 50 66     788 . (defined $input->{filename} ? $input->{filename} : '') if $self->required and not @to_insert;
    100          
149              
150 25 100       700 my $verb = $self->flatten ? 'flattening' : 'inserting';
151 25         728 $self->log_debug($verb . q{ } . $self->region_name . ' into pod');
152 25         1300 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.018
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 SUPPORT
202              
203             This module has the same support period as perl itself: it supports the two
204             most recent versions of perl. (That is, if the most recently released version
205             is v5.40, then this module should work on both v5.40 and v5.38.)
206              
207             Although it may work on older versions of perl, no guarantee is made that the
208             minimum required version will not be increased. The version may be increased
209             for any reason, and there is no promise that patches will be accepted to lower
210             the minimum required perl.
211              
212             =head1 ATTRIBUTES
213              
214             =head2 required
215              
216             A boolean value specifying whether this region is required to be present or not. Defaults
217             to false.
218              
219             If it's enabled and the region can't be found an exception will be raised.
220              
221             =head2 region_name
222              
223             The name of this region. Defaults to the plugin name.
224              
225             =head2 allow_nonpod
226              
227             A boolean value specifying whether nonpod regions are allowed or not. Defaults to false.
228              
229             C<nonpod> regions are regions I<without> a C<:> prefix as explained in
230             L<< perlpodspec|perlpodspec/About Data Paragraphs and "=begin/=end" Regions >>
231              
232             # region_name = myregion
233             # is_pod = false
234             =begin myregion
235              
236             # region_name = myregion
237             # is_pod = true
238             =begin :myregion
239              
240             =head2 flatten
241              
242             A boolean value specifying whether the region's contents should be flattened or not. Defaults to true.
243              
244             #unflattened
245             =begin :myregion
246              
247             =head1
248              
249             =end :myregion
250              
251             #flattened
252             =head1
253              
254             =head1 AUTHOR
255              
256             Ricardo SIGNES <rjbs@semiotic.systems>
257              
258             =head1 COPYRIGHT AND LICENSE
259              
260             This software is copyright (c) 2021 by Ricardo SIGNES.
261              
262             This is free software; you can redistribute it and/or modify it under
263             the same terms as the Perl 5 programming language system itself.
264              
265             =cut