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;
2             # ABSTRACT: a generic section, found by lifting sections
3             $Pod::Weaver::Section::Generic::VERSION = '4.017';
4 5     5   20929 use Moose;
  5         63  
  5         266  
5             with 'Pod::Weaver::Role::Section';
6              
7 5     5   35493 use v5.20.0;
  5         20  
8 5     5   3460 use experimental 'postderef'; # this experiment succeeded -- rjbs, 2021-04-02
  5         19593  
  5         41  
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   1024 use Pod::Elemental::Element::Pod5::Region;
  5         18  
  5         234  
36 5     5   33 use Pod::Elemental::Selectors -all;
  5         163  
  5         70  
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 58 my ($self, $document, $input) = @_;
81              
82 23         602 my $in_node = $input->{pod_document}->children;
83              
84             my @found = grep {
85 23         193 $self->selector->($in_node->[$_]);
  86         4268  
86             } (0 .. $#$in_node);
87              
88             confess "Couldn't find required Generic section for " . $self->header . " in file "
89 23 50 66     729 . (defined $input->{filename} ? $input->{filename} : '') if $self->required and not @found;
    100          
90              
91 22         614 $self->log_debug('adding ' . $self->header . ' back into pod');
92              
93 22         1114 push $document->children->@*, map { splice @$in_node, $_, 1 } reverse @found;
  14         184  
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.017
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 ATTRIBUTES
137              
138             =head2 required
139              
140             A boolean value specifying whether this section is required to be present or not. Defaults
141             to false.
142              
143             If it's enabled and the section can't be found an exception will be raised.
144              
145             =head2 header
146              
147             The name of this section. Defaults to the plugin name.
148              
149             =head1 AUTHOR
150              
151             Ricardo SIGNES <rjbs@cpan.org>
152              
153             =head1 COPYRIGHT AND LICENSE
154              
155             This software is copyright (c) 2021 by Ricardo SIGNES.
156              
157             This is free software; you can redistribute it and/or modify it under
158             the same terms as the Perl 5 programming language system itself.
159              
160             =cut