| 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 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | if ( !( Term::ANSIColor->VERSION >= 3 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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.20 | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | our $VERSION = '0.20'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # increase this number by one to force the cache to generate new md5 numbers | ||||||||||||||||||||||||||||
| 32 | 2 | my $FORMAT_NUMBER = 1; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | *Syntax::Highlight::Engine::Kate::Template::logwarning = sub { }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 2 | my ( $class, $args ) = @_; | |||||||||||||||||||||||||||
| 41 | 2 | my $self = bless {} => $class; | |||||||||||||||||||||||||||
| 42 | 2 | $self->_initialize($args); | |||||||||||||||||||||||||||
| 43 | 2 | return $self; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | ▶ | sub _initialize { | ||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 47 | 2 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $cache_dir and not -d $cache_dir
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
| |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | mkdir $cache_dir or die "Cannot mkdir ($cache_dir): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 2 | ▶ | sub _debug { | ||||||||||||||||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 96 | 2 | my ( $self, $message ) = @_; | |||||||||||||||||||||||||||
| 97 | 2 | ▶ | return unless my $debug = $self->{debug_fh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 2 | print $debug "$message\n"; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub highlight_text { | ||||||||||||||||||||||||||
|
Subroutine
highlight_text: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 0 | my ( $self, $code ) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ( $self->_should_cache ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | my ( $path, $file ) = $self->_get_path_and_file($code); | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | unless ( -d $path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | make_path($path); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 0 | $file = catfile( $path, $file ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( -e $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | open my $fh, '<', $file or die "Cannot open '$file' for reading: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub _get_highlighted_text { | ||||||||||||||||||||||||||
|
Subroutine
_get_highlighted_text: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 134 | 1 | my ( $self, $code ) = @_; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 143 | 89 | $in_pod = 1; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 2047 | ▶ | if ($in_pod) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 2 | ▶ | sub _get_path_and_file { | ||||||||||||||||||||||||||
|
Subroutine
_get_path_and_file: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 166 | 2 | my ( $self, $code ) = @_; | |||||||||||||||||||||||||||
| 167 | 2 | ▶ | unless ( $self->_should_cache ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 2 | ▶ | sub _format_number { | ||||||||||||||||||||||||||
|
Subroutine
_format_number: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 181 | 2 | return $FORMAT_NUMBER; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 2 | ▶ | sub _get_unique_factors { | ||||||||||||||||||||||||||
|
Subroutine
_get_unique_factors: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 185 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 186 | 2 | return ( $self->_format_number, ref $self ); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 2 | 1; | |||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||