File Coverage

blib/lib/Pod/Weaver/Section/Version.pm
Criterion Covered Total %
statement 63 68 92.6
branch 15 26 57.6
condition 3 3 100.0
subroutine 15 15 100.0
pod 1 3 33.3
total 97 115 84.3


line stmt bran cond sub pod time code
1             package Pod::Weaver::Section::Version 4.019;
2             # ABSTRACT: add a VERSION pod section
3              
4 7     7   27601 use Moose;
  7         17  
  7         63  
5             with 'Pod::Weaver::Role::Section',
6             'Pod::Weaver::Role::StringFromComment';
7              
8             # BEGIN BOILERPLATE
9 7     7   50620 use v5.20.0;
  7         29  
10 7     7   55 use warnings;
  7         27  
  7         270  
11 7     7   50 use utf8;
  7         16  
  7         73  
12 7     7   277 no feature 'switch';
  7         16  
  7         879  
13 7     7   55 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  7         16  
  7         64  
14             # END BOILERPLATE
15              
16 7     7   863 use Module::Runtime qw(use_module);
  7         22  
  7         74  
17 7     7   438 use namespace::autoclean;
  7         20  
  7         99  
18              
19             #pod =head1 OVERVIEW
20             #pod
21             #pod This section plugin will produce a hunk of Pod meant to indicate the version of
22             #pod the document being viewed, like this:
23             #pod
24             #pod =head1 VERSION
25             #pod
26             #pod version 1.234
27             #pod
28             #pod It will do nothing if there is no C<version> entry in the input.
29             #pod
30             #pod =attr header
31             #pod
32             #pod The title of the header to be added.
33             #pod (default: "VERSION")
34             #pod
35             #pod =cut
36              
37             has header => (
38             is => 'ro',
39             isa => 'Str',
40             default => 'VERSION',
41             );
42              
43 7     7   8875 use DateTime;
  7         2568752  
  7         391  
44 7     7   71 use Moose::Util::TypeConstraints;
  7         19  
  7         114  
45              
46             my $MARKER;
47 7     7   20443 BEGIN { $MARKER = "\x{2316}" }
48              
49             use String::Formatter 0.100680 stringf => {
50             -as => '_format_version',
51              
52             input_processor => 'require_single_input',
53             string_replacer => 'method_replace',
54             codes => {
55 17         2100 v => sub { $_[0]->{version} },
56             V => sub { $_[0]->{version}
57             . ($_[0]->{is_trial}
58 0 0       0 ? (defined $_[1] ? $_[1] : '-TRIAL') : '') },
    0          
59              
60             d => sub {
61 1         16 use_module( 'DateTime', '0.44' ); # CLDR fixes
62             DateTime->from_epoch(epoch => $^T, time_zone => $_[0]->{self}->time_zone)
63 1         100 ->format_cldr($_[1]),
64             },
65 0         0 r => sub { $_[0]->{zilla}->name },
66             m => sub {
67 2 50       36 return $_[0]->{module} if defined $_[0]->{module};
68             $_[0]->{self}->log_fatal([
69             "%%m format used for Version section, but no package declaration found in %s",
70             $_[0]->{filename},
71 0         0 ]);
72             },
73              
74 4         269 T => sub { $MARKER },
75 0         0 n => sub { "\n" },
76 0         0 s => sub { q{ } },
77 6         360 t => sub { "\t" },
78             },
79 7     7   7462 };
  7         23302  
  7         196  
