File: Pod.pm

package info (click to toggle)
libmodule-cpants-analyse-perl 1.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312 kB
  • sloc: perl: 2,192; makefile: 2
file content (214 lines) | stat: -rw-r--r-- 6,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package Module::CPANTS::Kwalitee::Pod;
use warnings;
use strict;
use File::Spec::Functions qw/catfile/;
use Encode;
use Data::Binary qw/is_binary/;

our $VERSION = '1.02';
$VERSION =~ s/_//; ## no critic

our @ABSTRACT_STUBS = (
  q{Perl extension for blah blah blah}, # h2xs
  q{[One line description of module's purpose here]}, # Module::Starter etc
  q{The great new}, # Module::Starter
  q{It's new $module}, # Minilla
);

sub order { 100 }

##################################################################
# Analyse
##################################################################

sub analyse {
    my ($class, $me) = @_;
    my $distdir = $me->distdir;
    my @errors;

    my @files = map { $_->{file} } @{$me->d->{modules} || []};

    for my $file (@{$me->d->{files_array} || []}) {
        # sometimes pod for .pm file is put into .pod
        # and scripts may have abstract
        if (
            ( $file =~ /\.pod$/ && ($file =~ m!^lib/! or $file =~ m!^[^/]+$!) )
            or $file =~ m!^(?:bin|scripts?)/!
        ) {
            push @files, $file;
        }
    }

    for my $file (@files) {
        local $@;
        my ($package, $abstract, $error, $has_binary_data) = $class->_parse_abstract(catfile($distdir, $file));
        push @errors, "$error ($package)" if $error;
        $me->d->{abstracts_in_pod}{$package} = $abstract if $package;
        $me->d->{files_hash}{$file}{has_binary_data} = 1 if $has_binary_data;
    }
    $me->d->{error}{has_abstract_in_pod} = join ';', @errors if @errors;
}

# adapted from ExtUtils::MM_Unix and Module::Build::PodParser
sub _parse_abstract {
    my ($class, $file) = @_;
    my ($package, $abstract);
    my $inpod = 0;
    open my $fh, '<', $file or return;
    my $directive;
    my $encoding;
    my $package_name_pattern = '(?:[A-Za-z0-9_]+::)*[A-Za-z0-9_]+ | [BCIF] < (?:[A-Za-z0-9_]+::)*[A-Za-z0-9_]+ >';
    if ( $file !~ /\.p(?:m|od)$/ ) {
        $package_name_pattern .= ' | [A-Za-z0-9_.-]+ | [BCIF] < [A-Za-z0-9_.-]+ >';
    }
    while(<$fh>) {
        if (/^\s*__DATA__\s*$/) {
            my $copy = $_ = <$fh>;
            last unless defined $copy;
            return (undef, undef, undef, 1) if is_binary($copy);
        }
        if (substr($_, 0, 1) eq '=') {
            if (/^=encoding\s+(.+)/) {
                $encoding = $1;
            }
            if (/^=cut/) {
                $inpod = 0;
            } elsif (/^=(?!cut)(.+)/) {
                $directive = $1;
                $inpod = 1;
            }
        }
        next if !$inpod;
        next unless $directive =~ /^head/;
        if ( /^\s*(${package_name_pattern}) \s+ -+ (?:\s+ (.*)\s*$|$)/x ) {
            ($package, $abstract) = ($1, $2);
            $package =~ s![BCIF]<([^>]+)>!$1!;
            next;
        }
        next unless $abstract;
        last if /^\s*$/ || /^=/;
        s/\s+$//s;
        $abstract .= "\n$_";
    }

    my $error;
    if ($encoding && $abstract) {
        my $encoder = find_encoding($encoding);
        if (!$encoder) {
            $error = "unknown encoding: $encoding";
        } else {
            $abstract = eval { $encoder->decode($abstract) };
            if ($@) {
                $error = $@;
                $error =~ s|\s*at .+ line \d+.+$||s;
            }
        }
    }
    return ($package, $abstract, $error);
}

##################################################################
# Kwalitee Indicators
##################################################################

sub kwalitee_indicators {
    return [
      {
          name => 'has_abstract_in_pod',
          error => q{No abstract (short description of a module) is found in pod from this distribution.},
          remedy => q{Provide a short description in the NAME section of the pod (after the module name followed by a hyphen) at least for the main module of this distribution.},
          code => sub {
              my $d = shift;
              return 0 if $d->{error}{has_abstract_in_pod};
              my @abstracts = grep {defined $_ && length $_} values %{$d->{abstracts_in_pod} || {}};
              return @abstracts ? 1 : 0;
          },
          details => sub {
              my $d = shift;
              return "No abstracts in pod";
          },
      },
      {
          name => 'no_abstract_stub_in_pod',
          is_extra => 1,
          error => q{A well-known abstract stub (typically generated by an authoring tool) is found in this distribution.},
          remedy => q{Modify the stub. You might need to modify other stubs (for name, synopsis, license, etc) as well.},
          code => sub {
              my $d = shift;
              my %mapping = map {$_ => 1} @ABSTRACT_STUBS;
              my @errors;
              for (sort keys %{$d->{abstracts_in_pod} || {}}) {
                  push @errors, $_ if $mapping{$d->{abstracts_in_pod}{$_} || ''};
              }
              if (@errors) {
                  $d->{error}{no_abstract_stub_in_pod} = join ',', @errors;
              }
              return @errors ? 0 : 1;
          },
          details => sub {
              my $d = shift;
              my %mapping = map {$_ => 1} @ABSTRACT_STUBS;
              return "Abstracts in the following packages are stubs:". $d->{error}{no_abstract_stub_in_pod};
          },
      },
    ];
}


q{Favourite record of the moment:
  Fat Freddys Drop: Based on a true story};

__END__

=encoding UTF-8

=head1 NAME

Module::CPANTS::Kwalitee::Pod - Check Pod

=head1 SYNOPSIS

Some of the check in this module has moved to L<Module::CPANTS::SiteKwalitee::Pod|https://github.com/cpants/Module-CPANTS-SiteKwalitee> to double-check the pod correctness on the server side.

If you do care, it is recommended to add a test to test pod (with L<Test::Pod>) in "xt/" directory in your distribution.

=head1 DESCRIPTION

=head2 Methods

=head3 order

Defines the order in which Kwalitee tests should be run.

Returns C<100>.

=head3 analyse

Parses pod to see if it has a proper abstract.

=head3 kwalitee_indicators

Returns the Kwalitee Indicators data structure.

=over 4

=item * has_abstract_in_pod

=item * no_abstract_stub_in_pod

=back

=head1 SEE ALSO

L<Module::CPANTS::Analyse>

=head1 AUTHOR

L<Thomas Klausner|https://metacpan.org/author/domm>

=head1 COPYRIGHT AND LICENSE

Copyright © 2003–2006, 2009 L<Thomas Klausner|https://metacpan.org/author/domm>

You may use and distribute this module according to the same terms
that Perl is distributed under.