| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 2737933 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 2 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 3 | 1 |  |  | 1 |  | 17 | use 5.020; | 
|  | 1 |  |  |  |  | 4 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | package Dist::Zilla::Plugin::InsertExample 0.12 { | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 5 | use Moose; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 8 | 1 |  |  | 1 |  | 5797 | use Encode qw( encode ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 54 |  | 
| 9 | 1 |  |  | 1 |  | 5 | use List::Util qw( first ); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 60 |  | 
| 10 | 1 |  |  | 1 |  | 6 | use experimental qw( signatures postderef ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | # ABSTRACT: Insert example into your POD from a file | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | with 'Dist::Zilla::Role::FileMunger'; | 
| 16 |  |  |  |  |  |  | with 'Dist::Zilla::Role::FileFinderUser' => { | 
| 17 |  |  |  |  |  |  | default_finders => [ qw( :InstallModules :ExecFiles ) ], | 
| 18 |  |  |  |  |  |  | }; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | has remove_boiler => (is => 'ro', isa => 'Int'); | 
| 21 |  |  |  |  |  |  | has indent        => (is => 'ro', isa => 'Int', default => 1); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub munge_files ($self) | 
| 24 | 6 |  |  | 6 | 0 | 108468 | { | 
|  | 6 |  |  |  |  | 17 |  | 
|  | 6 |  |  |  |  | 12 |  | 
| 25 | 6 |  |  |  |  | 32 | $self->munge_file($_) for $self->found_files->@*; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 6 |  |  |  |  | 10 | sub munge_file ($self, $file) | 
| 29 | 6 |  |  | 6 | 0 | 9012 | { | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 11 |  | 
| 30 | 6 |  |  |  |  | 27 | my $content = $file->content; | 
| 31 | 6 | 50 |  |  |  | 4598 | if($content =~ s{^#\s*EXAMPLE:\s*(.*)\s*$}{$self->_slurp_example($1)."\n"}meg) | 
|  | 6 |  |  |  |  | 24 |  | 
| 32 |  |  |  |  |  |  | { | 
| 33 | 6 |  |  |  |  | 29 | $self->log([ 'adding examples in %s', $file->name]); | 
| 34 | 6 |  |  |  |  | 2153 | $file->content($content); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 6 |  |  |  |  | 13 | sub _slurp_example ($self, $filename) | 
| 39 | 6 |  |  | 6 |  | 15 | { | 
|  | 6 |  |  |  |  | 13 |  | 
|  | 6 |  |  |  |  | 8 |  | 
| 40 | 6 |  |  |  |  | 13 | my $fh; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 6 | 100 |  | 18 |  | 162 | if(my $file = first { $_->name eq $filename } $self->zilla->files->@*) | 
|  | 18 | 50 |  |  |  | 634 |  | 
| 43 |  |  |  |  |  |  | { | 
| 44 | 4 |  |  |  |  | 157 | my $content = encode 'UTF-8', $file->content; | 
| 45 | 1 | 50 |  | 1 |  | 7 | open $fh, '<', \$content | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 1464 |  | 
| 46 | 0 |  |  |  |  | 0 | or $self->log_fatal("unable to open content of @{[ $file->name ]} as in memory string"); | 
| 47 | 4 |  |  |  |  | 781 | binmode $fh, ':utf8'; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | elsif($file = $self->zilla->root->child($filename)) | 
| 50 |  |  |  |  |  |  | { | 
| 51 | 2 | 50 |  |  |  | 264 | $self->log_fatal("no such example file $filename") unless -r $file; | 
| 52 | 2 |  |  |  |  | 60 | $fh = $file->openr_utf8; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 6 |  |  |  |  | 1455 | my $indent = ' ' x $self->indent; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 6 |  |  |  |  | 76 | while(my $line = <$fh>) | 
| 58 |  |  |  |  |  |  | { | 
| 59 | 6 | 50 |  |  |  | 213 | if($self->remove_boiler) | 
| 60 |  |  |  |  |  |  | { | 
| 61 | 0 | 0 |  |  |  | 0 | next if $line =~ /^\s*$/; | 
| 62 | 0 | 0 |  |  |  | 0 | next if $line =~ /^#!\/usr\/bin\/perl/; | 
| 63 | 0 | 0 |  |  |  | 0 | next if $line =~ /^#!\/usr\/bin\/env perl/; | 
| 64 | 0 | 0 |  |  |  | 0 | next if $line =~ /^use strict;$/; | 
| 65 | 0 | 0 |  |  |  | 0 | next if $line =~ /^use warnings;$/; | 
| 66 | 0 | 0 |  |  |  | 0 | return '' if eof $fh; | 
| 67 |  |  |  |  |  |  | } | 
| 68 | 6 | 100 |  |  |  | 15 | return join "\n", map { "$indent$_" } split /\n/, $line . do { local $/; my $rest = <$fh>; defined $rest ? $rest : '' }; | 
|  | 36 |  |  |  |  | 172 |  | 
|  | 6 |  |  |  |  | 49 |  | 
|  | 6 |  |  |  |  | 60 |  | 
|  | 6 |  |  |  |  | 86 |  | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | 1; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | __END__ | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | =pod | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =encoding UTF-8 | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =head1 NAME | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | Dist::Zilla::Plugin::InsertExample - Insert example into your POD from a file | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =head1 VERSION | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | version 0.12 | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | In your dist.ini: | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | [InsertExample] | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | In your POD: | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head1 EXAMPLE | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | Here is an exaple that writes hello world to the terminal: | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | # EXAMPLE: example/hello.pl | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | File in your dist named example/hello.pl | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | #!/usr/bin/perl | 
| 109 |  |  |  |  |  |  | say 'hello world'; | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | After dzil build your POD becomes: | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head1 EXAMPLE | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | Here is an example that writes hello world to the terminal: | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | #!/usr/bin/perl | 
| 118 |  |  |  |  |  |  | say 'hello world'; | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | and example/hello.pl is there too (unless you prune it with another | 
| 121 |  |  |  |  |  |  | plugin). | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | This plugin takes examples included in your distribution and | 
| 126 |  |  |  |  |  |  | inserts them in your POD where you have an EXAMPLE directive. | 
| 127 |  |  |  |  |  |  | This allows you to keep a version in the distribution which | 
| 128 |  |  |  |  |  |  | can be run by you and your users, as well as making it | 
| 129 |  |  |  |  |  |  | available in your POD documentation, without the need for | 
| 130 |  |  |  |  |  |  | updating example scripts in multiple places. | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | When the example is inserted into your pod a space will be appended | 
| 133 |  |  |  |  |  |  | at the start of each line so that it is printed in a fixed width | 
| 134 |  |  |  |  |  |  | font. | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | This plugin will first look for examples in the currently | 
| 137 |  |  |  |  |  |  | building distribution, including generated and munged files. | 
| 138 |  |  |  |  |  |  | If no matching filename is found, it will look in the distribution | 
| 139 |  |  |  |  |  |  | source root. | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =head1 OPTIONS | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =head2 remove_boiler | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | Remove the C<#!/usr/bin/perl>, C<use strict;> or C<use warnings;> from | 
| 146 |  |  |  |  |  |  | the beginning of your example before inserting them into the POD. | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =head2 indent | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | Specifies the number of spaces to indent by.  This is 1 by default, | 
| 151 |  |  |  |  |  |  | because it is sufficient to force POD to consider it a verbatim | 
| 152 |  |  |  |  |  |  | paragraph.  I understand a lot of Perl programmers out there prefer | 
| 153 |  |  |  |  |  |  | 4 spaces.  You can also set this to 0 to get no indentation at all | 
| 154 |  |  |  |  |  |  | and it won't be a verbatim paragraph at all. | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =head1 AUTHOR | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | Graham Ollis <plicease@cpan.org> | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | This software is copyright (c) 2013 by Graham Ollis. | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 165 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =cut |