File Coverage

blib/lib/Dist/Zilla/Plugin/PodPurler.pm
Criterion Covered Total %
statement 32 39 82.0
branch 0 2 0.0
condition 0 6 0.0
subroutine 11 13 84.6
pod 0 2 0.0
total 43 62 69.3


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::PodPurler 0.094;
2             # ABSTRACT: like PodWeaver, but more erratic and amateurish
3              
4 1     1   521 use v5.20.0;
  1         3  
5 1     1   1306 use Moose;
  1         418751  
  1         7  
6 1     1   6885 use Moose::Autobox 0.08;
  1         205794  
  1         8  
7 1     1   687 use List::MoreUtils qw(any);
  1         3  
  1         29  
8             with 'Dist::Zilla::Role::FileMunger';
9              
10 1     1   1035 use namespace::autoclean;
  1         3  
  1         7  
11              
12 1     1   709 use Pod::Elemental 0.092930;
  1         670333  
  1         12  
13 1     1   471 use Pod::Elemental::Selectors -all;
  1         2  
  1         13  
14 1     1   615 use Pod::Elemental::Transformer::Pod5;
  1         3  
  1         31  
15 1     1   851 use Pod::Elemental::Transformer::Nester;
  1         122390  
  1         54  
16 1     1   793 use Pod::Elemental::Transformer::Gatherer;
  1         37580  
  1         1280  
17              
18             #pod =head1 WARNING
19             #pod
20             #pod This library has been superceded by L<Pod::Weaver> and
21             #pod L<Dist::Zilla::Plugin::PodWeaver>. It is unlikely to be updated again unless
22             #pod there are serious security problems (!?) or someone gives me some money.
23             #pod
24             #pod =head1 DESCRIPTION
25             #pod
26             #pod PodPurler ress, which rips apart your kinda-POD and reconstructs it as boring
27             #pod old real POD.
28             #pod
29             #pod =cut
30              
31             sub munge_file {
32 0     0 0   my ($self, $file) = @_;
33              
34 0 0 0       return $self->munge_pod($file)
      0        
35             if $file->name =~ /\.(?:pm|pod)$/i
36             and ($file->name !~ m{/} or $file->name =~ m{^lib/});
37              
38 0           return;
39             }
40              
41             sub munge_perl_string {
42             my ($self, $doc, $arg) = @_;
43              
44             my $document = $doc->{pod};
45             Pod::Elemental::Transformer::Pod5->new->transform_node($document);
46              
47             my $nester = Pod::Elemental::Transformer::Nester->new({
48             top_selector => s_command([ qw(head1 method attr) ]),
49             content_selectors => [
50             s_flat,
51             s_command( [ qw(head2 head3 head4 over item back) ]),
52             ],
53             });
54              
55             $nester->transform_node($document);
56              
57             for my $pair (
58             [ method => 'METHODS' ],
59             [ attr => 'ATTRIBUTES' ],
60             ) {
61             my $sel = s_command($pair->[0]);
62             if ($document->children->grep($sel)->length) {
63             my $gatherer = Pod::Elemental::Transformer::Gatherer->new({
64             gather_selector => $sel,
65             container => Pod::Elemental::Element::Nested->new({
66             command => 'head1',
67             content => "$pair->[1]\n",
68             }),
69             });
70              
71             $gatherer->transform_node($document);
72              
73             $gatherer->container->children->grep($sel)->each_value(sub {
74             $_->command('head2');
75             });
76             }
77             }
78              
79             unless (
80             $document->children->grep(sub {
81             s_command('head1', $_) and $_->content eq "VERSION\n"
82             })->length
83             ) {
84             my $version_section = Pod::Elemental::Element::Nested->new({
85             command => 'head1',
86             content => "VERSION\n",
87             children => [
88             Pod::Elemental::Element::Pod5::Ordinary->new({
89             content => sprintf "version %s\n", $self->zilla->version,
90             }),
91             ],
92             });
93              
94             $document->children->unshift($version_section);
95             }
96              
97             unless (
98             $document->children->grep(sub {
99             s_command('head1', $_) and $_->content eq "NAME\n"
100             })->length
101             ) {
102             Carp::croak "couldn't find package declaration in " . $arg->{filename}
103             unless my $pkg_node = $doc->{ppi}->find_first('PPI::Statement::Package');
104              
105             my $package = $pkg_node->namespace;
106              
107             $self->log("couldn't find abstract in " . $arg->{filename})
108             unless my ($abstract) = $doc->{ppi} =~ /^\s*#+\s*ABSTRACT:\s*(.+)$/m;
109              
110             my $name = $package;
111             $name .= " - $abstract" if $abstract;
112              
113             my $name_section = Pod::Elemental::Element::Nested->new({
114             command => 'head1',
115             content => "NAME\n",
116             children => [
117             Pod::Elemental::Element::Pod5::Ordinary->new({
118             content => "$name\n",
119             }),
120             ],
121             });
122              
123             $document->children->unshift($name_section);
124             }
125              
126             unless (
127             $document->children->grep(sub {
128             s_command('head1', $_) and $_->content =~ /\AAUTHORS?\n\z/
129             })->length
130             ) {
131             my @authors = $self->zilla->authors->flatten;
132             my $name = @authors > 1 ? 'AUTHORS' : 'AUTHOR';
133              
134             my $author_section = Pod::Elemental::Element::Nested->new({
135             command => 'head1',
136             content => "$name\n",
137             children => [
138             Pod::Elemental::Element::Pod5::Ordinary->new({
139             content => join("\n", @authors) . "\n"
140             }),
141             ],
142             });
143              
144             $document->children->push($author_section);
145             }
146              
147             unless (
148             $document->children->grep(sub {
149             s_command('head1', $_) and $_->content =~ /\A(?:COPYRIGHT|LICENSE)\n\z/
150             })->length
151             ) {
152             my $legal_section = Pod::Elemental::Element::Nested->new({
153             command => 'head1',
154             content => "COPYRIGHT AND LICENSE\n",
155             children => [
156             Pod::Elemental::Element::Pod5::Ordinary->new({
157             content => $self->zilla->license->notice
158             }),
159             ],
160             });
161              
162             $document->children->push($legal_section);
163             }
164              
165             return {
166             pod => $document,
167             ppi => $doc->{ppi},
168             };
169             }
170              
171             sub munge_pod {
172 0     0 0   my ($self, $file) = @_;
173              
174 0           my $content = $file->content;
175 0           my $new_content = $self->munge_perl_string(
176             $content,
177             {
178             filename => $file->name,
179             },
180             );
181              
182 0           $file->content($new_content);
183             }
184              
185             with 'Pod::Elemental::PerlMunger';
186              
187             __PACKAGE__->meta->make_immutable;
188 1     1   12 no Moose;
  1         2  
  1         33  
