Checker.pm revision 7c478bd95313f5f23a4c958a745db2134aa03244
#############################################################################
# Pod/Checker.pm -- check pod documents for syntax errors
#
# Copyright (C) 1994-2000 by Bradford Appleton. All rights reserved.
# This file is part of "PodParser". PodParser is free software;
# as Perl itself.
#############################################################################
use vars qw($VERSION);
require 5.005; ## requires this Perl version or later
=head1 NAME
Pod::Checker, podchecker() - check pod documents for syntax errors
=head1 SYNOPSIS
use Pod::Checker;
$syntax_okay = podchecker($filepath, $outputpath, %options);
my $checker = new Pod::Checker %options;
$checker->parse_from_file($filepath, \*STDERR);
C<$filepath> is the input POD to read and C<$outputpath> is
where to write POD syntax error messages. Either argument may be a scalar
indicating a file-path, or else a reference to an open filehandle.
If unspecified, the input-file it defaults to C<\*STDIN>, and
the output-file defaults to C<\*STDERR>.
=head2 podchecker()
This function can take a hash of options:
=over 4
=item B<-warnings> =E<gt> I<val>
trigger additional warnings. See L<"Warnings">.
=back
=head1 DESCRIPTION
B<podchecker> will perform syntax checking of Perl5 POD format documentation.
I<NOTE THAT THIS MODULE IS CURRENTLY IN THE BETA STAGE!>
additional features they wish to see in B<Pod::Checker> and B<podchecker>
and verify that the checks are consistent with L<perlpod>.
The following checks are currently preformed:
=over 4
=item *
Unknown '=xxxx' commands, unknown 'XE<lt>...E<gt>' interior-sequences,
and unterminated interior sequences.
=item *
Check for proper balancing of C<=begin> and C<=end>. The contents of such
a block are generally ignored, i.e. no syntax checks are performed.
=item *
Check for proper nesting and balancing of C<=over>, C<=item> and C<=back>.
=item *
Check for same nested interior-sequences (e.g.
C<LE<lt>...LE<lt>...E<gt>...E<gt>>).
=item *
Check for malformed or nonexisting entities C<EE<lt>...E<gt>>.
=item *
Check for correct syntax of hyperlinks C<LE<lt>...E<gt>>. See L<perlpod>
for details.
=item *
Check for unresolved document-internal links. This check may also reveal
misspelled links that seem to be internal links but should be links
to something else.
=back
=head1 DIAGNOSTICS
=head2 Errors
=over 4
=item * empty =headn
A heading (C<=head1> or C<=head2>) without any text? That ain't no
heading!
=item * =over on line I<N> without closing =back
The C<=over> command does not have a corresponding C<=back> before the
next heading (C<=head1> or C<=head2>) or the end of the file.
=item * =item without previous =over
=item * =back without previous =over
An C<=item> or C<=back> command has been found outside a
C<=over>/C<=back> block.
=item * No argument for =begin
A C<=begin> command was found that is not followed by the formatter
specification.
=item * =end without =begin
A standalone C<=end> command was found.
=item * Nested =begin's
There were at least two consecutive C<=begin> commands without
the corresponding C<=end>. Only one C<=begin> may be active at
a time.
=item * =for without formatter specification
There is no specification of the formatter after the C<=for> command.
=item * unresolved internal link I<NAME>
The given link to I<NAME> does not have a matching node in the current
POD. This also happend when a single word node name is not enclosed in
C<"">.
=item * Unknown command "I<CMD>"
An invalid POD command has been found. Valid are C<=head1>, C<=head2>,
C<=over>, C<=item>, C<=back>, C<=begin>, C<=end>, C<=for>, C<=pod>,
C<=cut>
=item * Unknown interior-sequence "I<SEQ>"
An invalid markup command has been encountered. Valid are:
C<BE<lt>E<gt>>, C<CE<lt>E<gt>>, C<EE<lt>E<gt>>, C<FE<lt>E<gt>>,
C<IE<lt>E<gt>>, C<LE<lt>E<gt>>, C<SE<lt>E<gt>>, C<XE<lt>E<gt>>,
C<ZE<lt>E<gt>>
=item * nested commands I<CMD>E<lt>...I<CMD>E<lt>...E<gt>...E<gt>
Two nested identical markup commands have been found. Generally this
does not make sense.
=item * garbled entity I<STRING>
The I<STRING> found cannot be interpreted as a character entity.
=item * Entity number out of range
An entity specified by number (dec, hex, oct) is out of range (1-255).
=item * malformed link LE<lt>E<gt>
The link found cannot be parsed because it does not conform to the
syntax described in L<perlpod>.
=item * nonempty ZE<lt>E<gt>
The C<ZE<lt>E<gt>> sequence is supposed to be empty.
=item * empty XE<lt>E<gt>
The index entry specified contains nothing but whitespace.
=item * Spurious text after =pod / =cut
The commands C<=pod> and C<=cut> do not take any arguments.
=item * Spurious character(s) after =back
The C<=back> command does not take any arguments.
=back
=head2 Warnings
These may not necessarily cause trouble, but indicate mediocre style.
=over 4
=item * multiple occurence of link target I<name>
the same text. Potential hyperlinks to such a text cannot be unique then.
=item * line containing nothing but whitespace in paragraph
There is some whitespace on a seemingly empty line. POD is very sensitive
to such things, so this is flagged. B<vi> users switch on the B<list>
option to avoid this problem.
=begin _disabled_
=item * file does not start with =head
The file starts with a different POD directive than head.
This is most probably something you do not want.
=end _disabled_
=item * previous =item has no contents
There is a list C<=item> right above the flagged line that has no
text contents. You probably want to delete empty items.
=item * preceding non-item paragraph(s)
A list introduced by C<=over> starts with a text or verbatim paragraph,
but continues with C<=item>s. Move the non-item paragraph out of the
C<=over>/C<=back> block.
=item * =item type mismatch (I<one> vs. I<two>)
A list started with e.g. a bulletted C<=item> and continued with a
numbered one. This is obviously inconsistent. For most translators the
type of the I<first> C<=item> determines the type of the list.
=item * I<N> unescaped C<E<lt>E<gt>> in paragraph
Angle brackets not written as C<E<lt>ltE<gt>> and C<E<lt>gtE<gt>>
can potentially cause errors as they could be misinterpreted as
markup commands. This is only printed when the -warnings level is
greater than 1.
=item * Unknown entity
A character entity was found that does not belong to the standard
ISO set or the POD specials C<verbar> and C<sol>.
=item * No items in =over
The list opened with C<=over> does not contain any items.
=item * No argument for =item
C<=item> without any parameters is deprecated. It should either be followed
by C<*> to indicate an unordered list, by a number (optionally followed
by a dot) to indicate an ordered (numbered) list or simple text for a
definition list.
=item * empty section in previous paragraph
The previous section (introduced by a C<=head> command) does not contain
any text. This usually indicates that something is missing. Note: A
C<=head1> followed immediately by C<=head2> does not trigger this warning.
=item * Verbatim paragraph in NAME section
The NAME section (C<=head1 NAME>) should consist of a single paragraph
description of what the thing is good for.
=back
=head2 Hyperlinks
There are some warnings wrt. malformed hyperlinks.
=over 4
There is whitespace at the beginning or the end of the contents of
LE<lt>...E<gt>.
=item * (section) in '$page' deprecated
There is a section detected in the page name of LE<lt>...E<gt>, e.g.
C<LE<gt>passwd(2)E<gt>>. POD hyperlinks may point to POD documents only.
Please write C<CE<lt>passwd(2)E<gt>> instead. Some formatters are able
to expand this to appropriate code. For links to (builtin) functions,
The characters C<|> and C</> are special in the LE<lt>...E<gt> context.
Although the hyperlink parser does its best to determine which "/" is
text and which is a delimiter in case of doubt, one ought to escape
these literal characters like this:
/ E<sol>
| E<verbar>
=back
=head1 RETURN VALUE
B<podchecker> returns the number of POD syntax errors found or -1 if
there were no POD commands at all found in the file.
=head1 EXAMPLES
I<[T.B.D.]>
=head1 INTERFACE
While checking, this module collects document properties, e.g. the nodes
for hyperlinks (C<=headX>, C<=item>) and index entries (C<XE<lt>E<gt>>).
POD translators can use this feature to syntax-check and get the nodes in
a first pass before actually starting to convert. This is expensive in terms
of execution time, but allows for very robust conversions.
=cut
#############################################################################
use strict;
#use diagnostics;
use Carp;
use Exporter;
@EXPORT = qw(&podchecker);
use vars qw(%VALID_COMMANDS %VALID_SEQUENCES);
my %VALID_COMMANDS = (
'pod' => 1,
'cut' => 1,
'head1' => 1,
'head2' => 1,
'over' => 1,
'back' => 1,
'item' => 1,
'for' => 1,
'begin' => 1,
'end' => 1,
);
my %VALID_SEQUENCES = (
'I' => 1,
'B' => 1,
'S' => 1,
'C' => 1,
'L' => 1,
'F' => 1,
'X' => 1,
'Z' => 1,
'E' => 1,
);
# stolen from HTML::Entities
my %ENTITIES = (
# Some normal chars that have special meaning in SGML context
'gt' => '>', # greater than
'lt' => '<', # less than
# PUBLIC ISO 8879-1986//ENTITIES Added Latin 1//EN//HTML
# Some extra Latin 1 chars that are listed in the HTML3.2 draft (21-May-96)
# Additional ISO-8859/1 entities listed in rfc1866 (section 14)
iexcl => '�',
cent => '�',
pound => '�',
curren => '�',
yen => '�',
brvbar => '�',
sect => '�',
uml => '�',
ordf => '�',
laquo => '�',
'not' => '�', # not is a keyword in perl
shy => '�',
macr => '�',
deg => '�',
plusmn => '�',
sup1 => '�',
sup2 => '�',
sup3 => '�',
acute => '�',
micro => '�',
para => '�',
middot => '�',
cedil => '�',
ordm => '�',
raquo => '�',
frac14 => '�',
frac12 => '�',
frac34 => '�',
iquest => '�',
'times' => '�', # times is a keyword in perl
divide => '�',
# some POD special entities
verbar => '|',
sol => '/'
);
##---------------------------------------------------------------------------
##---------------------------------
## Function definitions begin here
##---------------------------------
sub podchecker( $ ; $ % ) {
local $_;
## Set defaults
## Now create a pod checker
## Now check the pod document for errors
## Return the number of errors found
return $checker->num_errors();
}
##---------------------------------------------------------------------------
##-------------------------------
## Method definitions begin here
##-------------------------------
##################################
=over 4
=item C<Pod::Checker-E<gt>new( %options )>
Return a reference to a new Pod::Checker object that inherits from
Pod::Parser and is used for calling the required methods later. The
following options are recognized:
C<-warnings =E<gt> num>
Print warnings if C<num> is true. The higher the value of C<num>,
the more warnings are printed. Currently there are only levels 1 and 2.
C<-quiet =E<gt> num>
when Pod::Checker is used to munge POD code into plain text from within
POD formatters.
=cut
## sub new {
## my $this = shift;
## my $class = ref($this) || $this;
## my %params = @_;
## my $self = {%params};
## bless $self, $class;
## $self->initialize();
## return $self;
## }
sub initialize {
my $self = shift;
## Initialize number of errors, and setup an error function to
## increment this number and then print to the designated output.
# set the error handling subroutine
# print warnings?
}
##################################
=item C<$checker-E<gt>poderror( @args )>
=item C<$checker-E<gt>poderror( {%opts}, @args )>
Internal method for printing errors and warnings. If no options are
given, simply prints "@_". The following options are recognized and used
to form the output:
-msg
A message to print prior to C<@args>.
-line
The line number the error occurred in.
-file
The file (name) the error occurred in.
-severity
The error level, should be 'WARNING' or 'ERROR'.
=cut
# Invoked as $self->poderror( @args ), or $self->poderror( {%opts}, @args )
sub poderror {
my $self = shift;
my %opts = (ref $_[0]) ? %{shift()} : ();
## Retrieve options
## See if can find severity in message prefix
}
## Increment error count and print message "
++($self->{_NUM_ERRORS})
}
##################################
=item C<$checker-E<gt>num_errors()>
Set (if argument specified) and retrieve the number of errors found.
=cut
sub num_errors {
}
##################################
=item C<$checker-E<gt>name()>
Set (if argument specified) and retrieve the canonical name of POD as
found in the C<=head1 NAME> section.
=cut
sub name {
return (@_ > 1 && $_[1]) ?
}
##################################
=item C<$checker-E<gt>node()>
Add (if argument specified) and retrieve the nodes (as defined by C<=headX>
and C<=item>) of the current POD. The nodes are returned in the order of
their occurence. They consist of plain text, each piece of whitespace is
collapsed to a single blank.
=cut
sub node {
if(defined $text) {
$text =~ s/\s+$//s; # strip trailing whitespace
# add node, order important!
# keep also a uniqueness counter
return $text;
}
}
##################################
=item C<$checker-E<gt>idx()>
Add (if argument specified) and retrieve the index entries (as defined by
C<XE<lt>E<gt>>) of the current POD. They consist of plain text, each piece
of whitespace is collapsed to a single blank.
=cut
sub idx {
if(defined $text) {
$text =~ s/\s+$//s; # strip trailing whitespace
# add node, order important!
# keep also a uniqueness counter
return $text;
}
}
##################################
=item C<$checker-E<gt>hyperlink()>
Add (if argument specified) and retrieve the hyperlinks (as defined by
C<LE<lt>E<gt>>) of the current POD. They consist of an 2-item array: line
number and C<Pod::Hyperlink> object.
=back
=cut
sub hyperlink {
my $self = shift;
if($_[0]) {
return $_[0];
}
}
## overrides for Pod::Parser
sub end_pod {
## Do some final checks and
## print the number of errors found
my $self = shift;
if(@{$self->{_list_stack}}) {
# _TODO_ display, but don't count them for now
my $list;
}
}
# check validity of document internal hyperlinks
# first build the node names from the paragraph text
my %nodes;
$nodes{$_} = 1;
if(/^(\S+)\s+\S/) {
# we have more than one word. Use the first as a node, too.
# This is used heavily in perlfunc.pod
}
}
}
# _TODO_ what if there is a link to the page itself by the name,
# e.g. in Tk::Pod : L<Tk::Pod/"DESCRIPTION">
-severity => 'ERROR',
-msg => "unresolved internal link '$node'"});
}
}
}
# check the internal nodes for uniqueness. This pertains to
# =headX, =item and X<...>
keys %{$self->{_unique_nodes}})) {
-severity => 'WARNING',
-msg => "multiple occurence of link target '$_'"});
}
## Print the number of errors found
if ($num_errors > 0) {
printf $out_fh ("$infile has $num_errors pod syntax %s.\n",
}
print $out_fh "$infile does not contain any pod commands.\n";
}
else {
print $out_fh "$infile pod syntax OK.\n";
}
}
# check a POD command directive
sub command {
## Check the command syntax
my $arg; # this will hold the command argument
if (! $VALID_COMMANDS{$cmd}) {
-msg => "Unknown command '$cmd'" });
}
else { # found a valid command
##### following check disabled due to strong request
#if(!$self->{_commands}++ && $cmd !~ /^head/) {
# $self->poderror({ -line => $line, -file => $file,
# -severity => 'WARNING',
# -msg => "file does not start with =head" });
#}
# check syntax of particular command
if($cmd eq 'over') {
# check for argument
$indent = $1;
}
# start a new list
}
elsif($cmd eq 'item') {
# are we in a list?
unless(@{$self->{_list_stack}}) {
-severity => 'ERROR',
-msg => "=item without previous =over" });
# auto-open in case we encounter many more
}
# check whether the previous item had some contents
if(defined $self->{_list_item_contents} &&
-severity => 'WARNING',
-msg => "previous =item has no contents" });
}
-severity => 'WARNING',
-msg => "preceding non-item paragraph(s)" });
}
# check for argument
$arg =~ s/[\s\n]+$//;
my $type;
if($arg =~ /^[*]\s*(\S*.*)/) {
$type = 'bullet';
$arg = $1;
}
elsif($arg =~ /^\d+\.?\s*(\S*)/) {
$type = 'number';
$arg = $1;
}
else {
$type = 'definition';
}
-severity => 'WARNING',
-msg => "=item type mismatch ('$first' vs. '$type')"});
}
else { # first item
}
}
else {
-severity => 'WARNING',
-msg => "No argument for =item" });
}
# add this item
# remember this node
}
elsif($cmd eq 'back') {
# check if we have an open list
unless(@{$self->{_list_stack}}) {
-severity => 'ERROR',
-msg => "=back without previous =over" });
}
else {
# check for spurious characters
-severity => 'ERROR',
-msg => "Spurious character(s) after =back" });
}
# close list
# check for empty lists
-severity => 'WARNING',
-msg => "No items in =over (at line " .
}
}
}
# check whether the previous =head section had some contents
if(defined $self->{_commands_in_head} &&
defined $self->{_last_head} &&
-severity => 'WARNING',
-msg => "empty section in previous paragraph"});
}
# check if there is an open list
if(@{$self->{_list_stack}}) {
my $list;
-severity => 'ERROR',
" without closing =back (at $cmd)" });
}
}
# remember this node
$arg =~ s/[\s\n]+$//s;
unless(length($arg)) {
-severity => 'ERROR',
-msg => "empty =$cmd"});
}
if($cmd eq 'head1') {
} else {
}
}
elsif($cmd eq 'begin') {
if($self->{_have_begin}) {
# already have a begin
-severity => 'ERROR',
-msg => "Nested =begin's (first at line " .
}
else {
# check for argument
-severity => 'ERROR',
-msg => "No argument for =begin"});
}
# remember the =begin
}
}
elsif($cmd eq 'end') {
if($self->{_have_begin}) {
# close the existing =begin
# check for spurious characters
# the closing argument is optional
#if($arg && $arg =~ /\S/) {
# $self->poderror({ -line => $line, -file => $file,
# -severity => 'WARNING',
# -msg => "Spurious character(s) after =end" });
#}
}
else {
# don't have a matching =begin
-severity => 'ERROR',
-msg => "=end without =begin" });
}
}
elsif($cmd eq 'for') {
unless($paragraph =~ /\s*(\S+)\s*/) {
-severity => 'ERROR',
-msg => "=for without formatter specification" });
}
}
# check for argument
-severity => 'ERROR',
-msg => "Spurious text after =$cmd"});
}
}
$self->{_commands_in_head}++;
## Check the interior sequences in the command-text
unless(defined $arg);
}
}
sub _open_list
{
undef $self->{_list_item_contents};
$list;
}
sub _close_list
{
if(defined $self->{_list_item_contents} &&
-severity => 'WARNING',
-msg => "previous =item has no contents" });
}
undef $self->{_list_item_contents};
$list;
}
# process a block of some text
sub interpolate_and_check {
## Check the interior sequences in the command-text
# and return the text
$self->_check_ptree(
}
sub _check_ptree {
local($_);
my $text = '';
# process each node in the parse tree
foreach(@$ptree) {
# regular text chunk
unless(ref) {
my $count;
# count the unescaped angle brackets
# complain only when warning level is greater than 1
my $i = $_;
if($count = $i =~ tr/<>/<>/) {
-severity => 'WARNING',
-msg => "$count unescaped <> in paragraph" })
}
$text .= $i;
next;
}
# have an interior sequence
my $contents = $_->parse_tree();
# check for valid tag
if (! $VALID_SEQUENCES{$cmd}) {
-severity => 'ERROR',
# expand it anyway
next;
}
-severity => 'ERROR',
-msg => "nested commands $cmd<...$cmd<...>...>"});
# _TODO_ should we add the contents anyway?
# expand it anyway, see below
}
if($cmd eq 'E') {
# preserve entities
-severity => 'ERROR',
next;
}
my $val;
# hexadec entity
}
elsif($ent =~ /^0\d+$/) {
# octal
}
elsif($ent =~ /^\d+$/) {
# numeric entity
}
if(defined $val) {
}
else {
-severity => 'ERROR',
}
}
# known ISO entity
}
else {
-severity => 'WARNING',
$text .= "E<$ent>";
}
}
elsif($cmd eq 'L') {
# try to parse the hyperlink
unless(defined $link) {
-severity => 'ERROR',
next;
}
if($self->{-warnings}) {
-severity => 'WARNING',
-msg => $w });
}
}
# check the link text
# remember link
}
# add the guts
}
elsif($cmd eq 'Z') {
-severity => 'ERROR',
-msg => "Nonempty Z<>"});
}
}
elsif($cmd eq 'X') {
if($idx =~ /^\s*$/s) {
-severity => 'ERROR',
-msg => "Empty X<>"});
}
else {
# remember this node
}
}
else {
# not reached
die "internal error";
}
}
$text;
}
# process a block of verbatim text
sub verbatim {
## Nothing particular to check
-severity => 'WARNING',
-msg => 'Verbatim paragraph in NAME section' });
}
}
# process a block of regular text
sub textblock {
# skip this paragraph if in a =begin block
unless($self->{_have_begin}) {
if($block =~ /^\s*(\S+?)\s*[,-]/) {
# this is the canonical name
}
}
}
}
sub _preproc_par
{
my $self = shift;
$_[0] =~ s/[\s\n]+$//;
if($_[0]) {
$self->{_commands_in_head}++;
}
}
}
1;
=head1 AUTHOR
Brad Appleton E<lt>bradapp@enteract.comE<gt> (initial version),
Marek Rouchal E<lt>marek@saftsack.fs.uni-bayreuth.deE<gt>
Based on code for B<Pod::Text::pod2text()> written by
Tom Christiansen E<lt>tchrist@mox.perl.comE<gt>
=cut