Fix for Exporter error reporting behaviour
[p5sagit/p5-mst-13.2.git] / lib / Pod / Text.pm
index 3a1dc7b..e62e7c2 100644 (file)
@@ -1,5 +1,5 @@
 # Pod::Text -- Convert POD data to formatted ASCII text.
-# $Id: Text.pm,v 2.13 2001/10/20 08:07:21 eagle Exp $
+# $Id: Text.pm,v 2.15 2001/11/23 06:14:10 eagle Exp $
 #
 # Copyright 1999, 2000, 2001 by Russ Allbery <rra@stanford.edu>
 #
@@ -26,6 +26,7 @@ require 5.004;
 
 use Carp qw(carp croak);
 use Exporter ();
+use Pod::ParseLink qw(parselink);
 use Pod::Select ();
 
 use strict;
@@ -41,7 +42,7 @@ use vars qw(@ISA @EXPORT %ESCAPES $VERSION);
 # Don't use the CVS revision as the version, since this module is also in Perl
 # core and too many things could munge CVS magic revision strings.  This
 # number should ideally be the same as the CVS revision in podlators, however.
-$VERSION = 2.13;
+$VERSION = 2.15;
 
 
 ##############################################################################
@@ -54,6 +55,7 @@ $VERSION = 2.13;
 # "divide" added by Tim Jenness.
 %ESCAPES = (
     'amp'       =>    '&',      # ampersand
+    'apos'      =>    "'",      # apostrophe
     'lt'        =>    '<',      # left chevron, less-than
     'gt'        =>    '>',      # right chevron, greater-than
     'quot'      =>    '"',      # double quote
@@ -139,7 +141,7 @@ $VERSION = 2.13;
     "copy"      =>    "\xA9",   # Copyright symbol
     "ordf"      =>    "\xAA",   # feminine ordinal indicator
     "not"       =>    "\xAC",   # not sign
-    "shy"       =>    "\xAD",   # soft hyphen
+    "shy"       =>    '',       # soft (discretionary) hyphen
     "reg"       =>    "\xAE",   # registered trademark
     "macr"      =>    "\xAF",   # macron, overline
     "deg"       =>    "\xB0",   # degree sign
@@ -159,6 +161,8 @@ $VERSION = 2.13;
     "iquest"    =>    "\xBF",   # inverted question mark
     "times"     =>    "\xD7",   # multiplication sign
     "divide"    =>    "\xF7",   # division sign
+
+    "nbsp"      =>    "\x01",   # non-breaking space
 );
 
 
