$! and $1 are PVMG(), but their content is undefined when peeking
[p5sagit/p5-mst-13.2.git] / lib / Pod / Text / Overstrike.pm
CommitLineData
73849855 1# Pod::Text::Overstrike -- Convert POD data to formatted overstrike text
73849855 2#
3# Created by Joe Smith <Joe.Smith@inwap.com> 30-Nov-2000
4# (based on Pod::Text::Color by Russ Allbery <rra@stanford.edu>)
0e4e3f6e 5# Copyright 2000 Joe Smith <Joe.Smith@inwap.com>.
6# Copyright 2001, 2004, 2008 Russ Allbery <rra@stanford.edu>.
73849855 7#
3c014959 8# This program is free software; you may redistribute it and/or modify it
73849855 9# under the same terms as Perl itself.
10#
11# This was written because the output from:
12#
13# pod2text Text.pm > plain.txt; less plain.txt
14#
15# is not as rich as the output from
16#
17# pod2man Text.pm | nroff -man > fancy.txt; less fancy.txt
18#
19# and because both Pod::Text::Color and Pod::Text::Termcap are not device
20# independent.
21
3c014959 22##############################################################################
73849855 23# Modules and declarations
3c014959 24##############################################################################
73849855 25
26package Pod::Text::Overstrike;
27
28require 5.004;
29
30use Pod::Text ();
31
32use strict;
33use vars qw(@ISA $VERSION);
34
35@ISA = qw(Pod::Text);
36
0e4e3f6e 37$VERSION = 2.01;
73849855 38
3c014959 39##############################################################################
73849855 40# Overrides
3c014959 41##############################################################################
73849855 42
43# Make level one headings bold, overridding any existing formatting.
44sub cmd_head1 {
b7ae008f 45 my ($self, $attrs, $text) = @_;
b616daaf 46 $text =~ s/\s+$//;
b7ae008f 47 $text = $self->strip_format ($text);
b616daaf 48 $text =~ s/(.)/$1\b$1/g;
b7ae008f 49 return $self->SUPER::cmd_head1 ($attrs, $text);
73849855 50}
51
52# Make level two headings bold, overriding any existing formatting.
53sub cmd_head2 {
b7ae008f 54 my ($self, $attrs, $text) = @_;
b616daaf 55 $text =~ s/\s+$//;
b7ae008f 56 $text = $self->strip_format ($text);
b616daaf 57 $text =~ s/(.)/$1\b$1/g;
b7ae008f 58 return $self->SUPER::cmd_head2 ($attrs, $text);
73849855 59}
60
61# Make level three headings underscored, overriding any existing formatting.
62sub cmd_head3 {
b7ae008f 63 my ($self, $attrs, $text) = @_;
b616daaf 64 $text =~ s/\s+$//;
b7ae008f 65 $text = $self->strip_format ($text);
b616daaf 66 $text =~ s/(.)/_\b$1/g;
b7ae008f 67 return $self->SUPER::cmd_head3 ($attrs, $text);
b616daaf 68}
69
70# Level four headings look like level three headings.
71sub cmd_head4 {
b7ae008f 72 my ($self, $attrs, $text) = @_;
b616daaf 73 $text =~ s/\s+$//;
b7ae008f 74 $text = $self->strip_format ($text);
b616daaf 75 $text =~ s/(.)/_\b$1/g;
b7ae008f 76 return $self->SUPER::cmd_head4 ($attrs, $text);
b616daaf 77}
78
79# The common code for handling all headers. We have to override to avoid
80# interpolating twice and because we don't want to honor alt.
81sub heading {
b7ae008f 82 my ($self, $text, $indent, $marker) = @_;
b616daaf 83 $self->item ("\n\n") if defined $$self{ITEM};
b7ae008f 84 $text .= "\n" if $$self{opt_loose};
85 my $margin = ' ' x ($$self{opt_margin} + $indent);
11f72409 86 $self->output ($margin . $text . "\n");
b7ae008f 87 return '';
73849855 88}
89
b84d8b9e 90# Fix the various formatting codes.
b7ae008f 91sub cmd_b { local $_ = $_[0]->strip_format ($_[2]); s/(.)/$1\b$1/g; $_ }
92sub cmd_f { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
93sub cmd_i { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
73849855 94
59548eca 95# Output any included code in bold.
96sub output_code {
97 my ($self, $code) = @_;
98 $code =~ s/(.)/$1\b$1/g;
99 $self->output ($code);
100}
101
73849855 102# We unfortunately have to override the wrapping code here, since the normal
b84d8b9e 103# wrapping code gets really confused by all the backspaces.
73849855 104sub wrap {
105 my $self = shift;
106 local $_ = shift;
107 my $output = '';
108 my $spaces = ' ' x $$self{MARGIN};
b7ae008f 109 my $width = $$self{opt_width} - $$self{MARGIN};
73849855 110 while (length > $width) {
21e6de9e 111 # This regex represents a single character, that's possibly underlined
112 # or in bold (in which case, it's three characters; the character, a
113 # backspace, and a character). Use [^\n] rather than . to protect
114 # against odd settings of $*.
115 my $char = '(?:[^\n][\b])?[^\n]';
116 if (s/^((?>$char){0,$width})(?:\Z|\s+)//) {
73849855 117 $output .= $spaces . $1 . "\n";
118 } else {
119 last;
120 }
121 }
122 $output .= $spaces . $_;
123 $output =~ s/\s+$/\n\n/;
b7ae008f 124 return $output;
73849855 125}
126
3c014959 127##############################################################################
b616daaf 128# Utility functions
129##############################################################################
130
131# Strip all of the formatting from a provided string, returning the stripped
132# version.
133sub strip_format {
134 my ($self, $text) = @_;
6ce9a2f8 135 $text =~ s/(.)[\b]\1/$1/g;
136 $text =~ s/_[\b]//g;
b616daaf 137 return $text;
138}
139
140##############################################################################
73849855 141# Module return value and documentation
3c014959 142##############################################################################
73849855 143
1441;
145__END__
146
147=head1 NAME
148
149Pod::Text::Overstrike - Convert POD data to formatted overstrike text
150
0e4e3f6e 151=for stopwords
152overstruck Overstruck Allbery
153
73849855 154=head1 SYNOPSIS
155
156 use Pod::Text::Overstrike;
157 my $parser = Pod::Text::Overstrike->new (sentence => 0, width => 78);
158
159 # Read POD from STDIN and write to STDOUT.
160 $parser->parse_from_filehandle;
161
162 # Read POD from file.pod and write to file.txt.
163 $parser->parse_from_file ('file.pod', 'file.txt');
164
165=head1 DESCRIPTION
166
167Pod::Text::Overstrike is a simple subclass of Pod::Text that highlights
168output text using overstrike sequences, in a manner similar to nroff.
0e4e3f6e 169Characters in bold text are overstruck (character, backspace, character)
170and characters in underlined text are converted to overstruck underscores
171(underscore, backspace, character). This format was originally designed
172for hard-copy terminals and/or line printers, yet is readable on soft-copy
173(CRT) terminals.
73849855 174
175Overstruck text is best viewed by page-at-a-time programs that take
176advantage of the terminal's B<stand-out> and I<underline> capabilities, such
177as the less program on Unix.
178
179Apart from the overstrike, it in all ways functions like Pod::Text. See
180L<Pod::Text> for details and available options.
181
182=head1 BUGS
183
184Currently, the outermost formatting instruction wins, so for example
185underlined text inside a region of bold text is displayed as simply bold.
186There may be some better approach possible.
187
188=head1 SEE ALSO
189
b7ae008f 190L<Pod::Text>, L<Pod::Simple>
73849855 191
fd20da51 192The current version of this module is always available from its web site at
193L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the
194Perl core distribution as of 5.6.0.
195
73849855 196=head1 AUTHOR
197
3c014959 198Joe Smith <Joe.Smith@inwap.com>, using the framework created by Russ Allbery
199<rra@stanford.edu>.
200
201=head1 COPYRIGHT AND LICENSE
202
203Copyright 2000 by Joe Smith <Joe.Smith@inwap.com>.
b7ae008f 204Copyright 2001, 2004 by Russ Allbery <rra@stanford.edu>.
3c014959 205
206This program is free software; you may redistribute it and/or modify it
207under the same terms as Perl itself.
73849855 208
209=cut