File Coverage

blib/lib/Pod/Weaver/Section/Region.pm
Criterion Covered Total %
statement 30 30 100.0
branch 11 14 78.5
condition 7 9 77.7
subroutine 5 5 100.0
pod 0 1 0.0
total 53 59 89.8


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