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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
|
package Perl::Critic::Utils::POD;
use 5.010001;
use strict;
use warnings;
use English qw< -no_match_vars >;
use Pod::PlainText ();
use Pod::Select ();
# TODO: non-fatal generic?
use Perl::Critic::Exception::Fatal::Generic qw< throw_generic >;
use Perl::Critic::Exception::IO qw< throw_io >;
use Perl::Critic::Utils qw< :characters >;
use Exporter 'import';
our $VERSION = '1.156';
#-----------------------------------------------------------------------------
our @EXPORT_OK = qw(
get_pod_file_for_module
get_raw_pod_section_from_file
get_raw_pod_section_from_filehandle
get_raw_pod_section_from_string
get_raw_pod_section_for_module
get_pod_section_from_file
get_pod_section_from_filehandle
get_pod_section_from_string
get_pod_section_for_module
trim_raw_pod_section
trim_pod_section
get_raw_module_abstract_from_file
get_raw_module_abstract_from_filehandle
get_raw_module_abstract_from_string
get_raw_module_abstract_for_module
get_module_abstract_from_file
get_module_abstract_from_filehandle
get_module_abstract_from_string
get_module_abstract_for_module
);
our %EXPORT_TAGS = (
all => \@EXPORT_OK,
);
#-----------------------------------------------------------------------------
sub get_pod_file_for_module {
my ($module_name) = @_;
# No File::Spec: %INC always uses forward slashes.
(my $relative_path = $module_name) =~ s< :: ></>xmsg;
$relative_path .= '.pm';
my $absolute_path = $INC{$relative_path} or return;
(my $pod_path = $absolute_path) =~ s< [.] [^.]+ \z><.pod>xms;
return $pod_path if -f $pod_path;
return $absolute_path;
}
#-----------------------------------------------------------------------------
sub get_raw_pod_section_from_file {
my ($file_name, $section_name) = @_;
return _get_pod_section_from_file(
$file_name,
$section_name,
Pod::Select->new(),
);
}
#-----------------------------------------------------------------------------
sub get_raw_pod_section_from_filehandle {
my ($file_handle, $section_name) = @_;
return _get_pod_section_from_filehandle(
$file_handle,
$section_name,
Pod::Select->new(),
);
}
#-----------------------------------------------------------------------------
sub get_raw_pod_section_from_string {
my ($source, $section_name) = @_;
return _get_pod_section_from_string(
$source,
$section_name,
Pod::Select->new(),
);
}
#-----------------------------------------------------------------------------
sub get_raw_pod_section_for_module {
my ($module_name, $section_name) = @_;
my $file_name = get_pod_file_for_module($module_name)
or throw_generic qq<Could not find POD for "$module_name".>;
return get_raw_pod_section_from_file($file_name, $section_name);
}
#-----------------------------------------------------------------------------
sub get_pod_section_from_file {
my ($file_name, $section_name) = @_;
return _get_pod_section_from_file(
$file_name,
$section_name,
Pod::PlainText->new(),
);
}
#-----------------------------------------------------------------------------
sub get_pod_section_from_filehandle {
my ($file_handle, $section_name) = @_;
return _get_pod_section_from_filehandle(
$file_handle,
$section_name,
Pod::PlainText->new(),
);
}
#-----------------------------------------------------------------------------
sub get_pod_section_from_string {
my ($source, $section_name) = @_;
return _get_pod_section_from_string(
$source,
$section_name,
Pod::PlainText->new(),
);
}
#-----------------------------------------------------------------------------
sub get_pod_section_for_module {
my ($module_name, $section_name) = @_;
my $file_name = get_pod_file_for_module($module_name)
or throw_generic qq<Could not find POD for "$module_name".>;
return get_pod_section_from_file($file_name, $section_name);
}
#-----------------------------------------------------------------------------
sub _get_pod_section_from_file {
my ($file_name, $section_name, $parser) = @_;
open my $file_handle, '<', $file_name
or throw_io
message => qq<Could not open "$file_name": $ERRNO>,
file_name => $file_name,
errno => $ERRNO;
my $content =
_get_pod_section_from_filehandle(
$file_handle, $section_name, $parser,
);
close $file_handle
or throw_io
message => qq<Could not close "$file_name": $ERRNO>,
file_name => $file_name,
errno => $ERRNO;
return $content;
}
#-----------------------------------------------------------------------------
sub _get_pod_section_from_filehandle {
my ($file_handle, $section_name, $parser) = @_;
$parser->select($section_name);
my $content = $EMPTY;
open my $content_handle, '>', \$content
or throw_generic "error opening scalar: $OS_ERROR";
$parser->parse_from_filehandle( $file_handle, $content_handle );
close $content_handle or throw_generic "Failed to close in memory file: $OS_ERROR";
return if $content eq $EMPTY;
return $content;
}
#-----------------------------------------------------------------------------
sub _get_pod_section_from_string {
my ($source, $section_name, $parser) = @_;
open my $source_handle, '<', \$source
or throw_generic "error opening scalar: $OS_ERROR";
my $content = _get_pod_section_from_filehandle(
$source_handle, $section_name, $parser,
);
close $source_handle or throw_generic "Failed to close in memory file: $OS_ERROR";
return $content;
}
#-----------------------------------------------------------------------------
sub trim_raw_pod_section {
my ($pod) = @_;
return if not defined $pod;
$pod =~ s< \A =head1 \b [^\n]* \n $ ><>xms;
$pod =~ s< \A \s+ ><>xms;
$pod =~ s< \s+ \z ><>xms;
return $pod;
}
#-----------------------------------------------------------------------------
sub trim_pod_section {
my ($pod) = @_;
return if not defined $pod;
$pod =~ s< \A [^\n]* \n ><>xms;
$pod =~ s< \A \s* \n ><>xms;
$pod =~ s< \s+ \z ><>xms;
return $pod;
}
#-----------------------------------------------------------------------------
sub get_raw_module_abstract_from_file {
my ($file_name) = @_;
return
_get_module_abstract_from_file(
$file_name,
Pod::Select->new(),
\&trim_raw_pod_section,
);
}
#-----------------------------------------------------------------------------
sub get_raw_module_abstract_from_filehandle {
my ($file_handle) = @_;
return
_get_module_abstract_from_filehandle(
$file_handle,
Pod::Select->new(),
\&trim_raw_pod_section,
);
}
#-----------------------------------------------------------------------------
sub get_raw_module_abstract_from_string {
my ($source) = @_;
return
_get_module_abstract_from_string(
$source,
Pod::Select->new(),
\&trim_raw_pod_section,
);
}
#-----------------------------------------------------------------------------
sub get_raw_module_abstract_for_module {
my ($module_name) = @_;
my $file_name = get_pod_file_for_module($module_name)
or throw_generic qq<Could not find POD for "$module_name".>;
return get_raw_module_abstract_from_file($file_name);
}
#-----------------------------------------------------------------------------
sub get_module_abstract_from_file {
my ($file_name) = @_;
return
_get_module_abstract_from_file(
$file_name,
Pod::PlainText->new(),
\&trim_pod_section,
);
}
#-----------------------------------------------------------------------------
sub get_module_abstract_from_filehandle {
my ($file_handle) = @_;
return
_get_module_abstract_from_filehandle(
$file_handle,
Pod::PlainText->new(),
\&trim_pod_section,
);
}
#-----------------------------------------------------------------------------
sub get_module_abstract_from_string {
my ($source) = @_;
return
_get_module_abstract_from_string(
$source,
Pod::PlainText->new(),
\&trim_pod_section,
);
}
#-----------------------------------------------------------------------------
sub get_module_abstract_for_module {
my ($module_name) = @_;
my $file_name = get_pod_file_for_module($module_name)
or throw_generic qq<Could not find POD for "$module_name".>;
return get_module_abstract_from_file($file_name);
}
#-----------------------------------------------------------------------------
sub _get_module_abstract_from_file {
my ($file_name, $parser, $trimmer) = @_;
open my $file_handle, '<', $file_name
or throw_io
message => qq<Could not open "$file_name": $ERRNO>,
file_name => $file_name,
errno => $ERRNO;
my $module_abstract =
_get_module_abstract_from_filehandle(
$file_handle, $parser, $trimmer,
);
close $file_handle
or throw_io
message => qq<Could not close "$file_name": $ERRNO>,
file_name => $file_name,
errno => $ERRNO;
return $module_abstract;
}
#-----------------------------------------------------------------------------
sub _get_module_abstract_from_filehandle { ## no critic (RequireFinalReturn)
my ($file_handle, $parser, $trimmer) = @_;
my $name_section =
_get_pod_section_from_filehandle( $file_handle, 'NAME', $parser );
return if not $name_section;
$name_section = $trimmer->($name_section);
return if not $name_section;
# Testing for parser class, blech. But it's a lot simpler and it's all
# hidden in the implementation.
if ('Pod::Select' eq ref $parser) {
if ( $name_section =~ m< \n >xms ) {
throw_generic
qq<Malformed NAME section in "$name_section". >
. q<It must be on a single line>;
}
}
else {
$name_section =~ s< \s+ >< >xmsg;
# Ugh. Pod::PlainText splits up module names.
if (
$name_section =~ m<
\A
\s*
(
\w [ \w:]+ \w
)
(
\s*
-
.*
)?
\z
>xms
) {
my ($module_name, $rest) = ($1, $2);
$module_name =~ s/ [ ] //xms;
$name_section = $module_name . ( $rest ? $rest : $EMPTY );
}
}
if (
$name_section =~ m<
\A
\s*
[\w:]+ # Module name.
\s+
- # The required single hyphen.
\s+
(
\S # At least one non-whitespace.
(?: .* \S)? # Everything up to the last non-whitespace.
)
\s*
\z
>xms
) {
my $module_abstract = $1;
return $module_abstract;
}
if (
$name_section =~ m<
\A
\s*
[\w:]+ # Module name.
(?: \s* - )? # The single hyphen is now optional.
\s*
\z
>xms
) {
return;
}
throw_generic qq<Malformed NAME section in "$name_section".>;
}
#-----------------------------------------------------------------------------
sub _get_module_abstract_from_string {
my ($source, $parser, $trimmer) = @_;
open my $source_handle, '<', \$source
or throw_generic "error opening scalar: $OS_ERROR";
my $module_abstract = _get_module_abstract_from_filehandle(
$source_handle, $parser, $trimmer,
);
close $source_handle or throw_generic "Failed to close in memory file: $OS_ERROR";
return $module_abstract;
}
#-----------------------------------------------------------------------------
1;
__END__
#-----------------------------------------------------------------------------
=pod
=for stopwords
=head1 NAME
Perl::Critic::Utils::POD - Utility functions for dealing with POD.
=head1 SYNOPSIS
use Perl::Critic::Utils::POD qw< get_pod_section_from_file >;
my $synopsis =
get_pod_section_from_file('Perl/Critic/Utils/POD.pm', 'SYNOPSIS');
my $see_also =
get_pod_section_from_filehandle($file_handle, 'SEE ALSO');
my $see_also_content = trim_pod_section($see_also);
# "Utility functions for dealing with POD."
my $module_abstract =
get_module_abstract_from_file('Perl/Critic/Utils/POD.pm');
my $module_abstract =
get_module_abstract_from_filehandle($file_handle);
=head1 DESCRIPTION
Provides means of accessing chunks of POD.
=head1 INTERFACE SUPPORT
This is considered to be a public module. Any changes to its
interface will go through a deprecation cycle.
=head1 IMPORTABLE SUBROUTINES
=over
=item C<get_pod_file_for_module( $module_name )>
Figure out where to find the POD for the parameter.
This depends upon the module already being loaded; it will not find
the path for arbitrary modules.
If there is a file with a ".pod" extension next to the real module
location, it will be returned in preference to the actual module.
=item C<get_raw_pod_section_from_file( $file_name, $section_name )>
Retrieves the specified section of POD (i.e. something marked by
C<=head1>) from the file. This is uninterpreted; escapes are not
processed and any sub-sections will be present. E.g. if the content
contains "CZ<><$x>", the return value will contain "CZ<><$x>".
Returns nothing if no such section is found.
Throws a L<Perl::Critic::Exception::IO|Perl::Critic::Exception::IO> if
there's a problem with the file.
=item C<get_raw_pod_section_from_filehandle( $file_handle, $section_name )>
Does the same as C<get_raw_pod_section_from_file()>, but with a file
handle.
=item C<get_raw_pod_section_from_string( $source, $section_name )>
Does the same as C<get_raw_pod_section_from_file()>, but with a string
that contains the raw POD.
=item C<get_raw_pod_section_for_module( $module_name, $section_name )>
Does the same as C<get_raw_pod_section_from_file()>, but with a module
name.
Throws a
L<Perl::Critic::Exception::Generic|Perl::Critic::Exception::Generic>
if a file containing POD for the module can't be found.
=item C<get_pod_section_from_file( $file_name, $section_name )>
Retrieves the specified section of POD (i.e. something marked by
C<=head1>) from the file. This is interpreted into plain text.
Returns nothing if no such section is found.
Throws a L<Perl::Critic::Exception::IO|Perl::Critic::Exception::IO> if
there's a problem with the file.
=item C<get_pod_section_from_filehandle( $file_handle, $section_name )>
Does the same as C<get_pod_section_from_file()>, but with a file
handle.
=item C<get_pod_section_from_string( $source, $section_name )>
Does the same as C<get_pod_section_from_file()>, but with a string
that contains the raw POD.
=item C<get_pod_section_for_module( $module_name, $section_name )>
Does the same as C<get_pod_section_from_file()>, but with a module
name.
Throws a
L<Perl::Critic::Exception::Generic|Perl::Critic::Exception::Generic>
if a file containing POD for the module can't be found.
=item C<trim_raw_pod_section( $pod_section )>
Returns a copy of the parameter, with any starting C<=item1 BLAH>
removed and all leading and trailing whitespace (including newlines)
removed after that.
For example, using one of the C<get_raw_pod_section_from_*> functions
to get the "NAME" section of this module and then calling
C<trim_raw_pod_section()> on the result would give you
"Perl::Critic::Utils::POD - Utility functions for dealing with POD.".
=item C<trim_pod_section( $pod_section )>
Returns a copy of the parameter, with any starting line removed and
leading blank lines and trailing whitespace (including newlines)
removed after that. Note that only leading whitespace on the first
real line of the section will remain.
Since this cannot count upon a C<=item1> marker, this is much less
reliable than C<trim_raw_pod_section()>.
=item C<get_raw_module_abstract_from_file( $file_name )>
Attempts to parse the "NAME" section of the specified file and get the
abstract of the module from that. If it succeeds, it returns the
abstract. If it fails, either because there is no "NAME" section or
there is no abstract after the module name, returns nothing. If it
looks like there's a malformed abstract, throws a
L<Perl::Critic::Exception::Fatal::Generic|Perl::Critic::Exception::Fatal::Generic>.
Example "well formed" "NAME" sections without abstracts:
Some::Module
Some::Other::Module -
Example "NAME" sections that will result in an exception:
Some::Bad::Module This has no hyphen.
Some::Mean::Module -- This has double hyphens.
Some::Nasty::Module - This one attempts to
span multiple lines.
=item C<get_raw_module_abstract_from_filehandle( $file_handle )>
Does the same as C<get_raw_module_abstract_from_file()>, but with a
file handle.
=item C<get_raw_module_abstract_from_string( $source )>
Does the same as C<get_raw_module_abstract_from_file()>, but with a
string that contains the raw POD.
=item C<get_raw_module_abstract_for_module( $module_name )>
Does the same as C<get_raw_module_abstract_from_file()>, but for a
module name.
=item C<get_module_abstract_from_file( $file_name )>
Does the same as C<get_raw_module_abstract_from_file()>, but with
escapes interpreted.
=item C<get_module_abstract_from_filehandle( $file_handle )>
Does the same as C<get_module_abstract_from_file()>, but with a file
handle.
=item C<get_module_abstract_from_string( $source )>
Does the same as C<get_module_abstract_from_file()>, but with a string
that contains the raw POD.
=item C<get_module_abstract_for_module( $module_name )>
Does the same as C<get_module_abstract_from_file()>, but for a module
name.
=back
=head1 AUTHOR
Elliot Shank <perl@galumph.com>
=head1 COPYRIGHT
Copyright (c) 2008-2011 Elliot Shank.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. The full text of this license
can be found in the LICENSE file included with this module.
=cut
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 78
# indent-tabs-mode: nil
# c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
|