File Coverage

blib/lib/Dist/Zilla/Plugin/PodWeaver.pm
Criterion Covered Total %
statement 26 56 46.4
branch 0 6 0.0
condition 0 3 0.0
subroutine 9 14 64.2
pod 1 5 20.0
total 36 84 42.8


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::PodWeaver 4.010;
2             # ABSTRACT: weave your Pod together from configuration and Dist::Zilla
3              
4 1     1   3404 use Moose;
  1         423962  
  1         7  
5 1     1   6662 use Pod::Weaver 3.100710; # logging with proxies
  1         1468099  
  1         55  
6             with 'Dist::Zilla::Role::FileMunger',
7             'Dist::Zilla::Role::FileFinderUser' => {
8             default_finders => [ ':InstallModules', ':PerlExecFiles' ],
9             };
10              
11             # BEGIN BOILERPLATE
12 1     1   13 use v5.20.0;
  1         4  
13 1     1   5 use warnings;
  1         3  
  1         30  
14 1     1   6 use utf8;
  1         3  
  1         5  
15 1     1   26 no feature 'switch';
  1         3  
  1         96  
16 1     1   8 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  1         29  
  1         11  
17             # END BOILERPLATE
18              
19 1     1   94 use namespace::autoclean;
  1         4  
  1         8  
20              
21             #pod =head1 DESCRIPTION
22             #pod
23             #pod [PodWeaver] is the bridge between L<Dist::Zilla> and L<Pod::Weaver>. It rips
24             #pod apart your kinda-Pod and reconstructs it as boring old real Pod.
25             #pod
26             #pod =head1 CONFIGURATION
27             #pod
28             #pod If the C<config_plugin> attribute is given, it will be treated like a
29             #pod Pod::Weaver section heading. For example, C<@Default> could be given. It may
30             #pod be given multiple times.
31             #pod
32             #pod Otherwise, if a file matching C<./weaver.*> exists, Pod::Weaver will be told to
33             #pod look for configuration in the current directory.
34             #pod
35             #pod Otherwise, it will use the default configuration.
36             #pod
37             #pod =attr finder
38             #pod
39             #pod [PodWeaver] is a L<Dist::Zilla::Role::FileFinderUser>. The L<FileFinder> given
40             #pod for its C<finder> attribute is used to decide which files to munge. By
41             #pod default, it will munge:
42             #pod
43             #pod =for :list
44             #pod * C<:InstallModules>
45             #pod * C<:ExecFiles>
46             #pod
47             #pod =method weaver
48             #pod
49             #pod This method returns the Pod::Weaver object to be used. The current
50             #pod implementation builds a new weaver on each invocation, because one or two core
51             #pod Pod::Weaver plugins cannot be trusted to handle multiple documents per plugin
52             #pod instance. In the future, when that is fixed, this may become an accessor of an
53             #pod attribute with a builder. Until this is clearer, use caution when modifying
54             #pod this method in subclasses.
55             #pod
56             #pod =cut
57              
58             sub weaver {
59 0     0 1   my ($self) = @_;
60              
61 0           my $root = $self->zilla->root->stringify;
62              
63 0           my @files = glob("$root/weaver.*");
64              
65 0           my $arg = {
66             root => $root,
67             root_config => { logger => $self->logger },
68             };
69              
70 0 0         if ($self->has_config_plugins) {
    0          
71 0           my $assembler = Pod::Weaver::Config::Assembler->new;
72              
73 0           my $root = $assembler->section_class->new({ name => '_' });
74 0           $assembler->sequence->add_section($root);
75              
76 0           for my $header ($self->config_plugins) {
77 0           $assembler->change_section($header);
78 0           $assembler->end_section;
79             }
80              
81 0           return Pod::Weaver->new_from_config_sequence($assembler->sequence, $arg);
82             } elsif (@files) {
83 0           return Pod::Weaver->new_from_config(
84             { root => $root },
85             { root_config => { logger => $self->logger } },
86             );
87             } else {
88 0           return Pod::Weaver->new_with_default_config($arg);
89             }
90             }
91              
92              
93 0     0 0   sub mvp_aliases { return { config_plugin => 'config_plugins' } }
94             sub mvp_multivalue_args { qw(config_plugins) }
95              
96             has config_plugins => (
97             isa => 'ArrayRef[Str]',
98             traits => [ 'Array' ],
99             default => sub { [] },
100             handles => {
101             config_plugins => 'elements',
102             has_config_plugins => 'count',
103             },
104             );
105              
106             around dump_config => sub
107             {
108             my ($orig, $self) = @_;
109             my $config = $self->$orig;
110              
111             my $our = {
112             $self->has_config_plugins
113             ? ( config_plugins => [ $self->config_plugins ] )
114             : (),
115             finder => $self->finder,
116             };
117              
118             $our->{plugins} = [];
119             for my $plugin ($self->weaver->plugins->@*) {
120             push $our->{plugins}->@*, {
121             class => $plugin->meta->name,
122             name => $plugin->plugin_name,
123             version => $plugin->VERSION,
124             };
125             }
126              
127             $config->{'' . __PACKAGE__} = $our;
128              
129             return $config;
130             };
131              
132             sub munge_files {
133 0     0 0   my ($self) = @_;
134              
135 0           require PPI;
136 0           require Pod::Weaver;
137 0           require Pod::Weaver::Config::Assembler;
138              
139 0           $self->munge_file($_) for $self->found_files->@*;
140             }
141              
142             sub munge_file {
143 0     0 0   my ($self, $file) = @_;
144              
145 0           $self->log_debug([ 'weaving pod in %s', $file->name ]);
146              
147 0           $self->munge_pod($file);
148 0           return;
149             }
150              
151             sub munge_perl_string {
152             my ($self, $doc, $arg) = @_;
153              
154             my $weaver = $self->weaver;
155             my $new_doc = $weaver->weave_document({
156             %$arg,
157             pod_document => $doc->{pod},
158             ppi_document => $doc->{ppi},
159             });
160              
161             return {
162             pod => $new_doc,
163             ppi => $doc->{ppi},
164             }
165             }
166              
167             sub munge_pod {
168 0     0 0   my ($self, $file) = @_;
169              
170 0           my @authors = $self->zilla->authors;
171 0 0 0       @authors = $authors[0]->@* if @authors == 1 && ref $authors[0];
172              
173 0           my $new_content = $self->munge_perl_string(
174             $file->content,
175             {
176             zilla => scalar $self->zilla,
177             filename => scalar $file->name,
178             version => scalar $self->zilla->version,
179             license => scalar $self->zilla->license,
180             authors => \@authors,
181             distmeta => scalar $self->zilla->distmeta,
182             },
183             );
184              
185 0           $file->content($new_content);
186              
187 0           return;
188             }
189              
190             with 'Pod::Elemental::PerlMunger' => { -version => 0.100000 };
191              
192             __PACKAGE__->meta->make_immutable;
193 1     1   1100 no Moose;
  1         2  
  1         8  
