1 #############################################################################
2 # Pod/ParseUtils.pm -- helpers for POD parsing and conversion
4 # Copyright (C) 1999 by Marek Rouchal. 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
8 #############################################################################
10 package Pod::ParseUtils;
12 use vars qw($VERSION);
13 $VERSION = 0.2; ## Current version of this package
14 require 5.004; ## requires this Perl version or later
18 Pod::ParseUtils - helpers for POD parsing and conversion
24 my $list = new Pod::List;
25 my $link = Pod::Hyperlink->new('Pod::Parser');
29 B<Pod::ParseUtils> contains a few object-oriented helper packages for
30 POD parsing and processing (i.e. in POD formatters and translators).
34 #-----------------------------------------------------------------------------
37 # class to hold POD list info (=over, =item, =back)
38 #-----------------------------------------------------------------------------
46 B<Pod::List> can be used to hold information about POD lists
47 (written as =over ... =item ... =back) for further processing.
48 The following methods are available:
54 Create a new list object. Properties may be specified through a hash
57 my $list = Pod::List->new({ -start => $., -indent => 4 });
59 See the individual methods/properties for details.
65 my $class = ref($this) || $this;
75 $self->{-file} ||= 'unknown';
76 $self->{-start} ||= 'unknown';
77 $self->{-indent} ||= 4; # perlpod: "should be the default"
79 $self->{-type} ||= '';
84 Without argument, retrieves the file name the list is in. This must
85 have been set before by either specifying B<-file> in the B<new()>
86 method or by calling the B<file()> method with a scalar argument.
90 # The POD file name the list appears in
92 return (@_ > 1) ? ($_[0]->{-file} = $_[1]) : $_[0]->{-file};
97 Without argument, retrieves the line number where the list started.
98 This must have been set before by either specifying B<-start> in the
99 B<new()> method or by calling the B<start()> method with a scalar
104 # The line in the file the node appears
106 return (@_ > 1) ? ($_[0]->{-start} = $_[1]) : $_[0]->{-start};
111 Without argument, retrieves the indent level of the list as specified
112 in C<=over n>. This must have been set before by either specifying
113 B<-indent> in the B<new()> method or by calling the B<indent()> method
114 with a scalar argument.
120 return (@_ > 1) ? ($_[0]->{-indent} = $_[1]) : $_[0]->{-indent};
125 Without argument, retrieves the list type, which can be an arbitrary value,
126 e.g. C<OL>, C<UL>, ... when thinking the HTML way.
127 This must have been set before by either specifying
128 B<-type> in the B<new()> method or by calling the B<type()> method
129 with a scalar argument.
133 # The type of the list (UL, OL, ...)
135 return (@_ > 1) ? ($_[0]->{-type} = $_[1]) : $_[0]->{-type};
140 Without argument, retrieves a regular expression for simplifying the
141 individual item strings once the list type has been determined. Usage:
142 E.g. when converting to HTML, one might strip the leading number in
143 an ordered list as C<E<lt>OLE<gt>> already prints numbers itself.
144 This must have been set before by either specifying
145 B<-rx> in the B<new()> method or by calling the B<rx()> method
146 with a scalar argument.
150 # The regular expression to simplify the items
152 return (@_ > 1) ? ($_[0]->{-rx} = $_[1]) : $_[0]->{-rx};
157 Without argument, retrieves the array of the items in this list.
158 The items may be represented by any scalar.
159 If an argument has been given, it is pushed on the list of items.
163 # The individual =items of this list
165 my ($self,$item) = @_;
167 push(@{$self->{_items}}, $item);
171 return @{$self->{_items}};
177 Without argument, retrieves information about the parent holding this
178 list, which is represented as an arbitrary scalar.
179 This must have been set before by either specifying
180 B<-parent> in the B<new()> method or by calling the B<parent()> method
181 with a scalar argument.
185 # possibility for parsers/translators to store information about the
186 # lists's parent object
188 return (@_ > 1) ? ($_[0]->{-parent} = $_[1]) : $_[0]->{-parent};
193 Without argument, retrieves information about the list tag, which can be
195 This must have been set before by either specifying
196 B<-tag> in the B<new()> method or by calling the B<tag()> method
197 with a scalar argument.
203 # possibility for parsers/translators to store information about the
206 return (@_ > 1) ? ($_[0]->{-tag} = $_[1]) : $_[0]->{-tag};
209 #-----------------------------------------------------------------------------
212 # class to manipulate POD hyperlinks (L<>)
213 #-----------------------------------------------------------------------------
215 package Pod::Hyperlink;
217 =head2 Pod::Hyperlink
219 B<Pod::Hyperlink> is a class for manipulation of POD hyperlinks. Usage:
221 my $link = Pod::Hyperlink->new('alternative text|page/"section in page"');
223 The B<Pod::Hyperlink> class is mainly designed to parse the contents of the
224 C<LE<lt>...E<gt>> sequence, providing a simple interface for accessing the
225 different parts of a POD hyperlink for further processing. It can also be
226 used to construct hyperlinks.
232 The B<new()> method can either be passed a set of key/value pairs or a single
233 scalar value, namely the contents of a C<LE<lt>...E<gt>> sequence. An object
234 of the class C<Pod::Hyperlink> is returned. The value C<undef> indicates a
235 failure, the error message is stored in C<$@>.
243 my $class = ref($this) || $this;
249 # called with a list of parameters
251 $self->_construct_text();
254 # called with L<> contents
255 return undef unless($self->parse($_[0]));
263 $self->{-line} ||= 'undef';
264 $self->{-file} ||= 'undef';
265 $self->{-page} ||= '';
266 $self->{-node} ||= '';
267 $self->{-alttext} ||= '';
268 $self->{-type} ||= 'undef';
269 $self->{_warnings} = [];
274 This method can be used to (re)parse a (new) hyperlink, i.e. the contents
275 of a C<LE<lt>...E<gt>> sequence. The result is stored in the current object.
282 # syntax check the link and extract destination
283 my ($alttext,$page,$node,$type) = ('','','','');
285 $self->{_warnings} = [];
287 # collapse newlines with whitespace
288 if(s/\s*\n+\s*/ /g) {
289 $self->warning("collapsing newlines to blanks");
291 # strip leading/trailing whitespace
293 $self->warning("ignoring leading whitespace in link");
296 $self->warning("ignoring trailing whitespace in link");
299 _invalid_link("empty link");
303 ## Check for different possibilities. This is tedious and error-prone
304 # we match all possibilities (alttext, page, section/item)
305 #warn "DEBUG: link=$_\n";
308 if(m!^(\w+(?:::\w+)*)\s*(\(\w*\)|)$!) {
312 # alttext, page and section
313 elsif(m!^(.+?)\s*[|]\s*(\w+(?:::\w+)*)\s*(\(\w*\)|)\s*/\s*"(.+)"$!) {
314 ($alttext, $page, $node) = ($1, $2 . $3, $4);
318 elsif(m!^(\w+(?:::\w+)*)\s*(\(\w*\)|)\s*/\s*"(.+)"$!) {
319 ($page, $node) = ($1 . $2, $3);
323 elsif(m!^(\w+(?:::\w+)*)\s*(\(\w*\)|)\s*/\s*(.+)$!) {
324 ($page, $node) = ($1 . $2, $3);
328 elsif(m!^(?:/\s*|)"(.+)"$!) {
337 # non-standard: Hyperlink
338 elsif(m!^((?:http|ftp|mailto|news):.+)$!i) {
342 # alttext, page and item
343 elsif(m!^(.+?)\s*[|]\s*(\w+(?:::\w+)*)\s*(\(\w*\)|)\s*/\s*(.+)$!) {
344 ($alttext, $page, $node) = ($1, $2 . $3, $4);
348 elsif(m!^(.+?)\s*[|]\s*(\w+(?:::\w+)*)\s*(\(\w*\)|)$!) {
349 ($alttext, $page) = ($1, $2 . $3);
352 # alttext and section
353 elsif(m!^(.+?)\s*[|]\s*(?:/\s*|)"(.+)"$!) {
354 ($alttext, $node) = ($1,$2);
358 elsif(m!^(.+?)\s*[|]\s*/(.+)$!) {
359 ($alttext, $node) = ($1,$2);
361 # nonstandard: alttext and hyperlink
362 elsif(m!^(.+?)\s*[|]\s*((?:http|ftp|mailto|news):.+)$!) {
363 ($alttext, $node) = ($1,$2);
366 # must be an item or a "malformed" section (without "")
372 if($page =~ /[(]\w*[)]$/) {
373 $self->warning("section in `$page' deprecated");
375 $self->{-page} = $page;
376 $self->{-node} = $node;
377 $self->{-alttext} = $alttext;
378 #warn "DEBUG: page=$page section=$section item=$item alttext=$alttext\n";
379 $self->{-type} = $type;
380 $self->_construct_text();
384 sub _construct_text {
386 my $alttext = $self->alttext();
387 my $type = $self->type();
388 my $section = $self->node();
389 my $page = $self->page();
391 $page =~ s/([(]\w*[)])$// && ($page_ext = $1);
393 $self->{_text} = $alttext;
395 elsif($type eq 'hyperlink') {
396 $self->{_text} = $section;
399 $self->{_text} = (!$section ? '' :
400 $type eq 'item' ? "the $section entry" :
401 "the section on $section" ) .
402 ($page ? ($section ? ' in ':'') . "the $page$page_ext manpage" :
403 ' elsewhere in this document');
405 # for being marked up later
406 # use the non-standard markers P<> and Q<>, so that the resulting
407 # text can be parsed by the translators. It's their job to put
408 # the correct hypertext around the linktext
410 $self->{_markup} = "Q<$alttext>";
412 elsif($type eq 'hyperlink') {
413 $self->{_markup} = "Q<$section>";
416 $self->{_markup} = (!$section ? '' :
417 $type eq 'item' ? "the Q<$section> entry" :
418 "the section on Q<$section>" ) .
419 ($page ? ($section ? ' in ':'') . "the P<$page>$page_ext manpage" :
420 ' elsewhere in this document');
424 =item markup($string)
426 Set/retrieve the textual value of the link. This string contains special
427 markers C<PE<lt>E<gt>> and C<QE<lt>E<gt>> that should be expanded by the
428 translator's interior sequence expansion engine to the
429 formatter-specific code to highlight/activate the hyperlink. The details
430 have to be implemented in the translator.
434 #' retrieve/set markuped text
436 return (@_ > 1) ? ($_[0]->{_markup} = $_[1]) : $_[0]->{_markup};
441 This method returns the textual representation of the hyperlink as above,
442 but without markers (read only). Depending on the link type this is one of
443 the following alternatives (the + and * denote the portions of the text
447 the *$|* entry in the +perlvar+ manpage
448 the section on *OPTIONS* in the +perldoc+ manpage
449 the section on *DESCRIPTION* elsewhere in this document
453 # The complete link's text
460 After parsing, this method returns any warnings encountered during the
465 # Set/retrieve warnings
469 push(@{$self->{_warnings}}, @_);
472 return @{$self->{_warnings}};
477 Just simple slots for storing information about the line and the file
478 the link was encountered in. Has to be filled in manually.
482 # The line in the file the link appears
484 return (@_ > 1) ? ($_[0]->{-line} = $_[1]) : $_[0]->{-line};
487 # The POD file name the link appears in
489 return (@_ > 1) ? ($_[0]->{-file} = $_[1]) : $_[0]->{-file};
494 This method sets or returns the POD page this link points to.
498 # The POD page the link appears on
501 $_[0]->{-page} = $_[1];
502 $_[0]->_construct_text();
509 As above, but the destination node text of the link.
513 # The link destination
516 $_[0]->{-node} = $_[1];
517 $_[0]->_construct_text();
524 Sets or returns an alternative text specified in the link.
528 # Potential alternative text
531 $_[0]->{-alttext} = $_[1];
532 $_[0]->_construct_text();
539 The node type, either C<section> or C<item>. As an unofficial type,
540 there is also C<hyperlink>, derived from e.g. C<LE<lt>http://perl.comE<gt>>
544 # The type: item or headn
546 return (@_ > 1) ? ($_[0]->{-type} = $_[1]) : $_[0]->{-type};
551 Returns the link as contents of C<LE<lt>E<gt>>. Reciprocal to B<parse()>.
560 my $link = $self->page() || '';
562 if($self->type() eq 'section') {
563 $link .= ($link ? '/' : '') . '"' . $self->node() . '"';
565 elsif($self->type() eq 'hyperlink') {
566 $link = $self->node();
569 $link .= '/' . $self->node();
572 if($self->alttext()) {
573 $link = $self->alttext() . '|' . $link;
581 #eval { die "$msg\n" };
583 $@ = $msg; # this seems to work, too!
587 #-----------------------------------------------------------------------------
590 # class to hold POD page details
591 #-----------------------------------------------------------------------------
597 B<Pod::Cache> holds information about a set of POD documents,
598 especially the nodes for hyperlinks.
599 The following methods are available:
605 Create a new cache object. This object can hold an arbitrary number of
606 POD documents of class Pod::Cache::Item.
612 my $class = ref($this) || $this;
620 Add a new item to the cache. Without arguments, this method returns a
621 list of all cache elements.
626 my ($self,%param) = @_;
628 my $item = Pod::Cache::Item->new(%param);
637 =item find_page($name)
639 Look for a POD document named C<$name> in the cache. Returns the
640 reference to the corresponding Pod::Cache::Item object or undef if
648 my ($self,$page) = @_;
650 if($_->page() eq $page) {
657 package Pod::Cache::Item;
659 =head2 Pod::Cache::Item
661 B<Pod::Cache::Item> holds information about individual POD documents,
662 that can be grouped in a Pod::Cache object.
663 It is intended to hold information about the hyperlink nodes of POD
665 The following methods are available:
677 my $class = ref($this) || $this;
679 my $self = {%params};
687 $self->{-nodes} = [] unless(defined $self->{-nodes});
692 Set/retrieve the POD document name (e.g. "Pod::Parser").
698 return (@_ > 1) ? ($_[0]->{-page} = $_[1]) : $_[0]->{-page};
703 Set/retrieve the POD short description as found in the C<=head1 NAME>
708 # The POD description, taken out of NAME if present
710 return (@_ > 1) ? ($_[0]->{-description} = $_[1]) : $_[0]->{-description};
715 Set/retrieve the POD file storage path.
721 return (@_ > 1) ? ($_[0]->{-path} = $_[1]) : $_[0]->{-path};
726 Set/retrieve the POD file name.
732 return (@_ > 1) ? ($_[0]->{-file} = $_[1]) : $_[0]->{-file};
737 Add a node (or a list of nodes) to the document's node list. Note that
738 the order is kept, i.e. start with the first node and end with the last.
739 If no argument is given, the current list of nodes is returned in the
740 same order the nodes have been added.
741 A node can be any scalar, but usually is a pair of node string and
742 unique id for the C<find_node> method to work correctly.
748 my ($self,@nodes) = @_;
750 push(@{$self->{-nodes}}, @nodes);
754 return @{$self->{-nodes}};
758 =item find_node($name)
760 Look for a node named C<$name> in the object's node list. Returns the
761 unique id of the node (i.e. the second element of the array stored in
762 the node arry) or undef if not found.
769 my ($self,$node) = @_;
770 foreach(@{$self->{-nodes}}) {
771 if($_->[0] eq $node) {
781 Marek Rouchal E<lt>marek@saftsack.fs.uni-bayreuth.deE<gt>, borrowing
782 a lot of things from L<pod2man> and L<pod2roff> as well as other POD
783 processing tools by Tom Christiansen, Brad Appleton and Russ Allbery.
787 L<pod2man>, L<pod2roff>, L<Pod::Parser>, L<Pod::Checker>,