@@ -213,7 +217,6 @@ sub command {
     my $command = shift;
     return if $command eq 'pod';
     return if ($$self{EXCLUDE} && $command ne 'end');
-    $self->item ("\n") if defined $$self{ITEM};
     if ($self->can ('cmd_' . $command)) {
         $command = 'cmd_' . $command;
         $self->$command (@_);
@@ -250,46 +253,7 @@ sub textblock {
     local $_ = shift;
     my $line = shift;
 
-    # Perform a little magic to collapse multiple L<> references.  This is
-    # here mostly for backwards-compatibility.  We'll just rewrite the whole
-    # thing into actual text at this part, bypassing the whole internal
-    # sequence parsing thing.
-    s{
-        (
-          L<                    # A link of the form L</something>.
-              /
-              (
-                  [:\w]+        # The item has to be a simple word...
-                  (\(\))?       # ...or simple function.
-              )
-          >
-          (
-              ,?\s+(and\s+)?    # Allow lots of them, conjuncted.
-              L<
-                  /
-                  (
-                      [:\w]+
-                      (\(\))?
-                  )
-              >
-          )+
-        )
-    } {
-        local $_ = $1;
-        s%L</([^>]+)>%$1%g;
-        my @items = split /(?:,?\s+(?:and\s+)?)/;
-        my $string = "the ";
-        my $i;
-        for ($i = 0; $i < @items; $i++) {
-            $string .= $items[$i];
-            $string .= ", " if @items > 2 && $i != $#items;
-            $string .= " and " if ($i == $#items - 1);
-        }
-        $string .= " entries elsewhere in this document";
-        $string;
-    }gex;
-
-    # Now actually interpolate and output the paragraph.
+    # Interpolate and output the paragraph.
     $_ = $self->interpolate ($_, $line);
     s/\s+$/\n/;
     if (defined $$self{ITEM}) {
@@ -304,9 +268,20 @@ sub textblock {
 # Calls code, bold, italic, file, and link to handle those types of sequences,
 # and handles S<>, E<>, X<>, and Z<> directly.
 sub interior_sequence {
-    my $self = shift;
-    my $command = shift;
-    local $_ = shift;
+    local $_;
+    my ($self, $command, $seq);
+    ($self, $command, $_, $seq) = @_;
+
+    # We have to defer processing of the inside of an L<> formatting code.  If
+    # this sequence is nested inside an L<> sequence, return the literal raw
+    # text of it.
+    my $parent = $seq->nested;
+    while (defined $parent) {
+        return $seq->raw_text if ($parent->cmd_name eq 'L');
+        $parent = $parent->nested;
+    }
+
+    # Index entries are ignored in plain text.
     return '' if ($command eq 'X' || $command eq 'Z');
 
     # Expand escapes into the actual character now, warning if invalid.
@@ -328,7 +303,7 @@ sub interior_sequence {
     # For S<>, compress all internal whitespace and then map spaces to \01.
     # When we output the text, we'll map this back.
     if ($command eq 'S') {
-        s/\s{2,}/ /g;
+        s/\s+/ /g;
         tr/ /\01/;
         return $_;
     }
@@ -338,7 +313,7 @@ sub interior_sequence {
     elsif ($command eq 'C') { return $self->seq_c ($_) }
     elsif ($command eq 'F') { return $self->seq_f ($_) }
     elsif ($command eq 'I') { return $self->seq_i ($_) }
-    elsif ($command eq 'L') { return $self->seq_l ($_) }
+    elsif ($command eq 'L') { return $self->seq_l ($_, $seq) }
     else {
         my $seq = shift;
         my ($file, $line) = $seq->file_line;
@@ -367,61 +342,33 @@ sub preprocess_paragraph {
 
 # First level heading.
 sub cmd_head1 {
-    my $self = shift;
-    local $_ = shift;
-    s/\s+$//;
-    $_ = $self->interpolate ($_, shift);
-    if ($$self{alt}) {
-        $self->output ("\n==== $_ ====\n\n");
-    } else {
-        $_ .= "\n" if $$self{loose};
-        $self->output ($_ . "\n");
-    }
+    my ($self, $text, $line) = @_;
+    $self->heading ($text, $line, 0, '====');
 }
 
 # Second level heading.
 sub cmd_head2 {
-    my $self = shift;
-    local $_ = shift;
-    s/\s+$//;
-    $_ = $self->interpolate ($_, shift);
-    if ($$self{alt}) {
-        $self->output ("\n==   $_   ==\n\n");
-    } else {
-        $self->output (' ' x ($$self{indent} / 2) . $_ . "\n\n");
-    }
+    my ($self, $text, $line) = @_;
+    $self->heading ($text, $line, $$self{indent} / 2, '==  ');
 }
 
 # Third level heading.
 sub cmd_head3 {
-    my $self = shift;
-    local $_ = shift;
-    s/\s+$//;
-    $_ = $self->interpolate ($_, shift);
-    if ($$self{alt}) {
-        $self->output ("\n=    $_    =\n\n");
-    } else {
-        $self->output (' ' x ($$self{indent} * 2 / 3 + 0.5) . $_ . "\n\n");
-    }
+    my ($self, $text, $line) = @_;
+    $self->heading ($text, $line, $$self{indent} * 2 / 3 + 0.5, '=   ');
 }
 
 # Third level heading.
 sub cmd_head4 {
-    my $self = shift;
-    local $_ = shift;
-    s/\s+$//;
-    $_ = $self->interpolate ($_, shift);
-    if ($$self{alt}) {
-        $self->output ("\n-    $_    -\n\n");
-    } else {
-        $self->output (' ' x ($$self{indent} * 3 / 4 + 0.5) . $_ . "\n\n");
-    }
+    my ($self, $text, $line) = @_;
+    $self->heading ($text, $line, $$self{indent} * 3 / 4 + 0.5, '-   ');
 }
 
 # Start a list.
 sub cmd_over {
     my $self = shift;
     local $_ = shift;
+    $self->item ("\n\n") if defined $$self{ITEM};
     unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} }
     push (@{ $$self{INDENTS} }, $$self{MARGIN});
     $$self{MARGIN} += ($_ + 0);
@@ -430,6 +377,7 @@ sub cmd_over {
 # End a list.
 sub cmd_back {
     my ($self, $text, $line, $paragraph) = @_;
+    $self->item ("\n\n") if defined $$self{ITEM};
     $$self{MARGIN} = pop @{ $$self{INDENTS} };
     unless (defined $$self{MARGIN}) {
         my $file;
@@ -445,7 +393,7 @@ sub cmd_item {
     if (defined $$self{ITEM}) { $self->item }
     local $_ = shift;
     s/\s+$//;
-    $$self{ITEM} = $self->interpolate ($_);
+    $$self{ITEM} = $_ ? $self->interpolate ($_) : '*';
 }
 
 # Begin a block for a particular translator.  Setting VERBATIM triggers
@@ -522,55 +470,38 @@ sub seq_c {
     return $$self{alt} ? "``$_''" : "$$self{LQUOTE}$_$$self{RQUOTE}";
 }
 
-# The complicated one.  Handle links.  Since this is plain text, we can't
-# actually make any real links, so this is all to figure out what text we
-# print out.
+# Handle links.  Since this is plain text, we can't actually make any real
+# links, so this is all to figure out what text we print out.  Most of the
+# work is done by Pod::ParseLink.
 sub seq_l {
-    my $self = shift;
-    local $_ = shift;
-
-    # Smash whitespace in case we were split across multiple lines.
-    s/\s+/ /g;
-
-    # If we were given any explicit text, just output it.
-    if (/^([^|]+)\|/) { return $1 }
+    my ($self, $link, $seq) = @_;
+    my ($text, $type) = (parselink ($link))[1,4];
+    my ($file, $line) = $seq->file_line;
+    $text = $self->interpolate ($text, $line);
+    $text = '<' . $text . '>' if $type eq 'url';
+    return $text || '';
+}
 
-    # Okay, leading and trailing whitespace isn't important; get rid of it.
-    s/^\s+//;
-    s/\s+$//;
 
-    # If the argument looks like a URL, return it verbatim.  This only handles
-    # URLs that use the server syntax.
-    if (m%^[a-z]+://\S+$%) { return $_ }
-
-    # Default to using the whole content of the link entry as a section name.
-    # Note that L<manpage/> forces a manpage interpretation, as does something
-    # looking like L<manpage(section)>.  The latter is an enhancement over the
-    # original Pod::Text.
-    my ($manpage, $section) = ('', $_);
-    if (/^"\s*(.*?)\s*"$/) {
-        $section = '"' . $1 . '"';
-    } elsif (m/^[-:.\w]+(?:\(\S+\))?$/) {
-        ($manpage, $section) = ($_, '');
-    } elsif (m%/%) {
-        ($manpage, $section) = split (/\s*\/\s*/, $_, 2);
-    }
+##############################################################################
+# Header handling
+##############################################################################
 
-    # Now build the actual output text.
-    my $text = '';
-    if (!length $section) {
-        $text = "the $manpage manpage" if length $manpage;
-    } elsif ($section =~ /^[:\w]+(?:\(\))?/) {
-        $text .= 'the ' . $section . ' entry';
-        $text .= (length $manpage) ? " in the $manpage manpage"
-                                   : " elsewhere in this document";
+# The common code for handling all headers.  Takes the interpolated header
+# text, the line number, the indentation, and the surrounding marker for the
+# alt formatting method.
+sub heading {
+    my ($self, $text, $line, $indent, $marker) = @_;
+    $self->item ("\n\n") if defined $$self{ITEM};
+    $text =~ s/\s+$//;
+    $text = $self->interpolate ($text, $line);
+    if ($$self{alt}) {
+        my $closemark = reverse (split (//, $marker));
+        $self->output ("\n" . "$marker $text $closemark" . "\n\n");
     } else {
-        $section =~ s/^\"\s*//;
-        $section =~ s/\s*\"$//;
-        $text .= 'the section on "' . $section . '"';
-        $text .= " in the $manpage manpage" if length $manpage;
+        $text .= "\n" if $$self{loose};
+        $self->output (' ' x $indent . $text . "\n");
     }
-    $text;
 }
 
 
@@ -603,9 +534,16 @@ sub item {
         $$self{MARGIN} = $indent;
         my $output = $self->reformat ($tag);
         $output =~ s/\n*$/\n/;
+
+        # If the text is just whitespace, we have an empty item paragraph;
+        # this can result from =over/=item/=back without any intermixed
+        # paragraphs.  Insert some whitespace to keep the =item from merging
+        # into the next paragraph.
+        $output .= "\n" if $_ && $_ =~ /^\s*$/;
+
         $self->output ($output);
         $$self{MARGIN} = $margin;
-        $self->output ($self->reformat ($_)) if /\S/;
+        $self->output ($self->reformat ($_)) if $_ && /\S/;
     } else {
         $_ = $self->reformat ($_);
         s/^ /:/ if ($$self{alt} && $indent > 0);
@@ -745,7 +683,7 @@ suitable for nearly any device.
 
 As a derived class from Pod::Parser, Pod::Text supports the same methods and
 interfaces.  See L<Pod::Parser> for all the details; briefly, one creates a
-new parser with C<Pod::Text-E<gt>new()> and then calls either
+new parser with C<< Pod::Text->new() >> and then calls either
 parse_from_filehandle() or parse_from_file().
 
 new() can take options, in the form of key/value pairs, that control the
@@ -870,19 +808,17 @@ though.
 The original Pod::Text contained code to do formatting via termcap
 sequences, although it wasn't turned on by default and it was problematic to
 get it to work at all.  This rewrite doesn't even try to do that, but a
-subclass of it does.  Look for L<Pod::Text::Termcap|Pod::Text::Termcap>.
+subclass of it does.  Look for L<Pod::Text::Termcap>.
 
 =head1 SEE ALSO
 
-L<Pod::Parser|Pod::Parser>, L<Pod::Text::Termcap|Pod::Text::Termcap>,
-pod2text(1)
+L<Pod::Parser>, L<Pod::Text::Termcap>, L<pod2text(1)>
 
 =head1 AUTHOR
 
-Russ Allbery E<lt>rra@stanford.eduE<gt>, based I<very> heavily on the
-original Pod::Text by Tom Christiansen E<lt>tchrist@mox.perl.comE<gt> and
-its conversion to Pod::Parser by Brad Appleton
-E<lt>bradapp@enteract.comE<gt>.
+Russ Allbery <rra@stanford.edu>, based I<very> heavily on the original
+Pod::Text by Tom Christiansen <tchrist@mox.perl.com> and its conversion to
+Pod::Parser by Brad Appleton <bradapp@enteract.com>.
 
 =head1 COPYRIGHT AND LICENSE