| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package EPublisher::Utils::PPI; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # ABSTRACT: PPI utility for EPublisher | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 14 |  |  | 14 |  | 527 | use strict; | 
|  | 14 |  |  |  |  | 33 |  | 
|  | 14 |  |  |  |  | 378 |  | 
| 6 | 14 |  |  | 14 |  | 68 | use warnings; | 
|  | 14 |  |  |  |  | 24 |  | 
|  | 14 |  |  |  |  | 301 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 14 |  |  | 14 |  | 72 | use Exporter; | 
|  | 14 |  |  |  |  | 26 |  | 
|  | 14 |  |  |  |  | 450 |  | 
| 9 | 14 |  |  | 14 |  | 7047 | use PPI; | 
|  | 14 |  |  |  |  | 1660327 |  | 
|  | 14 |  |  |  |  | 5616 |  | 
| 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 | 12 | my ($file, $config) = @_; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 6 | 50 | 33 |  |  | 113 | return if !$file || ! -f $file; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 6 |  |  |  |  | 13 | my $content; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 6 | 50 |  |  |  | 207 | if ( open my $fh, '<', $file ) { | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 6 | 50 |  |  |  | 24 | if ( $config->{encoding} ) { | 
| 31 | 0 |  |  |  |  | 0 | binmode $fh, ':encoding(' . $config->{encoding} . ')'; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 6 |  |  |  |  | 26 | local $/; | 
| 35 | 6 |  |  |  |  | 131 | $content = <$fh>; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 6 | 50 |  |  |  | 20 | return if !$content; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 6 |  |  |  |  | 40 | my $parser    = PPI::Document->new( \$content ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 6 | 50 |  |  |  | 8497 | return if !$parser; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 6 |  |  |  |  | 24 | my $stmt = $parser->find_first('PPI::Statement::Package'); | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 6 | 100 |  |  |  | 1460 | return if !$stmt; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 4 |  |  |  |  | 15 | my $package = $stmt->namespace; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 4 |  |  |  |  | 109 | return $package; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub extract_pod { | 
| 54 | 77 |  |  | 77 | 1 | 294 | my ($file, $config) = @_; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 77 | 50 | 33 |  |  | 1831 | return if !$file || ! -f $file; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 77 |  |  |  |  | 236 | my $content; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 77 | 50 |  |  |  | 3225 | if ( open my $fh, '<', $file ) { | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 77 | 50 |  |  |  | 355 | if ( $config->{encoding} ) { | 
| 63 | 0 |  |  |  |  | 0 | binmode $fh, ':encoding(' . $config->{encoding} . ')'; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 77 |  |  |  |  | 420 | local $/; | 
| 67 | 77 |  |  |  |  | 3116 | $content = <$fh>; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 77 |  |  |  |  | 327 | return extract_pod_from_code( $content ); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub extract_pod_from_code { | 
| 74 | 77 |  |  | 77 | 1 | 206 | my ($code) = @_; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 77 | 50 |  |  |  | 223 | return if !$code; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 77 |  |  |  |  | 643 | my $parser    = PPI::Document->new( \$code ); | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 77 | 50 |  |  |  | 2868607 | return if !$parser; | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | my $pod_nodes = $parser->find( | 
| 83 |  |  |  |  |  |  | sub { | 
| 84 | 25073 |  |  | 25073 |  | 262110 | $_[1]->isa( 'PPI::Token::Pod' ); | 
| 85 |  |  |  |  |  |  | }, | 
| 86 | 77 |  |  |  |  | 705 | ); | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 77 | 100 |  |  |  | 1150 | my $merged = PPI::Token::Pod->merge( @{$pod_nodes || []} ); | 
|  | 77 |  |  |  |  | 569 |  | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 77 | 100 |  |  |  | 8896 | return '' if !$merged; | 
| 91 | 48 |  |  |  |  | 148 | return $merged->content; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | 1; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | __END__ |