1N/A# Pod::Text::Termcap -- Convert POD data to ASCII text with format escapes.
1N/A# $Id: Termcap.pm,v 1.11 2003/07/09 21:52:30 eagle Exp $
1N/A#
1N/A# Copyright 1999, 2001, 2002 by Russ Allbery <rra@stanford.edu>
1N/A#
1N/A# This program is free software; you may redistribute it and/or modify it
1N/A# under the same terms as Perl itself.
1N/A#
1N/A# This is a simple subclass of Pod::Text that overrides a few key methods to
1N/A# output the right termcap escape sequences for formatted text on the current
1N/A# terminal type.
1N/A
1N/A##############################################################################
1N/A# Modules and declarations
1N/A##############################################################################
1N/A
1N/Apackage Pod::Text::Termcap;
1N/A
1N/Arequire 5.004;
1N/A
1N/Ause Pod::Text ();
1N/Ause POSIX ();
1N/Ause Term::Cap;
1N/A
1N/Ause strict;
1N/Ause vars qw(@ISA $VERSION);
1N/A
1N/A@ISA = qw(Pod::Text);
1N/A
1N/A# Don't use the CVS revision as the version, since this module is also in Perl
1N/A# core and too many things could munge CVS magic revision strings. This
1N/A# number should ideally be the same as the CVS revision in podlators, however.
1N/A$VERSION = 1.11;
1N/A
1N/A
1N/A##############################################################################
1N/A# Overrides
1N/A##############################################################################
1N/A
1N/A# In the initialization method, grab our terminal characteristics as well as
1N/A# do all the stuff we normally do.
1N/Asub initialize {
1N/A my $self = shift;
1N/A my ($ospeed, $term, $termios);
1N/A
1N/A # $ENV{HOME} is usually not set on Windows. The default Term::Cap path
1N/A # may not work on Solaris.
1N/A my $home = exists $ENV{HOME} ? "$ENV{HOME}/.termcap:" : '';
1N/A $ENV{TERMPATH} = $home . '/etc/termcap:/usr/share/misc/termcap'
1N/A . ':/usr/share/lib/termcap';
1N/A
1N/A # Fall back on a hard-coded terminal speed if POSIX::Termios isn't
1N/A # available (such as on VMS).
1N/A eval { $termios = POSIX::Termios->new };
1N/A if ($@) {
1N/A $ospeed = 9600;
1N/A } else {
1N/A $termios->getattr;
1N/A $ospeed = $termios->getospeed || 9600;
1N/A }
1N/A
1N/A # Fall back on the ANSI escape sequences if Term::Cap doesn't work.
1N/A eval { $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed } };
1N/A $$self{BOLD} = $$term{_md} || "\e[1m";
1N/A $$self{UNDL} = $$term{_us} || "\e[4m";
1N/A $$self{NORM} = $$term{_me} || "\e[m";
1N/A
1N/A unless (defined $$self{width}) {
1N/A $$self{width} = $ENV{COLUMNS} || $$term{_co} || 80;
1N/A $$self{width} -= 2;
1N/A }
1N/A
1N/A $self->SUPER::initialize;
1N/A}
1N/A
1N/A# Make level one headings bold.
1N/Asub cmd_head1 {
1N/A my $self = shift;
1N/A local $_ = shift;
1N/A s/\s+$//;
1N/A $self->SUPER::cmd_head1 ("$$self{BOLD}$_$$self{NORM}");
1N/A}
1N/A
1N/A# Make level two headings bold.
1N/Asub cmd_head2 {
1N/A my $self = shift;
1N/A local $_ = shift;
1N/A s/\s+$//;
1N/A $self->SUPER::cmd_head2 ("$$self{BOLD}$_$$self{NORM}");
1N/A}
1N/A
1N/A# Fix up B<> and I<>. Note that we intentionally don't do F<>.
1N/Asub seq_b { my $self = shift; return "$$self{BOLD}$_[0]$$self{NORM}" }
1N/Asub seq_i { my $self = shift; return "$$self{UNDL}$_[0]$$self{NORM}" }
1N/A
1N/A# Output any included code in bold.
1N/Asub output_code {
1N/A my ($self, $code) = @_;
1N/A $self->output ($$self{BOLD} . $code . $$self{NORM});
1N/A}
1N/A
1N/A# Override the wrapping code to igore the special sequences.
1N/Asub wrap {
1N/A my $self = shift;
1N/A local $_ = shift;
1N/A my $output = '';
1N/A my $spaces = ' ' x $$self{MARGIN};
1N/A my $width = $$self{width} - $$self{MARGIN};
1N/A my $code = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
1N/A while (length > $width) {
1N/A if (s/^((?:$code?[^\n]){0,$width})\s+//
1N/A || s/^((?:$code?[^\n]){$width})//) {
1N/A $output .= $spaces . $1 . "\n";
1N/A } else {
1N/A last;
1N/A }
1N/A }
1N/A $output .= $spaces . $_;
1N/A $output =~ s/\s+$/\n\n/;
1N/A $output;
1N/A}
1N/A
1N/A
1N/A##############################################################################
1N/A# Module return value and documentation
1N/A##############################################################################
1N/A
1N/A1;
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/APod::Text::Termcap - Convert POD data to ASCII text with format escapes
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A use Pod::Text::Termcap;
1N/A my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78);
1N/A
1N/A # Read POD from STDIN and write to STDOUT.
1N/A $parser->parse_from_filehandle;
1N/A
1N/A # Read POD from file.pod and write to file.txt.
1N/A $parser->parse_from_file ('file.pod', 'file.txt');
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/APod::Text::Termcap is a simple subclass of Pod::Text that highlights output
1N/Atext using the correct termcap escape sequences for the current terminal.
1N/AApart from the format codes, it in all ways functions like Pod::Text. See
1N/AL<Pod::Text> for details and available options.
1N/A
1N/A=head1 NOTES
1N/A
1N/AThis module uses Term::Cap to retrieve the formatting escape sequences for
1N/Athe current terminal, and falls back on the ECMA-48 (the same in this
1N/Aregard as ANSI X3.64 and ISO 6429, the escape codes also used by DEC VT100
1N/Aterminals) if the bold, underline, and reset codes aren't set in the
1N/Atermcap information.
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AL<Pod::Text>, L<Pod::Parser>, L<Term::Cap>
1N/A
1N/AThe current version of this module is always available from its web site at
1N/AL<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the
1N/APerl core distribution as of 5.6.0.
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ARuss Allbery <rra@stanford.edu>.
1N/A
1N/A=head1 COPYRIGHT AND LICENSE
1N/A
1N/ACopyright 1999, 2001, 2002 by Russ Allbery <rra@stanford.edu>.
1N/A
1N/AThis program is free software; you may redistribute it and/or modify it
1N/Aunder the same terms as Perl itself.
1N/A
1N/A=cut