Integrate #16254 from macperl;
[p5sagit/p5-mst-13.2.git] / lib / Pod / Text / Overstrike.pm
index c405235..8b19fb4 100644 (file)
@@ -1,5 +1,5 @@
 # Pod::Text::Overstrike -- Convert POD data to formatted overstrike text
-# $Id: Overstrike.pm,v 1.3 2001/10/20 08:11:29 eagle Exp $
+# $Id: Overstrike.pm,v 1.8 2002/02/17 04:38:03 eagle Exp $
 #
 # Created by Joe Smith <Joe.Smith@inwap.com> 30-Nov-2000
 #   (based on Pod::Text::Color by Russ Allbery <rra@stanford.edu>)
@@ -36,7 +36,7 @@ use vars qw(@ISA $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 = 1.03;
+$VERSION = 1.08;
 
 
 ##############################################################################
@@ -45,41 +45,53 @@ $VERSION = 1.03;
 
 # Make level one headings bold, overridding any existing formatting.
 sub cmd_head1 {
-    my $self = shift;
-    local $_ = shift;
-    s/\s+$//;
-    s/(.)\cH\1//g;
-    s/_\cH//g;
-    s/(.)/$1\b$1/g;
-    $self->SUPER::cmd_head1 ($_);
+    my ($self, $text, $line) = @_;
+    $text =~ s/\s+$//;
+    $text = $self->strip_format ($self->interpolate ($text, $line));
+    $text =~ s/(.)/$1\b$1/g;
+    $self->SUPER::cmd_head1 ($text);
 }
 
 # Make level two headings bold, overriding any existing formatting.
 sub cmd_head2 {
-    my $self = shift;
-    local $_ = shift;
-    s/\s+$//;
-    s/(.)\cH\1//g;
-    s/_\cH//g;
-    s/(.)/$1\b$1/g;
-    $self->SUPER::cmd_head2 ($_);
+    my ($self, $text, $line) = @_;
+    $text =~ s/\s+$//;
+    $text = $self->strip_format ($self->interpolate ($text, $line));
+    $text =~ s/(.)/$1\b$1/g;
+    $self->SUPER::cmd_head2 ($text);
 }
 
 # Make level three headings underscored, overriding any existing formatting.
 sub cmd_head3 {
-    my $self = shift;
-    local $_ = shift;
-    s/\s+$//;
-    s/(.)\cH\1//g;
-    s/_\cH//g;
-    s/(.)/_\b$1/g;
-    $self->SUPER::cmd_head3 ($_);
+    my ($self, $text, $line) = @_;
+    $text =~ s/\s+$//;
+    $text = $self->strip_format ($self->interpolate ($text, $line));
+    $text =~ s/(.)/_\b$1/g;
+    $self->SUPER::cmd_head3 ($text);
 }
 
-# Fix the various interior sequences.
-sub seq_b { local $_ = $_[1]; s/(.)\cH\1//g; s/_\cH//g; s/(.)/$1\b$1/g; $_ }
-sub seq_f { local $_ = $_[1]; s/(.)\cH\1//g; s/_\cH//g; s/(.)/_\b$1/g; $_ }
-sub seq_i { local $_ = $_[1]; s/(.)\cH\1//g; s/_\cH//g; s/(.)/_\b$1/g; $_ }
+# Level four headings look like level three headings.
+sub cmd_head4 {
+    my ($self, $text, $line) = @_;
+    $text =~ s/\s+$//;
+    $text = $self->strip_format ($self->interpolate ($text, $line));
+    $text =~ s/(.)/_\b$1/g;
+    $self->SUPER::cmd_head4 ($text);
+}
+
+# The common code for handling all headers.  We have to override to avoid
+# interpolating twice and because we don't want to honor alt.
+sub heading {
+    my ($self, $text, $line, $indent, $marker) = @_;
+    $self->item ("\n\n") if defined $$self{ITEM};
+    $text .= "\n" if $$self{loose};
+    $self->output (' ' x $indent . $text . "\n");
+}
+
+# Fix the various formatting codes.
+sub seq_b { local $_ = strip_format (@_); s/(.)/$1\b$1/g; $_ }
+sub seq_f { local $_ = strip_format (@_); s/(.)/_\b$1/g; $_ }
+sub seq_i { local $_ = strip_format (@_); s/(.)/_\b$1/g; $_ }
 
 # Output any included code in bold.
 sub output_code {
@@ -89,7 +101,7 @@ sub output_code {
 }
 
 # We unfortunately have to override the wrapping code here, since the normal
-# wrapping code gets really confused by all the escape sequences.
+# wrapping code gets really confused by all the backspaces.
 sub wrap {
     my $self = shift;
     local $_ = shift;
@@ -97,8 +109,12 @@ sub wrap {
     my $spaces = ' ' x $$self{MARGIN};
     my $width = $$self{width} - $$self{MARGIN};
     while (length > $width) {
-        if (s/^((?:(?:[^\n]\cH)?[^\n]){0,$width})(\Z|\s+)//
-            || s/^((?:(?:[^\n]\cH)?[^\n]){$width})//) {
+        # This regex represents a single character, that's possibly underlined
+        # or in bold (in which case, it's three characters; the character, a
+        # backspace, and a character).  Use [^\n] rather than . to protect
+        # against odd settings of $*.
+        my $char = '(?:[^\n][\b])?[^\n]';
+        if (s/^((?>$char){0,$width})(?:\Z|\s+)//) {
             $output .= $spaces . $1 . "\n";
         } else {
             last;
@@ -110,6 +126,19 @@ sub wrap {
 }
 
 ##############################################################################
+# Utility functions
+##############################################################################
+
+# Strip all of the formatting from a provided string, returning the stripped
+# version.
+sub strip_format {
+    my ($self, $text) = @_;
+    $text =~ s/(.)[\b]\1/$1/g;
+    $text =~ s/_[\b]//g;
+    return $text;
+}
+
+##############################################################################
 # Module return value and documentation
 ##############################################################################
 
@@ -156,7 +185,7 @@ There may be some better approach possible.
 
 =head1 SEE ALSO
 
-L<Pod::Text|Pod::Text>, L<Pod::Parser|Pod::Parser>
+L<Pod::Text>, L<Pod::Parser>
 
 =head1 AUTHOR