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