Readme/Filter.pm

filter: Statementstmt 81.6% 147 / 180 Branchbran 57.1% 40 / 70 Conditioncond 28.5% 4 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 97.3% 37 / 38 Podpod 0.0% 0 / 12 totaltotal 71.2% 230 / 323 CC 42 SCAR 33.8
CC
42
Cov
72%
cmd_include
156.0
filter_line
18.2
process_for
5.6
CRAP 29.3
↑ Summary Readme/Plugin/changes.pm »
1 package Pod::Readme::Filter;
2  
3 5 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = 'v1.2.3';
8  
9 5 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 with 'Pod::Readme::Plugin';
11  
12 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use File::Slurp qw/ read_file /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use IO qw/ File Handle /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Module::Load qw/ load /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Types::Standard qw/ Bool InstanceOf Int RegexpRef Str /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use Pod::Readme::Types qw/ Dir File ReadIO WriteIO TargetName DistZilla /;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 Pod::Readme::Filter - Filter README from POD
25  
26 =head1 SYNOPSIS
27  
28 use Pod::Readme::Filter;
29  
30 my $prf = Pod::Readme::Filter->new(
31 target => 'readme',
32 base_dir => '.',
33 input_file => 'lib/MyApp.pm',
34 output_file => 'README.pod',
35 );
36  
37 =head1 DESCRIPTION
38  
39 This module provides the basic filtering and minimal processing to
40 extract a F<README.pod> from a module's POD. It is used internally by
41 L<Pod::Readme>.
42  
43 =cut
44  
45 5 has encoding => (
46 is => 'ro',
47 isa => Str,
48 default => ':utf8',
49 );
50  
51 has base_dir => (
52 is => 'ro',
53 isa => Dir,
54 11 ▶ coerce => sub { Dir->coerce(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 5 default => '.',
56 );
57  
58 has input_file => (
59 is => 'ro',
60 isa => File,
61 required => 0,
62 5 ▶ coerce => sub { File->coerce(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 5 );
64  
65 has output_file => (
66 is => 'ro',
67 isa => File,
68 required => 0,
69 4 ▶ coerce => sub { File->coerce(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 5 );
71  
72 has input_fh => (
73 is => 'ro',
74 isa => ReadIO,
75 lazy => 1,
76 builder => '_build_input_fh',
77 4 ▶ coerce => sub { ReadIO->coerce(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 5 );
79  
80 2 ▶sub _build_input_fh {
Subroutine _build_input_fh: called CC=3 SCAR=16.7
81 2 my ($self) = @_;
82 2 ▶ if ( $self->input_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->input_file) { } 2 0 2
83 2 $self->input_file->openr;
84 }
85 else {
86 0 my $fh = IO::Handle->new;
87 0 ▶ if ( $fh->fdopen( fileno(STDIN), 'r' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->fdopen(fileno STDIN, 'r')) { } 0 0 0
88 0 return $fh;
89 }
90 else {
91 0 croak "Cannot get a filehandle for STDIN";
92 }
93 }
94 }
95  
96 has output_fh => (
97 is => 'ro',
98 isa => WriteIO,
99 lazy => 1,
100 builder => '_build_output_fh',
101 9 ▶ coerce => sub { WriteIO->coerce(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
102 5 );
103  
104 4 ▶sub _build_output_fh {
Subroutine _build_output_fh: called CC=3 SCAR=16.7
105 4 my ($self) = @_;
106 4 ▶ if ( $self->output_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->output_file) { } 4 0 4
107 4 $self->output_file->openw;
108 }
109 else {
110 0 my $fh = IO::Handle->new;
111 0 ▶ if ( $fh->fdopen( fileno(STDOUT), 'w' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->fdopen(fileno STDOUT, 'w')) { } 0 0 0
112 0 return $fh;
113 }
114 else {
115 0 croak "Cannot get a filehandle for STDOUT";
116 }
117 }
118 }
119  
120 5 has target => (
121 is => 'ro',
122 isa => TargetName,
123 default => 'readme',
124 );
125  
126 5 has in_target => (
127 is => 'ro',
128 isa => Bool,
129 init_arg => undef,
130 default => 1,
131 writer => '_set_in_target',
132 );
133  
134 has _target_regex => (
135 is => 'ro',
136 isa => RegexpRef,
137 init_arg => undef,
138 lazy => 1,
139 5 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
140 5 my $self = shift;
141 5 my $target = $self->target;
142 5 qr/^[:]?${target}$/;
143 },
144 5 );
145  
146 5 has mode => (
147 is => 'rw',
148 isa => Str,
149 default => 'default',
150 init_arg => undef,
151 );
152  
153 5 has _line_no => (
154 is => 'ro',
155 isa => Int,
156 default => 0,
157 writer => '_set_line_no',
158 );
159  
160 44 ▶sub _inc_line_no {
Subroutine _inc_line_no: called CC=1 SCAR=0.0
161 44 my ($self) = @_;
162 44 $self->_set_line_no( 1 + $self->_line_no );
163 }
164  
165 7 ▶sub depends_on {
Subroutine depends_on: called CC=2 SCAR=6.9
Pod undocumented
166 7 my ($self) = @_;
167 7 my @files;
168 7 ▶ push @files, $self->input_file if $self->input_file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->input_file 6 1 7
169 7 return @files;
170 }
171  
172 124 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod undocumented
173 124 my ( $self, $line ) = @_;
174 124 my $fh = $self->output_fh;
175 124 print {$fh} $line;
176 }
177  
178 125 ▶sub in_pod {
Subroutine in_pod: called CC=1 SCAR=0.0
Pod undocumented
179 125 my ($self) = @_;
180 125 $self->mode eq 'pod';
181 }
182  
183 5 has _for_buffer => (
184 is => 'rw',
185 isa => Str,
186 init_arg => undef,
187 default => '',
188 handles_via => 'String',
189 handles => {
190 _append_for_buffer => 'append',
191 _clear_for_buffer => 'clear',
192 },
193 );
194  
195 5 has _begin_args => (
196 is => 'rw',
197 isa => Str,
198 init_arg => undef,
199 default => '',
200 handles_via => 'String',
201 handles => { _clear_begin_args => 'clear', },
202 );
203  
204 5 has zilla => (
205 is => 'ro',
206 isa => InstanceOf[ 'Dist::Zilla' ],
207 );
208  
209 18 ▶sub process_for {
Subroutine process_for: called CC=5 SCAR=17.2
Pod undocumented
210 18 my ( $self, $data ) = @_;
211  
212 18 my ( $target, @args ) = $self->_parse_arguments($data);
213  
214 18 ▶ if ( $target && $target =~ $self->_target_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target and $target =~ $self->_target_regex) 18 0 18
Condition33%1/3
$target and $target =~ $self->_target_regex
!ll&&!rl&&r
0018
MC/DC0%0/2
$target and $target =~ $self->_target_regex
$target $target =~ $self->_target_regex
Truth table33%1/3
A and B
A= $targetB= $target =~ $self->_target_regex
ABresult
0X0
100
111
215 18 ▶ if ( my $cmd = shift @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmd = shift @args) 18 0 18
216 18 $cmd =~ s/-/_/g;
217 18 ▶ if ( my $method = $self->can("cmd_${cmd}") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can("cmd_$cmd")) { } 18 0 18
218 18 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
219 18 $self->$method(@args);
220 }
221 4 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
222 4 s/\n$//;
223 4 die
224 sprintf( "\%s at input line \%d\n", $_, $self->_line_no );
225 18 };
226 }
227 else {
228 0 die sprintf( "Unknown command: '\%s' at input line \%d\n",
229 $cmd, $self->_line_no );
230 }
231  
232 }
233  
234 }
235 14 $self->_clear_for_buffer;
236 }
237  
238 119 ▶sub filter_line {
Subroutine filter_line: called CC=18 SCAR=29.0
Pod undocumented
239 119 my ( $self, $line ) = @_;
240  
241 # Modes:
242 #
243 # pod = POD mode
244 #
245 # pod:for = buffering text for =for command
246 #
247 # pod:begin = don't print this line, skip next line
248 #
249 # target:* = begin block for something other than readme
250 #
251 # default = code
252 #
253  
254 119 state $blank = qr/^\s*\n$/;
255  
256 119 my $mode = $self->mode;
257  
258 119 ▶ if ( $mode eq 'pod:for' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'pod:for') { } 18 101 119
elsif ($mode eq 'pod:begin') { } 7 94 101
259 18 ▶ if ( $line =~ $blank ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$blank/) { } 18 0 18
260 18 $self->process_for( $self->_for_buffer );
261 14 $mode = $self->mode('pod');
262 }
263 else {
264 0 $self->_append_for_buffer($line);
265 }
266 14 return 1;
267 }
268 elsif ( $mode eq 'pod:begin' ) {
269  
270 7 ▶ unless ( $line =~ $blank ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /$blank/) 0 7 7
271 0 die sprintf( "Expected new paragraph after command at line \%d\n",
272 $self->_line_no );
273 }
274  
275 7 $self->mode('pod');
276 7 return 1;
277 }
278  
279 94 ▶ if ( my ($cmd) = ( $line =~ /^=(\w+)\s/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($cmd) = $line =~ /^=(\w+)\s/) 41 53 94
280 41 ▶ $mode = $self->mode( $cmd eq 'cut' ? 'default' : 'pod' );
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'cut' ? : 1 40 41
281  
282 41 ▶ if ( $self->in_pod ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->in_pod) 40 1 41
283  
284 40 ▶ if ( $cmd eq 'for' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($cmd eq 'for') { } 18 22 40
elsif ($cmd eq 'begin') { } 4 18 22
elsif ($cmd eq 'end') { } 4 14 18
285  
286 18 $self->mode('pod:for');
287 18 $self->_for_buffer( substr( $line, 4 ) );
288  
289 }
290 elsif ( $cmd eq 'begin' ) {
291  
292 4 my ( $target, @args ) =
293 $self->_parse_arguments( substr( $line, 6 ) );
294  
295 4 ▶ if ( $target =~ $self->_target_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ $self->_target_regex) { } 3 1 4
296  
297 3 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 1 2 3
298  
299 1 my $buffer = join( ' ', @args );
300  
301 1 ▶ if ( substr( $target, 0, 1 ) eq ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($target, 0, 1) eq ":") 0 1 1
302 0 die sprintf( "Can only target POD at line \%d\n",
303 $self->_line_no + 1 );
304 }
305  
306 1 $self->write_begin( $self->_begin_args($buffer) );
307 }
308  
309 3 $self->mode('pod:begin');
310  
311 }
312 else {
313 1 $self->mode( 'target:' . $target );
314 }
315  
316 }
317 elsif ( $cmd eq 'end' ) {
318  
319 4 my ( $target, @args ) =
320 $self->_parse_arguments( substr( $line, 4 ) );
321  
322 4 ▶ if ( $target =~ $self->_target_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ $self->_target_regex) 3 1 4
323 3 my $buffer = $self->_begin_args;
324 3 ▶ if ( $buffer ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer ne "") 2 1 3
325 2 $self->write_end($buffer);
326 2 $self->_clear_begin_args;
327 }
328 }
329  
330 4 $self->mode('pod:begin');
331 }
332 }
333  
334 }
335  
336 94 ▶ $self->write($line) if $self->in_target && $self->in_pod;
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_target and $self->in_pod 55 39 94
Condition100%3/3
$self->in_target and $self->in_pod
!ll&&!rl&&r
102955
MC/DC100%2/2
$self->in_target and $self->in_pod
$self->in_target $self->in_pod
Truth table100%3/3
A and B
A= $self->in_targetB= $self->in_pod
ABresult
0X0
100
111
337  
338 94 return 1;
339 }
340  
341 4 ▶sub filter_file {
Subroutine filter_file: called CC=3 SCAR=11.1
Pod undocumented
342 4 my ($self) = @_;
343  
344 4 foreach
345 my $line ( read_file( $self->input_fh, binmode => $self->encoding ) )
346 {
347 44 ▶ $self->filter_line($line)
Branch: 1 branch
BranchTrueFalseTotal
$self->filter_line($line) or last 44 0 44
348 or last;
349 44 $self->_inc_line_no;
350 }
351 }
352  
353 4 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
354 4 my ($self) = @_;
355 4 $self->filter_file;
356 }
357  
358 2 ▶sub cmd_continue {
Subroutine cmd_continue: called CC=1 SCAR=0.0
Pod undocumented
359 2 my ($self) = @_;
360 2 $self->cmd_start;
361 }
362  
363 0 ▶sub cmd_include {
Subroutine cmd_include: not called CC=12 SCAR=50.5
Pod undocumented
364 0 my ( $self, @args ) = @_;
365  
366 0 my $res = $self->parse_cmd_args( [qw/ file type start stop /], @args );
367  
368 0 my $start = $res->{start};
369 0 ▶ $start = qr/${start}/ if $start;
Branch: 1 branch
BranchTrueFalseTotal
if $start 0 0 0
370 0 my $stop = $res->{stop};
371 0 ▶ $stop = qr/${stop}/ if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 0 0 0
372  
373 0 ▶ my $type = $res->{type} // 'pod';
Condition0%0/2
$res->{"type"} // "pod"
l!l
00
MC/DC0%0/1
$res->{"type"} // "pod"
$res->{"type"}
Truth table0%0/2
A
A= $res->{"type"}
Aresult
11
00
374 0 ▶ unless ( $type =~ /^(?:text|pod)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^(?:text|pod)$/) 0 0 0
375 0 die "Unsupported include type: '${type}'\n";
376 }
377  
378 0 my $file = $res->{file};
379 0 ▶ my $fh = IO::File->new( $file, 'r' )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file, "r") or die "Unable to open file '${file}': $!\n" 0 0 0
380 or die "Unable to open file '${file}': $!\n";
381  
382 0 $self->write("\n");
383  
384 0 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
385  
386 0 ▶ next if ( $start && $line !~ $start );
Branch: 1 branch
BranchTrueFalseTotal
if $start and not $line =~ /$start/ 0 0 0
Condition0%0/3
$start and not $line =~ /$start/
!ll&&!rl&&r
000
MC/DC0%0/2
$start and not $line =~ /$start/
$start not $line =~ /$start/
Truth table0%0/3
A and not(B)
A= $startB= not $line =~ /$start/
ABresult
0X0
100
111
387 0 ▶ last if ( $stop && $line =~ $stop );
Branch: 1 branch
BranchTrueFalseTotal
if $stop and $line =~ /$stop/ 0 0 0
Condition0%0/3
$stop and $line =~ /$stop/
!ll&&!rl&&r
000
MC/DC0%0/2
$stop and $line =~ /$stop/
$stop $line =~ /$stop/
Truth table0%0/3
A and B
A= $stopB= $line =~ /$stop/
ABresult
0X0
100
111
388  
389 0 $start = undef;
390  
391 0 ▶ if ( $type eq 'text' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'text') { } 0 0 0
392 0 $self->write_verbatim($line);
393 }
394 else {
395 0 $self->write($line);
396 }
397  
398 }
399  
400 0 $self->write("\n");
401  
402 0 close $fh;
403  
404 }
405  
406 4 ▶sub cmd_start {
Subroutine cmd_start: called CC=1 SCAR=0.0
Pod undocumented
407 4 my ($self) = @_;
408 4 $self->_set_in_target(1);
409 }
410  
411 4 ▶sub cmd_stop {
Subroutine cmd_stop: called CC=1 SCAR=0.0
Pod undocumented
412 4 my ($self) = @_;
413 4 $self->_set_in_target(0);
414 }
415  
416 6 ▶sub _load_plugin {
Subroutine _load_plugin: called CC=1 SCAR=0.0
417 6 my ( $self, $plugin ) = @_;
418 6 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
419 6 my $module = "Pod::Readme::Plugin::${plugin}";
420 6 load $module;
421 4 require Role::Tiny;
422 4 Role::Tiny->apply_roles_to_object( $self, $module );
423 }
424 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
425 2 die "Unable to locate plugin '${plugin}': $_";
426 6 };
427 }
428  
429 14 ▶sub cmd_plugin {
Subroutine cmd_plugin: called CC=3 SCAR=11.0
Pod undocumented
430 14 my ( $self, $plugin, @args ) = @_;
431 14 my $name = "cmd_${plugin}";
432 14 ▶ $self->_load_plugin($plugin) unless $self->can($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($name) 6 8 14
433 12 ▶ if ( my $method = $self->can($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can($name)) 12 0 12
434 12 $self->$method(@args);
435 }
436 }
437  
438 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
439  
440 5 1;
↑ Summary Readme/Plugin/changes.pm »