File Coverage

blib/lib/Pod/Text/Color/Delight.pm
Criterion Covered Total %
statement 108 112 96.4
branch 16 18 88.8
condition 9 11 81.8
subroutine 24 24 100.0
pod 1 12 8.3
total 158 177 89.2


line stmt bran cond sub pod time code
1             package Pod::Text::Color::Delight;
2 9     9   338334 use 5.008005;
  9         34  
  9         365  
3 9     9   47 use strict;
  9         18  
  9         259  
4 9     9   58 use warnings;
  9         19  
  9         288  
5 9     9   10487 use Term::ANSIColor ();
  9         92430  
  9         429  
6 9     9   2068 use File::Spec::Functions qw(catfile);
  9         1598  
  9         701  
7 9     9   15082 use Syntax::Highlight::Perl::Improved;
  9         180452  
  9         883  
8 9     9   1987 use parent 'Pod::Text::Color';
  9         615  
  9         84  
9              
10             our $VERSION = '0.07';
11              
12 9         14763 use constant COLOR_TABLE => {
13             head1 => 'bright_cyan',
14             head2 => 'reset bold',
15             head3 => '',
16             head4 => '',
17             bold => 'reset bold',
18             file => 'bright_green',
19             italic => 'reset italic',
20             link => 'rgb045',
21             code => {
22             Character => 'cyan',
23             String => 'rgb542',
24             Quote => 'rgb542',
25             Label => 'rgb542',
26              
27             Builtin_Function => 'bright_red',
28             Builtin_Operator => 'bright_red',
29              
30             Keyword => 'bright_red',
31             Package => 'rgb345',
32              
33             Subroutine => 'rgb454',
34             Bareword => 'white',
35             Symbol => 'white',
36             Operator => 'white',
37             Number => 'white',
38              
39             Variable_Hash => 'rgb520',
40             Variable_Array => 'rgb520',
41             Variable_Scalar => 'rgb520',
42             Variable_Typeglob => 'rgb520',
43              
44             Comment_Normal => 'grey10',
45             Comment_POD => 'grey10',
46             DATA => 'grey10',
47             Directive => 'bright_green',
48             },
49 9     9   1071891 };
  9         24  