194             1;
195              
196             __END__
197              
198             =pod
199              
200             =encoding UTF-8
201              
202             =head1 NAME
203              
204             Dist::Zilla::Plugin::PodWeaver - weave your Pod together from configuration and Dist::Zilla
205              
206             =head1 VERSION
207              
208             version 4.010
209              
210             =head1 DESCRIPTION
211              
212             [PodWeaver] is the bridge between L<Dist::Zilla> and L<Pod::Weaver>. It rips
213             apart your kinda-Pod and reconstructs it as boring old real Pod.
214              
215             =head1 PERL VERSION
216              
217             This module should work on any version of perl still receiving updates from
218             the Perl 5 Porters. This means it should work on any version of perl released
219             in the last two to three years. (That is, if the most recently released
220             version is v5.40, then this module should work on both v5.40 and v5.38.)
221              
222             Although it may work on older versions of perl, no guarantee is made that the
223             minimum required version will not be increased. The version may be increased
224             for any reason, and there is no promise that patches will be accepted to lower
225             the minimum required perl.
226              
227             =head1 ATTRIBUTES
228              
229             =head2 finder
230              
231             [PodWeaver] is a L<Dist::Zilla::Role::FileFinderUser>. The L<FileFinder> given
232             for its C<finder> attribute is used to decide which files to munge. By
233             default, it will munge:
234              
235             =over 4
236              
237             =item *
238              
239             C<:InstallModules>
240              
241             =item *
242              
243             C<:ExecFiles>
244              
245             =back
246              
247             =head1 METHODS
248              
249             =head2 weaver
250              
251             This method returns the Pod::Weaver object to be used. The current
252             implementation builds a new weaver on each invocation, because one or two core
253             Pod::Weaver plugins cannot be trusted to handle multiple documents per plugin
254             instance. In the future, when that is fixed, this may become an accessor of an
255             attribute with a builder. Until this is clearer, use caution when modifying
256             this method in subclasses.
257              
258             =head1 CONFIGURATION
259              
260             If the C<config_plugin> attribute is given, it will be treated like a
261             Pod::Weaver section heading. For example, C<@Default> could be given. It may
262             be given multiple times.
263              
264             Otherwise, if a file matching C<./weaver.*> exists, Pod::Weaver will be told to
265             look for configuration in the current directory.
266              
267             Otherwise, it will use the default configuration.
268              
269             =head1 AUTHOR
270              
271             Ricardo SIGNES <cpan@semiotic.systems>
272              
273             =head1 CONTRIBUTORS
274              
275             =for stopwords David Golden Florian Ragwitz Karen Etheridge Ricardo Signes Yasutaka ATARASHI Сергей Романов
276              
277             =over 4
278              
279             =item *
280              
281             David Golden <dagolden@cpan.org>
282              
283             =item *
284              
285             Florian Ragwitz <rafl@debian.org>
286              
287             =item *
288              
289             Karen Etheridge <ether@cpan.org>
290              
291             =item *
292              
293             Ricardo Signes <rjbs@semiotic.systems>
294              
295             =item *
296              
297             Yasutaka ATARASHI <yakex@cpan.org>
298              
299             =item *
300              
301             Сергей Романов <sromanov-dev@yandex.ru>
302              
303             =back
304              
305             =head1 COPYRIGHT AND LICENSE
306              
307             This software is copyright (c) 2023 by Ricardo SIGNES.
308              
309             This is free software; you can redistribute it and/or modify it under
310             the same terms as the Perl 5 programming language system itself.
311              
312             =cut