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.018;
2             # ABSTRACT: add a VERSION pod section
3              
4 7     7   25729 use Moose;
  7         16  
  7         60  
5             with 'Pod::Weaver::Role::Section',
6             'Pod::Weaver::Role::StringFromComment';
7              
8             # BEGIN BOILERPLATE
9 7     7   48422 use v5.20.0;
  7         26  
10 7     7   42 use warnings;
  7         14  
  7         276  
11 7     7   47 use utf8;
  7         16  
  7         62  
12 7     7   252 no feature 'switch';
  7         17  
  7         869  
13 7     7   51 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  7         17  
  7         94  
14             # END BOILERPLATE
15              
16 7     7   884 use Module::Runtime qw(use_module);
  7         15  
  7         66  
17 7     7   442 use namespace::autoclean;
  7         23  
  7         86  
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   7631 use DateTime;
  7         2178855  
  7         365  
44 7     7   69 use Moose::Util::TypeConstraints;
  7         19  
  7         105  
45              
46             my $MARKER;
47 7     7   19054 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         1995 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         17 use_module( 'DateTime', '0.44' ); # CLDR fixes
62             DateTime->from_epoch(epoch => $^T, time_zone => $_[0]->{self}->time_zone)
63 1         105 ->format_cldr($_[1]),
64             },
65 0         0 r => sub { $_[0]->{zilla}->name },
66             m => sub {
67 2 50       32 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         312 T => sub { $MARKER },
75 0         0 n => sub { "\n" },
76 0         0 s => sub { q{ } },
77 6         365 t => sub { "\t" },
78             },
79 7     7   4737 };
  7         21352  
  7         169  
80              
81             # Needed by Config::MVP.
82 17     17 0 62498 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 62 my ($self, $input) = @_;
190 17 50       81 return unless $input->{version};
191              
192             my %args = (
193             self => $self,
194             version => $input->{version},
195             filename => $input->{filename},
196 17         127 );
197 17 50       71 $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       161 : undef;
    100          
202              
203 17 50       74 if ( exists $input->{ppi_document} ) {
204 17         89 my $pkg_node = $input->{ppi_document}->find_first('PPI::Statement::Package');
205             $args{module}
206             = $pkg_node
207             ? $pkg_node->namespace
208 17 100       4449 : $self->_extract_comment_content($input->{ppi_document}, 'PODNAME')
209             ;
210             }
211              
212 17         441 my $content = q{};
213 17         584 LINE: for my $format ($self->format->@*) {
214 30         138 my $line = _format_version($format, \%args);
215 30 100 100     2836 next if $line =~ s/^$MARKER\s*// and ! $args{is_trial};
216              
217 28 50       155 Carp::croak("%T format used inside line") if $line =~ /$MARKER/;
218              
219 28         104 $content .= "$line\n";
220             }
221              
222 17 100       645 if ( $self->is_verbatim ) {
223 3         103 $content = Pod::Elemental::Element::Pod5::Verbatim->new({
224             content => " $content",
225             });
226             } else {
227 14         459 $content = Pod::Elemental::Element::Pod5::Ordinary->new({
228             content => $content,
229             });
230             }
231              
232 17         3943 return ($content);
233             }
234              
235             sub weave_section {
236 17     17 0 61 my ($self, $document, $input) = @_;
237 17 50       76 return unless $input->{version};
238              
239 17         112 my @content = $self->build_content($input);
240              
241 17         536 $self->log_debug('adding ' . $self->header . ' section to pod');
242              
243 17         890 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.018
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 SUPPORT
280              
281             This module has the same support period as perl itself: it supports the two
282             most recent versions of perl. (That is, if the most recently released version
283             is v5.40, then this module should work on both v5.40 and v5.38.)
284              
285             Although it may work on older versions of perl, no guarantee is made that the
286             minimum required version will not be increased. The version may be increased
287             for any reason, and there is no promise that patches will be accepted to lower
288             the minimum required perl.
289              
290             =head1 ATTRIBUTES
291              
292             =head2 header
293              
294             The title of the header to be added.
295             (default: "VERSION")
296              
297             =head2 format
298              
299             The string to use when generating the version string.
300              
301             Default: version %v
302              
303             The following variables are available:
304              
305             =over 4
306              
307             =item *
308              
309             v - the version
310              
311             =item *
312              
313             V - the version, suffixed by "-TRIAL" if a trial release
314              
315             =item *
316              
317             d - the CLDR format for L<DateTime>
318              
319             =item *
320              
321             n - a newline
322              
323             =item *
324              
325             t - a tab
326              
327             =item *
328              
329             s - a space
330              
331             =item *
332              
333             r - the name of the dist, present only if you use L<Dist::Zilla> to generate the POD!
334              
335             =item *
336              
337             m - the name of the module, present only if L<PPI> parsed the document and it contained a package declaration!
338              
339             =item *
340              
341             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
342              
343             =back
344              
345             If multiple strings are supplied as an array ref, a line of POD is
346             produced for each string. Each line will be separated by a newline.
347             This is useful for splitting longer text across multiple lines in a
348             C<weaver.ini> file, for example:
349              
350             ; weaver.ini
351             [Version]
352             format = version %v
353             format =
354             format = This module's version numbers follow the conventions described at
355             format = L<semver.org|http://semver.org/>.
356             format = %T
357             format = %T This is a trial release!
358              
359             =head2 is_verbatim
360              
361             A boolean value specifying whether the version paragraph should be verbatim or not.
362              
363             Default: false
364              
365             =head2 time_zone
366              
367             The timezone to use when using L<DateTime> for the format.
368              
369             Default: local
370              
371             =head1 METHODS
372              
373             =head2 build_content
374              
375             my @pod_elements = $section->build_content(\%input);
376              
377             This method is passed the same C<\%input> that goes to the C<weave_section>
378             method, and should return a list of pod elements to insert.
379              
380             In almost all cases, this method is used internally, but could be usefully
381             overridden in a subclass.
382              
383             =head1 AUTHOR
384              
385             Ricardo SIGNES <rjbs@semiotic.systems>
386              
387             =head1 COPYRIGHT AND LICENSE
388              
389             This software is copyright (c) 2021 by Ricardo SIGNES.
390              
391             This is free software; you can redistribute it and/or modify it under
392             the same terms as the Perl 5 programming language system itself.
393              
394             =cut