50              
51             sub new {
52 8     8 1 145 my $class = shift;
53              
54 8         121 my $self = $class->SUPER::new;
55              
56 8         1847 my $color_table_file = catfile($ENV{HOME}, '.pod_text_color_delight');
57 8         27 my $color_table = COLOR_TABLE;
58 8 50 66     76 if (!$ENV{POD_TEXT_COLOR_DELIGHT_DEFAULT} && -f $color_table_file) {
59 0         0 $color_table = do $color_table_file;
60             }
61              
62 8         23 $self->{color_table} = $color_table;
63 8         30 return $self;
64             }
65              
66             sub cmd_head1 {
67 6     6 0 11307 my ($self, $attrs, $text) = @_;
68 6         40 $self->SUPER::cmd_head1($attrs, $self->_colored($text, $self->_select_color('head1')));
69             }
70              
71             sub cmd_head2 {
72 1     1 0 473 my ($self, $attrs, $text) = @_;
73 1         5 $self->SUPER::cmd_head2($attrs, $self->_colored($text, $self->_select_color('head2')));
74             }
75              
76             sub cmd_head3 {
77 1     1 0 297 my ($self, $attrs, $text) = @_;
78 1         5 $self->SUPER::cmd_head3($attrs, $self->_colored($text, $self->_select_color('head3')));
79             }
80              
81             sub cmd_head4 {
82 1     1 0 230 my ($self, $attrs, $text) = @_;
83 1         6 $self->SUPER::cmd_head4($attrs, $self->_colored($text, $self->_select_color('head4')));
84             }
85              
86             sub cmd_b {
87 9     9 0 10216 my ($self, $attrs, $text) = @_;
88              
89 9 100       31 if ($self->{PENDING}->[2]) {
90 3         10 return "B<$text>";
91             }
92              
93 6         19 $text = $self->_highlight_raw_format($attrs, $text);
94 6         18 $self->SUPER::cmd_b($attrs, $self->_colored($text, $self->_select_color('bold')));
95             }
96              
97             sub cmd_f {
98 6     6 0 877 my ($self, $attrs, $text) = @_;
99              
100 6 100       20 if ($self->{PENDING}->[2]) {
101 2         6 return "F<$text>";
102             }
103              
104 4         13 $text = $self->_highlight_raw_format($attrs, $text);
105 4         12 $self->SUPER::cmd_f($attrs, $self->_colored($text, $self->_select_color('file')));
106             }
107              
108             sub cmd_c {
109 11     11 0 4568 my ($self, $attrs, $text) = @_;
110              
111 11         38 my $highlighted = $self->SUPER::cmd_c($attrs, $self->_highlight_code($attrs, $text));
112 11         30366 $self->{raw} = $text; # for C<...> that is in `=item`
113              
114             # XXX for file format
115 11         33 $highlighted =~ s/\e\[37mF\e\[0m\e\[37m<\e\[0m(.*?)\e\[37m>\e\[0m
116 2         38 /$self->cmd_f(Term::ANSIColor::colorstrip($attrs, $1))
117             /gex;
118              
119             # XXX for italic format
120 11         82 $highlighted =~ s/\e\[37mI\e\[0m\e\[37m<\e\[0m(.*?)\e\[37m>\e\[0m
121 1         8 /$self->cmd_i(Term::ANSIColor::colorstrip($attrs, $1))
122             /gex;
123              
124             # XXX for bold format
125 11         71 $highlighted =~ s/\e\[37mB\e\[0m\e\[37m<\e\[0m(.*?)\e\[37m>\e\[0m
126 1         6 /$self->cmd_b(Term::ANSIColor::colorstrip($attrs, $1))
127             /gex;
128              
129             # XXX for link format
130 11         73 $highlighted =~ s/\e\[37mL\e\[0m\e\[37m<\e\[0m(.*?)\e\[37m>\e\[0m
131 2         12 /$self->cmd_l(Term::ANSIColor::colorstrip({type => 'pod'}, $1))
132             /gex;
133              
134 11         80 return $highlighted;
135             }
136              
137             sub cmd_item_text {
138 4     4 0 906 my ($self, $attrs, $text) = @_;
139              
140 4         7 my $raw = $self->{raw};
141 4         24 my ($_text) = Term::ANSIColor::colorstrip($text) =~ m/^"(.*)"$/;
142              
143 4 100 100     99 if ($raw && $_text && $_text eq $raw) {
      66        
144 2         5 $text = $self->{raw};
145 2         6 $text = '"' . $text . '"';
146             }
147 4         26 $self->SUPER::cmd_item_text($attrs, $text);
148              
149 4         384 undef $self->{raw};
150             }
151              
152             sub cmd_i {
153 6     6 0 1389 my ($self, $attrs, $text) = @_;
154              
155 6 100       43 if ($self->{PENDING}->[2]) {
156 2         6 return "I<$text>";
157             }
158              
159 4         15 $text = $self->_highlight_raw_format($attrs, $text);
160 4         12 $self->SUPER::cmd_i($attrs, $self->_colored($text, $self->_select_color('italic')));
161             }
162              
163             sub cmd_l {
164 6     6 0 2651 my ($self, $attrs, $text) = @_;
165              
166             # to fix the issues that are displayed on the double
167 6 100       26 if ($attrs->{type} eq 'url') {
168 2         5 $attrs->{type} = 'pod';
169             }
170              
171 6 100       32 if ($self->{PENDING}->[2]) {
172 2         8 return "L<$text>";
173             }
174              
175 4         26 $text = $self->_highlight_raw_format($attrs, $text);
176 4         21 $self->SUPER::cmd_l($attrs, $self->_colored($text, $self->_select_color('link')));
177             }
178              
179             sub cmd_verbatim {
180 1     1 0 560 my ($self, $attrs, $text) = @_;
181 1         6 $self->SUPER::cmd_verbatim($attrs, $self->_highlight_code($attrs, $text));
182             }
183              
184             sub _highlight_code {
185 12     12   19 my ($self, $attrs, $text) = @_;
186              
187 12         85 my $formatter = Syntax::Highlight::Perl::Improved->new;
188              
189 12         4501 while (my ($type, $style) = each %{$self->_select_color('code')}) {
  264         10742  
190 252         573 Term::ANSIColor::colored('dummy', $style);
191 252 50       6195 if ($@) {
192 0         0 $style = 'reset';
193             }
194 252         508 $formatter->set_format($type, [Term::ANSIColor::color($style), Term::ANSIColor::color('reset')]);
195             }
196              
197 12         51 return $formatter->format_string($text);
198             }
199              
200             sub _select_color {
201 291     291   390 my ($self, $element) = @_;
202              
203 291   100     1508 return $self->{color_table}->{$element} || COLOR_TABLE->{$element} || 'reset';
204             }
205              
206             sub _colored {
207 29     29   1984 my ($self, $text, $color) = @_;
208              
209 29         40 my $colored_text = '';
210 29         45 eval { $colored_text = Term::ANSIColor::colored($text, $color) };
  29         100  
211              
212 29 100       1273 if ($@) {
213 1         3 $colored_text = Term::ANSIColor::colored($text, 'reset');
214             }
215              
216 29         270 return $colored_text;
217             }
218              
219             sub _highlight_raw_format {
220 18     18   30 my ($self, $attrs, $text) = @_;
221              
222             # XXX for file format
223 18         32 $text =~ s/F<(.*?)>
224 0         0 /$self->cmd_f(Term::ANSIColor::colorstrip($attrs, $1))
225             /ex;
226              
227             # XXX for italic format
228 18         29 $text =~ s/I<(.*?)>
229 1         4 /$self->cmd_i(Term::ANSIColor::colorstrip($attrs, $1))
230             /ex;
231              
232             # XXX for bold format
233 18         57 $text =~ s/B<(.*?)>
234 2         8 /$self->cmd_b(Term::ANSIColor::colorstrip($attrs, $1))
235             /ex;
236              
237             # XXX for link format
238 18         75 $text =~ s/L<(.*?)>
239 0         0 /$self->cmd_l(Term::ANSIColor::colorstrip({type => 'pod'}, $1))
240             /ex;
241              
242 18         41 return $text;
243             }
244             1;
245             __END__