Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 399 lines (282 sloc) 9.598 kb
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
# Copyright (C) 2004-2012, Parrot Foundation.

=head1 NAME

Parrot::Docs::File - Docs-Related File

=head1 SYNOPSIS

use Parrot::Docs::File;
my $file = Parrot::Docs::File->new('MANIFEST');

=head1 DESCRIPTION

This C<Parrot::IO::File> subclass adds a few documentation-related
methods to do with POD and file type.

It's used by the documentation tools in F<tools/docs>.

=head2 Class Methods

=over 4

=cut

package Parrot::Docs::File;

use strict;
use warnings;

use base qw( Parrot::IO::File );

use Pod::Simple::Checker;
use Parrot::Docs::POD2HTML;

# wrapped here to reset the error timestamp to speed up check_pod()
sub write {
    my $self = shift;
    $self->{POD_ERRORS_TIME} = 0;
    return $self->SUPER::write(@_);
}

my $UNDEFINED = 'Undefined';

# These are the Parrot file types excluding the ICU specifc ones.

my %type_for_suffix = (
    'BASIC' => 'README file',
    'C' => 'C code',
    'PL' => 'Perl script',
    'SKIP' => 'MANIFEST skip file',
    'TXT' => 'Text file',
    'txt' => 'Text file',
    'a' => 'Library file',
    'bas' => 'BACIC code',
    'bef' => 'Befunge code',
    'bf' => 'BF code',
    'bnf' => 'Grammar file',
    'c' => 'C code',
    'cs' => 'C# code',
    'declarations' => 'Library declarations file',
    'def' => 'Library definitions file',
    'dev' => 'Development documentation',
    'dump' => 'Dump file',
    'el' => 'eMacs Lisp code',
    'exp' => 'Perl 6 expected parse tree',
    'flag' => 'Some kind of IMCC file',
    'generated' => 'MANIFEST generated file',
    'h' => 'C header',
    'hacking' => 'README file',
    'il' => 'MSIL assembler code',
    'imc' => 'IMC code',
    'in' => 'Configuration file',
    'jako' => 'Jako code',
    'jit' => 'JIT file',
    'l' => 'Lex file',
    'num' => 'Opcode numbering file',
    'o' => 'Compiled file',
    'ook' => 'Ook! code',
    'ops' => 'Parrot opcode file',
    'p6' => 'Perl 6 code',
    'pasm' => 'Parrot assembly code',
    'pbc' => 'Parrot bytecode',
    'pl' => 'Perl script',
    'pm' => 'Perl module',
    'pmc' => 'PMC code',
    'pod' => 'POD documentation',
    'prd' => 'Parse::RecDescent grammar file',
    'ps' => 'Postscript code',
    'py' => 'Python code',
    'rb' => 'Ruby code',
    's' => 'Some kind of configuration file',
    'scheme' => 'Scheme code',
    'sh' => 'Shell script',
    'spec' => 'RPM build specification',
    't' => 'Test file',
    'tbl' => 'Vtable file',
    'txt' => 'Text file',
    'urm' => 'URM code',
    'vim' => 'Vim file',
    'xml' => 'XML file',
    'xs' => 'XS code',
    'y' => 'Yacc file'
);

# These are the various types of files without suffix.

my %type_for_name = (
    'Artistic' => 'Licence file',
    'BUGS' => 'Project info',
    'ChangeLog' => 'Project info',
    'Changes' => 'Project info',
    'CREDITS' => 'Project info',
    'DEVELOPING' => 'Project info',
    'harness' => 'Perl test harness',
    'INSTALL' => 'Installation documentation',
    'LICENSE' => 'Licence file',
    'MAINTAINER' => 'Maintainer info',
    'Makefile' => 'Makefile',
    'MANIFEST' => 'Manifest file',
    'PBC_COMPAT' => 'Bytecode compatibility file',
    'PLATFORMS' => 'Project info',
    'README' => 'README file',
    'RELEASE_INSTRUCTIONS' => 'Project info',
    'RESPONSIBLE_PARTIES' => 'Project info',
    'TODO' => 'TODO file',
    'VERSION' => 'Project info',
);

=item C<type_for_suffix($suffix)>

This is a class method that converts a file suffix to a description of
the type of files which have this suffix.

=cut

sub type_for_suffix {
    my $self = shift;
    my $suffix = shift;

    return $type_for_suffix{$suffix} if exists $type_for_suffix{$suffix};

    return $UNDEFINED;
}

=item C<type_for_name($name)>

This is a class method that converts a file name to a description of the
type of files which have this name.

=cut

sub type_for_name {
    my $self = shift;
    my $name = shift;

    return $type_for_name{$name} if exists $type_for_name{$name};

    return $UNDEFINED;
}

=back

=head2 Instance Methods

=over 4

=item C<type()>

This first tries to find a type for the file's suffix, failing that it
looks at the file's name. If it fails for both it returns 'Undefined'.

=cut

