Commit | Line | Data |
6055f9d4 |
1 | # Pod::Text::Termcap -- Convert POD data to ASCII text with format escapes. |
2ccddc90 |
2 | # $Id: Termcap.pm,v 1.11 2003/07/09 21:52:30 eagle Exp $ |
6055f9d4 |
3 | # |
2da3dd12 |
4 | # Copyright 1999, 2001, 2002 by Russ Allbery <rra@stanford.edu> |
6055f9d4 |
5 | # |
3c014959 |
6 | # This program is free software; you may redistribute it and/or modify it |
6055f9d4 |
7 | # under the same terms as Perl itself. |
8 | # |
9741dab0 |
9 | # This is a simple subclass of Pod::Text that overrides a few key methods to |
3c014959 |
10 | # output the right termcap escape sequences for formatted text on the current |
11 | # terminal type. |
6055f9d4 |
12 | |
3c014959 |
13 | ############################################################################## |
6055f9d4 |
14 | # Modules and declarations |
3c014959 |
15 | ############################################################################## |
6055f9d4 |
16 | |
17 | package Pod::Text::Termcap; |
18 | |
19 | require 5.004; |
20 | |
21 | use Pod::Text (); |
22 | use POSIX (); |
23 | use Term::Cap; |
9741dab0 |
24 | |
6055f9d4 |
25 | use strict; |
26 | use vars qw(@ISA $VERSION); |
27 | |
28 | @ISA = qw(Pod::Text); |
29 | |
3c014959 |
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. |
2ccddc90 |
33 | $VERSION = 1.11; |
6055f9d4 |
34 | |
35 | |
3c014959 |
36 | ############################################################################## |
6055f9d4 |
37 | # Overrides |
3c014959 |
38 | ############################################################################## |
6055f9d4 |
39 | |
40 | # In the initialization method, grab our terminal characteristics as well as |
41 | # do all the stuff we normally do. |
42 | sub initialize { |
43 | my $self = shift; |
a97e9142 |
44 | my ($ospeed, $term, $termios); |
6055f9d4 |
45 | |
2ccddc90 |
46 | # $ENV{HOME} is usually not set on Windows. The default Term::Cap path |
47 | # may not work on Solaris. |
8bafa735 |
48 | my $home = exists $ENV{HOME} ? "$ENV{HOME}/.termcap:" : ''; |
49 | $ENV{TERMPATH} = $home . '/etc/termcap:/usr/share/misc/termcap' |
50 | . ':/usr/share/lib/termcap'; |
6055f9d4 |
51 | |
a97e9142 |
52 | # Fall back on a hard-coded terminal speed if POSIX::Termios isn't |
4989cd70 |
53 | # available (such as on VMS). |
a97e9142 |
54 | eval { $termios = POSIX::Termios->new }; |
55 | if ($@) { |
2da3dd12 |
56 | $ospeed = 9600; |
a97e9142 |
57 | } else { |
58 | $termios->getattr; |
2da3dd12 |
59 | $ospeed = $termios->getospeed || 9600; |
8ef7c2e5 |
60 | } |
a97e9142 |
61 | |
62 | # Fall back on the ANSI escape sequences if Term::Cap doesn't work. |
b4558dc4 |
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"; |
6055f9d4 |
67 | |
68 | unless (defined $$self{width}) { |
a97e9142 |
69 | $$self{width} = $ENV{COLUMNS} || $$term{_co} || 80; |
6055f9d4 |
70 | $$self{width} -= 2; |
71 | } |
72 | |
73 | $self->SUPER::initialize; |
74 | } |
75 | |
76 | # Make level one headings bold. |
77 | sub cmd_head1 { |
78 | my $self = shift; |
79 | local $_ = shift; |
80 | s/\s+$//; |
81 | $self->SUPER::cmd_head1 ("$$self{BOLD}$_$$self{NORM}"); |
82 | } |
83 | |
84 | # Make level two headings bold. |
85 | sub cmd_head2 { |
86 | my $self = shift; |
87 | local $_ = shift; |
88 | s/\s+$//; |
89 | $self->SUPER::cmd_head2 ("$$self{BOLD}$_$$self{NORM}"); |
90 | } |
91 | |
92 | # Fix up B<> and I<>. Note that we intentionally don't do F<>. |
93 | sub seq_b { my $self = shift; return "$$self{BOLD}$_[0]$$self{NORM}" } |
94 | sub seq_i { my $self = shift; return "$$self{UNDL}$_[0]$$self{NORM}" } |
95 | |
59548eca |
96 | # Output any included code in bold. |
97 | sub output_code { |
98 | my ($self, $code) = @_; |
99 | $self->output ($$self{BOLD} . $code . $$self{NORM}); |
100 | } |
101 | |
6055f9d4 |
102 | # Override the wrapping code to igore the special sequences. |
103 | sub wrap { |
104 | my $self = shift; |
105 | local $_ = shift; |
106 | my $output = ''; |
107 | my $spaces = ' ' x $$self{MARGIN}; |
108 | my $width = $$self{width} - $$self{MARGIN}; |
109 | my $code = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)"; |
110 | while (length > $width) { |
111 | if (s/^((?:$code?[^\n]){0,$width})\s+// |
112 | || s/^((?:$code?[^\n]){$width})//) { |
113 | $output .= $spaces . $1 . "\n"; |
114 | } else { |
115 | last; |
116 | } |
117 | } |
118 | $output .= $spaces . $_; |
119 | $output =~ s/\s+$/\n\n/; |
120 | $output; |
121 | } |
122 | |
123 | |
3c014959 |
124 | ############################################################################## |
6055f9d4 |
125 | # Module return value and documentation |
3c014959 |
126 | ############################################################################## |
6055f9d4 |
127 | |
128 | 1; |
129 | __END__ |
130 | |
131 | =head1 NAME |
132 | |
fd20da51 |
133 | Pod::Text::Termcap - Convert POD data to ASCII text with format escapes |
6055f9d4 |
134 | |
135 | =head1 SYNOPSIS |
136 | |
137 | use Pod::Text::Termcap; |
138 | my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78); |
139 | |
140 | # Read POD from STDIN and write to STDOUT. |
141 | $parser->parse_from_filehandle; |
142 | |
143 | # Read POD from file.pod and write to file.txt. |
144 | $parser->parse_from_file ('file.pod', 'file.txt'); |
145 | |
146 | =head1 DESCRIPTION |
147 | |
9741dab0 |
148 | Pod::Text::Termcap is a simple subclass of Pod::Text that highlights output |
149 | text using the correct termcap escape sequences for the current terminal. |
150 | Apart from the format codes, it in all ways functions like Pod::Text. See |
151 | L<Pod::Text> for details and available options. |
6055f9d4 |
152 | |
b84d8b9e |
153 | =head1 NOTES |
b4558dc4 |
154 | |
155 | This module uses Term::Cap to retrieve the formatting escape sequences for |
156 | the current terminal, and falls back on the ECMA-48 (the same in this |
157 | regard as ANSI X3.64 and ISO 6429, the escape codes also used by DEC VT100 |
158 | terminals) if the bold, underline, and reset codes aren't set in the |
159 | termcap information. |
160 | |
6055f9d4 |
161 | =head1 SEE ALSO |
162 | |
b4558dc4 |
163 | L<Pod::Text>, L<Pod::Parser>, L<Term::Cap> |
6055f9d4 |
164 | |
fd20da51 |
165 | The current version of this module is always available from its web site at |
166 | L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the |
167 | Perl core distribution as of 5.6.0. |
168 | |
6055f9d4 |
169 | =head1 AUTHOR |
170 | |
3c014959 |
171 | Russ Allbery <rra@stanford.edu>. |
172 | |
173 | =head1 COPYRIGHT AND LICENSE |
174 | |
2da3dd12 |
175 | Copyright 1999, 2001, 2002 by Russ Allbery <rra@stanford.edu>. |
3c014959 |
176 | |
177 | This program is free software; you may redistribute it and/or modify it |
178 | under the same terms as Perl itself. |
6055f9d4 |
179 | |
180 | =cut |