| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Footprintless::App::DocumentationUtil; | ||
| 5 | $Footprintless::App::DocumentationUtil::VERSION = '1.29'; | ||
| 6 | # ABSTRACT: A utility class for generating help documentation from POD | ||
| 7 | # PODNAME: Footprintless::App::DocumentationUtil | ||
| 8 | |||
| 9 | use Exporter qw(import); | ||
| 10 | use File::Spec; | ||
| 11 | use Log::Any; | ||
| 12 | |||
| 13 | my $logger = Log::Any->get_logger(); | ||
| 14 | |||
| 15 | our @EXPORT_OK = qw( | ||
| 16 | abstract | ||
| 17 | description | ||
| 18 | examples | ||
| 19 | pod_section | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub abstract { | ||
| 23 | my ($self_or_class) = @_; | ||
| 24 | |||
| 25 | my $pm_file = _pm_file($self_or_class); | ||
| 26 | return '(unknown)' unless ($pm_file); | ||
| 27 | |||
| 28 | require Footprintless::Util; | ||
| 29 | return Footprintless::Util::slurp($pm_file) =~ /^#+\s*ABSTRACT: (.*)$/m ? $1 : '(unknown)'; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub description { | ||
| 33 | return pod_section( $_[0], 'DESCRIPTION', 0, qr/Description:\n/ ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub examples { | ||
| 37 | return pod_section( $_[0], 'EXAMPLES' ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub pod_section { | ||
| 41 | my ( $self_or_class, $section, $indent, $remove ) = @_; | ||
| 42 | |||
| 43 | my $pm_file = _pm_file($self_or_class) | ||
| 44 | || return $self_or_class->abstract(); | ||
| 45 | |||
| 46 | my $pod = ''; | ||
| 47 | open( my $output, '>', \$pod ); | ||
| 48 | |||
| 49 | require Pod::Usage; | ||
| 50 | Pod::Usage::pod2usage( | ||
| 51 | -input => $pm_file, | ||
| 52 | -output => $output, | ||
| 53 | -exit => "NOEXIT", | ||
| 54 | -verbose => 99, | ||
| 55 | -sections => $section, | ||
| 56 | indent => $indent | ||
| 57 | ); | ||
| 58 | |||
| 59 | if ($pod) { | ||
| 60 | $pod =~ s/$remove//m if ($remove); | ||
| 61 | chomp($pod); | ||
| 62 | } | ||
| 63 | |||
| 64 | return $pod; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub _pm_file { | ||
| 68 | my ($self_or_class) = @_; | ||
| 69 | my $class = ref($self_or_class) || $self_or_class; | ||
| 70 | |||
| 71 | my @pm_file_parts = split( /::/, $class ); | ||
| 72 | $pm_file_parts[$#pm_file_parts] .= '.pm'; | ||
| 73 | my $pm_file = File::Spec->catfile(@pm_file_parts); | ||
| 74 | |||
| 75 | my $path = $INC{$pm_file}; | ||
| 76 | unless ($path) { | ||
| 77 | foreach my $prefix (@INC) { | ||
| 78 | my $prefix_path = File::Spec->catfile( $prefix, $pm_file ); | ||
| 79 | if ( -f $prefix_path ) { | ||
| 80 | $path = $prefix_path; | ||
| 81 | last; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | return $path; | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | |||
| 91 | __END__ |