1 # Pod::Text::Termcap -- Convert POD data to ASCII text with format escapes.
3 # Copyright 1999, 2001, 2002, 2004, 2006 by Russ Allbery <rra@stanford.edu>
5 # This program is free software; you may redistribute it and/or modify it
6 # under the same terms as Perl itself.
8 # This is a simple subclass of Pod::Text that overrides a few key methods to
9 # output the right termcap escape sequences for formatted text on the current
12 ##############################################################################
13 # Modules and declarations
14 ##############################################################################
16 package Pod::Text::Termcap;
25 use vars qw(@ISA $VERSION);
29 # Don't use the CVS revision as the version, since this module is also in Perl
30 # core and too many things could munge CVS magic revision strings. This
31 # number should ideally be the same as the CVS revision in podlators, however.
34 ##############################################################################
36 ##############################################################################
38 # In the initialization method, grab our terminal characteristics as well as
39 # do all the stuff we normally do.
41 my ($self, @args) = @_;
42 my ($ospeed, $term, $termios);
43 $self = $self->SUPER::new (@args);
45 # $ENV{HOME} is usually not set on Windows. The default Term::Cap path
46 # may not work on Solaris.
47 my $home = exists $ENV{HOME} ? "$ENV{HOME}/.termcap:" : '';
48 $ENV{TERMPATH} = $home . '/etc/termcap:/usr/share/misc/termcap'
49 . ':/usr/share/lib/termcap';
51 # Fall back on a hard-coded terminal speed if POSIX::Termios isn't
52 # available (such as on VMS).
53 eval { $termios = POSIX::Termios->new };
58 $ospeed = $termios->getospeed || 9600;
61 # Fall back on the ANSI escape sequences if Term::Cap doesn't work.
62 eval { $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed } };
63 $$self{BOLD} = $$term{_md} || "\e[1m";
64 $$self{UNDL} = $$term{_us} || "\e[4m";
65 $$self{NORM} = $$term{_me} || "\e[m";
67 unless (defined $$self{width}) {
68 $$self{opt_width} = $ENV{COLUMNS} || $$term{_co} || 80;
69 $$self{opt_width} -= 2;
75 # Make level one headings bold.
77 my ($self, $attrs, $text) = @_;
79 $self->SUPER::cmd_head1 ($attrs, "$$self{BOLD}$text$$self{NORM}");
82 # Make level two headings bold.
84 my ($self, $attrs, $text) = @_;
86 $self->SUPER::cmd_head2 ($attrs, "$$self{BOLD}$text$$self{NORM}");
89 # Fix up B<> and I<>. Note that we intentionally don't do F<>.
90 sub cmd_b { my $self = shift; return "$$self{BOLD}$_[1]$$self{NORM}" }
91 sub cmd_i { my $self = shift; return "$$self{UNDL}$_[1]$$self{NORM}" }
93 # Output any included code in bold.
95 my ($self, $code) = @_;
96 $self->output ($$self{BOLD} . $code . $$self{NORM});
99 # Override the wrapping code to igore the special sequences.
104 my $spaces = ' ' x $$self{MARGIN};
105 my $width = $$self{opt_width} - $$self{MARGIN};
107 # $codes matches a single special sequence. $char matches any number of
108 # special sequences preceeding a single character other than a newline.
109 # We have to do $shortchar and $longchar in variables because the
110 # construct ${char}{0,$width} didn't do the right thing until Perl 5.8.x.
111 my $codes = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
112 my $char = "(?:$codes*[^\\n])";
113 my $shortchar = $char . "{0,$width}";
114 my $longchar = $char . "{$width}";
115 while (length > $width) {
116 if (s/^($shortchar)\s+// || s/^($longchar)//) {
117 $output .= $spaces . $1 . "\n";
122 $output .= $spaces . $_;
123 $output =~ s/\s+$/\n\n/;
127 ##############################################################################
128 # Module return value and documentation
129 ##############################################################################
136 Pod::Text::Termcap - Convert POD data to ASCII text with format escapes
140 use Pod::Text::Termcap;
141 my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78);
143 # Read POD from STDIN and write to STDOUT.
144 $parser->parse_from_filehandle;
146 # Read POD from file.pod and write to file.txt.
147 $parser->parse_from_file ('file.pod', 'file.txt');
151 Pod::Text::Termcap is a simple subclass of Pod::Text that highlights output
152 text using the correct termcap escape sequences for the current terminal.
153 Apart from the format codes, it in all ways functions like Pod::Text. See
154 L<Pod::Text> for details and available options.
158 This module uses Term::Cap to retrieve the formatting escape sequences for
159 the current terminal, and falls back on the ECMA-48 (the same in this
160 regard as ANSI X3.64 and ISO 6429, the escape codes also used by DEC VT100
161 terminals) if the bold, underline, and reset codes aren't set in the
166 L<Pod::Text>, L<Pod::Simple>, L<Term::Cap>
168 The current version of this module is always available from its web site at
169 L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the
170 Perl core distribution as of 5.6.0.
174 Russ Allbery <rra@stanford.edu>.
176 =head1 COPYRIGHT AND LICENSE
178 Copyright 1999, 2001, 2002, 2004, 2006 by Russ Allbery <rra@stanford.edu>.
180 This program is free software; you may redistribute it and/or modify it
181 under the same terms as Perl itself.