sub type {
    my $self = shift;
    my $type = $self->type_for_suffix( $self->suffix );

    return $type unless $type eq $UNDEFINED;

    $type = $self->type_for_name( $self->name );

    return $type unless $type eq $UNDEFINED;

    return 'Executable' if $self->is_executable;

    return $UNDEFINED;
}

=item C<is_of_type($type)>

Returns whether the file is of the specified type.

=cut

sub is_of_type {
    my $self = shift;
    my $type = shift;

    return 0 unless defined $type;

    return $self->type eq $type;
}

=item C<check_pod()>

Runs C<Pod::Simple::Checker> on the contents of the file. Executable
files, and F<*.dump> files are assumed not to contain POD and therefore
not checked.

Note that the results are cached and the POD will only be rechecked
if the file has been modified since it was checked.

=cut

sub check_pod {
    my $self = shift;

    return
        if ( $self->is_executable and $self->name ne 'parrotbug' )
        or $self->suffix eq 'dump';

    if ( !exists $self->{POD_ERRORS_TIME}
        or $self->modified_since( $self->{POD_ERRORS_TIME} ) )
    {
        my $checker = Pod::Simple::Checker->new;

        $self->{POD_ERRORS_TIME} = time;
        $self->{POD_ERRORS} = '';
        $checker->output_string( \$self->{POD_ERRORS} );
        $checker->parse_file( $self->path );
        $self->{POD_ERRORS_COUNT} = $checker->errors_seen;
        $self->{CONTAINS_POD} = $checker->content_seen;
    }
}

=item C<contains_pod()>

Tells you whether there is any POD formatted documentation in the file.
Executable files are assumed not to contain POD.

=cut

sub contains_pod {
    my $self = shift;

    $self->check_pod;

    return $self->{CONTAINS_POD};
}

=item C<num_pod_errors()>

Tells you the number of POD errors in the file.

=cut

sub num_pod_errors {
    my $self = shift;

    $self->check_pod;

    return $self->{POD_ERRORS_COUNT} || 0;
}

=item C<pod_errors($options)>

Gives you a description of any POD errors in the file.

=cut

sub pod_errors {
    my $self = shift;

    $self->check_pod;

    return $self->{POD_ERRORS};
}

=item C<is_docs_link()>

Returns whether the file is suitable for inclusion in a documentation link.

If a file contains plain text rather than POD it may be directly linked to.

=cut

sub is_docs_link {
    my $self = shift;

    # GH #626 - This needs more thought. I'm trying to work out which files
    # it's sensible to link directly to. Suffixes other than txt are a
    # problem (for me at least) because the browser thinks it should
    # download the file.

    if ( $self->has_suffix ) {
        return 0 if $self->suffix !~ m/txt/i;
    }
    else {
        return 1 if $self->name =~ m/^[[:upper:]]+$/;
    }

    return $self->type =~ /Licence|info|docu|Text|TODO|status|MANIFEST|README/;
}

=item C<title()>

Returns the title of the file.

=cut

sub title {
    my $self = shift;

    return $self->name unless $self->contains_pod;

    my $text = $self->read;

    return ''
        unless $text =~ /^=head1\s+([^\n\r]+)\s*[\n\r]+/smo;

    return $1
        if ($1 ne 'NAME' and $1 ne 'TITLE');

    return ''
        unless $text =~ /^=head1\s+(?:NAME|TITLE)\s*[\n\r]+([^\n\r]+)/smo;

    $text = $1;

    # Tidy it up a bit.
    $text =~ s/^\s+//;
    $text =~ s/\s+$//;
    $text =~ s/\s*-$//;

    # There was not text, just another POD command (=head2 probably).
    return '' if $text =~ /^=\w/;

    return $text unless $text =~ /-/;

    # There has to be some space each side of the dash.
    my ( $path, $desc ) = split /\s+--?\s+/, $text, 2;

    return $desc;
}

=item C<short_description()>

Returns a short description of the file extracted from the C<NAME> section
of the POD documentation, if it exists. If an C<ABSTRACT> is found then
that is preferred.

=cut

sub short_description {
    my $self = shift;

    return '' unless $self->contains_pod;

    my @lines = $self->read;
    my $firstline = shift @lines;
    return $self->title unless $firstline =~ /^=head1\s+ABSTRACT/;

    my $all_text = join "\n" => @lines;
    $all_text =~ s/^\s+//;
    my @paragraphs = split /\n{2,}/, $all_text;
    my $desc;
    # For a short description, we take only the first paragraph of any
    # ABSTRACT.
    ($desc = $paragraphs[0]) =~ s/\n/ /g;
    $desc =~ s/\s+/ /sg;
    # We eliminate certain POD formatting characters.
    $desc =~ s/[CFL]<([^>]+)>/$1/sg;
    return $desc;
}

=back

=head1 SEE ALSO

=over 4

=item C<Parrot::Docs::Directory>

=item C<Parrot::Docs::POD2HTML>

=item C<Pod::Simple::Checker>

=back

=cut

1;

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
Something went wrong with that request. Please try again.