1 # Pod::Text::Overstrike -- Convert POD data to formatted overstrike text
3 # Created by Joe Smith <Joe.Smith@inwap.com> 30-Nov-2000
4 # (based on Pod::Text::Color by Russ Allbery <rra@stanford.edu>)
6 # This program is free software; you may redistribute it and/or modify it
7 # under the same terms as Perl itself.
9 # This was written because the output from:
11 # pod2text Text.pm > plain.txt; less plain.txt
13 # is not as rich as the output from
15 # pod2man Text.pm | nroff -man > fancy.txt; less fancy.txt
17 # and because both Pod::Text::Color and Pod::Text::Termcap are not device
20 ##############################################################################
21 # Modules and declarations
22 ##############################################################################
24 package Pod::Text::Overstrike;
31 use vars qw(@ISA $VERSION);
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.
40 ##############################################################################
42 ##############################################################################
44 # Make level one headings bold, overridding any existing formatting.
46 my ($self, $attrs, $text) = @_;
48 $text = $self->strip_format ($text);
49 $text =~ s/(.)/$1\b$1/g;
50 return $self->SUPER::cmd_head1 ($attrs, $text);
53 # Make level two headings bold, overriding any existing formatting.
55 my ($self, $attrs, $text) = @_;
57 $text = $self->strip_format ($text);
58 $text =~ s/(.)/$1\b$1/g;
59 return $self->SUPER::cmd_head2 ($attrs, $text);
62 # Make level three headings underscored, overriding any existing formatting.
64 my ($self, $attrs, $text) = @_;
66 $text = $self->strip_format ($text);
67 $text =~ s/(.)/_\b$1/g;
68 return $self->SUPER::cmd_head3 ($attrs, $text);
71 # Level four headings look like level three headings.
73 my ($self, $attrs, $text) = @_;
75 $text = $self->strip_format ($text);
76 $text =~ s/(.)/_\b$1/g;
77 return $self->SUPER::cmd_head4 ($attrs, $text);
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.
83 my ($self, $text, $indent, $marker) = @_;
84 $self->item ("\n\n") if defined $$self{ITEM};
85 $text .= "\n" if $$self{opt_loose};
86 my $margin = ' ' x ($$self{opt_margin} + $indent);
87 $self->output ($margin . $text . "\n");
91 # Fix the various formatting codes.
92 sub cmd_b { local $_ = $_[0]->strip_format ($_[2]); s/(.)/$1\b$1/g; $_ }
93 sub cmd_f { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
94 sub cmd_i { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
96 # Output any included code in bold.
98 my ($self, $code) = @_;
99 $code =~ s/(.)/$1\b$1/g;
100 $self->output ($code);
103 # We unfortunately have to override the wrapping code here, since the normal
104 # wrapping code gets really confused by all the backspaces.
109 my $spaces = ' ' x $$self{MARGIN};
110 my $width = $$self{opt_width} - $$self{MARGIN};
111 while (length > $width) {
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+)//) {
118 $output .= $spaces . $1 . "\n";
123 $output .= $spaces . $_;
124 $output =~ s/\s+$/\n\n/;
128 ##############################################################################
130 ##############################################################################
132 # Strip all of the formatting from a provided string, returning the stripped
135 my ($self, $text) = @_;
136 $text =~ s/(.)[\b]\1/$1/g;
141 ##############################################################################
142 # Module return value and documentation
143 ##############################################################################
150 Pod::Text::Overstrike - Convert POD data to formatted overstrike text
154 use Pod::Text::Overstrike;
155 my $parser = Pod::Text::Overstrike->new (sentence => 0, width => 78);
157 # Read POD from STDIN and write to STDOUT.
158 $parser->parse_from_filehandle;
160 # Read POD from file.pod and write to file.txt.
161 $parser->parse_from_file ('file.pod', 'file.txt');
165 Pod::Text::Overstrike is a simple subclass of Pod::Text that highlights
166 output text using overstrike sequences, in a manner similar to nroff.
167 Characters in bold text are overstruck (character, backspace, character) and
168 characters in underlined text are converted to overstruck underscores
169 (underscore, backspace, character). This format was originally designed for
170 hardcopy terminals and/or lineprinters, yet is readable on softcopy (CRT)
173 Overstruck text is best viewed by page-at-a-time programs that take
174 advantage of the terminal's B<stand-out> and I<underline> capabilities, such
175 as the less program on Unix.
177 Apart from the overstrike, it in all ways functions like Pod::Text. See
178 L<Pod::Text> for details and available options.
182 Currently, the outermost formatting instruction wins, so for example
183 underlined text inside a region of bold text is displayed as simply bold.
184 There may be some better approach possible.
188 L<Pod::Text>, L<Pod::Simple>
190 The current version of this module is always available from its web site at
191 L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the
192 Perl core distribution as of 5.6.0.
196 Joe Smith <Joe.Smith@inwap.com>, using the framework created by Russ Allbery
199 =head1 COPYRIGHT AND LICENSE
201 Copyright 2000 by Joe Smith <Joe.Smith@inwap.com>.
202 Copyright 2001, 2004 by Russ Allbery <rra@stanford.edu>.
204 This program is free software; you may redistribute it and/or modify it
205 under the same terms as Perl itself.