blib/lib/DB/Color/Highlight.pm

filter: Statementstmt 78.3% 83 / 106 Branchbran 42.8% 12 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 91.6% 22 / 24 Podpod 0.0% 0 / 2 totaltotal 72.7% 120 / 165 SCAR 31.9
CC
17
Cov
71%
highlight_text
56.0
_get_highlighted_text
5.0
_initialize
4.0
CRAP 24.2
« blib/lib/DB/Color.pm ↑ Summary Build »
1 package DB::Color::Highlight;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Term::ANSIColor ':constants';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Spec::Functions qw(catfile catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Path 'make_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 BEGIN {
11 2 ▶ if ( !( Term::ANSIColor->VERSION >= 3 ) ) {
Subroutine BEGIN: called CC=2 SCAR=7.9
Branch: 1 branch
BranchTrueFalseTotal
if (not "Term::ANSIColor"->VERSION >= 3) 0 2 2
12 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 0 ▶ *BRIGHT_BLUE = sub { BLUE };
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 }
15 }
16  
17 2 ▶use Syntax::Highlight::Engine::Kate::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 DB::Color::Highlight - Provides highlighting for DB::Color
22  
23 =head1 VERSION
24  
25 Version 0.10
26  
27 =cut
28  
29 2 our $VERSION = '0.10';
30  
31 # increase this number by one to force the cache to generate new md5 numbers
32 2 my $FORMAT_NUMBER = 1;
33  
34 BEGIN {
35 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶ *Syntax::Highlight::Engine::Kate::Template::logwarning = sub { };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
37 }
38  
39 sub new {
40 2 ▶ my ( $class, $args ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
41 2 my $self = bless {} => $class;
42 2 $self->_initialize($args);
43 2 return $self;
44 }
45  
46 sub _initialize {
47 2 ▶ my ( $self, $args ) = @_;
Subroutine _initialize: called CC=4 SCAR=14.0
48  
49 2 my $cache_dir = $args->{cache_dir};
50 2 $self->{debug_fh} = $args->{debug_fh};
51 2 $self->{cache_dir} = $cache_dir;
52  
53 2 ▶ if ( defined $cache_dir and not -d $cache_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_dir and not -d $cache_dir) 1 1 2
Condition66%2/3
defined $cache_dir and not -d $cache_dir
!ll&&!rl&&r
011
MC/DC50%1/2
defined $cache_dir and not -d $cache_dir
defined $cache_dir not -d $cache_dir
Truth table66%2/3
A and not(B)
A= defined $cache_dirB= not -d $cache_dir
ABresult
0X0
100
111
54 1 ▶ mkdir $cache_dir or die "Cannot mkdir ($cache_dir): $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $cache_dir or die "Cannot mkdir ($cache_dir): $!" 0 1 1
55 }
56  
57 # CLEAR RESET BOLD DARK
58 # FAINT ITALIC UNDERLINE UNDERSCORE
59 # BLINK REVERSE CONCEALED
60 #
61 # BLACK RED GREEN YELLOW
62 # BLUE MAGENTA CYAN WHITE
63 # BRIGHT_BLACK BRIGHT_RED BRIGHT_GREEN BRIGHT_YELLOW
64 # BRIGHT_BLUE BRIGHT_MAGENTA BRIGHT_CYAN BRIGHT_WHITE
65 #
66 # ON_BLACK ON_RED ON_GREEN ON_YELLOW
67 # ON_BLUE ON_MAGENTA ON_CYAN ON_WHITE
68 # ON_BRIGHT_BLACK ON_BRIGHT_RED ON_BRIGHT_GREEN ON_BRIGHT_YELLOW
69 # ON_BRIGHT_BLUE ON_BRIGHT_MAGENTA ON_BRIGHT_CYAN ON_BRIGHT_WHITE
70  
71 2 my $highlighter = Syntax::Highlight::Engine::Kate::Perl->new(
72 format_table => {
73 'Keyword' => [ YELLOW, RESET ],
74 'Comment' => [ BRIGHT_BLUE, RESET ],
75 'Decimal' => [ YELLOW, RESET ],
76 'Float' => [ YELLOW, RESET ],
77 'Function' => [ CYAN, RESET ],
78 'Identifier' => [ RED, RESET ],
79 'Normal' => [ WHITE, RESET ],
80 'Operator' => [ CYAN, RESET ],
81 'Preprocessor' => [ RED, RESET ],
82 'String' => [ MAGENTA, RESET ],
83 'String Char' => [ RED, RESET ],
84 'Symbol' => [ CYAN, RESET ],
85 'DataType' => [ CYAN, RESET ], # variable names
86 }
87 );
88 2 $self->{highlighter} = $highlighter;
89 }
90  
91 1 ▶sub _highlighter { $_[0]->{highlighter} }
Subroutine _highlighter: called CC=1 SCAR=0.0
92 4 ▶sub _cache_dir { $_[0]->{cache_dir} }
Subroutine _cache_dir: called CC=1 SCAR=0.0
93 2 ▶sub _should_cache { defined $_[0]->_cache_dir }
Subroutine _should_cache: called CC=1 SCAR=0.0
94  
95 sub _debug {
96 2 ▶ my ( $self, $message ) = @_;
Subroutine _debug: called CC=2 SCAR=7.1
97 2 ▶ return unless my $debug = $self->{debug_fh};
Branch: 1 branch
BranchTrueFalseTotal
unless my $debug = $self->{"debug_fh"} 0 2 2
98 2 print $debug "$message\n";
99 }
100  
101 sub highlight_text {
102 0 ▶ my ( $self, $code ) = @_;
Subroutine highlight_text: not called CC=7 SCAR=40.3
Pod undocumented
103  
104 0 ▶ if ( $self->_should_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_should_cache) { } 0 0 0
105 0 my ( $path, $file ) = $self->_get_path_and_file($code);
106 0 ▶ unless ( -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $path) 0 0 0
107 0 make_path($path);
108 }
109 0 $file = catfile( $path, $file );
110  
111 0 ▶ if ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 0 0 0
112 0 $self->_debug("Cache hit on '$file'");
113  
114 # update the atime, mtime to ensure that our naive cache recognizes
115 # this as a "recent" file
116 0 ▶ utime time, time, $file or die "Cannot 'utime atime, mtime $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
utime time, time, $file or die "Cannot 'utime atime, mtime ${file}: $!" 0 0 0
117 0 ▶ open my $fh, '<', $file or die "Cannot open '$file' for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Cannot open '${file}' for reading: $!" 0 0 0
118 0 return do { local $/; <$fh> };
119 }
120 else {
121 0 $self->_debug("Cache miss on '$file'");
122 0 my $highlighted = $self->_get_highlighted_text($code);
123 0 ▶ open my $fh, '>', $file or die "Cannot open '$file' for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Cannot open '${file}' for writing: $!" 0 0 0
124 0 print $fh $highlighted;
125 0 return $highlighted;
126 }
127 }
128 else {
129 0 return $self->_get_highlighted_text($code);
130 }
131 }
132  
133 sub _get_highlighted_text {
134 1 ▶ my ( $self, $code ) = @_;
Subroutine _get_highlighted_text: called CC=5 SCAR=16.1
135  
136 1 my @code;
137 1 my $line_num = 0;
138 1 my $in_pod = 0;
139 1 my %pod_lines;
140 my @pod_line_nums;
141 1 foreach ( split /\n/ => $code ) {
142 2047 ▶ if (/^=(?!cut\b)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^=(?!cut\b)/) 89 1958 2047
143 89 $in_pod = 1;
144 }
145 2047 ▶ if ($in_pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_pod) { } 1223 824 2047
146 1223 $pod_lines{$line_num} = $_;
147 1223 push @pod_line_nums => $line_num;
148 1223 push @code => '';
149 }
150 else {
151 824 push @code => $_;
152 }
153 2047 ▶ if (/^=cut\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^=cut\b/) 24 2023 2047
154 24 $in_pod = 0;
155 }
156 2047 $line_num++;
157 }
158 1 $code = join "\n" => @code;
159 1 my $highlighted = $self->_highlighter->highlightText($code);
160 1 @code = split /\n/ => $highlighted;
161 1 @code[@pod_line_nums] = @pod_lines{@pod_line_nums};
162 1 return join "\n" => map { BLUE . $_ . RESET } @code;
163 }
164  
165 sub _get_path_and_file {
166 2 ▶ my ( $self, $code ) = @_;
Subroutine _get_path_and_file: called CC=2 SCAR=7.2
167 2 ▶ unless ( $self->_should_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_should_cache) 0 2 2
168 0 $self->_debug("Caching disabled");
169 0 return;
170 }
171 2 my $md5 = md5_hex( $self->_get_unique_factors, $code );
172 2 my $dir = substr $md5, 0, 2, '';
173 2 my $file = $md5;
174  
175 2 my $path = catdir( $self->_cache_dir, $dir );
176 2 $self->_debug("Cache path is '$path'. Cache file is '$file'");
177 2 return $path, $file;
178 }
179  
180 sub _format_number {
181 2 ▶ return $FORMAT_NUMBER;
Subroutine _format_number: called CC=1 SCAR=0.0
182 }
183  
184 sub _get_unique_factors {
185 2 ▶ my $self = shift;
Subroutine _get_unique_factors: called CC=1 SCAR=0.0
186 2 return ( $self->_format_number, ref $self );
187 }
188  
189 2 1;
190 __END__
« blib/lib/DB/Color.pm ↑ Summary Build »