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