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