| 1 | package Template::Plugin::PodGenerated; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Class::ISA; | ||
| 5 | use Devel::Peek 'CvGV'; | ||
| 6 | use Devel::Symdump; | ||
| 7 | use Pod::Generated 'doc'; | ||
| 8 | use Text::Conjunct; | ||
| 9 | our $VERSION = '0.05'; | ||
| 10 | use base 'Template::Plugin'; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ($class, $context) = @_; | ||
| 14 | my $source = $context->stash->get('SOURCE'); | ||
| 15 | my $package; | ||
| 16 | if ($source =~ / | ||
| 17 | ^ | ||
| 18 | package \s* | ||
| 19 | (\w+(::(?:\w+))*) | ||
| 20 | \s* ; | ||
| 21 | /xms | ||
| 22 | ) { | ||
| 23 | $package = $1; | ||
| 24 | } else { | ||
| 25 | die "can't parse package from source\n"; | ||
| 26 | } | ||
| 27 | |||
| 28 | # This will generate documentation via add_doc. | ||
| 29 | eval $source; | ||
| 30 | die "can't eval source: $@\n" if $@; | ||
| 31 | |||
| 32 | # now that the source has been evaluated, we can look at the package | ||
| 33 | # variables. | ||
| 34 | my $version; | ||
| 35 | { | ||
| 36 | no strict 'refs'; | ||
| 37 | $version = ${"${package}::VERSION"}; | ||
| 38 | } | ||
| 39 | bless { | ||
| 40 | _CONTEXT => $context, | ||
| 41 | package => $package, | ||
| 42 | version => $version, | ||
| 43 | }, $class; | ||
| 44 | } | ||
| 45 | sub package { $_[0]->{package} } | ||
| 46 | sub version { $_[0]->{version} } | ||
| 47 | |||
| 48 | sub format { | ||
| 49 | my ($self, %args) = @_; | ||
| 50 | my $result = ''; | ||
| 51 | my $line = my $indent = ' ' x $args{indent}; | ||
| 52 | for my $word (split /\s+/ => $args{text}) { | ||
| 53 | if (length($line) + 1 + length($word) > $args{width}) { | ||
| 54 | $result .= "$line\n"; | ||
| 55 | $line = $indent . $word; | ||
| 56 | } else { | ||
| 57 | $line .= ' ' if $line =~ /\S/; | ||
| 58 | $line .= $word; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | $result .= "$line\n"; | ||
| 62 | $result; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Heuristic: It's not a method if it has been imported from another package. | ||
| 66 | # That is, if the glob is aliased to another package. CvGV() tells us that. | ||
| 67 | sub is_not_method { | ||
| 68 | my ($self, $function, $package) = @_; | ||
| 69 | my $current = "${package}::${function}"; | ||
| 70 | no strict 'refs'; | ||
| 71 | CvGV(*{$current}{CODE}) ne "*$current"; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub get_inheritance_data { | ||
| 75 | my $self = shift; | ||
| 76 | |||
| 77 | # If several packages define a function, only the lowest class gets | ||
| 78 | # mentioned, as it overrides the definition of its superclasses. Seed the | ||
| 79 | # lookup hash with the functions defined in the class that is being | ||
| 80 | # documented, as we don't want to report those functions from the | ||
| 81 | # inheritance. | ||
| 82 | my %seen; | ||
| 83 | $seen{$_} = 1 | ||
| 84 | for map { s/^ $self->{package} :://x; $_ } | ||
| 85 | Devel::Symdump->new($self->{package})->functions; | ||
| 86 | my @result; | ||
| 87 | for my $package (Class::ISA::super_path($self->{package})) { | ||
| 88 | my @functions; | ||
| 89 | for my $function (Devel::Symdump->new($package)->functions) { | ||
| 90 | $function =~ s/^ $package :://x; | ||
| 91 | next if $seen{$function}++; | ||
| 92 | next if $self->is_not_method($function, $package); | ||
| 93 | push @functions => $function; | ||
| 94 | } | ||
| 95 | push @result => $package, [ $self->sub_order(@functions) ]; | ||
| 96 | } | ||
| 97 | wantarray ? @result : \@result; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub write_inheritance { | ||
| 101 | my $self = shift; | ||
| 102 | no strict 'refs'; | ||
| 103 | my @inherited = @{ $self->{package} . '::ISA' }; | ||
| 104 | my $result = $self->format( | ||
| 105 | indent => 0, | ||
| 106 | width => 75, | ||
| 107 | text => sprintf "%s inherits from %s.\n", | ||
| 108 | $self->{package}, | ||
| 109 | conjunct and => map { "L<$_>" } @inherited | ||
| 110 | ); | ||
| 111 | my @inheritance = $self->get_inheritance_data; | ||
| 112 | while (my ($package, $functions) = splice(@inheritance, 0, 2)) { | ||
| 113 | next unless @$functions; | ||
| 114 | $result .= "\n"; | ||
| 115 | $result .= $self->format( | ||
| 116 | indent => 0, | ||
| 117 | width => 75, | ||
| 118 | text => | ||
| 119 | "The superclass L<$package> defines these methods and functions:" | ||
| 120 | ); | ||
| 121 | $result .= "\n"; | ||
| 122 | $result .= $self->format( | ||
| 123 | indent => 4, | ||
| 124 | width => 75, | ||
| 125 | text => join ', ' => | ||
| 126 | |||
| 127 | # map { "L<$_()|$package/$_>" } | ||
| 128 | map { "$_()" } @$functions, | ||
| 129 | ); | ||
| 130 | } | ||
| 131 | 1 while chomp $result; | ||
| 132 | $result; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub write_methods { | ||
| 136 | my $self = shift; | ||
| 137 | my %doc = doc(); | ||
| 138 | my $package_doc = $doc{ $self->{package} }{CODE} || {}; | ||
| 139 | my $result = ''; | ||
| 140 | for my $sub ($self->sub_order(keys %$package_doc)) { | ||
| 141 | my $vars = { | ||
| 142 | sub => $sub, | ||
| 143 | purpose => (join "\n" => @{ $package_doc->{$sub}{purpose} }), | ||
| 144 | example => ( | ||
| 145 | join "\n" => map { " $_" } @{ $package_doc->{$sub}{example} } | ||
| 146 | ), | ||
| 147 | }; | ||
| 148 | $result .= | ||
| 149 | "=item $vars->{sub}\n\n$vars->{example}\n\n$vars->{purpose}\n\n"; | ||
| 150 | } | ||
| 151 | 1 while chomp $result; | ||
| 152 | $result; | ||
| 153 | } | ||
| 154 | |||
| 155 | # Basically an alphabetic sort, but sort certain sub names first, such as | ||
| 156 | # 'new' and 'instance'. | ||
| 157 | sub sub_order { | ||
| 158 | my ($self, @names) = @_; | ||
| 159 | my %has; | ||
| 160 | @has{@names} = (); | ||
| 161 | |||
| 162 | # partition the names into those that should come first, and the rest | ||
| 163 | my @first; | ||
| 164 | for my $name (qw(new instance)) { | ||
| 165 | next unless exists $has{$name}; | ||
| 166 | delete $has{$name}; | ||
| 167 | push @first => $name; | ||
| 168 | } | ||
| 169 | (sort(@first), sort keys %has); | ||
| 170 | } | ||
| 171 | 1; | ||
| 172 | __END__ |