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