Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | ############################################################################# |
2 | # Pod/Checker.pm -- check pod documents for syntax errors | |
3 | # | |
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 | ############################################################################# | |
9 | ||
10 | package Pod::Checker; | |
11 | ||
12 | use vars qw($VERSION); | |
13 | $VERSION = 1.3; ## Current version of this package | |
14 | require 5.005; ## requires this Perl version or later | |
15 | ||
16 | use Pod::ParseUtils; ## for hyperlinks and lists | |
17 | ||
18 | =head1 NAME | |
19 | ||
20 | Pod::Checker, podchecker() - check pod documents for syntax errors | |
21 | ||
22 | =head1 SYNOPSIS | |
23 | ||
24 | use Pod::Checker; | |
25 | ||
26 | $syntax_okay = podchecker($filepath, $outputpath, %options); | |
27 | ||
28 | my $checker = new Pod::Checker %options; | |
29 | $checker->parse_from_file($filepath, \*STDERR); | |
30 | ||
31 | =head1 OPTIONS/ARGUMENTS | |
32 | ||
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>. | |
38 | ||
39 | =head2 podchecker() | |
40 | ||
41 | This function can take a hash of options: | |
42 | ||
43 | =over 4 | |
44 | ||
45 | =item B<-warnings> =E<gt> I<val> | |
46 | ||
47 | Turn warnings on/off. I<val> is usually 1 for on, but higher values | |
48 | trigger additional warnings. See L<"Warnings">. | |
49 | ||
50 | =back | |
51 | ||
52 | =head1 DESCRIPTION | |
53 | ||
54 | B<podchecker> will perform syntax checking of Perl5 POD format documentation. | |
55 | ||
56 | I<NOTE THAT THIS MODULE IS CURRENTLY IN THE BETA STAGE!> | |
57 | ||
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>. | |
61 | ||
62 | The following checks are currently preformed: | |
63 | ||
64 | =over 4 | |
65 | ||
66 | =item * | |
67 | ||
68 | Unknown '=xxxx' commands, unknown 'XE<lt>...E<gt>' interior-sequences, | |
69 | and unterminated interior sequences. | |
70 | ||
71 | =item * | |
72 | ||
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. | |
75 | ||
76 | =item * | |
77 | ||
78 | Check for proper nesting and balancing of C<=over>, C<=item> and C<=back>. | |
79 | ||
80 | =item * | |
81 | ||
82 | Check for same nested interior-sequences (e.g. | |
83 | C<LE<lt>...LE<lt>...E<gt>...E<gt>>). | |
84 | ||
85 | =item * | |
86 | ||
87 | Check for malformed or nonexisting entities C<EE<lt>...E<gt>>. | |
88 | ||
89 | =item * | |
90 | ||
91 | Check for correct syntax of hyperlinks C<LE<lt>...E<gt>>. See L<perlpod> | |
92 | for details. | |
93 | ||
94 | =item * | |
95 | ||
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. | |
99 | ||
100 | =back | |
101 | ||
102 | =head1 DIAGNOSTICS | |
103 | ||
104 | =head2 Errors | |
105 | ||
106 | =over 4 | |
107 | ||
108 | =item * empty =headn | |
109 | ||
110 | A heading (C<=head1> or C<=head2>) without any text? That ain't no | |
111 | heading! | |
112 | ||
113 | =item * =over on line I<N> without closing =back | |
114 | ||
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. | |
117 | ||
118 | =item * =item without previous =over | |
119 | ||
120 | =item * =back without previous =over | |
121 | ||
122 | An C<=item> or C<=back> command has been found outside a | |
123 | C<=over>/C<=back> block. | |
124 | ||
125 | =item * No argument for =begin | |
126 | ||
127 | A C<=begin> command was found that is not followed by the formatter | |
128 | specification. | |
129 | ||
130 | =item * =end without =begin | |
131 | ||
132 | A standalone C<=end> command was found. | |
133 | ||
134 | =item * Nested =begin's | |
135 | ||
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. | |
139 | ||
140 | =item * =for without formatter specification | |
141 | ||
142 | There is no specification of the formatter after the C<=for> command. | |
143 | ||
144 | =item * unresolved internal link I<NAME> | |
145 | ||
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<"">. | |
149 | ||
150 | =item * Unknown command "I<CMD>" | |
151 | ||
152 | An invalid POD command has been found. Valid are C<=head1>, C<=head2>, | |
153 | C<=head3>, C<=head4>, C<=over>, C<=item>, C<=back>, C<=begin>, C<=end>, | |
154 | C<=for>, C<=pod>, C<=cut> | |
155 | ||
156 | =item * Unknown interior-sequence "I<SEQ>" | |
157 | ||
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>> | |
162 | ||
163 | =item * nested commands I<CMD>E<lt>...I<CMD>E<lt>...E<gt>...E<gt> | |
164 | ||
165 | Two nested identical markup commands have been found. Generally this | |
166 | does not make sense. | |
167 | ||
168 | =item * garbled entity I<STRING> | |
169 | ||
170 | The I<STRING> found cannot be interpreted as a character entity. | |
171 | ||
172 | =item * Entity number out of range | |
173 | ||
174 | An entity specified by number (dec, hex, oct) is out of range (1-255). | |
175 | ||
176 | =item * malformed link LE<lt>E<gt> | |
177 | ||
178 | The link found cannot be parsed because it does not conform to the | |
179 | syntax described in L<perlpod>. | |
180 | ||
181 | =item * nonempty ZE<lt>E<gt> | |
182 | ||
183 | The C<ZE<lt>E<gt>> sequence is supposed to be empty. | |
184 | ||
185 | =item * empty XE<lt>E<gt> | |
186 | ||
187 | The index entry specified contains nothing but whitespace. | |
188 | ||
189 | =item * Spurious text after =pod / =cut | |
190 | ||
191 | The commands C<=pod> and C<=cut> do not take any arguments. | |
192 | ||
193 | =item * Spurious character(s) after =back | |
194 | ||
195 | The C<=back> command does not take any arguments. | |
196 | ||
197 | =back | |
198 | ||
199 | =head2 Warnings | |
200 | ||
201 | These may not necessarily cause trouble, but indicate mediocre style. | |
202 | ||
203 | =over 4 | |
204 | ||
205 | =item * multiple occurrence of link target I<name> | |
206 | ||
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. | |
209 | ||
210 | =item * line containing nothing but whitespace in paragraph | |
211 | ||
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. | |
215 | ||
216 | =begin _disabled_ | |
217 | ||
218 | =item * file does not start with =head | |
219 | ||
220 | The file starts with a different POD directive than head. | |
221 | This is most probably something you do not want. | |
222 | ||
223 | =end _disabled_ | |
224 | ||
225 | =item * previous =item has no contents | |
226 | ||
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. | |
229 | ||
230 | =item * preceding non-item paragraph(s) | |
231 | ||
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. | |
235 | ||
236 | =item * =item type mismatch (I<one> vs. I<two>) | |
237 | ||
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. | |
241 | ||
242 | =item * I<N> unescaped C<E<lt>E<gt>> in paragraph | |
243 | ||
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. | |
248 | ||
249 | =item * Unknown entity | |
250 | ||
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>. | |
253 | ||
254 | =item * No items in =over | |
255 | ||
256 | The list opened with C<=over> does not contain any items. | |
257 | ||
258 | =item * No argument for =item | |
259 | ||
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. | |
264 | ||
265 | =item * empty section in previous paragraph | |
266 | ||
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. | |
270 | ||
271 | =item * Verbatim paragraph in NAME section | |
272 | ||
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. | |
276 | ||
277 | =back | |
278 | ||
279 | =head2 Hyperlinks | |
280 | ||
281 | There are some warnings wrt. malformed hyperlinks. | |
282 | ||
283 | =over 4 | |
284 | ||
285 | =item * ignoring leading/trailing whitespace in link | |
286 | ||
287 | There is whitespace at the beginning or the end of the contents of | |
288 | LE<lt>...E<gt>. | |
289 | ||
290 | =item * (section) in '$page' deprecated | |
291 | ||
292 | There is a section detected in the page name of LE<lt>...E<gt>, e.g. | |
293 | C<LE<lt>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 (). | |
297 | ||
298 | =item * alternative text/node '%s' contains non-escaped | or / | |
299 | ||
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: | |
304 | ||
305 | / E<sol> | |
306 | | E<verbar> | |
307 | ||
308 | =back | |
309 | ||
310 | =head1 RETURN VALUE | |
311 | ||
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. | |
314 | ||
315 | =head1 EXAMPLES | |
316 | ||
317 | I<[T.B.D.]> | |
318 | ||
319 | =head1 INTERFACE | |
320 | ||
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. | |
326 | ||
327 | =cut | |
328 | ||
329 | ############################################################################# | |
330 | ||
331 | use strict; | |
332 | #use diagnostics; | |
333 | use Carp; | |
334 | use Exporter; | |
335 | use Pod::Parser; | |
336 | ||
337 | use vars qw(@ISA @EXPORT); | |
338 | @ISA = qw(Pod::Parser); | |
339 | @EXPORT = qw(&podchecker); | |
340 | ||
341 | use vars qw(%VALID_COMMANDS %VALID_SEQUENCES); | |
342 | ||
343 | my %VALID_COMMANDS = ( | |
344 | 'pod' => 1, | |
345 | 'cut' => 1, | |
346 | 'head1' => 1, | |
347 | 'head2' => 1, | |
348 | 'head3' => 1, | |
349 | 'head4' => 1, | |
350 | 'over' => 1, | |
351 | 'back' => 1, | |
352 | 'item' => 1, | |
353 | 'for' => 1, | |
354 | 'begin' => 1, | |
355 | 'end' => 1, | |
356 | ); | |
357 | ||
358 | my %VALID_SEQUENCES = ( | |
359 | 'I' => 1, | |
360 | 'B' => 1, | |
361 | 'S' => 1, | |
362 | 'C' => 1, | |
363 | 'L' => 1, | |
364 | 'F' => 1, | |
365 | 'X' => 1, | |
366 | 'Z' => 1, | |
367 | 'E' => 1, | |
368 | ); | |
369 | ||
370 | # stolen from HTML::Entities | |
371 | my %ENTITIES = ( | |
372 | # Some normal chars that have special meaning in SGML context | |
373 | amp => '&', # ampersand | |
374 | 'gt' => '>', # greater than | |
375 | 'lt' => '<', # less than | |
376 | quot => '"', # double quote | |
377 | ||
378 | # PUBLIC ISO 8879-1986//ENTITIES Added Latin 1//EN//HTML | |
379 |