File Coverage

blib/lib/Pod/Readme/Filter.pm
Criterion Covered Total %
statement 122 155 78.7
branch 40 68 58.8
condition 4 14 28.5
subroutine 31 32 96.8
pod 0 12 0.0
total 197 281 70.1


line stmt bran cond sub pod time code
1             package Pod::Readme::Filter;
2              
3 5     5   2994 use v5.10.1;
  5         18  
4              
5 5     5   582 use Moo;
  5         11344  
  5         32  
6              
7             our $VERSION = 'v1.2.3';
8              
9 5     5   5183 use MooX::HandlesVia;
  5         47699  
  5         33  
10             with 'Pod::Readme::Plugin';
11              
12 5     5   701 use Carp;
  5         11  
  5         273  
13 5     5   2704 use File::Slurp qw/ read_file /;
  5         63035  
  5         400  
14 5     5   38 use IO qw/ File Handle /;
  5         11  
  5         35  
15 5     5   2517 use Module::Load qw/ load /;
  5         1120  
  5         38  
16 5     5   416 use Path::Tiny;
  5         10  
  5         245  
17 5     5   481 use Try::Tiny;
  5         1326  
  5         264  
18 5     5   549 use Types::Standard qw/ Bool InstanceOf Int RegexpRef Str /;
  5         74763  
  5         46  
