| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package EPublisher::Utils::PPI; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # ABSTRACT: PPI utility for EPublisher | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 15 |  |  | 15 |  | 567 | use strict; | 
|  | 15 |  |  |  |  | 32 |  | 
|  | 15 |  |  |  |  | 407 |  | 
| 6 | 15 |  |  | 15 |  | 73 | use warnings; | 
|  | 15 |  |  |  |  | 27 |  | 
|  | 15 |  |  |  |  | 336 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 15 |  |  | 15 |  | 68 | use Exporter; | 
|  | 15 |  |  |  |  | 30 |  | 
|  | 15 |  |  |  |  | 503 |  | 
| 9 | 15 |  |  | 15 |  | 7746 | use PPI; | 
|  | 15 |  |  |  |  | 1787154 |  | 
|  | 15 |  |  |  |  | 5905 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our @ISA = qw(Exporter); | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our @EXPORT_OK = qw( | 
| 14 |  |  |  |  |  |  | extract_package | 
| 15 |  |  |  |  |  |  | extract_pod | 
| 16 |  |  |  |  |  |  | extract_pod_from_code | 
| 17 |  |  |  |  |  |  | ); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | our $VERSION = 0.5; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | sub extract_package { | 
| 22 | 6 |  |  | 6 | 1 | 16 | my ($file, $config) = @_; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 6 | 50 | 33 |  |  | 142 | return if !$file || ! -f $file; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 6 |  |  |  |  | 16 | my $content; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 6 | 50 |  |  |  | 216 | if ( open my $fh, '<', $file ) { | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 6 | 50 |  |  |  | 23 | if ( $config->{encoding} ) { | 
| 31 | 0 |  |  |  |  | 0 | binmode $fh, ':encoding(' . $config->{encoding} . ')'; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 6 |  |  |  |  | 27 | local $/; | 
| 35 | 6 |  |  |  |  | 132 | $content = <$fh>; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 6 | 50 |  |  |  | 21 | return if !$content; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 6 |  |  |  |  | 41 | my $parser    = PPI::Document->new( \$content ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 6 | 50 |  |  |  | 10034 | return if !$parser; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 6 |  |  |  |  | 26 | my $stmt = $parser->find_first('PPI::Statement::Package'); | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 6 | 100 |  |  |  | 1423 | return if !$stmt; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 4 |  |  |  |  | 18 | my $package = $stmt->namespace; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 4 |  |  |  |  | 127 | return $package; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub extract_pod { | 
| 54 | 83 |  |  | 83 | 1 | 324 | my ($file, $config) = @_; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 83 | 50 | 33 |  |  | 1917 | return if !$file || ! -f $file; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 83 |  |  |  |  | 238 | my $content; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 83 | 50 |  |  |  | 3455 | if ( open my $fh, '<', $file ) { | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 83 | 50 |  |  |  | 377 | if ( $config->{encoding} ) { | 
| 63 | 0 |  |  |  |  | 0 | binmode $fh, ':encoding(' . $config->{encoding} . ')'; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 83 |  |  |  |  | 428 | local $/; | 
| 67 | 83 |  |  |  |  | 3567 | $content = <$fh>; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 83 |  |  |  |  | 337 | return extract_pod_from_code( $content ); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub extract_pod_from_code { | 
| 74 | 83 |  |  | 83 | 1 | 204 | my ($code) = @_; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 83 | 50 |  |  |  | 237 | return if !$code; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 83 |  |  |  |  | 668 | my $parser    = PPI::Document->new( \$code ); | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 83 | 50 |  |  |  | 2934001 | return if !$parser; | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | my $pod_nodes = $parser->find( | 
| 83 |  |  |  |  |  |  | sub { | 
| 84 | 26455 |  |  | 26455 |  | 273616 | $_[1]->isa( 'PPI::Token::Pod' ); | 
| 85 |  |  |  |  |  |  | }, | 
| 86 | 83 |  |  |  |  | 685 | ); | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 83 | 100 |  |  |  | 1231 | my $merged = PPI::Token::Pod->merge( @{$pod_nodes || []} ); | 
|  | 83 |  |  |  |  | 538 |  | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 83 | 100 |  |  |  | 8980 | return '' if !$merged; | 
| 91 | 53 |  |  |  |  | 159 | return $merged->content; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | 1; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | __END__ |