| 311 |
dpurdie |
1 |
#############################################################################
|
|
|
2 |
# Pod/Select.pm -- function to select portions of POD docs
|
|
|
3 |
#
|
|
|
4 |
# Copyright (C) 1996-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::Select;
|
|
|
11 |
use strict;
|
|
|
12 |
|
|
|
13 |
use vars qw($VERSION @ISA @EXPORT $MAX_HEADING_LEVEL %myData @section_headings @selected_sections);
|
|
|
14 |
$VERSION = '1.36'; ## Current version of this package
|
|
|
15 |
require 5.005; ## requires this Perl version or later
|
|
|
16 |
use JatsError;
|
|
|
17 |
|
|
|
18 |
#############################################################################
|
|
|
19 |
|
|
|
20 |
=head1 NAME
|
|
|
21 |
|
|
|
22 |
Pod::Select, podselect() - extract selected sections of POD from input
|
|
|
23 |
|
|
|
24 |
=head1 SYNOPSIS
|
|
|
25 |
|
|
|
26 |
use Pod::Select;
|
|
|
27 |
|
|
|
28 |
## Select all the POD sections for each file in @filelist
|
|
|
29 |
## and print the result on standard output.
|
|
|
30 |
podselect(@filelist);
|
|
|
31 |
|
|
|
32 |
## Same as above, but write to tmp.out
|
|
|
33 |
podselect({-output => "tmp.out"}, @filelist):
|
|
|
34 |
|
|
|
35 |
## Select from the given filelist, only those POD sections that are
|
|
|
36 |
## within a 1st level section named any of: NAME, SYNOPSIS, OPTIONS.
|
|
|
37 |
podselect({-sections => ["NAME|SYNOPSIS", "OPTIONS"]}, @filelist):
|
|
|
38 |
|
|
|
39 |
## Select the "DESCRIPTION" section of the PODs from STDIN and write
|
|
|
40 |
## the result to STDERR.
|
|
|
41 |
podselect({-output => ">&STDERR", -sections => ["DESCRIPTION"]}, \*STDIN);
|
|
|
42 |
|
|
|
43 |
or
|
|
|
44 |
|
|
|
45 |
use Pod::Select;
|
|
|
46 |
|
|
|
47 |
## Create a parser object for selecting POD sections from the input
|
|
|
48 |
$parser = new Pod::Select();
|
|
|
49 |
|
|
|
50 |
## Select all the POD sections for each file in @filelist
|
|
|
51 |
## and print the result to tmp.out.
|
|
|
52 |
$parser->parse_from_file("<&STDIN", "tmp.out");
|
|
|
53 |
|
|
|
54 |
## Select from the given filelist, only those POD sections that are
|
|
|
55 |
## within a 1st level section named any of: NAME, SYNOPSIS, OPTIONS.
|
|
|
56 |
$parser->select("NAME|SYNOPSIS", "OPTIONS");
|
|
|
57 |
for (@filelist) { $parser->parse_from_file($_); }
|
|
|
58 |
|
|
|
59 |
## Select the "DESCRIPTION" and "SEE ALSO" sections of the PODs from
|
|
|
60 |
## STDIN and write the result to STDERR.
|
|
|
61 |
$parser->select("DESCRIPTION");
|
|
|
62 |
$parser->add_selection("SEE ALSO");
|
|
|
63 |
$parser->parse_from_filehandle(\*STDIN, \*STDERR);
|
|
|
64 |
|
|
|
65 |
=head1 REQUIRES
|
|
|
66 |
|
|
|
67 |
perl5.005, Pod::Parser, Exporter, Carp
|
|
|
68 |
|
|
|
69 |
=head1 EXPORTS
|
|
|
70 |
|
|
|
71 |
podselect()
|
|
|
72 |
|
|
|
73 |
=head1 DESCRIPTION
|
|
|
74 |
|
|
|
75 |
B<podselect()> is a function which will extract specified sections of
|
|
|
76 |
pod documentation from an input stream. This ability is provided by the
|
|
|
77 |
B<Pod::Select> module which is a subclass of B<Pod::Parser>.
|
|
|
78 |
B<Pod::Select> provides a method named B<select()> to specify the set of
|
|
|
79 |
POD sections to select for processing/printing. B<podselect()> merely
|
|
|
80 |
creates a B<Pod::Select> object and then invokes the B<podselect()>
|
|
|
81 |
followed by B<parse_from_file()>.
|
|
|
82 |
|
|
|
83 |
=head1 SECTION SPECIFICATIONS
|
|
|
84 |
|
|
|
85 |
B<podselect()> and B<Pod::Select::select()> may be given one or more
|
|
|
86 |
"section specifications" to restrict the text processed to only the
|
|
|
87 |
desired set of sections and their corresponding subsections. A section
|
|
|
88 |
specification is a string containing one or more Perl-style regular
|
|
|
89 |
expressions separated by forward slashes ("/"). If you need to use a
|
|
|
90 |
forward slash literally within a section title you can escape it with a
|
|
|
91 |
backslash ("\/").
|
|
|
92 |
|
|
|
93 |
The formal syntax of a section specification is:
|
|
|
94 |
|
|
|
95 |
=over 4
|
|
|
96 |
|
|
|
97 |
=item *
|
|
|
98 |
|
|
|
99 |
I<head1-title-regex>/I<head2-title-regex>/...
|
|
|
100 |
|
|
|
101 |
=back
|
|
|
102 |
|
|
|
103 |
Any omitted or empty regular expressions will default to ".*".
|
|
|
104 |
Please note that each regular expression given is implicitly
|
|
|
105 |
anchored by adding "^" and "$" to the beginning and end. Also, if a
|
|
|
106 |
given regular expression starts with a "!" character, then the
|
|
|
107 |
expression is I<negated> (so C<!foo> would match anything I<except>
|
|
|
108 |
C<foo>).
|
|
|
109 |
|
|
|
110 |
Some example section specifications follow.
|
|
|
111 |
|
|
|
112 |
=over 4
|
|
|
113 |
|
|
|
114 |
=item *
|
|
|
115 |
|
|
|
116 |
Match the C<NAME> and C<SYNOPSIS> sections and all of their subsections:
|
|
|
117 |
|
|
|
118 |
C<NAME|SYNOPSIS>
|
|
|
119 |
|
|
|
120 |
=item *
|
|
|
121 |
|
|
|
122 |
Match only the C<Question> and C<Answer> subsections of the C<DESCRIPTION>
|
|
|
123 |
section:
|
|
|
124 |
|
|
|
125 |
C<DESCRIPTION/Question|Answer>
|
|
|
126 |
|
|
|
127 |
=item *
|
|
|
128 |
|
|
|
129 |
Match the C<Comments> subsection of I<all> sections:
|
|
|
130 |
|
|
|
131 |
C</Comments>
|
|
|
132 |
|
|
|
133 |
=item *
|
|
|
134 |
|
|
|
135 |
Match all subsections of C<DESCRIPTION> I<except> for C<Comments>:
|
|
|
136 |
|
|
|
137 |
C<DESCRIPTION/!Comments>
|
|
|
138 |
|
|
|
139 |
=item *
|
|
|
140 |
|
|
|
141 |
Match the C<DESCRIPTION> section but do I<not> match any of its subsections:
|
|
|
142 |
|
|
|
143 |
C<DESCRIPTION/!.+>
|
|
|
144 |
|
|
|
145 |
=item *
|
|
|
146 |
|
|
|
147 |
Match all top level sections but none of their subsections:
|
|
|
148 |
|
|
|
149 |
C</!.+>
|
|
|
150 |
|
|
|
151 |
=back
|
|
|
152 |
|
|
|
153 |
=begin _NOT_IMPLEMENTED_
|
|
|
154 |
|
|
|
155 |
=head1 RANGE SPECIFICATIONS
|
|
|
156 |
|
|
|
157 |
B<podselect()> and B<Pod::Select::select()> may be given one or more
|
|
|
158 |
"range specifications" to restrict the text processed to only the
|
|
|
159 |
desired ranges of paragraphs in the desired set of sections. A range
|
|
|
160 |
specification is a string containing a single Perl-style regular
|
|
|
161 |
expression (a regex), or else two Perl-style regular expressions
|
|
|
162 |
(regexs) separated by a ".." (Perl's "range" operator is "..").
|
|
|
163 |
The regexs in a range specification are delimited by forward slashes
|
|
|
164 |
("/"). If you need to use a forward slash literally within a regex you
|
|
|
165 |
can escape it with a backslash ("\/").
|
|
|
166 |
|
|
|
167 |
The formal syntax of a range specification is:
|
|
|
168 |
|
|
|
169 |
=over 4
|
|
|
170 |
|
|
|
171 |
=item *
|
|
|
172 |
|
|
|
173 |
/I<start-range-regex>/[../I<end-range-regex>/]
|
|
|
174 |
|
|
|
175 |
=back
|
|
|
176 |
|
|
|
177 |
Where each the item inside square brackets (the ".." followed by the
|
|
|
178 |
end-range-regex) is optional. Each "range-regex" is of the form:
|
|
|
179 |
|
|
|
180 |
=cmd-expr text-expr
|
|
|
181 |
|
|
|
182 |
Where I<cmd-expr> is intended to match the name of one or more POD
|
|
|
183 |
commands, and I<text-expr> is intended to match the paragraph text for
|
|
|
184 |
the command. If a range-regex is supposed to match a POD command, then
|
|
|
185 |
the first character of the regex (the one after the initial '/')
|
|
|
186 |
absolutely I<must> be a single '=' character; it may not be anything
|
|
|
187 |
else (not even a regex meta-character) if it is supposed to match
|
|
|
188 |
against the name of a POD command.
|
|
|
189 |
|
|
|
190 |
If no I<=cmd-expr> is given then the text-expr will be matched against
|
|
|
191 |
plain textblocks unless it is preceded by a space, in which case it is
|
|
|
192 |
matched against verbatim text-blocks. If no I<text-expr> is given then
|
|
|
193 |
only the command-portion of the paragraph is matched against.
|
|
|
194 |
|
|
|
195 |
Note that these two expressions are each implicitly anchored. This
|
|
|
196 |
means that when matching against the command-name, there will be an
|
|
|
197 |
implicit '^' and '$' around the given I<=cmd-expr>; and when matching
|
|
|
198 |
against the paragraph text there will be an implicit '\A' and '\Z'
|
|
|
199 |
around the given I<text-expr>.
|
|
|
200 |
|
|
|
201 |
Unlike with section-specs, the '!' character does I<not> have any special
|
|
|
202 |
meaning (negation or otherwise) at the beginning of a range-spec!
|
|
|
203 |
|
|
|
204 |
Some example range specifications follow.
|
|
|
205 |
|
|
|
206 |
=over 4
|
|
|
207 |
|
|
|
208 |
=item
|
|
|
209 |
Match all C<=for html> paragraphs:
|
|
|
210 |
|
|
|
211 |
C</=for html/>
|
|
|
212 |
|
|
|
213 |
=item
|
|
|
214 |
Match all paragraphs between C<=begin html> and C<=end html>
|
|
|
215 |
(note that this will I<not> work correctly if such sections
|
|
|
216 |
are nested):
|
|
|
217 |
|
|
|
218 |
C</=begin html/../=end html/>
|
|
|
219 |
|
|
|
220 |
=item
|
|
|
221 |
Match all paragraphs between the given C<=item> name until the end of the
|
|
|
222 |
current section:
|
|
|
223 |
|
|
|
224 |
C</=item mine/../=head\d/>
|
|
|
225 |
|
|
|
226 |
=item
|
|
|
227 |
Match all paragraphs between the given C<=item> until the next item, or
|
|
|
228 |
until the end of the itemized list (note that this will I<not> work as
|
|
|
229 |
desired if the item contains an itemized list nested within it):
|
|
|
230 |
|
|
|
231 |
C</=item mine/../=(item|back)/>
|
|
|
232 |
|
|
|
233 |
=back
|
|
|
234 |
|
|
|
235 |
=end _NOT_IMPLEMENTED_
|
|
|
236 |
|
|
|
237 |
=cut
|
|
|
238 |
|
|
|
239 |
#############################################################################
|
|
|
240 |
|
|
|
241 |
#use diagnostics;
|
|
|
242 |
use Carp;
|
|
|
243 |
use Pod::Parser 1.04;
|
|
|
244 |
|
|
|
245 |
@ISA = qw(Pod::Parser);
|
|
|
246 |
@EXPORT = qw(&podselect);
|
|
|
247 |
|
|
|
248 |
## Maximum number of heading levels supported for '=headN' directives
|
|
|
249 |
*MAX_HEADING_LEVEL = \3;
|
|
|
250 |
|
|
|
251 |
#############################################################################
|
|
|
252 |
|
|
|
253 |
=head1 OBJECT METHODS
|
|
|
254 |
|
|
|
255 |
The following methods are provided in this module. Each one takes a
|
|
|
256 |
reference to the object itself as an implicit first parameter.
|
|
|
257 |
|
|
|
258 |
=cut
|
|
|
259 |
|
|
|
260 |
##---------------------------------------------------------------------------
|
|
|
261 |
|
|
|
262 |
## =begin _PRIVATE_
|
|
|
263 |
##
|
|
|
264 |
## =head1 B<_init_headings()>
|
|
|
265 |
##
|
|
|
266 |
## Initialize the current set of active section headings.
|
|
|
267 |
##
|
|
|
268 |
## =cut
|
|
|
269 |
##
|
|
|
270 |
## =end _PRIVATE_
|
|
|
271 |
|
|
|
272 |
sub _init_headings {
|
|
|
273 |
my $self = shift;
|
|
|
274 |
local *myData = $self;
|
|
|
275 |
|
|
|
276 |
## Initialize current section heading titles if necessary
|
|
|
277 |
unless (defined $myData{_SECTION_HEADINGS}) {
|
|
|
278 |
local *section_headings = $myData{_SECTION_HEADINGS} = [];
|
|
|
279 |
for (my $i = 0; $i < $MAX_HEADING_LEVEL; ++$i) {
|
|
|
280 |
$section_headings[$i] = '';
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
##---------------------------------------------------------------------------
|
|
|
286 |
|
|
|
287 |
=head1 B<curr_headings()>
|
|
|
288 |
|
|
|
289 |
($head1, $head2, $head3, ...) = $parser->curr_headings();
|
|
|
290 |
$head1 = $parser->curr_headings(1);
|
|
|
291 |
|
|
|
292 |
This method returns a list of the currently active section headings and
|
|
|
293 |
subheadings in the document being parsed. The list of headings returned
|
|
|
294 |
corresponds to the most recently parsed paragraph of the input.
|
|
|
295 |
|
|
|
296 |
If an argument is given, it must correspond to the desired section
|
|
|
297 |
heading number, in which case only the specified section heading is
|
|
|
298 |
returned. If there is no current section heading at the specified
|
|
|
299 |
level, then C<undef> is returned.
|
|
|
300 |
|
|
|
301 |
=cut
|
|
|
302 |
|
|
|
303 |
sub curr_headings {
|
|
|
304 |
my $self = shift;
|
|
|
305 |
$self->_init_headings() unless (defined $self->{_SECTION_HEADINGS});
|
|
|
306 |
my @headings = @{ $self->{_SECTION_HEADINGS} };
|
|
|
307 |
return (@_ > 0 and $_[0] =~ /^\d+$/) ? $headings[$_[0] - 1] : @headings;
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
##---------------------------------------------------------------------------
|
|
|
311 |
|
|
|
312 |
=head1 B<select()>
|
|
|
313 |
|
|
|
314 |
$parser->select($section_spec1,$section_spec2,...);
|
|
|
315 |
|
|
|
316 |
This method is used to select the particular sections and subsections of
|
|
|
317 |
POD documentation that are to be printed and/or processed. The existing
|
|
|
318 |
set of selected sections is I<replaced> with the given set of sections.
|
|
|
319 |
See B<add_selection()> for adding to the current set of selected
|
|
|
320 |
sections.
|
|
|
321 |
|
|
|
322 |
Each of the C<$section_spec> arguments should be a section specification
|
|
|
323 |
as described in L<"SECTION SPECIFICATIONS">. The section specifications
|
|
|
324 |
are parsed by this method and the resulting regular expressions are
|
|
|
325 |
stored in the invoking object.
|
|
|
326 |
|
|
|
327 |
If no C<$section_spec> arguments are given, then the existing set of
|
|
|
328 |
selected sections is cleared out (which means C<all> sections will be
|
|
|
329 |
processed).
|
|
|
330 |
|
|
|
331 |
This method should I<not> normally be overridden by subclasses.
|
|
|
332 |
|
|
|
333 |
=cut
|
|
|
334 |
|
|
|
335 |
sub select {
|
|
|
336 |
my ($self, @sections) = @_;
|
|
|
337 |
local *myData = $self;
|
|
|
338 |
local $_;
|
|
|
339 |
|
|
|
340 |
### NEED TO DISCERN A SECTION-SPEC FROM A RANGE-SPEC (look for m{^/.+/$}?)
|
|
|
341 |
|
|
|
342 |
##---------------------------------------------------------------------
|
|
|
343 |
## The following is a blatant hack for backward compatibility, and for
|
|
|
344 |
## implementing add_selection(). If the *first* *argument* is the
|
|
|
345 |
## string "+", then the remaining section specifications are *added*
|
|
|
346 |
## to the current set of selections; otherwise the given section
|
|
|
347 |
## specifications will *replace* the current set of selections.
|
|
|
348 |
##
|
|
|
349 |
## This should probably be fixed someday, but for the present time,
|
|
|
350 |
## it seems incredibly unlikely that "+" would ever correspond to
|
|
|
351 |
## a legitimate section heading
|
|
|
352 |
##---------------------------------------------------------------------
|
|
|
353 |
my $add = ($sections[0] eq '+') ? shift(@sections) : '';
|
|
|
354 |
|
|
|
355 |
## Reset the set of sections to use
|
|
|
356 |
unless (@sections) {
|
|
|
357 |
delete $myData{_SELECTED_SECTIONS} unless ($add);
|
|
|
358 |
return;
|
|
|
359 |
}
|
|
|
360 |
$myData{_SELECTED_SECTIONS} = []
|
|
|
361 |
unless ($add && exists $myData{_SELECTED_SECTIONS});
|
|
|
362 |
local *selected_sections = $myData{_SELECTED_SECTIONS};
|
|
|
363 |
|
|
|
364 |
## Compile each spec
|
|
|
365 |
for my $spec (@sections) {
|
|
|
366 |
if ( defined($_ = _compile_section_spec($spec)) ) {
|
|
|
367 |
## Store them in our sections array
|
|
|
368 |
push(@selected_sections, $_);
|
|
|
369 |
}
|
|
|
370 |
else {
|
|
|
371 |
carp qq{Ignoring section spec "$spec"!\n};
|
|
|
372 |
}
|
|
|
373 |
}
|
|
|
374 |
#DebugDumpData("select",\@sections, \@selected_sections );
|
|
|
375 |
}
|
|
|
376 |
|
|
|
377 |
##---------------------------------------------------------------------------
|
|
|
378 |
|
|
|
379 |
=head1 B<add_selection()>
|
|
|
380 |
|
|
|
381 |
$parser->add_selection($section_spec1,$section_spec2,...);
|
|
|
382 |
|
|
|
383 |
This method is used to add to the currently selected sections and
|
|
|
384 |
subsections of POD documentation that are to be printed and/or
|
|
|
385 |
processed. See <select()> for replacing the currently selected sections.
|
|
|
386 |
|
|
|
387 |
Each of the C<$section_spec> arguments should be a section specification
|
|
|
388 |
as described in L<"SECTION SPECIFICATIONS">. The section specifications
|
|
|
389 |
are parsed by this method and the resulting regular expressions are
|
|
|
390 |
stored in the invoking object.
|
|
|
391 |
|
|
|
392 |
This method should I<not> normally be overridden by subclasses.
|
|
|
393 |
|
|
|
394 |
=cut
|
|
|
395 |
|
|
|
396 |
sub add_selection {
|
|
|
397 |
my $self = shift;
|
|
|
398 |
return $self->select('+', @_);
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
##---------------------------------------------------------------------------
|
|
|
402 |
|
|
|
403 |
=head1 B<clear_selections()>
|
|
|
404 |
|
|
|
405 |
$parser->clear_selections();
|
|
|
406 |
|
|
|
407 |
This method takes no arguments, it has the exact same effect as invoking
|
|
|
408 |
<select()> with no arguments.
|
|
|
409 |
|
|
|
410 |
=cut
|
|
|
411 |
|
|
|
412 |
sub clear_selections {
|
|
|
413 |
my $self = shift;
|
|
|
414 |
return $self->select();
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
##---------------------------------------------------------------------------
|
|
|
418 |
|
|
|
419 |
=head1 B<match_section()>
|
|
|
420 |
|
|
|
421 |
$boolean = $parser->match_section($heading1,$heading2,...);
|
|
|
422 |
|
|
|
423 |
Returns a value of true if the given section and subsection heading
|
|
|
424 |
titles match any of the currently selected section specifications in
|
|
|
425 |
effect from prior calls to B<select()> and B<add_selection()> (or if
|
|
|
426 |
there are no explicitly selected/deselected sections).
|
|
|
427 |
|
|
|
428 |
The arguments C<$heading1>, C<$heading2>, etc. are the heading titles of
|
|
|
429 |
the corresponding sections, subsections, etc. to try and match. If
|
|
|
430 |
C<$headingN> is omitted then it defaults to the current corresponding
|
|
|
431 |
section heading title in the input.
|
|
|
432 |
|
|
|
433 |
This method should I<not> normally be overridden by subclasses.
|
|
|
434 |
|
|
|
435 |
=cut
|
|
|
436 |
|
|
|
437 |
sub match_section {
|
|
|
438 |
my $self = shift;
|
|
|
439 |
my (@headings) = @_;
|
|
|
440 |
local *myData = $self;
|
|
|
441 |
|
|
|
442 |
## Return true if no restrictions were explicitly specified
|
|
|
443 |
my $selections = (exists $myData{_SELECTED_SECTIONS})
|
|
|
444 |
? $myData{_SELECTED_SECTIONS} : undef;
|
|
|
445 |
return 1 unless ((defined $selections) && @{$selections});
|
|
|
446 |
|
|
|
447 |
## Default any unspecified sections to the current one
|
|
|
448 |
my @current_headings = $self->curr_headings();
|
|
|
449 |
for (my $i = 0; $i < $MAX_HEADING_LEVEL; ++$i) {
|
|
|
450 |
(defined $headings[$i]) or $headings[$i] = $current_headings[$i];
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
## Look for a match against the specified section expressions
|
|
|
454 |
for my $section_spec ( @{$selections} ) {
|
|
|
455 |
##------------------------------------------------------
|
|
|
456 |
## Each portion of this spec must match in order for
|
|
|
457 |
## the spec to be matched. So we will start with a
|
|
|
458 |
## match-value of 'true' and logically 'and' it with
|
|
|
459 |
## the results of matching a given element of the spec.
|
|
|
460 |
##------------------------------------------------------
|
|
|
461 |
my $match = 1;
|
|
|
462 |
#DebugDumpData("Match", $section_spec, \@headings );
|
|
|
463 |
for (my $i = 0; $i < $MAX_HEADING_LEVEL; ++$i) {
|
|
|
464 |
my $regex = $section_spec->[$i];
|
|
|
465 |
my $negated = ($regex =~ s/^\!//);
|
|
|
466 |
$match &= ($negated ? ($headings[$i] !~ /${regex}/)
|
|
|
467 |
: ($headings[$i] =~ /${regex}/));
|
|
|
468 |
last unless ($match);
|
|
|
469 |
}
|
|
|
470 |
return 1 if ($match);
|
|
|
471 |
}
|
|
|
472 |
return 0; ## no match
|
|
|
473 |
}
|
|
|
474 |
|
|
|
475 |
##---------------------------------------------------------------------------
|
|
|
476 |
|
|
|
477 |
=head1 B<is_selected()>
|
|
|
478 |
|
|
|
479 |
$boolean = $parser->is_selected($paragraph);
|
|
|
480 |
|
|
|
481 |
This method is used to determine if the block of text given in
|
|
|
482 |
C<$paragraph> falls within the currently selected set of POD sections
|
|
|
483 |
and subsections to be printed or processed. This method is also
|
|
|
484 |
responsible for keeping track of the current input section and
|
|
|
485 |
subsections. It is assumed that C<$paragraph> is the most recently read
|
|
|
486 |
(but not yet processed) input paragraph.
|
|
|
487 |
|
|
|
488 |
The value returned will be true if the C<$paragraph> and the rest of the
|
|
|
489 |
text in the same section as C<$paragraph> should be selected (included)
|
|
|
490 |
for processing; otherwise a false value is returned.
|
|
|
491 |
|
|
|
492 |
=cut
|
|
|
493 |
|
|
|
494 |
sub is_selected {
|
|
|
495 |
my ($self, $paragraph) = @_;
|
|
|
496 |
local $_;
|
|
|
497 |
local *myData = $self;
|
|
|
498 |
|
|
|
499 |
$self->_init_headings() unless (defined $myData{_SECTION_HEADINGS});
|
|
|
500 |
|
|
|
501 |
## Keep track of current sections levels and headings
|
|
|
502 |
$_ = $paragraph;
|
|
|
503 |
if (/^=((?:sub)*)(?:head(?:ing)?|sec(?:tion)?)(\d*)\s+(.*?)\s*$/)
|
|
|
504 |
{
|
|
|
505 |
## This is a section heading command
|
|
|
506 |
my ($level, $heading) = ($2, $3);
|
|
|
507 |
$level = 1 + (length($1) / 3) if ((! length $level) || (length $1));
|
|
|
508 |
## Reset the current section heading at this level
|
|
|
509 |
$myData{_SECTION_HEADINGS}->[$level - 1] = $heading;
|
|
|
510 |
## Reset subsection headings of this one to empty
|
|
|
511 |
for (my $i = $level; $i < $MAX_HEADING_LEVEL; ++$i) {
|
|
|
512 |
$myData{_SECTION_HEADINGS}->[$i] = '';
|
|
|
513 |
}
|
|
|
514 |
}
|
|
|
515 |
|
|
|
516 |
return $self->match_section();
|
|
|
517 |
}
|
|
|
518 |
|
|
|
519 |
#############################################################################
|
|
|
520 |
|
|
|
521 |
=head1 EXPORTED FUNCTIONS
|
|
|
522 |
|
|
|
523 |
The following functions are exported by this module. Please note that
|
|
|
524 |
these are functions (not methods) and therefore C<do not> take an
|
|
|
525 |
implicit first argument.
|
|
|
526 |
|
|
|
527 |
=cut
|
|
|
528 |
|
|
|
529 |
##---------------------------------------------------------------------------
|
|
|
530 |
|
|
|
531 |
=head1 B<podselect()>
|
|
|
532 |
|
|
|
533 |
podselect(\%options,@filelist);
|
|
|
534 |
|
|
|
535 |
B<podselect> will print the raw (untranslated) POD paragraphs of all
|
|
|
536 |
POD sections in the given input files specified by C<@filelist>
|
|
|
537 |
according to the given options.
|
|
|
538 |
|
|
|
539 |
If any argument to B<podselect> is a reference to a hash
|
|
|
540 |
(associative array) then the values with the following keys are
|
|
|
541 |
processed as follows:
|
|
|
542 |
|
|
|
543 |
=over 4
|
|
|
544 |
|
|
|
545 |
=item B<-output>
|
|
|
546 |
|
|
|
547 |
A string corresponding to the desired output file (or ">&STDOUT"
|
|
|
548 |
or ">&STDERR"). The default is to use standard output.
|
|
|
549 |
|
|
|
550 |
=item B<-sections>
|
|
|
551 |
|
|
|
552 |
A reference to an array of sections specifications (as described in
|
|
|
553 |
L<"SECTION SPECIFICATIONS">) which indicate the desired set of POD
|
|
|
554 |
sections and subsections to be selected from input. If no section
|
|
|
555 |
specifications are given, then all sections of the PODs are used.
|
|
|
556 |
|
|
|
557 |
=begin _NOT_IMPLEMENTED_
|
|
|
558 |
|
|
|
559 |
=item B<-ranges>
|
|
|
560 |
|
|
|
561 |
A reference to an array of range specifications (as described in
|
|
|
562 |
L<"RANGE SPECIFICATIONS">) which indicate the desired range of POD
|
|
|
563 |
paragraphs to be selected from the desired input sections. If no range
|
|
|
564 |
specifications are given, then all paragraphs of the desired sections
|
|
|
565 |
are used.
|
|
|
566 |
|
|
|
567 |
=end _NOT_IMPLEMENTED_
|
|
|
568 |
|
|
|
569 |
=back
|
|
|
570 |
|
|
|
571 |
All other arguments should correspond to the names of input files
|
|
|
572 |
containing POD sections. A file name of "-" or "<&STDIN" will
|
|
|
573 |
be interpreted to mean standard input (which is the default if no
|
|
|
574 |
filenames are given).
|
|
|
575 |
|
|
|
576 |
=cut
|
|
|
577 |
|
|
|
578 |
sub podselect {
|
|
|
579 |
my(@argv) = @_;
|
|
|
580 |
my %defaults = ();
|
|
|
581 |
my $pod_parser = new Pod::Select(%defaults);
|
|
|
582 |
my $num_inputs = 0;
|
|
|
583 |
my $output = '>&STDOUT';
|
|
|
584 |
my %opts;
|
|
|
585 |
local $_;
|
|
|
586 |
for (@argv) {
|
|
|
587 |
if (ref($_)) {
|
|
|
588 |
next unless (ref($_) eq 'HASH');
|
|
|
589 |
%opts = (%defaults, %{$_});
|
|
|
590 |
|
|
|
591 |
##-------------------------------------------------------------
|
|
|
592 |
## Need this for backward compatibility since we formerly used
|
|
|
593 |
## options that were all uppercase words rather than ones that
|
|
|
594 |
## looked like Unix command-line options.
|
|
|
595 |
## to be uppercase keywords)
|
|
|
596 |
##-------------------------------------------------------------
|
|
|
597 |
%opts = map {
|
|
|
598 |
my ($key, $val) = (lc $_, $opts{$_});
|
|
|
599 |
$key =~ s/^(?=\w)/-/;
|
|
|
600 |
$key =~ /^-se[cl]/ and $key = '-sections';
|
|
|
601 |
#! $key eq '-range' and $key .= 's';
|
|
|
602 |
($key => $val);
|
|
|
603 |
} (keys %opts);
|
|
|
604 |
|
|
|
605 |
## Process the options
|
|
|
606 |
(exists $opts{'-output'}) and $output = $opts{'-output'};
|
|
|
607 |
|
|
|
608 |
## Select the desired sections
|
|
|
609 |
$pod_parser->select(@{ $opts{'-sections'} })
|
|
|
610 |
if ( (defined $opts{'-sections'})
|
|
|
611 |
&& ((ref $opts{'-sections'}) eq 'ARRAY') );
|
|
|
612 |
|
|
|
613 |
#! ## Select the desired paragraph ranges
|
|
|
614 |
#! $pod_parser->select(@{ $opts{'-ranges'} })
|
|
|
615 |
#! if ( (defined $opts{'-ranges'})
|
|
|
616 |
#! && ((ref $opts{'-ranges'}) eq 'ARRAY') );
|
|
|
617 |
}
|
|
|
618 |
else {
|
|
|
619 |
$pod_parser->parse_from_file($_, $output);
|
|
|
620 |
++$num_inputs;
|
|
|
621 |
}
|
|
|
622 |
}
|
|
|
623 |
$pod_parser->parse_from_file('-') unless ($num_inputs > 0);
|
|
|
624 |
}
|
|
|
625 |
|
|
|
626 |
#############################################################################
|
|
|
627 |
|
|
|
628 |
=head1 PRIVATE METHODS AND DATA
|
|
|
629 |
|
|
|
630 |
B<Pod::Select> makes uses a number of internal methods and data fields
|
|
|
631 |
which clients should not need to see or use. For the sake of avoiding
|
|
|
632 |
name collisions with client data and methods, these methods and fields
|
|
|
633 |
are briefly discussed here. Determined hackers may obtain further
|
|
|
634 |
information about them by reading the B<Pod::Select> source code.
|
|
|
635 |
|
|
|
636 |
Private data fields are stored in the hash-object whose reference is
|
|
|
637 |
returned by the B<new()> constructor for this class. The names of all
|
|
|
638 |
private methods and data-fields used by B<Pod::Select> begin with a
|
|
|
639 |
prefix of "_" and match the regular expression C</^_\w+$/>.
|
|
|
640 |
|
|
|
641 |
=cut
|
|
|
642 |
|
|
|
643 |
##---------------------------------------------------------------------------
|
|
|
644 |
|
|
|
645 |
=begin _PRIVATE_
|
|
|
646 |
|
|
|
647 |
=head1 B<_compile_section_spec()>
|
|
|
648 |
|
|
|
649 |
$listref = $parser->_compile_section_spec($section_spec);
|
|
|
650 |
|
|
|
651 |
This function (note it is a function and I<not> a method) takes a
|
|
|
652 |
section specification (as described in L<"SECTION SPECIFICATIONS">)
|
|
|
653 |
given in C<$section_sepc>, and compiles it into a list of regular
|
|
|
654 |
expressions. If C<$section_spec> has no syntax errors, then a reference
|
|
|
655 |
to the list (array) of corresponding regular expressions is returned;
|
|
|
656 |
otherwise C<undef> is returned and an error message is printed (using
|
|
|
657 |
B<carp>) for each invalid regex.
|
|
|
658 |
|
|
|
659 |
=end _PRIVATE_
|
|
|
660 |
|
|
|
661 |
=cut
|
|
|
662 |
|
|
|
663 |
sub _compile_section_spec {
|
|
|
664 |
my ($section_spec) = @_;
|
|
|
665 |
my (@regexs, $negated);
|
|
|
666 |
|
|
|
667 |
## Compile the spec into a list of regexs
|
|
|
668 |
local $_ = $section_spec;
|
|
|
669 |
s{\\\\}{\001}g; ## handle escaped backward slashes
|
|
|
670 |
s{\\/}{\002}g; ## handle escaped forward slashes
|
|
|
671 |
|
|
|
672 |
## Parse the regexs for the heading titles
|
|
|
673 |
@regexs = split(/\//, $_, $MAX_HEADING_LEVEL);
|
|
|
674 |
|
|
|
675 |
## Set default regex for ommitted levels
|
|
|
676 |
for (my $i = 0; $i < $MAX_HEADING_LEVEL; ++$i) {
|
|
|
677 |
$regexs[$i] = '.*' unless ((defined $regexs[$i])
|
|
|
678 |
&& (length $regexs[$i]));
|
|
|
679 |
}
|
|
|
680 |
## Modify the regexs as needed and validate their syntax
|
|
|
681 |
my $bad_regexs = 0;
|
|
|
682 |
for (@regexs) {
|
|
|
683 |
$_ .= '.+' if ($_ eq '!');
|
|
|
684 |
s{\001}{\\\\}g; ## restore escaped backward slashes
|
|
|
685 |
s{\002}{\\/}g; ## restore escaped forward slashes
|
|
|
686 |
$negated = s/^\!//; ## check for negation
|
|
|
687 |
eval "m{$_}"; ## check regex syntax
|
|
|
688 |
if ($@) {
|
|
|
689 |
++$bad_regexs;
|
|
|
690 |
carp qq{Bad regular expression /$_/ in "$section_spec": $@\n};
|
|
|
691 |
}
|
|
|
692 |
else {
|
|
|
693 |
## Add the forward and rear anchors (and put the negator back)
|
|
|
694 |
$_ = '^' . $_ unless (/^\^/);
|
|
|
695 |
$_ = $_ . '$' unless (/\$$/);
|
|
|
696 |
$_ = '!' . $_ if ($negated);
|
|
|
697 |
}
|
|
|
698 |
}
|
|
|
699 |
return (! $bad_regexs) ? [ @regexs ] : undef;
|
|
|
700 |
}
|
|
|
701 |
|
|
|
702 |
##---------------------------------------------------------------------------
|
|
|
703 |
|
|
|
704 |
=begin _PRIVATE_
|
|
|
705 |
|
|
|
706 |
=head2 $self->{_SECTION_HEADINGS}
|
|
|
707 |
|
|
|
708 |
A reference to an array of the current section heading titles for each
|
|
|
709 |
heading level (note that the first heading level title is at index 0).
|
|
|
710 |
|
|
|
711 |
=end _PRIVATE_
|
|
|
712 |
|
|
|
713 |
=cut
|
|
|
714 |
|
|
|
715 |
##---------------------------------------------------------------------------
|
|
|
716 |
|
|
|
717 |
=begin _PRIVATE_
|
|
|
718 |
|
|
|
719 |
=head2 $self->{_SELECTED_SECTIONS}
|
|
|
720 |
|
|
|
721 |
A reference to an array of references to arrays. Each subarray is a list
|
|
|
722 |
of anchored regular expressions (preceded by a "!" if the expression is to
|
|
|
723 |
be negated). The index of the expression in the subarray should correspond
|
|
|
724 |
to the index of the heading title in C<$self-E<gt>{_SECTION_HEADINGS}>
|
|
|
725 |
that it is to be matched against.
|
|
|
726 |
|
|
|
727 |
=end _PRIVATE_
|
|
|
728 |
|
|
|
729 |
=cut
|
|
|
730 |
|
|
|
731 |
#############################################################################
|
|
|
732 |
|
|
|
733 |
=head1 SEE ALSO
|
|
|
734 |
|
|
|
735 |
L<Pod::Parser>
|
|
|
736 |
|
|
|
737 |
=head1 AUTHOR
|
|
|
738 |
|
|
|
739 |
Please report bugs using L<http://rt.cpan.org>.
|
|
|
740 |
|
|
|
741 |
Brad Appleton E<lt>bradapp@enteract.comE<gt>
|
|
|
742 |
|
|
|
743 |
Based on code for B<pod2text> written by
|
|
|
744 |
Tom Christiansen E<lt>tchrist@mox.perl.comE<gt>
|
|
|
745 |
|
|
|
746 |
=cut
|
|
|
747 |
|
|
|
748 |
1;
|
|
|
749 |
# vim: ts=4 sw=4 et
|