lib/MojoMojo/WordDiff.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 93.3% 56 / 60 CC 8 SCAR 12.5
CC
8
Cov
92%
word_diff
5.0
_split_html_str
4.0
BEGIN
1.0
CRAP 3.5
« lib/MojoMojo/Schema/Result/Page.pm ↑ Summary lib/MojoMojo/Formatter/SyntaxHighlight.pm »
1 package MojoMojo::WordDiff;
2  
3 # based on Text::WordDiff, but with many differences.
4  
5 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 41 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 41 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 41 ▶use Algorithm::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 41 our @EXPORT = qw(word_diff);
11  
12 10 ▶sub _split_html_str {
Subroutine _split_html_str: called CC=4 SCAR=13.9
13 10 my $str = shift;
14 10 my @array;
15 10 my @tags = split qr/(?:(?<=>)|(?=<))/msx, $str;
16 10 foreach(@tags) {
17 18 ▶ if(length($_) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 0) 0 18 18
18 0 next;
19 }
20 18 ▶ if($_ =~ /^</) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^</) { } 8 10 18
21 8 push @array, $_;
22 } else {
23 10 my $tmp_str = decode_entities($_);
24 10 my @tmp_arr = split qr/(?:(?<!\p{IsWord})(?=\p{IsWord})|(?<!\p{IsSpace})(?=\p{IsSpace})|(?<!\p{IsPunct})(?=\p{IsPunct}))/msx, $tmp_str;
25 10 @tmp_arr = map {$_ = encode_entities($_)} @tmp_arr;
26 10 push @array, @tmp_arr;
27 }
28 }
29 10 return @array;
30 }
31  
32 5 ▶sub word_diff {
Subroutine word_diff: called CC=5 SCAR=16.1
Pod documented
33 5 my @args = map {my @a = _split_html_str($_); \@a;} @_;
34 5 my $diff = Algorithm::Diff->new(@args);
35 5 my $out = "";
36 5 ▶ while ($diff->Next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->Next) 13 5 18
37 13 ▶ if (my @same = $diff->Same) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@same) = $diff->Same) { } 8 5 13
38 8 $out .= (join '', @same);
39 }
40 else {
41 5 ▶ if (my @del = $diff->Items(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@del) = $diff->Items(1)) 5 0 5
42 5 $out .= '<del>' . (join '', @del) . '</del>';
43 }
44 5 ▶ if (my @ins = $diff->Items(2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ins) = $diff->Items(2)) 5 0 5
45 5 $out .= '<ins>' . (join '', @ins) . '</ins>';
46 }
47 }
48 }
49 5 return $out;
50 }
51  
52 =head1 NAME
53  
54 MojoMojo::WordDiff - generate inline word-based HTML diffs
55  
56 =head1 DESCRIPTION
57  
58 Creates a word by word line diff for lines that are changed.
59  
60 =head1 METHODS
61  
62 =head2 word_diff
63  
64 Takes two conflicting lines, and returns a line with the diff in HTML.
65  
66 =head2
67  
68 =cut
69  
70 41 1;
« lib/MojoMojo/Schema/Result/Page.pm ↑ Summary lib/MojoMojo/Formatter/SyntaxHighlight.pm »