80              
81             # Needed by Config::MVP.
82 17     17 0 63195 sub mvp_multivalue_args { 'format' }
83              
84             #pod =attr format
85             #pod
86             #pod The string to use when generating the version string.
87             #pod
88             #pod Default: version %v
89             #pod
90             #pod The following variables are available:
91             #pod
92             #pod =begin :list
93             #pod
94             #pod * v - the version
95             #pod
96             #pod * V - the version, suffixed by "-TRIAL" if a trial release
97             #pod
98             #pod * d - the CLDR format for L<DateTime>
99             #pod
100             #pod * n - a newline
101             #pod
102             #pod * t - a tab
103             #pod
104             #pod * s - a space
105             #pod
106             #pod * r - the name of the dist, present only if you use L<Dist::Zilla> to generate
107             #pod the POD!
108             #pod
109             #pod * m - the name of the module, present only if L<PPI> parsed the document and it
110             #pod contained a package declaration!
111             #pod
112             #pod * T - special: at the beginning of the line, followed by any amount of
113             #pod whitespace, indicates that the line should only be included in trial
114             #pod releases; otherwise, results in a fatal error
115             #pod
116             #pod =end :list
117             #pod
118             #pod If multiple strings are supplied as an array ref, a line of POD is
119             #pod produced for each string. Each line will be separated by a newline.
120             #pod This is useful for splitting longer text across multiple lines in a
121             #pod C<weaver.ini> file, for example:
122             #pod
123             #pod ; weaver.ini
124             #pod [Version]
125             #pod format = version %v
126             #pod format =
127             #pod format = This module's version numbers follow the conventions described at
128             #pod format = L<semver.org|http://semver.org/>.
129             #pod format = %T
130             #pod format = %T This is a trial release!
131             #pod
132             #pod =cut
133              
134             subtype 'Pod::Weaver::Section::Version::_Format',
135             as 'ArrayRef[Str]';
136              
137             coerce 'Pod::Weaver::Section::Version::_Format',
138             from 'Str',
139             via { [ $_ ] };
140              
141             has format => (
142             is => 'ro',
143             isa => 'Pod::Weaver::Section::Version::_Format',
144             coerce => 1,
145             default => 'version %v',
146             );
147              
148             #pod =attr is_verbatim
149             #pod
150             #pod A boolean value specifying whether the version paragraph should be verbatim or not.
151             #pod
152             #pod Default: false
153             #pod
154             #pod =cut
155              
156             has is_verbatim => (
157             is => 'ro',
158             isa => 'Bool',
159             default => 0,
160             );
161              
162             #pod =attr time_zone
163             #pod
164             #pod The timezone to use when using L<DateTime> for the format.
165             #pod
166             #pod Default: local
167             #pod
168             #pod =cut
169              
170             has time_zone => (
171             is => 'ro',
172             isa => 'Str', # should be more validated later -- apocal
173             default => 'local',
174             );
175              
176             #pod =method build_content
177             #pod
178             #pod my @pod_elements = $section->build_content(\%input);
179             #pod
180             #pod This method is passed the same C<\%input> that goes to the C<weave_section>
181             #pod method, and should return a list of pod elements to insert.
182             #pod
183             #pod In almost all cases, this method is used internally, but could be usefully
184             #pod overridden in a subclass.
185             #pod
186             #pod =cut
187              
188             sub build_content {
189 17     17 1 55 my ($self, $input) = @_;
190 17 50       72 return unless $input->{version};
191              
192             my %args = (
193             self => $self,
194             version => $input->{version},
195             filename => $input->{filename},
196 17         131 );
197 17 50       96 $args{zilla} = $input->{zilla} if exists $input->{zilla};
198              
199             $args{is_trial} = exists $input->{is_trial} ? $input->{is_trial}
200             : $args{zilla} ? $args{zilla}->is_trial
201 17 50       136 : undef;
    100          
202              
203 17 50       72 if ( exists $input->{ppi_document} ) {
204 17         113 my $pkg_node = $input->{ppi_document}->find_first('PPI::Statement::Package');
205             $args{module}
206             = $pkg_node
207             ? $pkg_node->namespace
208 17 100       4535 : $self->_extract_comment_content($input->{ppi_document}, 'PODNAME')
209             ;
210             }
211              
212 17         455 my $content = q{};
213 17         589 LINE: for my $format ($self->format->@*) {
214 30         147 my $line = _format_version($format, \%args);
215 30 100 100     2861 next if $line =~ s/^$MARKER\s*// and ! $args{is_trial};
216              
217 28 50       158 Carp::croak("%T format used inside line") if $line =~ /$MARKER/;
218              
219 28         110 $content .= "$line\n";
220             }
221              
222 17 100       635 if ( $self->is_verbatim ) {
223 3         103 $content = Pod::Elemental::Element::Pod5::Verbatim->new({
224             content => " $content",
225             });
226             } else {
227 14         498 $content = Pod::Elemental::Element::Pod5::Ordinary->new({
228             content => $content,
229             });
230             }
231              
232 17         4105 return ($content);
233             }
234              
235             sub weave_section {
236 17     17 0 62 my ($self, $document, $input) = @_;
237 17 50       78 return unless $input->{version};
238              
239 17         105 my @content = $self->build_content($input);
240              
241 17         612 $self->log_debug('adding ' . $self->header . ' section to pod');
242              
243 17         950 push $document->children->@*,
244             Pod::Elemental::Element::Nested->new({
245             command => 'head1',
246             content => $self->header,
247             children => \@content,
248             });
249             }
250              
251             __PACKAGE__->meta->make_immutable;
252             1;
253              
254             __END__
255              
256             =pod
257              
258             =encoding UTF-8
259              
260             =head1 NAME
261              
262             Pod::Weaver::Section::Version - add a VERSION pod section
263              
264             =head1 VERSION
265              
266             version 4.019
267              
268             =head1 OVERVIEW
269              
270             This section plugin will produce a hunk of Pod meant to indicate the version of
271             the document being viewed, like this:
272              
273             =head1 VERSION
274              
275             version 1.234
276              
277             It will do nothing if there is no C<version> entry in the input.
278              
279             =head1 PERL VERSION
280              
281             This module should work on any version of perl still receiving updates from
282             the Perl 5 Porters. This means it should work on any version of perl released
283             in the last two to three years. (That is, if the most recently released
284             version is v5.40, then this module should work on both v5.40 and v5.38.)
285              
286             Although it may work on older versions of perl, no guarantee is made that the
287             minimum required version will not be increased. The version may be increased
288             for any reason, and there is no promise that patches will be accepted to lower
289             the minimum required perl.
290              
291             =head1 ATTRIBUTES
292              
293             =head2 header
294              
295             The title of the header to be added.
296             (default: "VERSION")
297              
298             =head2 format
299              
300             The string to use when generating the version string.
301              
302             Default: version %v
303              
304             The following variables are available:
305              
306             =over 4
307              
308             =item *
309              
310             v - the version
311              
312             =item *
313              
314             V - the version, suffixed by "-TRIAL" if a trial release
315              
316             =item *
317              
318             d - the CLDR format for L<DateTime>
319              
320             =item *
321              
322             n - a newline
323              
324             =item *
325              
326             t - a tab
327              
328             =item *
329              
330             s - a space
331              
332             =item *
333              
334             r - the name of the dist, present only if you use L<Dist::Zilla> to generate the POD!
335              
336             =item *
337              
338             m - the name of the module, present only if L<PPI> parsed the document and it contained a package declaration!
339              
340             =item *
341              
342             T - special: at the beginning of the line, followed by any amount of whitespace, indicates that the line should only be included in trial releases; otherwise, results in a fatal error
343              
344             =back
345              
346             If multiple strings are supplied as an array ref, a line of POD is
347             produced for each string. Each line will be separated by a newline.
348             This is useful for splitting longer text across multiple lines in a
349             C<weaver.ini> file, for example:
350              
351             ; weaver.ini
352             [Version]
353             format = version %v
354             format =
355             format = This module's version numbers follow the conventions described at
356             format = L<semver.org|http://semver.org/>.
357             format = %T
358             format = %T This is a trial release!
359              
360             =head2 is_verbatim
361              
362             A boolean value specifying whether the version paragraph should be verbatim or not.
363              
364             Default: false
365              
366             =head2 time_zone
367              
368             The timezone to use when using L<DateTime> for the format.
369              
370             Default: local
371              
372             =head1 METHODS
373              
374             =head2 build_content
375              
376             my @pod_elements = $section->build_content(\%input);
377              
378             This method is passed the same C<\%input> that goes to the C<weave_section>
379             method, and should return a list of pod elements to insert.
380              
381             In almost all cases, this method is used internally, but could be usefully
382             overridden in a subclass.
383              
384             =head1 AUTHOR
385              
386             Ricardo SIGNES <cpan@semiotic.systems>
387              
388             =head1 COPYRIGHT AND LICENSE
389              
390             This software is copyright (c) 2023 by Ricardo SIGNES.
391              
392             This is free software; you can redistribute it and/or modify it under
393             the same terms as the Perl 5 programming language system itself.
394              
395             =cut