Readme.pm

filter: Statementstmt 91.0% 91 / 100 Branchbran 43.3% 13 / 30 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 23 / 23 Podpod 100% 5 / 5 totaltotal 80.4% 140 / 174 CC 20 SCAR 11.5
CC
20
Cov
79%
dependencies_updated
5.7
translate_file
5.5
_build_translate_to_fh
5.3
CRAP 3.1
« Readme/Plugin/version.pm ↑ Summary Readme/Types.pm »
1 package Pod::Readme;
2  
3 =head1 NAME
4  
5 Pod::Readme - Intelligently generate a README file from POD
6  
7 =for readme plugin version
8  
9 =head1 SYNOPSIS
10  
11 In a module's POD:
12  
13 =head1 NAME
14  
15 MyApp - my nifty app
16  
17 =for readme plugin version
18  
19 =head1 DESCRIPTION
20  
21 This is a nifty app.
22  
23 =begin :readme
24  
25 =for readme plugin requires
26  
27 =head1 INSTALLATION
28  
29 ...
30  
31 =end :readme
32  
33 =for readme stop
34  
35 =head1 METHODS
36  
37 ...
38  
39 Then from the command-line:
40  
41 pod2readme lib/MyModule.pm README
42  
43 =for readme stop
44  
45 From within Perl:
46  
47 use Pod::Readme;
48  
49 my $prf = Pod::Readme->new(
50 input_file => 'lib/MyModule.pm',
51 translate_to_file => $dest,
52 translation_class => 'Pod::Simple::Text',
53 );
54  
55 $prf->run();
56  
57 =for readme start
58  
59 =head1 DESCRIPTION
60  
61 This module filters POD to generate a F<README> file, by using POD
62 commands to specify which parts are included or excluded from the
63 F<README> file.
64  
65 =begin :readme
66  
67 See the L<Pod::Readme> documentation for more details on the POD
68 syntax that this module recognizes.
69  
70 See L<pod2readme> for command-line usage.
71  
72 =head1 INSTALLATION
73  
74 See
75 L<How to install CPAN modules|http://www.cpan.org/modules/INSTALL.html>.
76  
77 =for readme plugin requires heading-level=2 title="Required Modules"
78  
79 =for readme plugin changes
80  
81 =end :readme
82  
83 =for readme stop
84  
85 =head1 POD COMMANDS
86  
87 =head2 C<=for readme stop>
88  
89 Stop including the POD that follows in the F<README>.
90  
91 =head2 C<=for readme start>
92  
93 =head2 C<=for readme continue>
94  
95 Start (or continue to) include the POD that follows in the F<README>.
96  
97 Note that the C<start> command was added as a synonym in version
98 1.0.0.
99  
100 =head2 C<=for readme include>
101  
102 =for readme include file="INSTALL" type="text"
103  
104 Include a text or POD file in the F<README>. It accepts the following
105 options:
106  
107 =over
108  
109 =item C<file>
110  
111 Required. This is the file name to include.
112  
113 =item C<type>
114  
115 Can be "text" or "pod" (default).
116  
117 =item C<start>
118  
119 An optional regex of where to start including the file.
120  
121 =item C<stop>
122  
123 An optional regex of where to stop including the file.
124  
125 =back
126  
127 =head2 C<=for readme plugin>
128  
129 Loads a plugin, e.g.
130  
131 =for readme plugin version
132  
133 Note that specific plugins may add options, e.g.
134  
135 =for readme plugin changes title='CHANGES'
136  
137 See L<Pod::Readme::Plugin> for more information.
138  
139 Note that the C<plugin> command was added in version 1.0.0.
140  
141 =head2 C<=begin :readme>
142  
143 =head2 C<=end :readme>
144  
145 Specify a block of POD to include only in the F<README>.
146  
147 You can also specify a block in another format:
148  
149 =begin readme text
150  
151 ...
152  
153 =end readme text
154  
155 This will be translated into
156  
157 =begin text
158  
159 ...
160  
161 =end text
162  
163 and will only be included in F<README> files of that format.
164  
165 Note: earlier versions of this module suggested using
166  
167 =begin readme
168  
169 ...
170  
171 =end readme
172  
173 While this version supports that syntax for backwards compatibility,
174 it is not standard POD.
175  
176 =cut
177  
178 4 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
181 4 extends 'Pod::Readme::Filter';
182  
183 4 our $VERSION = 'v1.2.3';
184  
185 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 4 ▶use IO qw/ File Handle /;
Subroutine BEGIN: called CC=1 SCAR=0.0
187 4 ▶use List::Util 1.33 qw/ any /;
Subroutine BEGIN: called CC=1 SCAR=0.0
188 4 ▶use Module::Load qw/ load /;
Subroutine BEGIN: called CC=1 SCAR=0.0
189 4 ▶use Path::Tiny qw/ path tempfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 4 ▶use Pod::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
191 4 ▶use Types::Standard qw/ Bool Maybe Str /;
Subroutine BEGIN: called CC=1 SCAR=0.0
192  
193 4 ▶use Pod::Readme::Types qw/ File WriteIO /;
Subroutine BEGIN: called CC=1 SCAR=0.0
194  
195 # RECOMMEND PREREQ: Pod::Man
196 # RECOMMEND PREREQ: Pod::Markdown
197 # RECOMMEND PREREQ: Pod::Markdown::Github
198 # RECOMMEND PREREQ: Pod::Simple::HTML
199 # RECOMMEND PREREQ: Pod::Simple::LaTeX
200 # RECOMMEND PREREQ: Pod::Simple::RTF
201 # RECOMMEND PREREQ: Pod::Simple::Text
202 # RECOMMEND PREREQ: Pod::Simple::XHTML
203  
204 =head1 ATTRIBUTES
205  
206 This module extends L<Pod::Readme::Filter> with the following
207 attributes:
208  
209 =head2 C<translation_class>
210  
211 The class used to translate the filtered POD into another format,
212 e.g. L<Pod::Simple::Text>.
213  
214 If it is C<undef>, then there is no translation.
215  
216 Only subclasses of L<Pod::Simple> are supported.
217  
218 =cut
219  
220 4 has translation_class => (
221 is => 'ro',
222 isa => Maybe [Str],
223 default => undef,
224 );
225  
226 =head2 C<translate_to_fh>
227  
228 The L<IO::Handle> to save the translated file to.
229  
230 =cut
231  
232 has translate_to_fh => (
233 is => 'ro',
234 isa => WriteIO,
235 lazy => 1,
236 builder => '_build_translate_to_fh',
237 4 ▶ coerce => sub { WriteIO->coerce(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
238 4 );
239  
240 2 ▶sub _build_translate_to_fh {
Subroutine _build_translate_to_fh: called CC=3 SCAR=16.7
241 2 my ($self) = @_;
242 2 ▶ if ( $self->translate_to_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->translate_to_file) { } 2 0 2
243 2 $self->translate_to_file->openw;
244 }
245 else {
246 0 my $fh = IO::Handle->new;
247 0 ▶ if ( $fh->fdopen( fileno(STDOUT), 'w' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->fdopen(fileno STDOUT, 'w')) { } 0 0 0
248 0 return $fh;
249 }
250 else {
251 0 croak "Cannot get a filehandle for STDOUT";
252 }
253 }
254 }
255  
256 =head2 C<translate_to_file>
257  
258 The L<Path::Tiny> filename to save the translated file to. If omitted,
259 then it will be saved to C<STDOUT>.
260  
261 =cut
262  
263 has translate_to_file => (
264 is => 'ro',
265 isa => File,
266 5 ▶ coerce => sub { File->coerce(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
267 4 lazy => 1,
268 builder => 'default_readme_file',
269 );
270  
271 =head2 C<output_file>
272  
273 The L<Pod::Readme::Filter> C<output_file> will default to a temporary
274 file.
275  
276 =cut
277  
278 has '+output_file' => (
279 lazy => 1,
280 4 ▶ default => sub { tempfile( SUFFIX => '.pod', UNLINK => 1 ); },
Subroutine __ANON__: called CC=1 SCAR=0.0
281 4 );
282  
283 4 ▶around '_build_output_fh' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
284 4 my ( $orig, $self ) = @_;
285 4 ▶ if ( defined $self->translation_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->translation_class) { } 4 0 4
286 4 $self->$orig();
287 }
288 else {
289 0 $self->translate_to_fh;
290 }
291 4 };
292  
293 =head2 C<force>
294  
295 For a new F<README> to be generated, even if the dependencies have not
296 been updated.
297  
298 See L</dependencies_updated>.
299  
300 =cut
301  
302 4 has 'force' => (
303 is => 'ro',
304 isa => Bool,
305 default => 0,
306 );
307  
308 =head2 C<zilla>
309  
310 For use with L<Dist::Zilla> plugins.
311  
312 This allows plugins which normally depend on files in the distribution
313 to use metadata from here instead.
314  
315 =cut
316  
317 =head1 METHODS
318  
319 This module extends L<Pod::Readme::Filter> with the following methods:
320  
321 =head2 C<default_readme_file>
322  
323 The default name of the F<README> file, which depends on the
324 L</translation_class>.
325  
326 =cut
327  
328 3 ▶sub default_readme_file {
Subroutine default_readme_file: called CC=3 SCAR=12.6
Pod documented
329 3 my ($self) = @_;
330  
331 3 my $name = uc( $self->target );
332  
333 3 state $extensions = {
334 'Pod::Man' => '.1',
335 'Pod::Markdown' => '.md',
336 'Pod::Simple::HTML' => '.html',
337 'Pod::Simple::LaTeX' => '.tex',
338 'Pod::Simple::RTF' => '.rtf',
339 'Pod::Simple::Text' => '',
340 'Pod::Simple::XHTML' => '.xhtml',
341 };
342  
343 3 my $class = $self->translation_class;
344 3 ▶ if ( defined $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) { } 0 3 3
345 0 ▶ if ( my $ext = $extensions->{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ext = $extensions->{$class}) 0 0 0
346 0 $name .= $ext;
347 }
348 }
349 else {
350 3 $name .= '.pod';
351 }
352  
353 3 path( $self->base_dir, $name );
354 }
355  
356 =head2 C<translate_file>
357  
358 This method runs translates the resulting POD from C<filter_file>.
359  
360 =cut
361  
362 4 ▶sub translate_file {
Subroutine translate_file: called CC=5 SCAR=17.1
Pod documented
363 4 my ($self) = @_;
364  
365 4 ▶ if ( my $class = $self->translation_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $self->translation_class) 4 0 4
366  
367 4 load $class;
368 4 ▶ my $converter = $class->new()
Branch: 1 branch
BranchTrueFalseTotal
my $converter = $class->new or croak("Cannot instantiate a $class object") 4 0 4
369 or croak "Cannot instantiate a ${class} object";
370  
371 4 ▶ if ( $converter->isa('Pod::Simple') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($converter->isa('Pod::Simple')) { } 4 0 4
372  
373 4 my $tmp_file = $self->output_file->stringify;
374  
375 4 ▶ close $self->output_fh
Branch: 1 branch
BranchTrueFalseTotal
close $self->output_fh or croak("Unable to close file $tmp_file") 4 0 4
376 or croak "Unable to close file ${tmp_file}";
377  
378 4 $converter->output_fh( $self->translate_to_fh );
379 4 $converter->parse_file($tmp_file);
380  
381 }
382 else {
383  
384 0 croak "Don't know how to translate POD using ${class}";
385  
386 }
387  
388 }
389 }
390  
391 =head2 C<dependencies_updated>
392  
393 Used to determine when the dependencies have been updated, and a
394 translation can be run.
395  
396 Note that this only returns a meaningful value after the POD has been
397 processed, since plugins may add to the dependencies. A side-effect
398 of this is that when generating a POD formatted F<README> is that it
399 will always be updated, even when L</force> is false.
400  
401 =cut
402  
403 3 ▶sub dependencies_updated {
Subroutine dependencies_updated: called CC=5 SCAR=17.4
Pod documented
404 3 my ($self) = @_;
405  
406 3 my $dest = $self->translate_to_file;
407  
408 3 ▶ if ( $dest and $self->input_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest and $self->input_file) { } 3 0 3
Condition33%1/3
$dest and $self->input_file
!ll&&!rl&&r
003
MC/DC0%0/2
$dest and $self->input_file
$dest $self->input_file
Truth table33%1/3
A and B
A= $destB= $self->input_file
ABresult
0X0
100
111
409  
410 3 ▶ return 1 unless -e $dest;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dest 0 3 3
411  
412 3 my $stat = $dest->stat;
413 3 ▶ return 1 unless $stat;
Branch: 1 branch
BranchTrueFalseTotal
unless $stat 0 3 3
414  
415 3 my $time = $stat->mtime;
416 3 ▶ return any { $_->mtime > $time } ( map { $_->stat } $self->depends_on );
Subroutine __ANON__: called CC=1 SCAR=0.0
417  
418 }
419 else {
420 0 return 1;
421 }
422 }
423  
424 =head2 C<run>
425  
426 This method runs C<filter_file> and then L</translate_file>.
427  
428 =cut
429  
430 4 ▶around 'run' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
431 4 my ( $orig, $self ) = @_;
432 4 $self->$orig();
433 4 ▶ if ( $self->force or $self->dependencies_updated ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->force or $self->dependencies_updated) 4 0 4
Condition33%1/3
$self->force or $self->dependencies_updated
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->force or $self->dependencies_updated
$self->force $self->dependencies_updated
Truth table33%1/3
A or B
A= $self->forceB= $self->dependencies_updated
ABresult
1X1
011
000
434 4 $self->translate_file();
435 }
436 4 };
437  
438 =head2 C<parse_from_file>
439  
440 my $parser = Pod::Readme->new();
441 $parser->parse_from_file( 'README.pod', 'README' );
442  
443 Pod::Readme->parse_from_file( 'README.pod', 'README' );
444  
445 This is a class method that acts as a L<Pod::Select> compatibility
446 shim for software that is designed for versions of L<Pod::Readme>
447 prior to v1.0.
448  
449 Its use is deprecated, and will be deleted in later versions.
450  
451 =cut
452  
453 2 ▶sub parse_from_file {
Subroutine parse_from_file: called CC=2 SCAR=6.9
Pod documented
454 2 my ( $self, $source, $dest ) = @_;
455  
456 2 ▶ my $class = ref($self) || __PACKAGE__;
Condition100%2/2
ref $self || __PACKAGE__
l!l
11
MC/DC100%1/1
ref $self || __PACKAGE__
ref $self
Truth table100%2/2
A
A= ref $self
Aresult
11
00
457 2 my $prf = $class->new(
458 input_file => $source,
459 translate_to_file => $dest,
460 translation_class => 'Pod::Simple::Text',
461 force => 1,
462 );
463 2 $prf->run();
464 }
465  
466 =head2 C<parse_from_filehandle>
467  
468 Like L</parse_from_file>, this exists as a compatibility shim.
469  
470 Its use is deprecated, and will be deleted in later versions.
471  
472 =cut
473  
474 2 ▶sub parse_from_filehandle {
Subroutine parse_from_filehandle: called CC=4 SCAR=14.0
Pod documented
475 2 my ( $self, $source_fh, $dest_fh ) = @_;
476  
477 2 ▶ my $class = ref($self) || __PACKAGE__;
Condition100%2/2
ref $self || __PACKAGE__
l!l
11
MC/DC100%1/1
ref $self || __PACKAGE__
ref $self
Truth table100%2/2
A
A= ref $self
Aresult
11
00
478  
479 2 ▶ my $src_io =
Branch: 1 branch
BranchTrueFalseTotal
defined $source_fh ? : 2 0 2
480 IO::Handle->new_from_fd( ( defined $source_fh ) ? fileno($source_fh) : 0,
481 'r' );
482  
483 2 ▶ my $dest_io =
Branch: 1 branch
BranchTrueFalseTotal
defined $dest_fh ? : 2 0 2
484 IO::Handle->new_from_fd( ( defined $dest_fh ) ? fileno($dest_fh) : 1,
485 'w' );
486  
487 2 my $prf = $class->new(
488 input_fh => $src_io,
489 translate_to_fh => $dest_io,
490 translation_class => 'Pod::Simple::Text',
491 force => 1,
492 );
493 2 $prf->run();
494 }
495  
496 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
497  
498 4 1;
499  
500 =for readme start
501  
502 =head1 CAVEATS
503  
504 This module is intended to be used by module authors for their own
505 modules. It is not recommended for generating F<README> files from
506 arbitrary Perl modules from untrusted sources.
507  
508 =head1 SEE ALSO
509  
510 See L<perlpod>, L<perlpodspec> and L<podlators>.
511  
512 =head1 AUTHORS
513  
514 The original version was by Robert Rothenberg <rrwo@cpan.org> until
515 2010, when maintenance was taken over by David Precious
516 <davidp@preshweb.co.uk>.
517  
518 In 2014, Robert Rothenberg rewrote the module to use filtering instead
519 of subclassing a POD parser.
520  
521 =head2 Acknowledgements
522  
523 Thanks to people who gave feedback and suggestions to posts about the
524 rewrite of this module on L<http://blogs.perl.org>.
525  
526 =head2 Suggestions, Bug Reporting and Contributing
527  
528 This module is developed on GitHub at
529 L<http://github.com/bigpresh/Pod-Readme>
530  
531 =head1 LICENSE
532  
533 Copyright (c) 2005-2014 Robert Rothenberg. All rights reserved.
534 This program is free software; you can redistribute it and/or
535 modify it under the same terms as Perl itself.
536  
537 =cut
« Readme/Plugin/version.pm ↑ Summary Readme/Types.pm »