File Coverage

lib/Kwiki/Diff.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Kwiki::Diff;
2 1     1   36385 use strict;
  1         3  
  1         44  
3 1     1   5 use warnings;
  1         3  
  1         39  
4              
5 1     1   414 use Kwiki::Plugin -Base;
  0            
  0            
6             use Kwiki::Installer -base;
7              
8             our $VERSION = '0.03';
9              
10             const class_title => 'Kwiki diffs';
11             const class_id => 'diff';
12             const cgi_class => 'Kwiki::Diff::CGI';
13             const css_file => 'diff.css';
14              
15             sub register {
16             my $registry = shift;
17             $registry->add( action => 'diff' );
18             $registry->add(
19             toolbar => 'diff_button',
20             template => 'diff_button.html',
21             show_for => 'revisions',
22             params_class => $self->class_id,
23             );
24             $registry->add(
25             toolbar => 'diff_controls',
26             template => 'diff_controls.html',
27             show_for => 'diff',
28             params_class => $self->class_id,
29             );
30             }
31              
32             sub diff {
33              
34             # a lot of chunks here stolen from ingy's Kwiki::Revisions
35             my $page = $self->pages->current;
36             $page->load;
37             my $revision_id = $self->cgi->revision_id
38             or return $self->redirect( $page->url );
39              
40             my $revisions = $page->revision_numbers;
41             $revision_id = $revisions->[ -$revision_id ] if ( $revision_id < 0 );
42             my $archive = $self->hub->archive;
43              
44             my $screen_title =
45             $page->title . " (Diff of Revision $revision_id)",
46              
47             # now the diff
48             my $current = [ split /\n/, $page->content ];
49             my $this_revision = [ split /\n/, $archive->fetch( $page, $revision_id ) ];
50             require Algorithm::Diff;
51             my @diff = Algorithm::Diff::sdiff( $this_revision, $current );
52              
53             # check out sdiff() in Algorithm::Diff -- i'm changing
54             # the markers to real words so we can use them in
55             # the template really easily
56             $_->[0] = {
57             '+' => 'added',
58             '-' => 'removed',
59             'u' => 'unmodified',
60             'c' => 'changed',
61             }->{ $_->[0] }
62             for @diff;
63              
64             $self->render_screen(
65             revision_id => $revision_id,
66             screen_title => $screen_title,
67             diff => \@diff,
68             );
69             }
70              
71             sub toolbar_params {
72             my $revision_id = $self->cgi->revision_id
73             or return $self->redirect( $self->pages->current->url );
74             return revision_id => $revision_id;
75             }
76              
77             package Kwiki::Diff::CGI;
78             use Kwiki::CGI '-base';
79             cgi 'revision_id';
80              
81             package Kwiki::Diff;
82              
83             __DATA__