| 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 ||= {}
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||||||||||||
| 28 | my %colors = ( | ||||||||||||||||||||||||||||||||
| 29 | '-' => $options->{'-'} || 'diff_minus', | ||||||||||||||||||||||||||||||||
| 30 | '+' => $options->{'+'} || 'diff_plus', | ||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | 'u' => $options->{'u'} || 'diff_unchanged', | ||||||||||||||||||||||||||||||
|
Condition50%3/6
$options->{'-'} || 'diff_minus'
$options->{'+'} || 'diff_plus'
$options->{'u'} || 'diff_unchanged'
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->{'-'}
Truth table50%1/2
A
A= $options->{'+'}
Truth table50%1/2
A
A= $options->{'u'}
| |||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | my $context_re = $options->{context} || qr/.*/; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'context'} || qr/.*/
MC/DC0%0/1
$options->{'context'} || qr/.*/
$options->{'context'}
Truth table50%1/2
A
A= $options->{'context'}
| |||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | my $gap = $options->{gap} || ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'gap'} || ''
MC/DC0%0/1
$options->{'gap'} || ''
$options->{'gap'}
Truth table50%1/2
A
A= $options->{'gap'}
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@sdiff and () = @{shift @sdiff;}
MC/DC50%1/2
@sdiff and () = @{shift @sdiff;}
@sdiff
() = @{shift @sdiff;}
Truth table66%2/3
A and B
A= @sdiffB= () = @{shift @sdiff;}
| |||||||||||||||||||||||||||||||||
| 40 | 6 | ▶ | if ($mod =~ /[+-]/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | push @html, _colored($s1 || $s2, $colors{$mod}); | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$s1 || $s2
MC/DC0%0/2
$s1 || $s2
$s1
$s2
Truth table0%0/3
A || B
A= $s1B= $s2
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@sdiff and $sdiff[0][0] eq $mod
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
$_ ||= ""
MC/DC0%0/1
$_ ||= ""
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 87 | ? $start_gap_end | ||||||||||||||||||||||||||||||||
| 88 | : $full_string; | ||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 1 | 1; | |||||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||||||