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);
30 $SIG{PIPE} = sub { $sigpipe=1; };
32 my $pid = open2(*DIFFOUT, *DIFFIN, 'diff', '-u', $orig, '-');
33 binmode($_, ':utf8') foreach (*DIFFIN, *DIFFOUT);
35 print DIFFIN $content;
50 return "couldn't run diff\n" if $sigpipe;
52 return "<pre>".encode_entities($ret)."</pre>";
55 sub formbuilder_setup {
57 my $form=$params{form};
59 return if ! defined $form->field("do") || $form->field("do") ne "edit";
61 my $page=$form->field("page");
62 $page = IkiWiki::possibly_foolish_untaint($page);
63 return unless exists $pagesources{$page};
65 push @{$params{buttons}}, "Diff";
67 if ($form->submitted eq "Diff") {
68 my $content=$form->field('editcontent');
69 $content=~s/\r\n/\n/g;
72 my $diff = diff(srcfile($pagesources{$page}), $content);
73 $form->tmpl_param("page_preview", $diff);