blib/lib/String/ShowHTMLDiff.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 80.0% 8 / 10 Conditioncond 52.1% 12 / 23 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 76.9% 80 / 104 CC 17 SCAR 23.9
CC
17
Cov
83%
html_colored_diff
16.4
_construct_glue
4.1
BEGIN
1.0
CRAP 10.9
↑ Summary
1 package String::ShowHTMLDiff;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 require Exporter;
6  
7 1 ▶use vars qw/@ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 @ISA = qw(Exporter);
10  
11 1 %EXPORT_TAGS = ( 'all' => [ qw(
12 html_colored_diff
13 ) ] );
14  
15 1 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
16  
17 1 @EXPORT = qw(
18
19 );
20  
21 1 $VERSION = '0.01';
22  
23 1 ▶use Algorithm::Diff qw/sdiff/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶sub html_colored_diff {
Subroutine html_colored_diff: called CC=14 SCAR=28.0
Pod documented
26 1 my ($string, $changed_string, $options) = @_;
27 1 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
01
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
28 my %colors = (
29 '-' => $options->{'-'} || 'diff_minus',
30 '+' => $options->{'+'} || 'diff_plus',
31 1 ▶ 'u' => $options->{'u'} || 'diff_unchanged',
Condition50%3/6
$options->{'-'} || 'diff_minus'
l!l
01
$options->{'+'} || 'diff_plus'
l!l
01
$options->{'u'} || 'diff_unchanged'
l!l
01
MC/DC0%0/1
$options->{'-'} || 'diff_minus'
$options->{'-'}
MC/DC0%0/1
$options->{'+'} || 'diff_plus'
$options->{'+'}
MC/DC0%0/1
$options->{'u'} || 'diff_unchanged'
$options->{'u'}
Truth table50%1/2
A
A= $options->{'-'}
Aresult
11
00
Truth table50%1/2
A
A= $options->{'+'}
Aresult
11
00
Truth table50%1/2
A
A= $options->{'u'}
Aresult
11
00
32 );
33 1 ▶ my $context_re = $options->{context} || qr/.*/;
Condition50%1/2
$options->{'context'} || qr/.*/
l!l
01
MC/DC0%0/1
$options->{'context'} || qr/.*/
$options->{'context'}
Truth table50%1/2
A
A= $options->{'context'}
Aresult
11
00
34 1 ▶ my $gap = $options->{gap} || '';
Condition50%1/2
$options->{'gap'} || ''
l!l
01
MC/DC0%0/1
$options->{'gap'} || ''
$options->{'gap'}
Truth table50%1/2
A
A= $options->{'gap'}
Aresult
11
00
35
36 1 my @sdiff = sdiff(map {[split //, $_]} $string, $changed_string);
37 1 my @html;
38 1 my $first_while_loop = 1;
39 1 ▶ while (@sdiff and my ($mod, $s1, $s2) = @{shift @sdiff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sdiff and () = @{shift @sdiff;}) 6 1 7
Condition66%2/3
@sdiff and () = @{shift @sdiff;}
!ll&&!rl&&r
106
MC/DC50%1/2
@sdiff and () = @{shift @sdiff;}
@sdiff () = @{shift @sdiff;}
Truth table66%2/3
A and B
A= @sdiffB= () = @{shift @sdiff;}
ABresult
0X0
100
111
40 6 ▶ if ($mod =~ /[+-]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /[+-]/) { } 0 6 6
41 0 ▶ push @html, _colored($s1 || $s2, $colors{$mod});
Condition0%0/3
$s1 || $s2
l!l&&r!l&&!r
000
MC/DC0%0/2
$s1 || $s2
$s1 $s2
Truth table0%0/3
A || B
A= $s1B= $s2
ABresult
1X1
011
000
42 } else { # Must be either a change or a part of unchanged characters
43 # So take a look, whether there are more chars that should be
44 # handled in a row
45 6 ▶ while (@sdiff && $sdiff[0]->[0] eq $mod) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sdiff and $sdiff[0][0] eq $mod) 5 6 11
Condition100%3/3
@sdiff and $sdiff[0][0] eq $mod
!ll&&!rl&&r
155
MC/DC100%2/2
@sdiff and $sdiff[0][0] eq $mod
@sdiff $sdiff[0][0] eq $mod
Truth table100%3/3
A and B
A= @sdiffB= $sdiff[0][0] eq $mod
ABresult
0X0
100
111
46 5 $s1 .= $sdiff[0]->[1]; # if so, join all chars from the old
47 # string to $s1
48 5 $s2 .= $sdiff[0]->[2]; # and from the new to $s2
49 5 shift @sdiff; # The information of this element
50 # is already in $s1, $s2 and $mod
51 # and thus unnecessary now
52 }
53 6 ▶ if ($mod eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod eq 'u') { } 3 3 6
54 3 my $unchanged_part = _construct_glue(
55 $s1, $context_re, $gap, $first_while_loop, @sdiff==0
56 );
57 3 push @html, _colorize_string($unchanged_part, $colors{'u'});
58 } else {
59 push @html,
60 _colorize_string($s1, $colors{'-'}),
61 3 _colorize_string($s2, $colors{'+'});
62 }
63 }
64 6 $first_while_loop = 0;
65 }
66 1 return join "", @html;
67 }
68  
69 16 ▶sub _colored {
Subroutine _colored: called CC=1 SCAR=0.0
70 16 my($text, $style) = @_;
71 16 return "<span class='$style'>$text</span>";
72 }
73  
74 # call with _colorize_string($string, $color)
75 9 ▶sub _colorize_string { join "", map {_colored($_,$_[1])} split //, $_[0] }
Subroutine _colorize_string: called CC=1 SCAR=0.0
76  
77 3 ▶sub _construct_glue {
Subroutine _construct_glue: called CC=4 SCAR=14.2
78 3 my ($full_string, $context_re, $gap, $at_beginning, $at_end) = @_;
79 3 my ($start) = $full_string =~ /^($context_re)/;
80 3 my ($end) = $full_string =~ /($context_re)$/;
81 3 ▶ $_ ||= "" for ($start, $end);
Condition50%1/2
$_ ||= ""
l!l
60
MC/DC0%0/1
$_ ||= ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
82  
83 # Return now the shorter string of either a constructed context with a gap
84 # string or the normal string between the two differences
85 3 my $start_gap_end = $start . $gap . $end;
86 3 ▶ return length($start_gap_end) < length($full_string)
Branch: 1 branch
BranchTrueFalseTotal
length $start_gap_end < length $full_string ? : 0 3 3
87 ? $start_gap_end
88 : $full_string;
89 }
90  
91 1 1;
92 __END__
↑ Summary