Start anew
[msysgit.git] / lib / perl5 / 5.6.1 / Pod / Checker.pm
blob0863c80fc8881c244de6c6ffa8932cfbb3cc9946
1 #############################################################################
2 # Pod/Checker.pm -- check pod documents for syntax errors
4 # Copyright (C) 1994-2000 by Bradford Appleton. All rights reserved.
5 # This file is part of "PodParser". PodParser is free software;
6 # you can redistribute it and/or modify it under the same terms
7 # as Perl itself.
8 #############################################################################
10 package Pod::Checker;
12 use vars qw($VERSION);
13 $VERSION = 1.2; ## Current version of this package
14 require 5.005; ## requires this Perl version or later
16 use Pod::ParseUtils; ## for hyperlinks and lists
18 =head1 NAME
20 Pod::Checker, podchecker() - check pod documents for syntax errors
22 =head1 SYNOPSIS
24 use Pod::Checker;
26 $syntax_okay = podchecker($filepath, $outputpath, %options);
28 my $checker = new Pod::Checker %options;
29 $checker->parse_from_file($filepath, \*STDERR);
31 =head1 OPTIONS/ARGUMENTS
33 C<$filepath> is the input POD to read and C<$outputpath> is
34 where to write POD syntax error messages. Either argument may be a scalar
35 indicating a file-path, or else a reference to an open filehandle.
36 If unspecified, the input-file it defaults to C<\*STDIN>, and
37 the output-file defaults to C<\*STDERR>.
39 =head2 podchecker()
41 This function can take a hash of options:
43 =over 4
45 =item B<-warnings> =E<gt> I<val>
47 Turn warnings on/off. I<val> is usually 1 for on, but higher values
48 trigger additional warnings. See L<"Warnings">.
50 =back
52 =head1 DESCRIPTION
54 B<podchecker> will perform syntax checking of Perl5 POD format documentation.
56 I<NOTE THAT THIS MODULE IS CURRENTLY IN THE BETA STAGE!>
58 It is hoped that curious/ambitious user will help flesh out and add the
59 additional features they wish to see in B<Pod::Checker> and B<podchecker>
60 and verify that the checks are consistent with L<perlpod>.
62 The following checks are currently preformed:
64 =over 4
66 =item *
68 Unknown '=xxxx' commands, unknown 'XE<lt>...E<gt>' interior-sequences,
69 and unterminated interior sequences.
71 =item *
73 Check for proper balancing of C<=begin> and C<=end>. The contents of such
74 a block are generally ignored, i.e. no syntax checks are performed.
76 =item *
78 Check for proper nesting and balancing of C<=over>, C<=item> and C<=back>.
80 =item *
82 Check for same nested interior-sequences (e.g.
83 C<LE<lt>...LE<lt>...E<gt>...E<gt>>).
85 =item *
87 Check for malformed or nonexisting entities C<EE<lt>...E<gt>>.
89 =item *
91 Check for correct syntax of hyperlinks C<LE<lt>...E<gt>>. See L<perlpod>
92 for details.
94 =item *
96 Check for unresolved document-internal links. This check may also reveal
97 misspelled links that seem to be internal links but should be links
98 to something else.
100 =back
102 =head1 DIAGNOSTICS
104 =head2 Errors
106 =over 4
108 =item * empty =headn
110 A heading (C<=head1> or C<=head2>) without any text? That ain't no
111 heading!
113 =item * =over on line I<N> without closing =back
115 The C<=over> command does not have a corresponding C<=back> before the
116 next heading (C<=head1> or C<=head2>) or the end of the file.
118 =item * =item without previous =over
120 =item * =back without previous =over
122 An C<=item> or C<=back> command has been found outside a
123 C<=over>/C<=back> block.
125 =item * No argument for =begin
127 A C<=begin> command was found that is not followed by the formatter
128 specification.
130 =item * =end without =begin
132 A standalone C<=end> command was found.
134 =item * Nested =begin's
136 There were at least two consecutive C<=begin> commands without
137 the corresponding C<=end>. Only one C<=begin> may be active at
138 a time.
140 =item * =for without formatter specification
142 There is no specification of the formatter after the C<=for> command.
144 =item * unresolved internal link I<NAME>
146 The given link to I<NAME> does not have a matching node in the current
147 POD. This also happend when a single word node name is not enclosed in
148 C<"">.
150 =item * Unknown command "I<CMD>"
152 An invalid POD command has been found. Valid are C<=head1>, C<=head2>,
153 C<=over>, C<=item>, C<=back>, C<=begin>, C<=end>, C<=for>, C<=pod>,
154 C<=cut>
156 =item * Unknown interior-sequence "I<SEQ>"
158 An invalid markup command has been encountered. Valid are:
159 C<BE<lt>E<gt>>, C<CE<lt>E<gt>>, C<EE<lt>E<gt>>, C<FE<lt>E<gt>>,
160 C<IE<lt>E<gt>>, C<LE<lt>E<gt>>, C<SE<lt>E<gt>>, C<XE<lt>E<gt>>,
161 C<ZE<lt>E<gt>>
163 =item * nested commands I<CMD>E<lt>...I<CMD>E<lt>...E<gt>...E<gt>
165 Two nested identical markup commands have been found. Generally this
166 does not make sense.
168 =item * garbled entity I<STRING>
170 The I<STRING> found cannot be interpreted as a character entity.
172 =item * Entity number out of range
174 An entity specified by number (dec, hex, oct) is out of range (1-255).
176 =item * malformed link LE<lt>E<gt>
178 The link found cannot be parsed because it does not conform to the
179 syntax described in L<perlpod>.
181 =item * nonempty ZE<lt>E<gt>
183 The C<ZE<lt>E<gt>> sequence is supposed to be empty.
185 =item * empty XE<lt>E<gt>
187 The index entry specified contains nothing but whitespace.
189 =item * Spurious text after =pod / =cut
191 The commands C<=pod> and C<=cut> do not take any arguments.
193 =item * Spurious character(s) after =back
195 The C<=back> command does not take any arguments.
197 =back
199 =head2 Warnings
201 These may not necessarily cause trouble, but indicate mediocre style.
203 =over 4
205 =item * multiple occurence of link target I<name>
207 The POD file has some C<=item> and/or C<=head> commands that have
208 the same text. Potential hyperlinks to such a text cannot be unique then.
210 =item * line containing nothing but whitespace in paragraph
212 There is some whitespace on a seemingly empty line. POD is very sensitive
213 to such things, so this is flagged. B<vi> users switch on the B<list>
214 option to avoid this problem.
216 =begin _disabled_
218 =item * file does not start with =head
220 The file starts with a different POD directive than head.
221 This is most probably something you do not want.
223 =end _disabled_
225 =item * previous =item has no contents
227 There is a list C<=item> right above the flagged line that has no
228 text contents. You probably want to delete empty items.
230 =item * preceding non-item paragraph(s)
232 A list introduced by C<=over> starts with a text or verbatim paragraph,
233 but continues with C<=item>s. Move the non-item paragraph out of the
234 C<=over>/C<=back> block.
236 =item * =item type mismatch (I<one> vs. I<two>)
238 A list started with e.g. a bulletted C<=item> and continued with a
239 numbered one. This is obviously inconsistent. For most translators the
240 type of the I<first> C<=item> determines the type of the list.
242 =item * I<N> unescaped C<E<lt>E<gt>> in paragraph
244 Angle brackets not written as C<E<lt>ltE<gt>> and C<E<lt>gtE<gt>>
245 can potentially cause errors as they could be misinterpreted as
246 markup commands. This is only printed when the -warnings level is
247 greater than 1.
249 =item * Unknown entity
251 A character entity was found that does not belong to the standard
252 ISO set or the POD specials C<verbar> and C<sol>.
254 =item * No items in =over
256 The list opened with C<=over> does not contain any items.
258 =item * No argument for =item
260 C<=item> without any parameters is deprecated. It should either be followed
261 by C<*> to indicate an unordered list, by a number (optionally followed
262 by a dot) to indicate an ordered (numbered) list or simple text for a
263 definition list.
265 =item * empty section in previous paragraph
267 The previous section (introduced by a C<=head> command) does not contain
268 any text. This usually indicates that something is missing. Note: A
269 C<=head1> followed immediately by C<=head2> does not trigger this warning.
271 =item * Verbatim paragraph in NAME section
273 The NAME section (C<=head1 NAME>) should consist of a single paragraph
274 with the script/module name, followed by a dash `-' and a very short
275 description of what the thing is good for.
277 =back
279 =head2 Hyperlinks
281 There are some warnings wrt. malformed hyperlinks.
283 =over 4
285 =item * ignoring leading/trailing whitespace in link
287 There is whitespace at the beginning or the end of the contents of
288 LE<lt>...E<gt>.
290 =item * (section) in '$page' deprecated
292 There is a section detected in the page name of LE<lt>...E<gt>, e.g.
293 C<LE<gt>passwd(2)E<gt>>. POD hyperlinks may point to POD documents only.
294 Please write C<CE<lt>passwd(2)E<gt>> instead. Some formatters are able
295 to expand this to appropriate code. For links to (builtin) functions,
296 please say C<LE<lt>perlfunc/mkdirE<gt>>, without ().
298 =item * alternative text/node '%s' contains non-escaped | or /
300 The characters C<|> and C</> are special in the LE<lt>...E<gt> context.
301 Although the hyperlink parser does its best to determine which "/" is
302 text and which is a delimiter in case of doubt, one ought to escape
303 these literal characters like this:
305 / E<sol>
306 | E<verbar>
308 =back
310 =head1 RETURN VALUE
312 B<podchecker> returns the number of POD syntax errors found or -1 if
313 there were no POD commands at all found in the file.
315 =head1 EXAMPLES
317 I<[T.B.D.]>
319 =head1 INTERFACE
321 While checking, this module collects document properties, e.g. the nodes
322 for hyperlinks (C<=headX>, C<=item>) and index entries (C<XE<lt>E<gt>>).
323 POD translators can use this feature to syntax-check and get the nodes in
324 a first pass before actually starting to convert. This is expensive in terms
325 of execution time, but allows for very robust conversions.
327 =cut
329 #############################################################################
331 use strict;
332 #use diagnostics;
333 use Carp;
334 use Exporter;
335 use Pod::Parser;
337 use vars qw(@ISA @EXPORT);
338 @ISA = qw(Pod::Parser);
339 @EXPORT = qw(&podchecker);
341 use vars qw(%VALID_COMMANDS %VALID_SEQUENCES);
343 my %VALID_COMMANDS = (
344 'pod' => 1,
345 'cut' => 1,
346 'head1' => 1,
347 'head2' => 1,
348 'over' => 1,
349 'back' => 1,
350 'item' => 1,
351 'for' => 1,
352 'begin' => 1,
353 'end' => 1,
356 my %VALID_SEQUENCES = (
357 'I' => 1,
358 'B' => 1,
359 'S' => 1,
360 'C' => 1,
361 'L' => 1,
362 'F' => 1,
363 'X' => 1,
364 'Z' => 1,
365 'E' => 1,
368 # stolen from HTML::Entities
369 my %ENTITIES = (
370 # Some normal chars that have special meaning in SGML context
371 amp => '&', # ampersand
372 'gt' => '>', # greater than
373 'lt' => '<', # less than
374 quot => '"', # double quote
376 # PUBLIC ISO 8879-1986//ENTITIES Added Latin 1//EN//HTML
377 AElig => 'Æ', # capital AE diphthong (ligature)
378 Aacute => 'Á', # capital A, acute accent
379 Acirc => 'Â', # capital A, circumflex accent
380 Agrave => 'À', # capital A, grave accent
381 Aring => 'Å', # capital A, ring
382 Atilde => 'Ã', # capital A, tilde
383 Auml => 'Ä', # capital A, dieresis or umlaut mark
384 Ccedil => 'Ç', # capital C, cedilla
385 ETH => 'Ð', # capital Eth, Icelandic
386 Eacute => 'É', # capital E, acute accent
387 Ecirc => 'Ê', # capital E, circumflex accent
388 Egrave => 'È', # capital E, grave accent
389 Euml => 'Ë', # capital E, dieresis or umlaut mark
390 Iacute => 'Í', # capital I, acute accent
391 Icirc => 'Î', # capital I, circumflex accent
392 Igrave => 'Ì', # capital I, grave accent
393 Iuml => 'Ï', # capital I, dieresis or umlaut mark
394 Ntilde => 'Ñ', # capital N, tilde
395 Oacute => 'Ó', # capital O, acute accent
396 Ocirc => 'Ô', # capital O, circumflex accent
397 Ograve => 'Ò', # capital O, grave accent
398 Oslash => 'Ø', # capital O, slash
399 Otilde => 'Õ', # capital O, tilde
400 Ouml => 'Ö', # capital O, dieresis or umlaut mark
401 THORN => 'Þ', # capital THORN, Icelandic
402 Uacute => 'Ú', # capital U, acute accent
403 Ucirc => 'Û', # capital U, circumflex accent
404 Ugrave => 'Ù', # capital U, grave accent
405 Uuml => 'Ü', # capital U, dieresis or umlaut mark
406 Yacute => 'Ý', # capital Y, acute accent
407 aacute => 'á', # small a, acute accent
408 acirc => 'â', # small a, circumflex accent
409 aelig => 'æ', # small ae diphthong (ligature)
410 agrave => 'à', # small a, grave accent
411 aring => 'å', # small a, ring
412 atilde => 'ã', # small a, tilde
413 auml => 'ä', # small a, dieresis or umlaut mark
414 ccedil => 'ç', # small c, cedilla
415 eacute => 'é', # small e, acute accent
416 ecirc => 'ê', # small e, circumflex accent
417 egrave => 'è', # small e, grave accent
418 eth => 'ð', # small eth, Icelandic
419 euml => 'ë', # small e, dieresis or umlaut mark
420 iacute => 'í', # small i, acute accent
421 icirc => 'î', # small i, circumflex accent
422 igrave => 'ì', # small i, grave accent
423 iuml => 'ï', # small i, dieresis or umlaut mark
424 ntilde => 'ñ', # small n, tilde
425 oacute => 'ó', # small o, acute accent
426 ocirc => 'ô', # small o, circumflex accent
427 ograve => 'ò', # small o, grave accent
428 oslash => 'ø', # small o, slash
429 otilde => 'õ', # small o, tilde
430 ouml => 'ö', # small o, dieresis or umlaut mark
431 szlig => 'ß', # small sharp s, German (sz ligature)
432 thorn => 'þ', # small thorn, Icelandic
433 uacute => 'ú', # small u, acute accent
434 ucirc => 'û', # small u, circumflex accent
435 ugrave => 'ù', # small u, grave accent
436 uuml => 'ü', # small u, dieresis or umlaut mark
437 yacute => 'ý', # small y, acute accent
438 yuml => 'ÿ', # small y, dieresis or umlaut mark
440 # Some extra Latin 1 chars that are listed in the HTML3.2 draft (21-May-96)
441 copy => '©', # copyright sign
442 reg => '®', # registered sign
443 nbsp => "\240", # non breaking space
445 # Additional ISO-8859/1 entities listed in rfc1866 (section 14)
446 iexcl => '¡',
447 cent => '¢',
448 pound => '£',
449 curren => '¤',
450 yen => '¥',
451 brvbar => '¦',
452 sect => '§',
453 uml => '¨',
454 ordf => 'ª',
455 laquo => '«',
456 'not' => '¬', # not is a keyword in perl
457 shy => '­',
458 macr => '¯',
459 deg => '°',
460 plusmn => '±',
461 sup1 => '¹',
462 sup2 => '²',
463 sup3 => '³',
464 acute => '´',
465 micro => 'µ',
466 para => '¶',
467 middot => '·',
468 cedil => '¸',
469 ordm => 'º',
470 raquo => '»',
471 frac14 => '¼',
472 frac12 => '½',
473 frac34 => '¾',
474 iquest => '¿',
475 'times' => '×', # times is a keyword in perl
476 divide => '÷',
478 # some POD special entities
479 verbar => '|',
480 sol => '/'
483 ##---------------------------------------------------------------------------
485 ##---------------------------------
486 ## Function definitions begin here
487 ##---------------------------------
489 sub podchecker( $ ; $ % ) {
490 my ($infile, $outfile, %options) = @_;
491 local $_;
493 ## Set defaults
494 $infile ||= \*STDIN;
495 $outfile ||= \*STDERR;
497 ## Now create a pod checker
498 my $checker = new Pod::Checker(%options);
500 ## Now check the pod document for errors
501 $checker->parse_from_file($infile, $outfile);
503 ## Return the number of errors found
504 return $checker->num_errors();
507 ##---------------------------------------------------------------------------
509 ##-------------------------------
510 ## Method definitions begin here
511 ##-------------------------------
513 ##################################
515 =over 4
517 =item C<Pod::Checker-E<gt>new( %options )>
519 Return a reference to a new Pod::Checker object that inherits from
520 Pod::Parser and is used for calling the required methods later. The
521 following options are recognized:
523 C<-warnings =E<gt> num>
524 Print warnings if C<num> is true. The higher the value of C<num>,
525 the more warnings are printed. Currently there are only levels 1 and 2.
527 C<-quiet =E<gt> num>
528 If C<num> is true, do not print any errors/warnings. This is useful
529 when Pod::Checker is used to munge POD code into plain text from within
530 POD formatters.
532 =cut
534 ## sub new {
535 ## my $this = shift;
536 ## my $class = ref($this) || $this;
537 ## my %params = @_;
538 ## my $self = {%params};
539 ## bless $self, $class;
540 ## $self->initialize();
541 ## return $self;
542 ## }
544 sub initialize {
545 my $self = shift;
546 ## Initialize number of errors, and setup an error function to
547 ## increment this number and then print to the designated output.
548 $self->{_NUM_ERRORS} = 0;
549 $self->{-quiet} ||= 0;
550 # set the error handling subroutine
551 $self->errorsub($self->{-quiet} ? sub { 1; } : 'poderror');
552 $self->{_commands} = 0; # total number of POD commands encountered
553 $self->{_list_stack} = []; # stack for nested lists
554 $self->{_have_begin} = ''; # stores =begin
555 $self->{_links} = []; # stack for internal hyperlinks
556 $self->{_nodes} = []; # stack for =head/=item nodes
557 $self->{_index} = []; # text in X<>
558 # print warnings?
559 $self->{-warnings} = 1 unless(defined $self->{-warnings});
560 $self->{_current_head1} = ''; # the current =head1 block
561 $self->parseopts(-process_cut_cmd => 1, -warnings => $self->{-warnings});
564 ##################################
566 =item C<$checker-E<gt>poderror( @args )>
568 =item C<$checker-E<gt>poderror( {%opts}, @args )>
570 Internal method for printing errors and warnings. If no options are
571 given, simply prints "@_". The following options are recognized and used
572 to form the output:
574 -msg
576 A message to print prior to C<@args>.
578 -line
580 The line number the error occurred in.
582 -file
584 The file (name) the error occurred in.
586 -severity
588 The error level, should be 'WARNING' or 'ERROR'.
590 =cut
592 # Invoked as $self->poderror( @args ), or $self->poderror( {%opts}, @args )
593 sub poderror {
594 my $self = shift;
595 my %opts = (ref $_[0]) ? %{shift()} : ();
597 ## Retrieve options
598 chomp( my $msg = ($opts{-msg} || "")."@_" );
599 my $line = (exists $opts{-line}) ? " at line $opts{-line}" : "";
600 my $file = (exists $opts{-file}) ? " in file $opts{-file}" : "";
601 unless (exists $opts{-severity}) {
602 ## See if can find severity in message prefix
603 $opts{-severity} = $1 if ( $msg =~ s/^\**\s*([A-Z]{3,}):\s+// );
605 my $severity = (exists $opts{-severity}) ? "*** $opts{-severity}: " : "";
607 ## Increment error count and print message "
608 ++($self->{_NUM_ERRORS})
609 if(!%opts || ($opts{-severity} && $opts{-severity} eq 'ERROR'));
610 my $out_fh = $self->output_handle() || \*STDERR;
611 print $out_fh ($severity, $msg, $line, $file, "\n")
612 if($self->{-warnings} || !%opts || $opts{-severity} ne 'WARNING');
615 ##################################
617 =item C<$checker-E<gt>num_errors()>
619 Set (if argument specified) and retrieve the number of errors found.
621 =cut
623 sub num_errors {
624 return (@_ > 1) ? ($_[0]->{_NUM_ERRORS} = $_[1]) : $_[0]->{_NUM_ERRORS};
627 ##################################
629 =item C<$checker-E<gt>name()>
631 Set (if argument specified) and retrieve the canonical name of POD as
632 found in the C<=head1 NAME> section.
634 =cut
636 sub name {
637 return (@_ > 1 && $_[1]) ?
638 ($_[0]->{-name} = $_[1]) : $_[0]->{-name};
641 ##################################
643 =item C<$checker-E<gt>node()>
645 Add (if argument specified) and retrieve the nodes (as defined by C<=headX>
646 and C<=item>) of the current POD. The nodes are returned in the order of
647 their occurence. They consist of plain text, each piece of whitespace is
648 collapsed to a single blank.
650 =cut
652 sub node {
653 my ($self,$text) = @_;
654 if(defined $text) {
655 $text =~ s/\s+$//s; # strip trailing whitespace
656 $text =~ s/\s+/ /gs; # collapse whitespace
657 # add node, order important!
658 push(@{$self->{_nodes}}, $text);
659 # keep also a uniqueness counter
660 $self->{_unique_nodes}->{$text}++ if($text !~ /^\s*$/s);
661 return $text;
663 @{$self->{_nodes}};
666 ##################################
668 =item C<$checker-E<gt>idx()>
670 Add (if argument specified) and retrieve the index entries (as defined by
671 C<XE<lt>E<gt>>) of the current POD. They consist of plain text, each piece
672 of whitespace is collapsed to a single blank.
674 =cut
676 # set/return index entries of current POD
677 sub idx {
678 my ($self,$text) = @_;
679 if(defined $text) {
680 $text =~ s/\s+$//s; # strip trailing whitespace
681 $text =~ s/\s+/ /gs; # collapse whitespace
682 # add node, order important!
683 push(@{$self->{_index}}, $text);
684 # keep also a uniqueness counter
685 $self->{_unique_nodes}->{$text}++ if($text !~ /^\s*$/s);
686 return $text;
688 @{$self->{_index}};
691 ##################################
693 =item C<$checker-E<gt>hyperlink()>
695 Add (if argument specified) and retrieve the hyperlinks (as defined by
696 C<LE<lt>E<gt>>) of the current POD. They consist of an 2-item array: line
697 number and C<Pod::Hyperlink> object.
699 =back
701 =cut
703 # set/return hyperlinks of the current POD
704 sub hyperlink {
705 my $self = shift;
706 if($_[0]) {
707 push(@{$self->{_links}}, $_[0]);
708 return $_[0];
710 @{$self->{_links}};
713 ## overrides for Pod::Parser
715 sub end_pod {
716 ## Do some final checks and
717 ## print the number of errors found
718 my $self = shift;
719 my $infile = $self->input_file();
720 my $out_fh = $self->output_handle();
722 if(@{$self->{_list_stack}}) {
723 # _TODO_ display, but don't count them for now
724 my $list;
725 while(($list = $self->_close_list('EOF',$infile)) &&
726 $list->indent() ne 'auto') {
727 $self->poderror({ -line => 'EOF', -file => $infile,
728 -severity => 'ERROR', -msg => "=over on line " .
729 $list->start() . " without closing =back" }); #"
733 # check validity of document internal hyperlinks
734 # first build the node names from the paragraph text
735 my %nodes;
736 foreach($self->node()) {
737 $nodes{$_} = 1;
738 if(/^(\S+)\s+\S/) {
739 # we have more than one word. Use the first as a node, too.
740 # This is used heavily in perlfunc.pod
741 $nodes{$1} ||= 2; # derived node
744 foreach($self->idx()) {
745 $nodes{$_} = 3; # index node
747 foreach($self->hyperlink()) {
748 my ($line,$link) = @$_;
749 # _TODO_ what if there is a link to the page itself by the name,
750 # e.g. in Tk::Pod : L<Tk::Pod/"DESCRIPTION">
751 if($link->node() && !$link->page() && $link->type() ne 'hyperlink') {
752 my $node = $self->_check_ptree($self->parse_text($link->node(),
753 $line), $line, $infile, 'L');
754 if($node && !$nodes{$node}) {
755 $self->poderror({ -line => $line || '', -file => $infile,
756 -severity => 'ERROR',
757 -msg => "unresolved internal link '$node'"});
762 # check the internal nodes for uniqueness. This pertains to
763 # =headX, =item and X<...>
764 foreach(grep($self->{_unique_nodes}->{$_} > 1,
765 keys %{$self->{_unique_nodes}})) {
766 $self->poderror({ -line => '-', -file => $infile,
767 -severity => 'WARNING',
768 -msg => "multiple occurence of link target '$_'"});
771 ## Print the number of errors found
772 my $num_errors = $self->num_errors();
773 if ($num_errors > 0) {
774 printf $out_fh ("$infile has $num_errors pod syntax %s.\n",
775 ($num_errors == 1) ? "error" : "errors");
777 elsif($self->{_commands} == 0) {
778 print $out_fh "$infile does not contain any pod commands.\n";
779 $self->num_errors(-1);
781 else {
782 print $out_fh "$infile pod syntax OK.\n";
786 # check a POD command directive
787 sub command {
788 my ($self, $cmd, $paragraph, $line_num, $pod_para) = @_;
789 my ($file, $line) = $pod_para->file_line;
790 ## Check the command syntax
791 my $arg; # this will hold the command argument
792 if (! $VALID_COMMANDS{$cmd}) {
793 $self->poderror({ -line => $line, -file => $file, -severity => 'ERROR',
794 -msg => "Unknown command '$cmd'" });
796 else { # found a valid command
797 $self->{_commands}++; # delete this line if below is enabled again
799 ##### following check disabled due to strong request
800 #if(!$self->{_commands}++ && $cmd !~ /^head/) {
801 # $self->poderror({ -line => $line, -file => $file,
802 # -severity => 'WARNING',
803 # -msg => "file does not start with =head" });
806 # check syntax of particular command
807 if($cmd eq 'over') {
808 # check for argument
809 $arg = $self->interpolate_and_check($paragraph, $line,$file);
810 my $indent = 4; # default
811 if($arg && $arg =~ /^\s*(\d+)\s*$/) {
812 $indent = $1;
814 # start a new list
815 $self->_open_list($indent,$line,$file);
817 elsif($cmd eq 'item') {
818 # are we in a list?
819 unless(@{$self->{_list_stack}}) {
820 $self->poderror({ -line => $line, -file => $file,
821 -severity => 'ERROR',
822 -msg => "=item without previous =over" });
823 # auto-open in case we encounter many more
824 $self->_open_list('auto',$line,$file);
826 my $list = $self->{_list_stack}->[0];
827 # check whether the previous item had some contents
828 if(defined $self->{_list_item_contents} &&
829 $self->{_list_item_contents} == 0) {
830 $self->poderror({ -line => $line, -file => $file,
831 -severity => 'WARNING',
832 -msg => "previous =item has no contents" });
834 if($list->{_has_par}) {
835 $self->poderror({ -line => $line, -file => $file,
836 -severity => 'WARNING',
837 -msg => "preceding non-item paragraph(s)" });
838 delete $list->{_has_par};
840 # check for argument
841 $arg = $self->interpolate_and_check($paragraph, $line, $file);
842 if($arg && $arg =~ /(\S+)/) {
843 $arg =~ s/[\s\n]+$//;
844 my $type;
845 if($arg =~ /^[*]\s*(\S*.*)/) {
846 $type = 'bullet';
847 $self->{_list_item_contents} = $1 ? 1 : 0;
848 $arg = $1;
850 elsif($arg =~ /^\d+\.?\s*(\S*)/) {
851 $type = 'number';
852 $self->{_list_item_contents} = $1 ? 1 : 0;
853 $arg = $1;
855 else {
856 $type = 'definition';
857 $self->{_list_item_contents} = 1;
859 my $first = $list->type();
860 if($first && $first ne $type) {
861 $self->poderror({ -line => $line, -file => $file,
862 -severity => 'WARNING',
863 -msg => "=item type mismatch ('$first' vs. '$type')"});
865 else { # first item
866 $list->type($type);
869 else {
870 $self->poderror({ -line => $line, -file => $file,
871 -severity => 'WARNING',
872 -msg => "No argument for =item" });
873 $arg = ' '; # empty
874 $self->{_list_item_contents} = 0;
876 # add this item
877 $list->item($arg);
878 # remember this node
879 $self->node($arg);
881 elsif($cmd eq 'back') {
882 # check if we have an open list
883 unless(@{$self->{_list_stack}}) {
884 $self->poderror({ -line => $line, -file => $file,
885 -severity => 'ERROR',
886 -msg => "=back without previous =over" });
888 else {
889 # check for spurious characters
890 $arg = $self->interpolate_and_check($paragraph, $line,$file);
891 if($arg && $arg =~ /\S/) {
892 $self->poderror({ -line => $line, -file => $file,
893 -severity => 'ERROR',
894 -msg => "Spurious character(s) after =back" });
896 # close list
897 my $list = $self->_close_list($line,$file);
898 # check for empty lists
899 if(!$list->item() && $self->{-warnings}) {
900 $self->poderror({ -line => $line, -file => $file,
901 -severity => 'WARNING',
902 -msg => "No items in =over (at line " .
903 $list->start() . ") / =back list"}); #"
907 elsif($cmd =~ /^head(\d+)/) {
908 # check whether the previous =head section had some contents
909 if(defined $self->{_commands_in_head} &&
910 $self->{_commands_in_head} == 0 &&
911 defined $self->{_last_head} &&
912 $self->{_last_head} >= $1) {
913 $self->poderror({ -line => $line, -file => $file,
914 -severity => 'WARNING',
915 -msg => "empty section in previous paragraph"});
917 $self->{_commands_in_head} = -1;
918 $self->{_last_head} = $1;
919 # check if there is an open list
920 if(@{$self->{_list_stack}}) {
921 my $list;
922 while(($list = $self->_close_list($line,$file)) &&
923 $list->indent() ne 'auto') {
924 $self->poderror({ -line => $line, -file => $file,
925 -severity => 'ERROR',
926 -msg => "=over on line ". $list->start() .
927 " without closing =back (at $cmd)" });
930 # remember this node
931 $arg = $self->interpolate_and_check($paragraph, $line,$file);
932 $arg =~ s/[\s\n]+$//s;
933 $self->node($arg);
934 unless(length($arg)) {
935 $self->poderror({ -line => $line, -file => $file,
936 -severity => 'ERROR',
937 -msg => "empty =$cmd"});
939 if($cmd eq 'head1') {
940 $self->{_current_head1} = $arg;
941 } else {
942 $self->{_current_head1} = '';
945 elsif($cmd eq 'begin') {
946 if($self->{_have_begin}) {
947 # already have a begin
948 $self->poderror({ -line => $line, -file => $file,
949 -severity => 'ERROR',
950 -msg => "Nested =begin's (first at line " .
951 $self->{_have_begin} . ")"});
953 else {
954 # check for argument
955 $arg = $self->interpolate_and_check($paragraph, $line,$file);
956 unless($arg && $arg =~ /(\S+)/) {
957 $self->poderror({ -line => $line, -file => $file,
958 -severity => 'ERROR',
959 -msg => "No argument for =begin"});
961 # remember the =begin
962 $self->{_have_begin} = "$line:$1";
965 elsif($cmd eq 'end') {
966 if($self->{_have_begin}) {
967 # close the existing =begin
968 $self->{_have_begin} = '';
969 # check for spurious characters
970 $arg = $self->interpolate_and_check($paragraph, $line,$file);
971 # the closing argument is optional
972 #if($arg && $arg =~ /\S/) {
973 # $self->poderror({ -line => $line, -file => $file,
974 # -severity => 'WARNING',
975 # -msg => "Spurious character(s) after =end" });
978 else {
979 # don't have a matching =begin
980 $self->poderror({ -line => $line, -file => $file,
981 -severity => 'ERROR',
982 -msg => "=end without =begin" });
985 elsif($cmd eq 'for') {
986 unless($paragraph =~ /\s*(\S+)\s*/) {
987 $self->poderror({ -line => $line, -file => $file,
988 -severity => 'ERROR',
989 -msg => "=for without formatter specification" });
991 $arg = ''; # do not expand paragraph below
993 elsif($cmd =~ /^(pod|cut)$/) {
994 # check for argument
995 $arg = $self->interpolate_and_check($paragraph, $line,$file);
996 if($arg && $arg =~ /(\S+)/) {
997 $self->poderror({ -line => $line, -file => $file,
998 -severity => 'ERROR',
999 -msg => "Spurious text after =$cmd"});
1002 $self->{_commands_in_head}++;
1003 ## Check the interior sequences in the command-text
1004 $self->interpolate_and_check($paragraph, $line,$file)
1005 unless(defined $arg);
1009 sub _open_list
1011 my ($self,$indent,$line,$file) = @_;
1012 my $list = Pod::List->new(
1013 -indent => $indent,
1014 -start => $line,
1015 -file => $file);
1016 unshift(@{$self->{_list_stack}}, $list);
1017 undef $self->{_list_item_contents};
1018 $list;
1021 sub _close_list
1023 my ($self,$line,$file) = @_;
1024 my $list = shift(@{$self->{_list_stack}});
1025 if(defined $self->{_list_item_contents} &&
1026 $self->{_list_item_contents} == 0) {
1027 $self->poderror({ -line => $line, -file => $file,
1028 -severity => 'WARNING',
1029 -msg => "previous =item has no contents" });
1031 undef $self->{_list_item_contents};
1032 $list;
1035 # process a block of some text
1036 sub interpolate_and_check {
1037 my ($self, $paragraph, $line, $file) = @_;
1038 ## Check the interior sequences in the command-text
1039 # and return the text
1040 $self->_check_ptree(
1041 $self->parse_text($paragraph,$line), $line, $file, '');
1044 sub _check_ptree {
1045 my ($self,$ptree,$line,$file,$nestlist) = @_;
1046 local($_);
1047 my $text = '';
1048 # process each node in the parse tree
1049 foreach(@$ptree) {
1050 # regular text chunk
1051 unless(ref) {
1052 my $count;
1053 # count the unescaped angle brackets
1054 # complain only when warning level is greater than 1
1055 my $i = $_;
1056 if($count = $i =~ tr/<>/<>/) {
1057 $self->poderror({ -line => $line, -file => $file,
1058 -severity => 'WARNING',
1059 -msg => "$count unescaped <> in paragraph" })
1060 if($self->{-warnings} && $self->{-warnings}>1);
1062 $text .= $i;
1063 next;
1065 # have an interior sequence
1066 my $cmd = $_->cmd_name();
1067 my $contents = $_->parse_tree();
1068 ($file,$line) = $_->file_line();
1069 # check for valid tag
1070 if (! $VALID_SEQUENCES{$cmd}) {
1071 $self->poderror({ -line => $line, -file => $file,
1072 -severity => 'ERROR',
1073 -msg => qq(Unknown interior-sequence '$cmd')});
1074 # expand it anyway
1075 $text .= $self->_check_ptree($contents, $line, $file, "$nestlist$cmd");
1076 next;
1078 if($nestlist =~ /$cmd/) {
1079 $self->poderror({ -line => $line, -file => $file,
1080 -severity => 'ERROR',
1081 -msg => "nested commands $cmd<...$cmd<...>...>"});
1082 # _TODO_ should we add the contents anyway?
1083 # expand it anyway, see below
1085 if($cmd eq 'E') {
1086 # preserve entities
1087 if(@$contents > 1 || ref $$contents[0] || $$contents[0] !~ /^\w+$/) {
1088 $self->poderror({ -line => $line, -file => $file,
1089 -severity => 'ERROR',
1090 -msg => "garbled entity " . $_->raw_text()});
1091 next;
1093 my $ent = $$contents[0];
1094 my $val;
1095 if($ent =~ /^0x[0-9a-f]+$/i) {
1096 # hexadec entity
1097 $val = hex($ent);
1099 elsif($ent =~ /^0\d+$/) {
1100 # octal
1101 $val = oct($ent);
1103 elsif($ent =~ /^\d+$/) {
1104 # numeric entity
1105 $val = $ent;
1107 if(defined $val) {
1108 if($val>0 && $val<256) {
1109 $text .= chr($val);
1111 else {
1112 $self->poderror({ -line => $line, -file => $file,
1113 -severity => 'ERROR',
1114 -msg => "Entity number out of range " . $_->raw_text()});
1117 elsif($ENTITIES{$ent}) {
1118 # known ISO entity
1119 $text .= $ENTITIES{$ent};
1121 else {
1122 $self->poderror({ -line => $line, -file => $file,
1123 -severity => 'WARNING',
1124 -msg => "Unknown entity " . $_->raw_text()});
1125 $text .= "E<$ent>";
1128 elsif($cmd eq 'L') {
1129 # try to parse the hyperlink
1130 my $link = Pod::Hyperlink->new($contents->raw_text());
1131 unless(defined $link) {
1132 $self->poderror({ -line => $line, -file => $file,
1133 -severity => 'ERROR',
1134 -msg => "malformed link " . $_->raw_text() ." : $@"});
1135 next;
1137 $link->line($line); # remember line
1138 if($self->{-warnings}) {
1139 foreach my $w ($link->warning()) {
1140 $self->poderror({ -line => $line, -file => $file,
1141 -severity => 'WARNING',
1142 -msg => $w });
1145 # check the link text
1146 $text .= $self->_check_ptree($self->parse_text($link->text(),
1147 $line), $line, $file, "$nestlist$cmd");
1148 # remember link
1149 $self->hyperlink([$line,$link]);
1151 elsif($cmd =~ /[BCFIS]/) {
1152 # add the guts
1153 $text .= $self->_check_ptree($contents, $line, $file, "$nestlist$cmd");
1155 elsif($cmd eq 'Z') {
1156 if(length($contents->raw_text())) {
1157 $self->poderror({ -line => $line, -file => $file,
1158 -severity => 'ERROR',
1159 -msg => "Nonempty Z<>"});
1162 elsif($cmd eq 'X') {
1163 my $idx = $self->_check_ptree($contents, $line, $file, "$nestlist$cmd");
1164 if($idx =~ /^\s*$/s) {
1165 $self->poderror({ -line => $line, -file => $file,
1166 -severity => 'ERROR',
1167 -msg => "Empty X<>"});
1169 else {
1170 # remember this node
1171 $self->idx($idx);
1174 else {
1175 # not reached
1176 die "internal error";
1179 $text;
1182 # process a block of verbatim text
1183 sub verbatim {
1184 ## Nothing particular to check
1185 my ($self, $paragraph, $line_num, $pod_para) = @_;
1187 $self->_preproc_par($paragraph);
1189 if($self->{_current_head1} eq 'NAME') {
1190 my ($file, $line) = $pod_para->file_line;
1191 $self->poderror({ -line => $line, -file => $file,
1192 -severity => 'WARNING',
1193 -msg => 'Verbatim paragraph in NAME section' });
1197 # process a block of regular text
1198 sub textblock {
1199 my ($self, $paragraph, $line_num, $pod_para) = @_;
1200 my ($file, $line) = $pod_para->file_line;
1202 $self->_preproc_par($paragraph);
1204 # skip this paragraph if in a =begin block
1205 unless($self->{_have_begin}) {
1206 my $block = $self->interpolate_and_check($paragraph, $line,$file);
1207 if($self->{_current_head1} eq 'NAME') {
1208 if($block =~ /^\s*(\S+?)\s*[,-]/) {
1209 # this is the canonical name
1210 $self->{-name} = $1 unless(defined $self->{-name});
1216 sub _preproc_par
1218 my $self = shift;
1219 $_[0] =~ s/[\s\n]+$//;
1220 if($_[0]) {
1221 $self->{_commands_in_head}++;
1222 $self->{_list_item_contents}++ if(defined $self->{_list_item_contents});
1223 if(@{$self->{_list_stack}} && !$self->{_list_stack}->[0]->item()) {
1224 $self->{_list_stack}->[0]->{_has_par} = 1;
1231 __END__
1233 =head1 AUTHOR
1235 Brad Appleton E<lt>bradapp@enteract.comE<gt> (initial version),
1236 Marek Rouchal E<lt>marek@saftsack.fs.uni-bayreuth.deE<gt>
1238 Based on code for B<Pod::Text::pod2text()> written by
1239 Tom Christiansen E<lt>tchrist@mox.perl.comE<gt>
1241 =cut