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