| 1 | package Padre::Plugin::PDL::Help; |
|---|
| 2 | |
|---|
| 3 | use 5.008; |
|---|
| 4 | use strict; |
|---|
| 5 | use warnings; |
|---|
| 6 | |
|---|
| 7 | # For Perl 6 documentation support |
|---|
| 8 | use Padre::Help (); |
|---|
| 9 | |
|---|
| 10 | our $VERSION = '0.05'; |
|---|
| 11 | |
|---|
| 12 | our @ISA = 'Padre::Help'; |
|---|
| 13 | |
|---|
| 14 | # |
|---|
| 15 | # Initialize help |
|---|
| 16 | # |
|---|
| 17 | sub help_init { |
|---|
| 18 | my $self = shift; |
|---|
| 19 | |
|---|
| 20 | eval { |
|---|
| 21 | require PDL::Doc; |
|---|
| 22 | |
|---|
| 23 | # Find the pdl documentation |
|---|
| 24 | my $pdldoc; |
|---|
| 25 | DIRECTORY: for my $dir (@INC) { |
|---|
| 26 | my $file = "$dir/PDL/pdldoc.db"; |
|---|
| 27 | if ( -f $file ) { |
|---|
| 28 | $pdldoc = new PDL::Doc($file); |
|---|
| 29 | last DIRECTORY; |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | if ( defined $pdldoc ) { |
|---|
| 34 | # Store in self for later access: |
|---|
| 35 | $self->{pdl_help} = $pdldoc; |
|---|
| 36 | } |
|---|
| 37 | }; |
|---|
| 38 | if ($@) { |
|---|
| 39 | warn "Failed to load PDL docs: $@"; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | # Workaround to get Perl + PDL help |
|---|
| 43 | require Padre::Document::Perl::Help; |
|---|
| 44 | $self->{p5_help} = Padre::Document::Perl::Help->new; |
|---|
| 45 | $self->{p5_help}->help_init; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | # |
|---|
| 49 | # Renders the help topic content |
|---|
| 50 | # |
|---|
| 51 | sub help_render { |
|---|
| 52 | my $self = shift; |
|---|
| 53 | my $topic = shift; |
|---|
| 54 | |
|---|
| 55 | my ( $html, $location ); |
|---|
| 56 | my $pdl_help = $self->{pdl_help}; |
|---|
| 57 | if ( defined $pdl_help && exists $pdl_help->gethash->{$topic} ) { |
|---|
| 58 | require Padre::Pod2HTML; |
|---|
| 59 | |
|---|
| 60 | # We have two possibilities: the $topic can either be a module, or it |
|---|
| 61 | # can be a function. If the latter, we extract its pod from the database. |
|---|
| 62 | # If the former, just pull the pod from the file. We distinguish between |
|---|
| 63 | # them by noting that functions have a Module key, whereas modules |
|---|
| 64 | # (ironically) don't. |
|---|
| 65 | if (exists $pdl_help->gethash->{$topic}->{Module}) { |
|---|
| 66 | # Get the pod docs from the docs database: |
|---|
| 67 | my $pod_handler = StrHandle->new; # defined in PDL::Doc |
|---|
| 68 | $pdl_help->funcdocs($topic, $pod_handler); |
|---|
| 69 | |
|---|
| 70 | # Convert them to HTML |
|---|
| 71 | $html = Padre::Pod2HTML->pod2html($pod_handler->text); |
|---|
| 72 | |
|---|
| 73 | # Replace the filename in the "docs from" section with the module name: |
|---|
| 74 | my $module_name = $pdl_help->gethash->{$topic}{Module}; |
|---|
| 75 | $html =~ s{Docs from .*\.pm} |
|---|
| 76 | {Docs from <a href="perldoc:$module_name">$module_name<\/a>}; |
|---|
| 77 | } |
|---|
| 78 | else { |
|---|
| 79 | $html = Padre::Pod2HTML->file2html($pdl_help->gethash->{$topic}->{File}); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | $location = $topic; |
|---|
| 83 | } else { |
|---|
| 84 | ( $html, $location ) = $self->{p5_help}->help_render($topic); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | return ( $html, $location ); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | # |
|---|
| 91 | # Returns the help topic list |
|---|
| 92 | # |
|---|
| 93 | sub help_list { |
|---|
| 94 | my $self = shift; |
|---|
| 95 | |
|---|
| 96 | # Return a unique sorted index |
|---|
| 97 | my @index = keys %{ $self->{pdl_help}->gethash }; |
|---|
| 98 | |
|---|
| 99 | # Add Perl 5 help index to PDL |
|---|
| 100 | #foreach my $topic ( @{ $self->{p5_help}->help_list } ) { |
|---|
| 101 | # push @index, $topic; |
|---|
| 102 | #} |
|---|
| 103 | # I think this is a faster way of doing the above: |
|---|
| 104 | push @index, @{ $self->{p5_help}->help_list }; |
|---|
| 105 | |
|---|
| 106 | # Make sure things are only listed once: |
|---|
| 107 | my %seen = (); |
|---|
| 108 | my @unique_sorted_index = sort grep { !$seen{$_}++ } @index; |
|---|
| 109 | return \@unique_sorted_index; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | 1; |
|---|
| 113 | |
|---|
| 114 | __END__ |
|---|
| 115 | |
|---|
| 116 | =head1 NAME |
|---|
| 117 | |
|---|
| 118 | Padre::Plugin::PDL::Help - PDL help provider for Padre |
|---|
| 119 | |
|---|
| 120 | =head1 DESCRIPTION |
|---|
| 121 | |
|---|
| 122 | PDL Help index is built here and rendered. |
|---|