File Coverage

blib/lib/Pod/Text/Termcap.pm
Criterion Covered Total %
statement 68 74 91.8
branch 4 10 40.0
condition 5 13 38.4
subroutine 14 15 93.3
pod 1 8 12.5
total 92 120 76.6


line stmt bran cond sub pod time code
1             # Pod::Text::Termcap -- Convert POD data to ASCII text with format escapes.
2             #
3             # This is a simple subclass of Pod::Text that overrides a few key methods to
4             # output the right termcap escape sequences for formatted text on the current
5             # terminal type.
6             #
7             # Copyright 1999, 2001, 2002, 2004, 2006, 2008, 2009, 2014, 2015
8             # Russ Allbery
9             #
10             # This program is free software; you may redistribute it and/or modify it
11             # under the same terms as Perl itself.
12              
13             ##############################################################################
14             # Modules and declarations
15             ##############################################################################
16              
17             package Pod::Text::Termcap;
18              
19 2     2   26934 use 5.006;
  2         7  
20 2     2   18 use strict;
  2         5  
  2         55  
21 2     2   8 use warnings;
  2         3  
  2         58  
22              
23 2     2   375 use Pod::Text ();
  2         3  
  2         38  
24 2     2   1178 use POSIX ();
  2         12326  
  2         69  
25 2     2   1597 use Term::Cap;
  2         5464  
  2         94  
26              
27 2     2   15 use vars qw(@ISA $VERSION);
  2         3  
  2         1449  
28              
29             @ISA = qw(Pod::Text);
30              
31             $VERSION = '4.09';
32              
33             ##############################################################################
34             # Overrides
35             ##############################################################################
36              
37             # In the initialization method, grab our terminal characteristics as well as
38             # do all the stuff we normally do.
39             sub new {
40 2     2 1 990 my ($self, @args) = @_;
41 2         21 my ($ospeed, $term, $termios);
42 2         21 $self = $self->SUPER::new (@args);
43              
44             # $ENV{HOME} is usually not set on Windows. The default Term::Cap path
45             # may not work on Solaris.
46 2 50       9 unless (exists $ENV{TERMPATH}) {
47 0 0       0 my $home = exists $ENV{HOME} ? "$ENV{HOME}/.termcap:" : '';
48             $ENV{TERMPATH} =
49 0         0 "${home}/etc/termcap:/usr/share/misc/termcap:/usr/share/lib/termcap";
50             }
51              
52             # Fall back on a hard-coded terminal speed if POSIX::Termios isn't
53             # available (such as on VMS).
54 2         3 eval { $termios = POSIX::Termios->new };
  2         42  
55 2 50       8 if ($@) {
56 0         0 $ospeed = 9600;
57             } else {
58 2         15 $termios->getattr;
59 2   50     24 $ospeed = $termios->getospeed || 9600;
60             }
61              
62             # Fall back on the ANSI escape sequences if Term::Cap doesn't work.
63 2         3 eval { $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed } };
  2         26  
64 2   50     573 $$self{BOLD} = $$term{_md} || "\e[1m";
65 2   50     9 $$self{UNDL} = $$term{_us} || "\e[4m";
66 2   50     8 $$self{NORM} = $$term{_me} || "\e[m";
67              
68 2 50       7 unless (defined $$self{width}) {
69 2   0     16 $$self{opt_width} = $ENV{COLUMNS} || $$term{_co} || 80;
70 2         6 $$self{opt_width} -= 2;
71             }
72              
73 2         26 return $self;
74             }
75              
76             # Make level one headings bold.
77             sub cmd_head1 {
78 11     11 0 18 my ($self, $attrs, $text) = @_;
79 11         35 $text =~ s/\s+$//;
80 11         73 $self->SUPER::cmd_head1 ($attrs, "$$self{BOLD}$text$$self{NORM}");
81             }
82              
83             # Make level two headings bold.
84             sub cmd_head2 {
85 2     2 0 3 my ($self, $attrs, $text) = @_;
86 2         9 $text =~ s/\s+$//;
87 2         12 $self->SUPER::cmd_head2 ($attrs, "$$self{BOLD}$text$$self{NORM}");
88             }
89              
90             # Fix up B<> and I<>. Note that we intentionally don't do F<>.
91 12     12 0 18 sub cmd_b { my $self = shift; return "$$self{BOLD}$_[1]$$self{NORM}" }
  12         38  
92 16     16 0 20 sub cmd_i { my $self = shift; return "$$self{UNDL}$_[1]$$self{NORM}" }
  16         44  
93              
94             # Output any included code in bold.
95             sub output_code {
96 0     0 0 0 my ($self, $code) = @_;
97 0         0 $self->output ($$self{BOLD} . $code . $$self{NORM});
98             }
99              
100             # Strip all of the formatting from a provided string, returning the stripped
101             # version.
102             sub strip_format {
103 29     29 0 39 my ($self, $text) = @_;
104 29         111 $text =~ s/\Q$$self{BOLD}//g;
105 29         53 $text =~ s/\Q$$self{UNDL}//g;
106 29         54 $text =~ s/\Q$$self{NORM}//g;
107 29         65 return $text;
108             }
109              
110             # Override the wrapping code to ignore the special sequences.
111             sub wrap {
112 101     101 0 114 my $self = shift;
113 101         109 local $_ = shift;
114 101         94 my $output = '';
115 101         180 my $spaces = ' ' x $$self{MARGIN};
116 101         144 my $width = $$self{opt_width} - $$self{MARGIN};
117              
118             # $codes matches a single special sequence. $char matches any number of
119             # special sequences preceding a single character other than a newline.
120             # We have to do $shortchar and $longchar in variables because the
121             # construct ${char}{0,$width} didn't do the right thing until Perl 5.8.x.
122 101         250 my $codes = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
123 101         128 my $char = "(?:$codes*[^\\n])";
124 101         146 my $shortchar = $char . "{0,$width}";
125 101         118 my $longchar = $char . "{$width}";
126 101         253 while (length > $width) {
127 31 50 33     1024 if (s/^($shortchar)\s+// || s/^($longchar)//) {
128 31         154 $output .= $spaces . $1 . "\n";
129             } else {
130 0         0 last;
131             }
132             }
133 101         158 $output .= $spaces . $_;
134 101         487 $output =~ s/\s+$/\n\n/;
135 101         389 return $output;
136             }
137              
138             ##############################################################################
139             # Module return value and documentation
140             ##############################################################################
141              
142             1;
143             __END__