2 # This plugin adds a "Diff" button to the page edit form.
3 package IkiWiki::Plugin::editdiff;
12 hook(type => "getsetup", id => "editdiff", call => \&getsetup);
13 hook(type => "formbuilder_setup", id => "editdiff",
14 call => \&formbuilder_setup);
31 $SIG{PIPE} = sub { $sigpipe=1; };
33 my $pid = open2(*DIFFOUT, *DIFFIN, 'diff', '-u', $orig, '-');
34 binmode($_, ':utf8') foreach (*DIFFIN, *DIFFOUT);
36 print DIFFIN $content;
51 return "couldn't run diff\n" if $sigpipe;
53 return "<pre>".encode_entities($ret)."</pre>";
56 sub formbuilder_setup {
58 my $form=$params{form};
60 return if ! defined $form->field("do") || $form->field("do") ne "edit";
62 my $page=$form->field("page");
63 $page = IkiWiki::possibly_foolish_untaint($page);
64 return unless exists $pagesources{$page};
66 push @{$params{buttons}}, "Diff";
68 if ($form->submitted eq "Diff") {
69 my $content=$form->field('editcontent');
70 $content=~s/\r\n/\n/g;
73 my $diff = diff(srcfile($pagesources{$page}), $content);
74 $form->tmpl_param("page_preview", $diff);