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.019;
2             # ABSTRACT: a generic section, found by lifting sections
3              
4 5     5   21413 use Moose;
  5         16  
  5         43  
5             with 'Pod::Weaver::Role::Section';
6              
7 5     5   36138 use v5.20.0;
  5         20  
8 5     5   37 use experimental 'postderef'; # this experiment succeeded -- rjbs, 2021-04-02
  5         11  
  5         40  
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   1001 use Pod::Elemental::Element::Pod5::Region;
  5         23  
  5         220  
36 5     5   34 use Pod::Elemental::Selectors -all;
  5         13  
  5         50  
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 60 my ($self, $document, $input) = @_;
81              
82 23         586 my $in_node = $input->{pod_document}->children;
83              
84             my @found = grep {
85 23         199 $self->selector->($in_node->[$_]);
  86         4303  
86             } (0 .. $#$in_node);
87              
88             confess "Couldn't find required Generic section for " . $self->header . " in file "
89 23 50 66     693 . (defined $input->{filename} ? $input->{filename} : '') if $self->required and not @found;
    100          
90              
91 22         623 $self->log_debug('adding ' . $self->header . ' back into pod');
92              
93 22         1109 push $document->children->@*, map { splice @$in_node, $_, 1 } reverse @found;
  14         175  
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.019
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
137              
138             This module should work on any version of perl still receiving updates from
139             the Perl 5 Porters. This means it should work on any version of perl released
140             in the last two to three years. (That is, if the most recently released
141             version is v5.40, then this module should work on both v5.40 and v5.38.)
142              
143             Although it may work on older versions of perl, no guarantee is made that the
144             minimum required version will not be increased. The version may be increased
145             for any reason, and there is no promise that patches will be accepted to lower
146             the minimum required perl.
147              
148             =head1 ATTRIBUTES
149              
150             =head2 required
151              
152             A boolean value specifying whether this section is required to be present or not. Defaults
153             to false.
154              
155             If it's enabled and the section can't be found an exception will be raised.
156              
157             =head2 header
158              
159             The name of this section. Defaults to the plugin name.
160              
161             =head1 AUTHOR
162              
163             Ricardo SIGNES <cpan@semiotic.systems>
164              
165             =head1 COPYRIGHT AND LICENSE
166              
167             This software is copyright (c) 2023 by Ricardo SIGNES.
168              
169             This is free software; you can redistribute it and/or modify it under
170             the same terms as the Perl 5 programming language system itself.
171              
172             =cut