xref: /openbsd-src/gnu/usr.bin/perl/cpan/Pod-Perldoc/lib/Pod/Perldoc/ToTerm.pm (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1package Pod::Perldoc::ToTerm;
2use strict;
3use warnings;
4
5use vars qw($VERSION);
6$VERSION = '3.25';
7
8use parent qw(Pod::Perldoc::BaseTo);
9
10sub is_pageable        { 1 }
11sub write_with_binmode { 0 }
12sub output_extension   { 'txt' }
13
14use Pod::Text::Termcap ();
15
16sub alt       { shift->_perldoc_elem('alt'     , @_) }
17sub indent    { shift->_perldoc_elem('indent'  , @_) }
18sub loose     { shift->_perldoc_elem('loose'   , @_) }
19sub quotes    { shift->_perldoc_elem('quotes'  , @_) }
20sub sentence  { shift->_perldoc_elem('sentence', @_) }
21sub width     {
22    my $self = shift;
23    $self->_perldoc_elem('width' , @_) ||
24    $self->_get_columns_from_manwidth  ||
25	$self->_get_columns_from_stty      ||
26	$self->_get_default_width;
27}
28
29sub _get_stty { `stty -a` }
30
31sub _get_columns_from_stty {
32	my $output = $_[0]->_get_stty;
33
34	if(    $output =~ /\bcolumns\s+(\d+)/ )    { return $1; }
35	elsif( $output =~ /;\s*(\d+)\s+columns;/ ) { return $1; }
36	else                                       { return  0 }
37	}
38
39sub _get_columns_from_manwidth {
40	my( $self ) = @_;
41
42	return 0 unless defined $ENV{MANWIDTH};
43
44	unless( $ENV{MANWIDTH} =~ m/\A\d+\z/ ) {
45		$self->warn( "Ignoring non-numeric MANWIDTH ($ENV{MANWIDTH})\n" );
46		return 0;
47		}
48
49	if( $ENV{MANWIDTH} == 0 ) {
50		$self->warn( "Ignoring MANWIDTH of 0. Really? Why even run the program? :)\n" );
51		return 0;
52		}
53
54	if( $ENV{MANWIDTH} =~ m/\A(\d+)\z/ ) { return $1 }
55
56	return 0;
57	}
58
59sub _get_default_width {
60	76
61	}
62
63
64sub new { return bless {}, ref($_[0]) || $_[0] }
65
66sub parse_from_file {
67  my $self = shift;
68
69  $self->{width} = $self->width();
70
71  my @options =
72    map {; $_, $self->{$_} }
73      grep !m/^_/s,
74        keys %$self
75  ;
76
77  defined(&Pod::Perldoc::DEBUG)
78   and Pod::Perldoc::DEBUG()
79   and print "About to call new Pod::Text::Termcap ",
80    $Pod::Text::VERSION ? "(v$Pod::Text::Termcap::VERSION) " : '',
81    "with options: ",
82    @options ? "[@options]" : "(nil)", "\n";
83  ;
84
85  Pod::Text::Termcap->new(@options)->parse_from_file(@_);
86}
87
881;
89
90=head1 NAME
91
92Pod::Perldoc::ToTerm - render Pod with terminal escapes
93
94=head1 SYNOPSIS
95
96  perldoc -o term Some::Modulename
97
98=head1 DESCRIPTION
99
100This is a "plug-in" class that allows Perldoc to use
101Pod::Text as a formatter class.
102
103It supports the following options, which are explained in
104L<Pod::Text>: alt, indent, loose, quotes, sentence, width
105
106For example:
107
108  perldoc -o term -w indent:5 Some::Modulename
109
110=head1 CAVEAT
111
112This module may change to use a different text formatter class in the
113future, and this may change what options are supported.
114
115=head1 SEE ALSO
116
117L<Pod::Text>, L<Pod::Text::Termcap>, L<Pod::Perldoc>
118
119=head1 COPYRIGHT AND DISCLAIMERS
120
121Copyright (c) 2011 Mark Allen.
122
123This program is free software; you can redistribute it and/or modify it
124under the terms of either: the GNU General Public License as published
125by the Free Software Foundation; or the Artistic License.
126
127See http://dev.perl.org/licenses/ for more information.
128
129=head1 AUTHOR
130
131Mark Allen C<< <mallen@cpan.org> >>
132
133=cut
134