develooper Front page | perl.perl5.porters | Postings from November 2000

Patch to pod2text for terminal-independent bold and underline.

Thread Next
From:
Joe.Smith
Date:
November 30, 2000 15:08
Subject:
Patch to pod2text for terminal-independent bold and underline.
Message ID:
200011302307.PAA15278@tardis.tymnet.com
Problem:
  "pod2text somefile | less" does not display bold or underline.
  "pod2text -t somefile >somefile.txt" is terminal dependent.
  "pod2man somefile | nroff -man >somefile.txt" is awkward.

Solution:
  "pod2text --overstrike somefile >somefile.txt" produces a file that
  can be viewed verbatim, or displayed using a termcap-aware program
  (such as 'less').

--- pod/pod2text.PL.orig	Sun Sep 10 07:36:37 2000
+++ pod/pod2text.PL	Thu Nov 30 14:55:26 2000
@@ -75,8 +75,8 @@
 $options{sentence} = 0;
 Getopt::Long::config ('bundling');
 GetOptions (\%options, 'alt|a', 'color|c', 'help|h', 'indent|i=i',
-            'loose|l', 'quotes|q=s', 'sentence|s', 'termcap|t',
-            'width|w=i') or exit 1;
+            'loose|l', 'overstrike|o', 'quotes|q=s', 'sentence|s',
+	    'termcap|t', 'width|w=i') or exit 1;
 pod2usage (1) if $options{help};
 
 # Figure out what formatter we're going to use.  -c overrides -t.
@@ -89,8 +89,11 @@
 } elsif ($options{termcap}) {
     $formatter = 'Pod::Text::Termcap';
     require Pod::Text::Termcap;
+} elsif ($options{overstrike}) {
+    $formatter = 'Pod::Text::Overstrike';
+    require Pod::Text::Overstrike;
 }
-delete @options{'color', 'termcap'};
+delete @options{'color', 'termcap', 'overstrike'};
 
 # Initialize and run the formatter.
 my $parser = $formatter->new (%options);
@@ -104,7 +107,7 @@
 
 =head1 SYNOPSIS
 
-pod2text [B<-aclst>] [B<-i> I<indent>] [B<-q> I<quotes>] [B<-w> I<width>]
+pod2text [B<-aclost>] [B<-i> I<indent>] [B<-q> I<quotes>] [B<-w> I<width>]
 [I<input> [I<output>]]
 
 pod2text B<-h>
@@ -149,6 +152,12 @@
 printed after C<=head1>, although one is still printed after C<=head2>,
 because this is the expected formatting for manual pages; if you're
 formatting arbitrary text documents, using this option is recommended.
+
+=item B<-o>, B<--overstrike>
+
+Format the output with overstruck printing.  Bold text is rendered as
+character+backspace+character.  Italics and file names are rendered as
+underscore+backspace+character.
 
 =item B<-q> I<quotes>, B<--quotes>=I<quotes>
 

--- lib/Pod/Text/Color.pm	Mon Jul 31 19:31:48 2000
+++ lib/Pod/Text/Overstrike.pm	Thu Nov 30 14:50:19 2000
@@ -1,33 +1,35 @@
-# Pod::Text::Color -- Convert POD data to formatted color ASCII text
-# $Id: Color.pm,v 0.5 1999/09/20 10:15:16 eagle Exp $
+# Pod::Text::Overstrike -- Convert POD data to formatted Overstrike ASCII text
 #
-# Copyright 1999 by Russ Allbery <rra@stanford.edu>
+# Created by Joe Smith <Joe.Smith@inwap.com> 30-Nov-2000.
+#   (based on Color.pm by Russ Allbery <rra@stanford.edu>)
 #
 # This program is free software; you can redistribute it and/or modify it
 # under the same terms as Perl itself.
 #
-# This is just a basic proof of concept.  It should later be modified to
-# make better use of color, take options changing what colors are used for
-# what text, and the like.
+# This was written because the output from
+#   pod2text Text.pm >plain.txt; less plain.txt
+# is not as rich as the output from
+#   pod2man Text.pm | nroff -man >fancy.txt; less fancy.txt
+# and because both "pod2text -c" and "pod2text -t" are not device independent.
+# Use "pod2text -o" to get bold and underline via overstruck characters.
 
 ############################################################################
 # Modules and declarations
 ############################################################################
 
-package Pod::Text::Color;
+package Pod::Text::Overstrike;
 
 require 5.004;
 
 use Pod::Text ();
-use Term::ANSIColor qw(colored);
 
 use strict;
 use vars qw(@ISA $VERSION);
 
 @ISA = qw(Pod::Text);
 
-# Use the CVS revision of this file as its version number.
-($VERSION = (split (' ', q$Revision: 0.5 $ ))[1]) =~ s/\.(\d)$/.0$1/;
+
+$VERSION = "0.5";
 
 
 ############################################################################