189             1;
190              
191             __END__
192              
193             =pod
194              
195             =encoding UTF-8
196              
197             =head1 NAME
198              
199             Dist::Zilla::Plugin::PodPurler - like PodWeaver, but more erratic and amateurish
200              
201             =head1 VERSION
202              
203             version 0.094
204              
205             =head1 DESCRIPTION
206              
207             PodPurler ress, which rips apart your kinda-POD and reconstructs it as boring
208             old real POD.
209              
210             =head1 PERL VERSION SUPPORT
211              
212             This code is effectively abandonware. Although releases will sometimes be made
213             to update contact info or to fix packaging flaws, bug reports will mostly be
214             ignored. Feature requests are even more likely to be ignored. (If someone
215             takes up maintenance of this code, they will presumably remove this notice.)
216              
217             =head1 WARNING
218              
219             This library has been superceded by L<Pod::Weaver> and
220             L<Dist::Zilla::Plugin::PodWeaver>. It is unlikely to be updated again unless
221             there are serious security problems (!?) or someone gives me some money.
222              
223             =head1 AUTHOR
224              
225             Ricardo SIGNES <rjbs@semiotic.systems>
226              
227             =head1 COPYRIGHT AND LICENSE
228              
229             This software is copyright (c) 2021 by Ricardo SIGNES.
230              
231             This is free software; you can redistribute it and/or modify it under
232             the same terms as the Perl 5 programming language system itself.
233              
234             =cut