19              
20 5     5   6124 use Pod::Readme::Types qw/ Dir File ReadIO WriteIO TargetName DistZilla /;
  5         14  
  5         11211  
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 from a module's POD. It is used internally by
41             L.
42              
43             =cut
44              
45             has encoding => (
46             is => 'ro',
47             isa => Str,
48             default => ':utf8',
49             );
50              
51             has base_dir => (
52             is => 'ro',
53             isa => Dir,
54             coerce => sub { Dir->coerce(@_) },
55             default => '.',
56             );
57              
58             has input_file => (
59             is => 'ro',
60             isa => File,
61             required => 0,
62             coerce => sub { File->coerce(@_) },
63             );
64              
65             has output_file => (
66             is => 'ro',
67             isa => File,
68             required => 0,
69             coerce => sub { File->coerce(@_) },
70             );
71              
72             has input_fh => (
73             is => 'ro',
74             isa => ReadIO,
75             lazy => 1,
76             builder => '_build_input_fh',
77             coerce => sub { ReadIO->coerce(@_) },
78             );
79              
80             sub _build_input_fh {
81 2     2   40 my ($self) = @_;
82 2 50       15 if ( $self->input_file ) {
83 2         10 $self->input_file->openr;
84             }
85             else {
86 0         0 my $fh = IO::Handle->new;
87 0 0       0 if ( $fh->fdopen( fileno(STDIN), 'r' ) ) {
88 0         0 return $fh;
89             }
90             else {
91 0         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             coerce => sub { WriteIO->coerce(@_) },
102             );
103              
104             sub _build_output_fh {
105 4     4   7 my ($self) = @_;
106 4 50       62 if ( $self->output_file ) {
107 4         183 $self->output_file->openw;
108             }
109             else {
110 0         0 my $fh = IO::Handle->new;
111 0 0       0 if ( $fh->fdopen( fileno(STDOUT), 'w' ) ) {
112 0         0 return $fh;
113             }
114             else {
115 0         0 croak "Cannot get a filehandle for STDOUT";
116             }
117             }
118             }
119              
120             has target => (
121             is => 'ro',
122             isa => TargetName,
123             default => 'readme',
124             );
125              
126             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             default => sub {
140             my $self = shift;
141             my $target = $self->target;
142             qr/^[:]?${target}$/;
143             },
144             );
145              
146             has mode => (
147             is => 'rw',
148             isa => Str,
149             default => 'default',
150             init_arg => undef,
151             );
152              
153             has _line_no => (
154             is => 'ro',
155             isa => Int,
156             default => 0,
157             writer => '_set_line_no',
158             );
159              
160             sub _inc_line_no {
161 44     44   66 my ($self) = @_;
162 44         731 $self->_set_line_no( 1 + $self->_line_no );
163             }
164              
165             sub depends_on {
166 7     7 0 1039 my ($self) = @_;
167 7         13 my @files;
168 7 100       74 push @files, $self->input_file if $self->input_file;
169 7         91 return @files;
170             }
171              
172             sub write {
173 124     124 0 626 my ( $self, $line ) = @_;
174 124         2076 my $fh = $self->output_fh;
175 124         1048 print {$fh} $line;
  124         389  
176             }
177              
178             sub in_pod {
179 125     125 0 221 my ($self) = @_;
180 125         2084 $self->mode eq 'pod';
181             }
182              
183             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             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             has zilla => (
205             is => 'ro',
206             isa => InstanceOf[ 'Dist::Zilla' ],
207             );
208              
209             sub process_for {
210 18     18 0 191 my ( $self, $data ) = @_;
211              
212 18         96 my ( $target, @args ) = $self->_parse_arguments($data);
213              
214 18 50 33     480 if ( $target && $target =~ $self->_target_regex ) {
215 18 50       386 if ( my $cmd = shift @args ) {
216 18         40 $cmd =~ s/-/_/g;
217 18 50       131 if ( my $method = $self->can("cmd_${cmd}") ) {
218             try {
219 18     18   1847 $self->$method(@args);
220             }
221             catch {
222 4     4   99 s/\n$//;
223 4         101 die
224             sprintf( "\%s at input line \%d\n", $_, $self->_line_no );
225 18         196 };
226             }
227             else {
228 0         0 die sprintf( "Unknown command: '\%s' at input line \%d\n",
229             $cmd, $self->_line_no );
230             }
231              
232             }
233              
234             }
235 14         1187 $self->_clear_for_buffer;
236             }
237              
238             sub filter_line {
239 119     119 0 45416 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         219 state $blank = qr/^\s*\n$/;
255              
256 119         2331 my $mode = $self->mode;
257              
258 119 100       944 if ( $mode eq 'pod:for' ) {
    100          
259 18 50       125 if ( $line =~ $blank ) {
260 18         306 $self->process_for( $self->_for_buffer );
261 14         2120 $mode = $self->mode('pod');
262             }
263             else {
264 0         0 $self->_append_for_buffer($line);
265             }
266 14         425 return 1;
267             }
268             elsif ( $mode eq 'pod:begin' ) {
269              
270 7 50       40 unless ( $line =~ $blank ) {
271 0         0 die sprintf( "Expected new paragraph after command at line \%d\n",
272             $self->_line_no );
273             }
274              
275 7         115 $self->mode('pod');
276 7         196 return 1;
277             }
278              
279 94 100       383 if ( my ($cmd) = ( $line =~ /^=(\w+)\s/ ) ) {
280 41 100       725 $mode = $self->mode( $cmd eq 'cut' ? 'default' : 'pod' );
281              
282 41 100       1157 if ( $self->in_pod ) {
283              
284 40 100       356 if ( $cmd eq 'for' ) {
    100          
    100          
285              
286 18         348 $self->mode('pod:for');
287 18         787 $self->_for_buffer( substr( $line, 4 ) );
288              
289             }
290             elsif ( $cmd eq 'begin' ) {
291              
292 4         17 my ( $target, @args ) =
293             $self->_parse_arguments( substr( $line, 6 ) );
294              
295 4 100       78 if ( $target =~ $self->_target_regex ) {
296              
297 3 100       38 if (@args) {
298              
299 1         3 my $buffer = join( ' ', @args );
300              
301 1 50       6 if ( substr( $target, 0, 1 ) eq ':' ) {
302 0         0 die sprintf( "Can only target POD at line \%d\n",
303             $self->_line_no + 1 );
304             }
305              
306 1         360 $self->write_begin( $self->_begin_args($buffer) );
307             }
308              
309 3         81 $self->mode('pod:begin');
310              
311             }
312             else {
313 1         27 $self->mode( 'target:' . $target );
314             }
315              
316             }
317             elsif ( $cmd eq 'end' ) {
318              
319 4         19 my ( $target, @args ) =
320             $self->_parse_arguments( substr( $line, 4 ) );
321              
322 4 100       71 if ( $target =~ $self->_target_regex ) {
323 3         85 my $buffer = $self->_begin_args;
324 3 100       30 if ( $buffer ne '' ) {
325 2         9 $self->write_end($buffer);
326 2         81 $self->_clear_begin_args;
327             }
328             }
329              
330 4         243 $self->mode('pod:begin');
331             }
332             }
333              
334             }
335              
336 94 100 100     1006 $self->write($line) if $self->in_target && $self->in_pod;
337              
338 94         681 return 1;
339             }
340              
341             sub filter_file {
342 4     4 0 7 my ($self) = @_;
343              
344 4         69 foreach
345             my $line ( read_file( $self->input_fh, binmode => $self->encoding ) )
346             {
347 44 50       1853 $self->filter_line($line)
348             or last;
349 44         110 $self->_inc_line_no;
350             }
351             }
352              
353             sub run {
354 4     4 0 8 my ($self) = @_;
355 4         15 $self->filter_file;
356             }
357              
358             sub cmd_continue {
359 2     2 0 620 my ($self) = @_;
360 2         6 $self->cmd_start;
361             }
362              
363             sub cmd_include {
364 0     0 0 0 my ( $self, @args ) = @_;
365              
366 0         0 my $res = $self->parse_cmd_args( [qw/ file type start stop /], @args );
367              
368 0         0 my $start = $res->{start};
369 0 0       0 $start = qr/${start}/ if $start;
370 0         0 my $stop = $res->{stop};
371 0 0       0 $stop = qr/${stop}/ if $stop;
372              
373 0   0     0 my $type = $res->{type} // 'pod';
374 0 0       0 unless ( $type =~ /^(?:text|pod)$/ ) {
375 0         0 die "Unsupported include type: '${type}'\n";
376             }
377              
378 0         0 my $file = $res->{file};
379 0 0       0 my $fh = IO::File->new( $file, 'r' )
380             or die "Unable to open file '${file}': $!\n";
381              
382 0         0 $self->write("\n");
383              
384 0         0 while ( my $line = <$fh> ) {
385              
386 0 0 0     0 next if ( $start && $line !~ $start );
387 0 0 0     0 last if ( $stop && $line =~ $stop );
388              
389 0         0 $start = undef;
390              
391 0 0       0 if ( $type eq 'text' ) {
392 0         0 $self->write_verbatim($line);
393             }
394             else {
395 0         0 $self->write($line);
396             }
397              
398             }
399              
400 0         0 $self->write("\n");
401              
402 0         0 close $fh;
403              
404             }
405              
406             sub cmd_start {
407 4     4 0 597 my ($self) = @_;
408 4         84 $self->_set_in_target(1);
409             }
410              
411             sub cmd_stop {
412 4     4 0 3901 my ($self) = @_;
413 4         92 $self->_set_in_target(0);
414             }
415              
416             sub _load_plugin {
417 6     6   18 my ( $self, $plugin ) = @_;
418             try {
419 6     6   556 my $module = "Pod::Readme::Plugin::${plugin}";
420 6         52 load $module;
421 4         6040 require Role::Tiny;
422 4         43 Role::Tiny->apply_roles_to_object( $self, $module );
423             }
424             catch {
425 2     2   797 die "Unable to locate plugin '${plugin}': $_";
426 6         86 };
427             }
428              
429             sub cmd_plugin {
430 14     14 0 45 my ( $self, $plugin, @args ) = @_;
431 14         36 my $name = "cmd_${plugin}";
432 14 100       116 $self->_load_plugin($plugin) unless $self->can($name);
433 12 50       4415 if ( my $method = $self->can($name) ) {
434 12         45 $self->$method(@args);
435             }
436             }
437              
438 5     5   505 use namespace::autoclean;
  5         11789  
  5         41  
439              
440             1;