Commit | Line | Data |
6055f9d4 |
1 | # Pod::Text::Termcap -- Convert POD data to ASCII text with format escapes. |
bf202ccd |
2 | # $Id: Termcap.pm,v 1.3 2001/11/15 08:04:18 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 |
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. |
bf202ccd |
33 | $VERSION = 1.03; |
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; |
44 | |
45 | # The default Term::Cap path won't work on Solaris. |
46 | $ENV{TERMPATH} = "$ENV{HOME}/.termcap:/etc/termcap" |
47 | . ":/usr/share/misc/termcap:/usr/share/lib/termcap"; |
48 | |
49 | my $termios = POSIX::Termios->new; |
50 | $termios->getattr; |
51 | my $ospeed = $termios->getospeed; |
52 | my $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed }; |
53 | $$self{BOLD} = $$term{_md} or die 'BOLD'; |
54 | $$self{UNDL} = $$term{_us} or die 'UNDL'; |
55 | $$self{NORM} = $$term{_me} or die 'NORM'; |
56 | |
57 | unless (defined $$self{width}) { |
58 | $$self{width} = $ENV{COLUMNS} || $$term{_co} || 78; |
59 | $$self{width} -= 2; |
60 | } |
61 | |
62 | $self->SUPER::initialize; |
63 | } |
64 | |
65 | # Make level one headings bold. |
66 | sub cmd_head1 { |
67 | my $self = shift; |
68 | local $_ = shift; |
69 | s/\s+$//; |
70 | $self->SUPER::cmd_head1 ("$$self{BOLD}$_$$self{NORM}"); |
71 | } |
72 | |
73 | # Make level two headings bold. |
74 | sub cmd_head2 { |
75 | my $self = shift; |
76 | local $_ = shift; |
77 | s/\s+$//; |
78 | $self->SUPER::cmd_head2 ("$$self{BOLD}$_$$self{NORM}"); |
79 | } |
80 | |
81 | # Fix up B<> and I<>. Note that we intentionally don't do F<>. |
82 | sub seq_b { my $self = shift; return "$$self{BOLD}$_[0]$$self{NORM}" } |
83 | sub seq_i { my $self = shift; return "$$self{UNDL}$_[0]$$self{NORM}" } |
84 | |
59548eca |
85 | # Output any included code in bold. |
86 | sub output_code { |
87 | my ($self, $code) = @_; |
88 | $self->output ($$self{BOLD} . $code . $$self{NORM}); |
89 | } |
90 | |
6055f9d4 |
91 | # Override the wrapping code to igore the special sequences. |
92 | sub wrap { |
93 | my $self = shift; |
94 | local $_ = shift; |
95 | my $output = ''; |
96 | my $spaces = ' ' x $$self{MARGIN}; |
97 | my $width = $$self{width} - $$self{MARGIN}; |
98 | my $code = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)"; |
99 | while (length > $width) { |
100 | if (s/^((?:$code?[^\n]){0,$width})\s+// |
101 | || s/^((?:$code?[^\n]){$width})//) { |
102 | $output .= $spaces . $1 . "\n"; |
103 | } else { |
104 | last; |
105 | } |
106 | } |
107 | $output .= $spaces . $_; |
108 | $output =~ s/\s+$/\n\n/; |
109 | $output; |
110 | } |
111 | |
112 | |
3c014959 |
113 | ############################################################################## |
6055f9d4 |
114 | # Module return value and documentation |
3c014959 |
115 | ############################################################################## |
6055f9d4 |
116 | |
117 | 1; |
118 | __END__ |
119 | |
120 | =head1 NAME |
121 | |
122 | Pod::Text::Color - Convert POD data to ASCII text with format escapes |
123 | |
124 | =head1 SYNOPSIS |
125 | |
126 | use Pod::Text::Termcap; |
127 | my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78); |
128 | |
129 | # Read POD from STDIN and write to STDOUT. |
130 | $parser->parse_from_filehandle; |
131 | |
132 | # Read POD from file.pod and write to file.txt. |
133 | $parser->parse_from_file ('file.pod', 'file.txt'); |
134 | |
135 | =head1 DESCRIPTION |
136 | |
9741dab0 |
137 | Pod::Text::Termcap is a simple subclass of Pod::Text that highlights output |
138 | text using the correct termcap escape sequences for the current terminal. |
139 | Apart from the format codes, it in all ways functions like Pod::Text. See |
140 | L<Pod::Text> for details and available options. |
6055f9d4 |
141 | |
142 | =head1 SEE ALSO |
143 | |
bf202ccd |
144 | L<Pod::Text>, L<Pod::Parser> |
6055f9d4 |
145 | |
146 | =head1 AUTHOR |
147 | |
3c014959 |
148 | Russ Allbery <rra@stanford.edu>. |
149 | |
150 | =head1 COPYRIGHT AND LICENSE |
151 | |
59548eca |
152 | Copyright 1999, 2001 by Russ Allbery <rra@stanford.edu>. |
3c014959 |
153 | |
154 | This program is free software; you may redistribute it and/or modify it |
155 | under the same terms as Perl itself. |
6055f9d4 |
156 | |
157 | =cut |