File Coverage

blib/lib/Pod/Weaver/Section/Name.pm
Criterion Covered Total %
statement 37 39 94.8
branch 5 10 50.0
condition 3 5 60.0
subroutine 9 9 100.0
pod 0 1 0.0
total 54 64 84.3


line stmt bran cond sub pod time code
1             package Pod::Weaver::Section::Name;
2             # ABSTRACT: add a NAME section with abstract (for your Perl module)
3             $Pod::Weaver::Section::Name::VERSION = '4.017';
4 9     9   33430 use Moose;
  9         24  
  9         82  
5             with 'Pod::Weaver::Role::Section';
6             with 'Pod::Weaver::Role::StringFromComment';
7              
8             #pod =head1 OVERVIEW
9             #pod
10             #pod This section plugin will produce a hunk of Pod giving the name of the document
11             #pod as well as an abstract, like this:
12             #pod
13             #pod =head1 NAME
14             #pod
15             #pod Some::Document - a document for some
16             #pod
17             #pod It will determine the name and abstract by inspecting the C<ppi_document> which
18             #pod must be given. It looks for comments in the form:
19             #pod
20             #pod
21             #pod # ABSTRACT: a document for some
22             #pod # PODNAME: Some::Package::Name
23             #pod
24             #pod If no C<PODNAME> comment is present, but a package declaration can be found,
25             #pod the package name will be used as the document name.
26             #pod
27             #pod =attr header
28             #pod
29             #pod The title of the header to be added.
30             #pod (default: "NAME")
31             #pod
32             #pod =cut
33              
34             has header => (
35             is => 'ro',
36             isa => 'Str',
37             default => 'NAME',
38             );
39              
40 9     9   64461 use Pod::Elemental::Element::Pod5::Command;
  9         23  
  9         397  
41 9     9   63 use Pod::Elemental::Element::Pod5::Ordinary;
  9         22  
  9         320  
42 9     9   58 use Pod::Elemental::Element::Nested;
  9         39  
  9         5421  
43              
44             sub _get_docname_via_statement {
45 27     27   94 my ($self, $ppi_document) = @_;
46              
47 27         101 my $pkg_node = $ppi_document->find_first('PPI::Statement::Package');
48 27 50       8552 return unless $pkg_node;
49 27         205 return $pkg_node->namespace;
50             }
51              
52             sub _get_docname_via_comment {
53 29     29   77 my ($self, $ppi_document) = @_;
54              
55 29         248 return $self->_extract_comment_content($ppi_document, 'PODNAME');
56             }
57              
58             sub _get_docname {
59 29     29   103 my ($self, $input) = @_;
60              
61 29         112 my $ppi_document = $input->{ppi_document};
62              
63 29   66     167 my $docname = $self->_get_docname_via_comment($ppi_document)
64             || $self->_get_docname_via_statement($ppi_document);
65              
66 29         1134 return $docname;
67             }
68              
69             sub _get_abstract {
70 29     29   99 my ($self, $input) = @_;
71              
72 29         126 my $comment = $self->_extract_comment_content($input->{ppi_document}, 'ABSTRACT');
73              
74 29 50       140 return $comment if $comment;
75              
76             # If that failed, fall back to searching the whole document
77             my ($abstract)
78 0         0 = $input->{ppi_document}->serialize =~ /^\s*#+\s*ABSTRACT:\s*(.+)$/m;
79              
80 0         0 return $abstract;
81             }
82              
83             sub weave_section {
84 29     29 0 107 my ($self, $document, $input) = @_;
85              
86 29   50     273 my $filename = $input->{filename} || 'file';
87              
88 29         136 my $docname = $self->_get_docname($input);
89 29         116 my $abstract = $self->_get_abstract($input);
90              
91 29 50       105 Carp::croak sprintf "couldn't determine document name for %s\nAdd something like this to %s:\n# PODNAME: bobby_tables.pl", $filename, $filename
92             unless $docname;
93              
94 29 50       128 $self->log([ "couldn't find abstract in %s", $filename ]) unless $abstract;
95              
96 29         82 my $name = $docname;
97 29 50       150 $name .= " - $abstract" if $abstract;
98              
99 29         256 $self->log_debug(qq{setting NAME to "$name"});
100              
101 29         2081 my $name_para = Pod::Elemental::Element::Nested->new({
102             command => 'head1',
103             content => $self->header,
104             children => [
105             Pod::Elemental::Element::Pod5::Ordinary->new({ content => $name }),
106             ],
107             });
108              
109 29         15915 push @{ $document->children }, $name_para;
  29         793  
110             }
111              
112             __PACKAGE__->meta->make_immutable;
113             1;
114              
115             __END__
116              
117             =pod
118              
119             =encoding UTF-8
120              
121             =head1 NAME
122              
123             Pod::Weaver::Section::Name - add a NAME section with abstract (for your Perl module)
124              
125             =head1 VERSION
126              
127             version 4.017
128              
129             =head1 OVERVIEW
130              
131             This section plugin will produce a hunk of Pod giving the name of the document
132             as well as an abstract, like this:
133              
134             =head1 NAME
135              
136             Some::Document - a document for some
137              
138             It will determine the name and abstract by inspecting the C<ppi_document> which
139             must be given. It looks for comments in the form:
140              
141             # ABSTRACT: a document for some
142             # PODNAME: Some::Package::Name
143              
144             If no C<PODNAME> comment is present, but a package declaration can be found,
145             the package name will be used as the document name.
146              
147             =head1 ATTRIBUTES
148              
149             =head2 header
150              
151             The title of the header to be added.
152             (default: "NAME")
153              
154             =head1 AUTHOR
155              
156             Ricardo SIGNES <rjbs@cpan.org>
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is copyright (c) 2021 by Ricardo SIGNES.
161              
162             This is free software; you can redistribute it and/or modify it under
163             the same terms as the Perl 5 programming language system itself.
164              
165             =cut