Commit | Line | Data |
6055f9d4 |
1 | # Pod::Text::Color -- Convert POD data to formatted color ASCII text |
6055f9d4 |
2 | # |
fe61459e |
3 | # Copyright 1999, 2001, 2004, 2006, 2008, 2009 Russ Allbery <rra@stanford.edu> |
6055f9d4 |
4 | # |
3c014959 |
5 | # This program is free software; you may redistribute it and/or modify it |
6055f9d4 |
6 | # under the same terms as Perl itself. |
7 | # |
3c014959 |
8 | # This is just a basic proof of concept. It should later be modified to make |
9 | # better use of color, take options changing what colors are used for what |
10 | # text, and the like. |
6055f9d4 |
11 | |
3c014959 |
12 | ############################################################################## |
6055f9d4 |
13 | # Modules and declarations |
3c014959 |
14 | ############################################################################## |
6055f9d4 |
15 | |
16 | package Pod::Text::Color; |
17 | |
18 | require 5.004; |
19 | |
20 | use Pod::Text (); |
21 | use Term::ANSIColor qw(colored); |
22 | |
23 | use strict; |
24 | use vars qw(@ISA $VERSION); |
25 | |
26 | @ISA = qw(Pod::Text); |
27 | |
9f2f055a |
28 | $VERSION = '2.05'; |
6055f9d4 |
29 | |
3c014959 |
30 | ############################################################################## |
6055f9d4 |
31 | # Overrides |
3c014959 |
32 | ############################################################################## |
6055f9d4 |
33 | |
34 | # Make level one headings bold. |
35 | sub cmd_head1 { |
b7ae008f |
36 | my ($self, $attrs, $text) = @_; |
37 | $text =~ s/\s+$//; |
38 | $self->SUPER::cmd_head1 ($attrs, colored ($text, 'bold')); |
6055f9d4 |
39 | } |
40 | |
41 | # Make level two headings bold. |
42 | sub cmd_head2 { |
b7ae008f |
43 | my ($self, $attrs, $text) = @_; |
44 | $text =~ s/\s+$//; |
45 | $self->SUPER::cmd_head2 ($attrs, colored ($text, 'bold')); |
6055f9d4 |
46 | } |
47 | |
b84d8b9e |
48 | # Fix the various formatting codes. |
b7ae008f |
49 | sub cmd_b { return colored ($_[2], 'bold') } |
50 | sub cmd_f { return colored ($_[2], 'cyan') } |
51 | sub cmd_i { return colored ($_[2], 'yellow') } |
6055f9d4 |
52 | |
59548eca |
53 | # Output any included code in green. |
54 | sub output_code { |
55 | my ($self, $code) = @_; |
56 | $code = colored ($code, 'green'); |
57 | $self->output ($code); |
58 | } |
59 | |
fe61459e |
60 | # Strip all of the formatting from a provided string, returning the stripped |
61 | # version. We will eventually want to use colorstrip() from Term::ANSIColor, |
62 | # but it's fairly new so avoid the tight dependency. |
63 | sub strip_format { |
64 | my ($self, $text) = @_; |
65 | $text =~ s/\e\[[\d;]*m//g; |
66 | return $text; |
67 | } |
68 | |
6055f9d4 |
69 | # We unfortunately have to override the wrapping code here, since the normal |
70 | # wrapping code gets really confused by all the escape sequences. |
71 | sub wrap { |
72 | my $self = shift; |
73 | local $_ = shift; |
74 | my $output = ''; |
75 | my $spaces = ' ' x $$self{MARGIN}; |
b7ae008f |
76 | my $width = $$self{opt_width} - $$self{MARGIN}; |
8f202758 |
77 | |
78 | # We have to do $shortchar and $longchar in variables because the |
79 | # construct ${char}{0,$width} didn't do the right thing until Perl 5.8.x. |
b7ae008f |
80 | my $char = '(?:(?:\e\[[\d;]+m)*[^\n])'; |
8f202758 |
81 | my $shortchar = $char . "{0,$width}"; |
82 | my $longchar = $char . "{$width}"; |
6055f9d4 |
83 | while (length > $width) { |
8f202758 |
84 | if (s/^($shortchar)\s+// || s/^($longchar)//) { |
6055f9d4 |
85 | $output .= $spaces . $1 . "\n"; |
86 | } else { |
87 | last; |
88 | } |
89 | } |
90 | $output .= $spaces . $_; |
91 | $output =~ s/\s+$/\n\n/; |
92 | $output; |
93 | } |
94 | |
3c014959 |
95 | ############################################################################## |
6055f9d4 |
96 | # Module return value and documentation |
3c014959 |
97 | ############################################################################## |
6055f9d4 |
98 | |
99 | 1; |
100 | __END__ |
101 | |
102 | =head1 NAME |
103 | |
104 | Pod::Text::Color - Convert POD data to formatted color ASCII text |
105 | |
0e4e3f6e |
106 | =for stopwords |
107 | Allbery |
108 | |
6055f9d4 |
109 | =head1 SYNOPSIS |
110 | |
111 | use Pod::Text::Color; |
112 | my $parser = Pod::Text::Color->new (sentence => 0, width => 78); |
113 | |
114 | # Read POD from STDIN and write to STDOUT. |
115 | $parser->parse_from_filehandle; |
116 | |
117 | # Read POD from file.pod and write to file.txt. |
118 | $parser->parse_from_file ('file.pod', 'file.txt'); |
119 | |
120 | =head1 DESCRIPTION |
121 | |
9741dab0 |
122 | Pod::Text::Color is a simple subclass of Pod::Text that highlights output |
123 | text using ANSI color escape sequences. Apart from the color, it in all |
124 | ways functions like Pod::Text. See L<Pod::Text> for details and available |
125 | options. |
126 | |
127 | Term::ANSIColor is used to get colors and therefore must be installed to use |
128 | this module. |
129 | |
130 | =head1 BUGS |
131 | |
132 | This is just a basic proof of concept. It should be seriously expanded to |
133 | support configurable coloration via options passed to the constructor, and |
134 | B<pod2text> should be taught about those. |
6055f9d4 |
135 | |
136 | =head1 SEE ALSO |
137 | |
b7ae008f |
138 | L<Pod::Text>, L<Pod::Simple> |
6055f9d4 |
139 | |
fd20da51 |
140 | The current version of this module is always available from its web site at |
141 | L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the |
142 | Perl core distribution as of 5.6.0. |
143 | |
6055f9d4 |
144 | =head1 AUTHOR |
145 | |
3c014959 |
146 | Russ Allbery <rra@stanford.edu>. |
147 | |
148 | =head1 COPYRIGHT AND LICENSE |
149 | |
fe61459e |
150 | Copyright 1999, 2001, 2004, 2006, 2008, 2009 Russ Allbery <rra@stanford.edu>. |
3c014959 |
151 | |
152 | This program is free software; you may redistribute it and/or modify it |
153 | under the same terms as Perl itself. |
6055f9d4 |
154 | |
155 | =cut |