@@ -39,7 +41,8 @@
     my $self = shift;
     local $_ = shift;
     s/\s+$//;
-    $self->SUPER::cmd_head1 (colored ($_, 'bold'));
+    s/(.)/$1\cH$1/g;		# Bold
+    $self->SUPER::cmd_head1($_);
 }
 
 # Make level two headings bold.
@@ -47,15 +50,16 @@
     my $self = shift;
     local $_ = shift;
     s/\s+$//;
-    $self->SUPER::cmd_head2 (colored ($_, 'bold'));
+    s/(.)/$1\cH\$1/g;		# Bold
+    $self->SUPER::cmd_head1($_);
 }
 
 # Fix the various interior sequences.
-sub seq_b { return colored ($_[1], 'bold')   }
-sub seq_f { return colored ($_[1], 'cyan')   }
-sub seq_i { return colored ($_[1], 'yellow') }
+sub seq_b { local $_ = $_[1]; s/(.)/\cB$1/g; $_ }	# Bold
+sub seq_f { local $_ = $_[1]; s/(.)/\cA$1/g; $_ }	# Underline
+sub seq_i { local $_ = $_[1]; s/(.)/\cA$1/g; $_ }	# Underling
 
-# We unfortunately have to override the wrapping code here, since the normal
+# We have to override the wrapping code here, since the normal
 # wrapping code gets really confused by all the escape sequences.
 sub wrap {
     my $self = shift;
@@ -64,8 +68,8 @@
     my $spaces = ' ' x $$self{MARGIN};
     my $width = $$self{width} - $$self{MARGIN};
     while (length > $width) {
-        if (s/^((?:(?:\e\[[\d;]+m)?[^\n]){0,$width})\s+//
-            || s/^((?:(?:\e\[[\d;]+m)?[^\n]){$width})//) {
+        if (s/^((?:(?:[\cA\cB])?[^\n]){0,$width})\s+//
+            || s/^((?:(?:[\cA\cB])?[^\n]){$width})//) {
             $output .= $spaces . $1 . "\n";
         } else {
             last;
@@ -73,6 +77,8 @@
     }
     $output .= $spaces . $_;
     $output =~ s/\s+$/\n\n/;
+    $output =~ s/\cA(.)/_\cH$1/g;	# Underscore + backspace + character
+    $output =~ s/\cB(.)/$1\cH$1/g;	# Character + backspace + character
     $output;
 }
 
@@ -85,12 +91,12 @@
 
 =head1 NAME
 
-Pod::Text::Color - Convert POD data to formatted color ASCII text
+Pod::Text::Overstrike - Convert POD data to formatted Overstrike ASCII text
 
 =head1 SYNOPSIS
 
-    use Pod::Text::Color;
-    my $parser = Pod::Text::Color->new (sentence => 0, width => 78);
+    use Pod::Text::Overstrike;
+    my $parser = Pod::Text::Overstrike->new (sentence => 0, width => 78);
 
     # Read POD from STDIN and write to STDOUT.
     $parser->parse_from_filehandle;
@@ -100,19 +106,25 @@
 
 =head1 DESCRIPTION
 
-Pod::Text::Color is a simple subclass of Pod::Text that highlights output
-text using ANSI color escape sequences.  Apart from the color, it in all
-ways functions like Pod::Text.  See L<Pod::Text> for details and available
-options.
+Pod::Text::Overstrike is a simple subclass of Pod::Text that highlights output
+text using overstrike sequences, in a manner similar to F<nroff>.  Characters
+in bold text are overstruck (character, backspace, character) and characters
+in underlined text are converted to overstruck underscores (underscore,
+backspace, character).  This format was originally designed for hardcopy
+terminals and/or lineprinters, yet is readable on softcopy (CRT) terminals.
+
+Overstruck text is best viewed by page-at-a-time programs that take advantage
+of the terminal's B<stand-out> and I<underline> capabilities, such as the
+F<less> program on Unix.
+
+Apart from the overstrike, it in all ways functions like Pod::Text.  See
+L<Pod::Text> for details and available options.
 
-Term::ANSIColor is used to get colors and therefore must be installed to use
-this module.
 
 =head1 BUGS
 
-This is just a basic proof of concept.  It should be seriously expanded to
-support configurable coloration via options passed to the constructor, and
-B<pod2text> should be taught about those.
+This is just a basic proof of concept.  See L<Pod::Color> for the original
+author's ideas of how to improve on the concept.
 
 =head1 SEE ALSO
 
@@ -120,6 +132,7 @@
 
 =head1 AUTHOR
 
+Joe Smith E<lt>Joe.Smith@inwap.comE<gt>, using the framework created by
 Russ Allbery E<lt>rra@stanford.eduE<gt>.
 
 =cut
-- 
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About