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