| 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
| |||||||||||
| 18 | 0 | next; | |||||||||
| 19 | } | ||||||||||
| 20 | 18 | ▶ | if($_ =~ /^</) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 37 | 13 | ▶ | if (my @same = $diff->Same) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 8 | $out .= (join '', @same); | |||||||||
| 39 | } | ||||||||||
| 40 | else { | ||||||||||
| 41 | 5 | ▶ | if (my @del = $diff->Items(1)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 5 | $out .= '<del>' . (join '', @del) . '</del>'; | |||||||||
| 43 | } | ||||||||||
| 44 | 5 | ▶ | if (my @ins = $diff->Items(2)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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; | |||||||||