File Coverage

blib/lib/Pod/Weaver/Section/Generic.pm
Criterion Covered Total %
statement 22 22 100.0
branch 3 4 75.0
condition 2 3 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 33 36 91.6


line stmt bran cond sub pod time code
1             package Pod::Weaver::Section::Generic 4.018;
2             # ABSTRACT: a generic section, found by lifting sections
3              
4 5     5   20294 use Moose;
  5         15  
  5         45  
5             with 'Pod::Weaver::Role::Section';
6              
7 5     5   33662 use v5.20.0;
  5         20  
8 5     5   35 use experimental 'postderef'; # this experiment succeeded -- rjbs, 2021-04-02
  5         11  
  5         45  
9              
10             #pod =head1 OVERVIEW
11             #pod
12             #pod This section will find and include a located hunk of Pod. In general, it will
13             #pod find a C<=head1> command with a content of the plugin's name.
14             #pod
15             #pod In other words, if your configuration include:
16             #pod
17             #pod [Generic]
18             #pod header = OVERVIEW
19             #pod
20             #pod ...then this weaver will look for "=head1 OVERVIEW" and include it at the
21             #pod appropriate location in your output.
22             #pod
23             #pod Since you'll probably want to use Generic several times, and that will require
24             #pod giving each use a unique name, you can omit C<header> if you provide a
25             #pod plugin name, and it will default to the plugin name. In other words, the
26             #pod configuration above could be specified just as:
27             #pod
28             #pod [Generic / OVERVIEW]
29             #pod
30             #pod If the C<required> attribute is given, and true, then an exception will be
31             #pod raised if this section can't be found.
32             #pod
33             #pod =cut
34              
35 5     5   974 use Pod::Elemental::Element::Pod5::Region;
  5         11  
  5         196  
36 5     5   31 use Pod::Elemental::Selectors -all;
  5         13  
  5         74  
37              
38             #pod =attr required
39             #pod
40             #pod A boolean value specifying whether this section is required to be present or not. Defaults
41             #pod to false.
42             #pod
43             #pod If it's enabled and the section can't be found an exception will be raised.
44             #pod
45             #pod =cut
46              
47             has required => (
48             is => 'ro',
49             isa => 'Bool',
50             default => 0,
51             );
52              
53             #pod =attr header
54             #pod
55             #pod The name of this section. Defaults to the plugin name.
56             #pod
57             #pod =cut
58              
59             has header => (
60             is => 'ro',
61             isa => 'Str',
62             lazy => 1,
63             default => sub { $_[0]->plugin_name },
64             );
65              
66             has selector => (
67             is => 'ro',
68             isa => 'CodeRef',
69             lazy => 1,
70             default => sub {
71             my ($self) = @_;
72             return sub {
73             return unless s_command(head1 => $_[0]);
74             return unless $_[0]->content eq $self->header;
75             };
76             },
77             );
78              
79             sub weave_section {
80 23     23 0 59 my ($self, $document, $input) = @_;
81              
82 23         593 my $in_node = $input->{pod_document}->children;
83              
84             my @found = grep {
85 23         180 $self->selector->($in_node->[$_]);
  86         4147  
86             } (0 .. $#$in_node);
87              
88             confess "Couldn't find required Generic section for " . $self->header . " in file "
89 23 50 66     668 . (defined $input->{filename} ? $input->{filename} : '') if $self->required and not @found;
    100          
90              
91 22         598 $self->log_debug('adding ' . $self->header . ' back into pod');
92              
93 22         1130 push $document->children->@*, map { splice @$in_node, $_, 1 } reverse @found;
  14         177  
94             }
95              
96             __PACKAGE__->meta->make_immutable;
97             1;
98              
99             __END__
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =head1 NAME
106              
107             Pod::Weaver::Section::Generic - a generic section, found by lifting sections
108              
109             =head1 VERSION
110              
111             version 4.018
112              
113             =head1 OVERVIEW
114              
115             This section will find and include a located hunk of Pod. In general, it will
116             find a C<=head1> command with a content of the plugin's name.
117              
118             In other words, if your configuration include:
119              
120             [Generic]
121             header = OVERVIEW
122              
123             ...then this weaver will look for "=head1 OVERVIEW" and include it at the
124             appropriate location in your output.
125              
126             Since you'll probably want to use Generic several times, and that will require
127             giving each use a unique name, you can omit C<header> if you provide a
128             plugin name, and it will default to the plugin name. In other words, the
129             configuration above could be specified just as:
130              
131             [Generic / OVERVIEW]
132              
133             If the C<required> attribute is given, and true, then an exception will be
134             raised if this section can't be found.
135              
136             =head1 PERL VERSION SUPPORT
137              
138             This module has the same support period as perl itself: it supports the two
139             most recent versions of perl. (That is, if the most recently released version
140             is v5.40, then this module should work on both v5.40 and v5.38.)
141              
142             Although it may work on older versions of perl, no guarantee is made that the
143             minimum required version will not be increased. The version may be increased
144             for any reason, and there is no promise that patches will be accepted to lower
145             the minimum required perl.
146              
147             =head1 ATTRIBUTES
148              
149             =head2 required
150              
151             A boolean value specifying whether this section is required to be present or not. Defaults
152             to false.
153              
154             If it's enabled and the section can't be found an exception will be raised.
155              
156             =head2 header
157              
158             The name of this section. Defaults to the plugin name.
159              
160             =head1 AUTHOR
161              
162             Ricardo SIGNES <rjbs@semiotic.systems>
163              
164             =head1 COPYRIGHT AND LICENSE
165              
166             This software is copyright (c) 2021 by Ricardo SIGNES.
167              
168             This is free software; you can redistribute it and/or modify it under
169             the same terms as the Perl 5 programming language system itself.
170              
171             =cut