Weaver/Section/Version.pm

filter: Statementstmt 93.7% 75 / 80 Branchbran 57.6% 15 / 26 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 84.0% 21 / 25 Podpod 33.3% 1 / 3 totaltotal 84.1% 117 / 139 CC 16 SCAR 16.9
CC
16
Cov
85%
build_content
12.3
__ANON__
12.0
__ANON__
2.5
CRAP 5.4
« Weaver/Plugin/SingleEncoding.pm ↑ Summary Weaver/Plugin/Transformer.pm »
1 package Pod::Weaver::Section::Version 4.022;
2 # ABSTRACT: add a VERSION pod section
3  
4 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 with 'Pod::Weaver::Role::Section',
6 'Pod::Weaver::Role::StringFromComment';
7  
8 # BEGIN BOILERPLATE
9 7 ▶use v5.22.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶no feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
Subroutine BEGIN: called CC=1 SCAR=0.0
14 # END BOILERPLATE
15  
16 7 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 7 has header => (
38 is => 'ro',
39 isa => 'Str',
40 default => 'VERSION',
41 );
42  
43 7 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 {
46 # This is irritating.
47 #
48 # Moose::Util::TypeConstraints will turn on strict and warnings. This will
49 # undermine the setup that "use experimental" did above. So I could move
50 # this above my boilerplate pragmata. But this is annoying too, where the
51 # order of libraries is based on largely invisible factors. I now believe
52 # that "Moose stuff turns on strict!" was a mistake, even if a
53 # well-intentioned one. So it goes.
54 #
55 # So, I'm putting it in a block. Why? Because the "use strict" that gets
56 # exported is *lexical* and so will be constrained by this block. The named
57 # subroutines, though, are *global* so they will still work.
58 #
59 # Can't push me around, perl! -- rjbs, 2024-03-21
60 7 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 }
62  
63 7 my $MARKER;
64 7 ▶BEGIN { $MARKER = "\x{2316}" }
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 7 ▶use String::Formatter 0.100680 stringf => {
Subroutine BEGIN: called CC=1 SCAR=1.2
67 -as => '_format_version',
68  
69 input_processor => 'require_single_input',
70 string_replacer => 'method_replace',
71 codes => {
72 17 ▶ v => sub { $_[0]->{version} },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 0 ▶ V => sub { $_[0]->{version}
Subroutine __ANON__: not called CC=3 SCAR=24.8
74 . ($_[0]->{is_trial}
75 0 ▶ ? (defined $_[1] ? $_[1] : '-TRIAL') : '') },
Branch: 2 branches
BranchTrueFalseTotal
$_[0]{'is_trial'} ? : 0 0 0
defined $_[1] ? : 0 0 0
76  
77 1 ▶ d => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 use_module( 'DateTime', '0.44' ); # CLDR fixes
79 DateTime->from_epoch(epoch => $^T, time_zone => $_[0]->{self}->time_zone)
80 1 ->format_cldr($_[1]),
81 },
82 0 ▶ r => sub { $_[0]->{zilla}->name },
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 2 ▶ m => sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
84 2 ▶ return $_[0]->{module} if defined $_[0]->{module};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"module"} 2 0 2
85 $_[0]->{self}->log_fatal([
86 "%%m format used for Version section, but no package declaration found in %s",
87 $_[0]->{filename},
88 0 ]);
89 },
90  
91 4 ▶ T => sub { $MARKER },
Subroutine __ANON__: called CC=1 SCAR=0.0
92 0 ▶ n => sub { "\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 ▶ s => sub { q{ } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 6 ▶ t => sub { "\t" },
Subroutine __ANON__: called CC=1 SCAR=0.0
95 },
96 7 };
97  
98 # Needed by Config::MVP.
99 17 ▶sub mvp_multivalue_args { 'format' }
Subroutine mvp_multivalue_args: called CC=1 SCAR=0.0
Pod undocumented
100  
101 #pod =attr format
102 #pod
103 #pod The string to use when generating the version string.
104 #pod
105 #pod Default: version %v
106 #pod
107 #pod The following variables are available:
108 #pod
109 #pod =begin :list
110 #pod
111 #pod * v - the version
112 #pod
113 #pod * V - the version, suffixed by "-TRIAL" if a trial release
114 #pod
115 #pod * d - the CLDR format for L<DateTime>
116 #pod
117 #pod * n - a newline
118 #pod
119 #pod * t - a tab
120 #pod
121 #pod * s - a space
122 #pod
123 #pod * r - the name of the dist, present only if you use L<Dist::Zilla> to generate
124 #pod the POD!
125 #pod
126 #pod * m - the name of the module, present only if L<PPI> parsed the document and it
127 #pod contained a package declaration!
128 #pod
129 #pod * T - special: at the beginning of the line, followed by any amount of
130 #pod whitespace, indicates that the line should only be included in trial
131 #pod releases; otherwise, results in a fatal error
132 #pod
133 #pod =end :list
134 #pod
135 #pod If multiple strings are supplied as an array ref, a line of POD is
136 #pod produced for each string. Each line will be separated by a newline.
137 #pod This is useful for splitting longer text across multiple lines in a
138 #pod C<weaver.ini> file, for example:
139 #pod
140 #pod ; weaver.ini
141 #pod [Version]
142 #pod format = version %v
143 #pod format =
144 #pod format = This module's version numbers follow the conventions described at
145 #pod format = L<semver.org|http://semver.org/>.
146 #pod format = %T
147 #pod format = %T This is a trial release!
148 #pod
149 #pod =cut
150  
151 7 subtype 'Pod::Weaver::Section::Version::_Format',
152 as 'ArrayRef[Str]';
153  
154 coerce 'Pod::Weaver::Section::Version::_Format',
155 from 'Str',
156 10 ▶ via { [ $_ ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
157  
158 7 has format => (
159 is => 'ro',
160 isa => 'Pod::Weaver::Section::Version::_Format',
161 coerce => 1,
162 default => 'version %v',
163 );
164  
165 #pod =attr is_verbatim
166 #pod
167 #pod A boolean value specifying whether the version paragraph should be verbatim or not.
168 #pod
169 #pod Default: false
170 #pod
171 #pod =cut
172  
173 7 has is_verbatim => (
174 is => 'ro',
175 isa => 'Bool',
176 default => 0,
177 );
178  
179 #pod =attr time_zone
180 #pod
181 #pod The timezone to use when using L<DateTime> for the format.
182 #pod
183 #pod Default: local
184 #pod
185 #pod =cut
186  
187 7 has time_zone => (
188 is => 'ro',
189 isa => 'Str', # should be more validated later -- apocal
190 default => 'local',
191 );
192  
193 #pod =method build_content
194 #pod
195 #pod my @pod_elements = $section->build_content(\%input);
196 #pod
197 #pod This method is passed the same C<\%input> that goes to the C<weave_section>
198 #pod method, and should return a list of pod elements to insert.
199 #pod
200 #pod In almost all cases, this method is used internally, but could be usefully
201 #pod overridden in a subclass.
202 #pod
203 #pod =cut
204  
205 17 ▶sub build_content {
Subroutine build_content: called CC=12 SCAR=25.1
Pod documented
206 17 my ($self, $input) = @_;
207 17 ▶ return unless $input->{version};
Branch: 1 branch
BranchTrueFalseTotal
unless $input->{"version"} 0 17 17
208  
209 my %args = (
210 self => $self,
211 version => $input->{version},
212 filename => $input->{filename},
213 17 );
214 17 ▶ $args{zilla} = $input->{zilla} if exists $input->{zilla};
Branch: 1 branch
BranchTrueFalseTotal
if exists $input->{"zilla"} 0 17 17
215  
216 $args{is_trial} = exists $input->{is_trial} ? $input->{is_trial}
217 : $args{zilla} ? $args{zilla}->is_trial
218 17 ▶ : undef;
Branch: 2 branches
BranchTrueFalseTotal
exists $input->{'is_trial'} ? : 1 16 17
$args{'zilla'} ? : 0 16 16
219  
220 17 ▶ if ( exists $input->{ppi_document} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $input->{"ppi_document"}) 17 0 17
221 17 my $pkg_node = $input->{ppi_document}->find_first('PPI::Statement::Package');
222 $args{module}
223 = $pkg_node
224 ? $pkg_node->namespace
225 17 ▶ : $self->_extract_comment_content($input->{ppi_document}, 'PODNAME')
Branch: 1 branch
BranchTrueFalseTotal
$pkg_node ? : 15 2 17
226 ;
227 }
228  
229 17 my $content = q{};
230 17 LINE: for my $format ($self->format->@*) {
231 30 my $line = _format_version($format, \%args);
232 30 ▶ next if $line =~ s/^$MARKER\s*// and ! $args{is_trial};
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ s/^$MARKER\s*//u and not $args{"is_trial"} 2 28 30
Condition100%3/3
$line =~ s/^$MARKER\s*//u and not $args{"is_trial"}
!ll&&!rl&&r
2622
MC/DC100%2/2
$line =~ s/^$MARKER\s*//u and not $args{"is_trial"}
$line =~ s/^$MARKER\s*//u not $args{"is_trial"}
Truth table100%3/3
A and not(B)
A= $line =~ s/^$MARKER\s*//uB= not $args{"is_trial"}
ABresult
0X0
100
111
233  
234 28 ▶ Carp::croak("%T format used inside line") if $line =~ /$MARKER/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$MARKER/u 0 28 28
235  
236 28 $content .= "$line\n";
237 }
238  
239 17 ▶ if ( $self->is_verbatim ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_verbatim) { } 3 14 17
240 3 $content = Pod::Elemental::Element::Pod5::Verbatim->new({
241 content => " $content",
242 });
243 } else {
244 14 $content = Pod::Elemental::Element::Pod5::Ordinary->new({
245 content => $content,
246 });
247 }
248  
249 17 return ($content);
250 }
251  
252 17 ▶sub weave_section {
Subroutine weave_section: called CC=2 SCAR=7.0
Pod undocumented
253 17 my ($self, $document, $input) = @_;
254 17 ▶ return unless $input->{version};
Branch: 1 branch
BranchTrueFalseTotal
unless $input->{"version"} 0 17 17
255  
256 17 my @content = $self->build_content($input);
257  
258 17 $self->log_debug('adding ' . $self->header . ' section to pod');
259  
260 17 push $document->children->@*,
261 Pod::Elemental::Element::Nested->new({
262 command => 'head1',
263 content => $self->header,
264 children => \@content,
265 });
266 }
267  
268 7 __PACKAGE__->meta->make_immutable;
269 7 1;
270  
271 __END__
« Weaver/Plugin/SingleEncoding.pm ↑ Summary Weaver/Plugin/Transformer.pm »