Wiki-Toolkit-Plugin-Diff-0.12/0002755000175000017500000000000010537465167014224 5ustar domdomWiki-Toolkit-Plugin-Diff-0.12/t/0002755000175000017500000000000010537465167014467 5ustar domdomWiki-Toolkit-Plugin-Diff-0.12/t/pod_coverage.t0000644000175000017500000000036310432403562017272 0ustar domdom# -*- perl -*- # t/pod_coverage.t - Generic Test::Pod::Coverage testing module use Test::More; eval "use Test::Pod::Coverage 1.00"; plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; all_pod_coverage_ok(); Wiki-Toolkit-Plugin-Diff-0.12/t/02_diff.t0000644000175000017500000002125210537317245016056 0ustar domdomuse strict; use Wiki::Toolkit::TestLib; use Test::More; use VCS::Lite; my $newlite = (VCS::Lite->VERSION >= 0.08); my $iterator = Wiki::Toolkit::TestLib->new_wiki_maker; plan tests => ( 1 + $iterator->number * 18 ); use_ok( "Wiki::Toolkit::Plugin::Diff" ); while ( my $wiki = $iterator->new_wiki ) { print "#\n##### TEST CONFIG: Store: " . (ref $wiki->store) . "\n"; # Add test data $wiki->write_node( "Jerusalem Tavern", "Pub in Clerkenwell with St Peter's beer.", undef, { category => [ "Pubs" ] } ); my %j1 = $wiki->retrieve_node( "Jerusalem Tavern"); $wiki->write_node( "Jerusalem Tavern", "Tiny pub in Clerkenwell with St Peter's beer. Near Farringdon station", $j1{checksum}, { category => [ "Pubs" ] } ); my %j2 = $wiki->retrieve_node( "Jerusalem Tavern"); $wiki->write_node( "Jerusalem Tavern", "Tiny pub in Clerkenwell with St Peter's beer. Near Farringdon station", $j2{checksum}, { category => [ "Pubs", "Real Ale" ], locale => [ "Farringdon" ] } ); my %j3 = $wiki->retrieve_node( "Jerusalem Tavern"); $wiki->write_node( "Jerusalem Tavern", "Tiny pub in Clerkenwell with St Peter's beer but no food. Near Farringdon station", $j3{checksum}, { category => [ "Pubs", "Real Ale" ], locale => [ "Farringdon" ] } ); $wiki->write_node( "IvorW", " In real life: Ivor Williams Ideas & things to work on: * Threaded discussion wiki * Generify diff * SuperSearch for Wiki::Toolkit * Authentication module * Autoindex generation ", undef, { username => 'Foo', metatest => 'Moo' }, ); my %i1 = $wiki->retrieve_node( "IvorW"); $wiki->write_node( "IvorW", $i1{content}." [[IvorW's Test Page]]\n", $i1{checksum}, { username => 'Bar', metatest => 'Boo' }, ); my %i2 = $wiki->retrieve_node( "IvorW"); $wiki->write_node( "IvorW", $i2{content}." [[Another Test Page]]\n", $i2{checksum}, { username => 'Bar', metatest => 'Quack' }, ); my %i3 = $wiki->retrieve_node( "IvorW"); my $newcont = $i3{content}; $newcont =~ s/\n/ \n/s; $wiki->write_node( "IvorW", $newcont, $i3{checksum}, { username => 'Bar', metatest => 'Quack' }, ); $wiki->write_node( "Test", "a", undef, { }, ); %i3 = $wiki->retrieve_node( "Test"); $wiki->write_node( "Test", "a\n", $i3{checksum}, { }, ); pass "backend primed with test data"; # Real tests my $differ = eval { Wiki::Toolkit::Plugin::Diff->new; }; is( $@, "", "'new' doesn't croak" ); isa_ok( $differ, "Wiki::Toolkit::Plugin::Diff" ); $wiki->register_plugin( plugin => $differ ); # Test ->null diff my %nulldiff = $differ->differences( node => "Jerusalem Tavern", left_version => 1, right_version => 1); ok( !exists($nulldiff{diff}), "Diffing the same version returns empty diff"); # Test ->body diff my %bodydiff = $differ->differences( node => "Jerusalem Tavern", left_version => 1, right_version => 2); is( @{$bodydiff{diff}}, 2, "Differ returns 2 elements for body diff"); is_deeply( $bodydiff{diff}[0], { left => "== Line 0 ==\n", right => "== Line 1 ==\n"}, "First element is line number on right"); is_deeply( $bodydiff{diff}[1], $newlite ? { left => 'Pub '. 'in Clerkenwell with St Peter\'s beer.'. "
", right => 'Tiny pub '. 'in Clerkenwell with St Peter\'s beer.'. '
'. "\nNear Farringdon station
". "
", } : { left => 'Pub '. 'in Clerkenwell with St Peter\'s beer.'. "
\n", right => 'Tiny pub '. 'in Clerkenwell with St Peter\'s beer.'. '
'. "\nNear Farringdon station
". "
\n", }, "Differences highlights body diff with span tags"); # Test ->meta diff my %metadiff = $differ->differences( node => "Jerusalem Tavern", left_version => 2, right_version => 3); is( @{$metadiff{diff}}, 2, "Differ returns 2 elements for meta diff"); is_deeply( $metadiff{diff}[0], { left => "== Line 2 ==\n", right => "== Line 2 ==\n"}, "First element is line number on right"); is_deeply( $metadiff{diff}[1], $newlite ? { left => "\ncategory='Pubs'\nlocale='Farringdon'", right => "\ncategory='Pubs'\n". 'category=\'Pubs,Real Ale\'
'. "\n
locale='Farringdon'", } : { left => "category='Pubs'", right => "category='Pubs". ',Real Ale\'
'. "\nlocale='Farringdon
'", }, "Differences highlights metadata diff with span tags"); # Another body diff with bracketed content %bodydiff = $differ->differences( node => 'IvorW', left_version => 1, right_version => 2); is_deeply( $bodydiff{diff}[0], { left => "== Line 11 ==\n", right => "== Line 11 ==\n"}, "Diff finds the right line number on right"); is_deeply( $bodydiff{diff}[1], $newlite ? { left => "\nmetatest='Moo'\nmetatest='Boo'", right => "\nmetatest='Moo'\n". ''. "[[IvorW's Test Page]]
\n". "
\n
". "metatest='Boo'" } : { left => "metatest='". 'Moo\'', right => ''. "[[IvorW's Test Page]]
\n". "
\n
". "metatest='". 'Boo\'', }, "Diff scans words correctly"); # And now a check for framing %bodydiff = $differ->differences( node => 'IvorW', left_version => 2, right_version => 3); is_deeply( $bodydiff{diff}[0], { left => "== Line 13 ==\n", right => "== Line 13 ==\n"}, "Diff finds the right line number on right"); is_deeply( $bodydiff{diff}[1], $newlite ? { left => "\nmetatest='Boo'\nmetatest='Quack'", right => "\nmetatest='Boo'\n". ''. "[[Another Test Page]]
\n". "
\n
". "metatest='Quack'", } : { left => "metatest='". 'Boo\'', right => ''. "[[Another Test Page]]
\n". "
\n
". "metatest='". 'Quack\'', }, "Diff frames correctly"); # Trailing whitespace test 1 %bodydiff = $differ->differences( node => 'IvorW', left_version => 3, right_version => 4); ok(!exists($bodydiff{diff}), 'No change found for trailing whitespace'); # Trailing whitespace test 2 %bodydiff = $differ->differences( node => 'Jerusalem Tavern', left_version => 3, right_version => 4); is_deeply( $bodydiff{diff}[0], { left => "== Line 0 ==\n", right => "== Line 0 ==\n" }, "Diff finds the right line numbers"); is_deeply( $bodydiff{diff}[1], $newlite ? { left => "Tiny pub in Clerkenwell with St Peter's beer". ".
", right => "Tiny pub in Clerkenwell with St Peter's beer". ' but no food.'. "
", } : { left => "Tiny pub in Clerkenwell with St Peter's beer". ".
\n", right => "Tiny pub in Clerkenwell with St Peter's beer". ' but no food.'. "
\n", }, "Diff handles trailing whitespace correctly"); eval { $differ->differences( node => 'Test', left_version => 1, right_version => 2 ) }; is( $@, "", "differences doesn't die when only difference is a newline"); } Wiki-Toolkit-Plugin-Diff-0.12/t/pod.t0000644000175000017500000000030010432403562015406 0ustar domdom# -*- perl -*- # t/pod.t - Generic Test::Pod testing module use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); Wiki-Toolkit-Plugin-Diff-0.12/lib/0002755000175000017500000000000010537465167014772 5ustar domdomWiki-Toolkit-Plugin-Diff-0.12/lib/Wiki/0002755000175000017500000000000010537465167015675 5ustar domdomWiki-Toolkit-Plugin-Diff-0.12/lib/Wiki/Toolkit/0002755000175000017500000000000010537465167017322 5ustar domdomWiki-Toolkit-Plugin-Diff-0.12/lib/Wiki/Toolkit/Plugin/0002755000175000017500000000000010537465167020560 5ustar domdomWiki-Toolkit-Plugin-Diff-0.12/lib/Wiki/Toolkit/Plugin/Diff.pm0000644000175000017500000002612610537317245021764 0ustar domdompackage Wiki::Toolkit::Plugin::Diff; use strict; use warnings; our $VERSION = '0.12'; use base 'Wiki::Toolkit::Plugin'; use Algorithm::Diff; use VCS::Lite; use Params::Validate::Dummy (); use Module::Optional qw(Params::Validate validate validate_pos SCALAR SCALARREF ARRAYREF HASHREF UNDEF); sub new { my $class = shift; my %par = validate( @_, { metadata_separator => { type => SCALAR, default => "
\n"} , line_number_format => { type => SCALAR, default => "== Line \$_ ==\n" }, word_matcher => { type => SCALARREF, default => qr( &.+?; #HTML special characters e.g. < | #Line breaks |\w+\s* #Word with trailing spaces |. #Any other single character )xsi }, } ); bless \%par, $class; } sub differences { my $self = shift; my %args = validate( @_, { node => { type => SCALAR}, left_version => { type => SCALAR}, right_version => { type => SCALAR}, meta_include => { type => ARRAYREF, optional => 1 }, meta_exclude => { type => ARRAYREF, optional => 1 } }); my ($node, $v1, $v2) = @args{ qw( node left_version right_version) }; my $store = $self->datastore; my $fmt = $self->formatter; my %ver1 = $store->retrieve_node( name => $node, version => $v1); my %ver2 = $store->retrieve_node( name => $node, version => $v2); my $verstring1 = "Version ".$ver1{version}; my $verstring2 = "Version ".$ver2{version}; my $el1 = VCS::Lite->new($verstring1,undef, $self->content_escape($ver1{content}). $self->{metadata_separator}. $self->serialise_metadata($ver1{metadata}, @args{qw(meta_include meta_exclude)})); my $el2 = VCS::Lite->new($verstring2,undef, $self->content_escape($ver2{content}). $self->{metadata_separator}. $self->serialise_metadata($ver2{metadata}, @args{qw(meta_include meta_exclude)})); my %pag = %ver1; $pag{left_version} = $verstring1; $pag{right_version} = $verstring2; $pag{content} = $fmt->format($ver1{content}); my $dlt = $el1->delta($el2) or return %pag; my @out; for ($dlt->hunks) { my ($lin1,$lin2,$out1,$out2); for (@$_) { my ($ind,$line,$text) = @$_; if ($ind ne '+') { $lin1 ||= $line; $out1 .= $text; } if ($ind ne '-') { $lin2 ||= $line; $out2 .= $text; } } push @out,{ left => $self->line_number($lin1), right => $self->line_number($lin2) }; my ($text1,$text2) = $self->intradiff($out1,$out2); push @out,{left => $text1, right => $text2}; } $pag{diff} = \@out; %pag; } sub line_number { my $self = shift; local ($_) = validate_pos(@_, {type => SCALAR | UNDEF, optional => 1} ); return '' unless defined $_; my $fmt = '"'. $self->{line_number_format} . '"'; eval $fmt; } sub serialise_metadata { my $self = shift; my ($all_meta,$include,$exclude) = validate_pos ( @_, { type => HASHREF }, { type => ARRAYREF | UNDEF, optional => 1 }, { type => ARRAYREF | UNDEF, optional => 1 }, ); $include ||= [keys %$all_meta]; $exclude ||= [qw(comment username __categories__checksum __locales__checksum)] ; my %metadata = map {$_,$all_meta->{$_}} @$include; delete $metadata{$_} for @$exclude; join $self->{metadata_separator}, map {"$_='".join (',',sort @{$metadata{$_}})."'"} sort keys %metadata; } sub content_escape { my $self = shift; my ($str) = validate_pos( @_, { type => SCALAR } ); $str =~ s/&/&/g; $str =~ s//>/g; $str =~ s!\s*?\n!
\n!gs; $str; } sub intradiff { my $self = shift; my ($str1,$str2) = validate_pos( @_, {type => SCALAR|UNDEF }, {type => SCALAR|UNDEF }); return (qq{$str1},"") unless $str2; return ("",qq{$str2}) unless $str1; my $re_wordmatcher = $self->{word_matcher}; my @diffs = Algorithm::Diff::sdiff([$str1 =~ /$re_wordmatcher/sg] ,[$str2 =~ /$re_wordmatcher/sg], sub {$self->get_token(@_)}); my $out1 = ''; my $out2 = ''; my ($mode1,$mode2); for (@diffs) { my ($ind,$c1,$c2) = @$_; my $newmode1 = $ind =~ /[c\-]/; my $newmode2 = $ind =~ /[c+]/; $out1 .= '' if $newmode1 && !$mode1; $out2 .= '' if $newmode2 && !$mode2; $out1 .= '' if !$newmode1 && $mode1; $out2 .= '' if !$newmode2 && $mode2; ($mode1,$mode2) = ($newmode1,$newmode2); $out1 .= $c1; $out2 .= $c2; } $out1 .= '' if $mode1; $out2 .= '' if $mode2; ($out1,$out2); } sub get_token { my ($self,$str) = @_; $str =~ /^(\S*)\s*$/; # Match all but trailing whitespace $1 || $str; } 1; __END__ =head1 NAME Wiki::Toolkit::Plugin::Diff - format differences between two Wiki::Toolkit pages =head1 SYNOPSIS use Wiki::Toolkit::Plugin::Diff; my $plugin = Wiki::Toolkit::Plugin::Diff->new; $wiki->register_plugin( plugin => $plugin ); # called before any node reads my %diff = $plugin->differences( node => 'Imperial College', left_version => 3, right_version => 5); =head1 DESCRIPTION A plug-in for Wiki::Toolkit sites, which provides a nice extract of differences between two versions of a node. =head1 BASIC USAGE B my %diff_vars = $plugin->differences( node => "Home Page", left_version => 3, right_version => 5 ); Takes a series of key/value pairs: =over 4 =item * B The node version whose content we're considering canonical. =item * B The node version that we're showing the differences from. =item * B Filter the list of metadata fields to only include a certain list in the diff output. The default is to include all metadata fields. =item * B Filter the list of metadata fields to exclude certain fields from the diff output. The default is the following list, to match previous version (OpenGuides) behaviour: C Agreed this list is hopelessly inadequate, especially for L. Hopefully, future wiki designers will use the meta_include parameter to specify exactly what metadata they want to appear on the diff. =back The differences method returns a list of key/value pairs, which can be assigned to a hash: =over 4 =item B The node version whose content we're considering canonical. =item B The node version that we're showing the differences from. =item B The (formatted) contents of the I version of the node. =item B An array of hashrefs of C of differences between the versions. It is assumed that the display will be rendered in HTML, and SPAN tags are inserted with a class of diff1 or diff2, to highlight which individual words have actually changed. Display the contents of diff using a EtableE, with each member of the array corresponding to a row ETRE, and keys {left} and {right} being two columns ETDE. Usually you will want to feed this through a templating system, such as Template Toolkit, which makes iterating the AoH very easy. =back =head1 ADVANCED Wiki::Toolkit::Plugin::Diff allows for a more flexible approach than HTML only rendering of pages. In particular, there are optional parameters to the constructor which control fine detail of the resultant output. If this is not sufficient, the module is also subclassable, and the programmer can supply alternative methods. =head2 METHODS Most of these are called internally by the plugin, but provide hooks for alternative code if the module is subclassed. =over 4 =item B my $plugin = Wiki::Toolkit::Plugin::Diff->new( option => value, option => value...); Here, I