summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/editdiff.pm
blob: 46eb1168edddd42b6086b646ae48d63cb7dd2111 (plain)
  1. #!/usr/bin/perl
  2. # This plugin adds a "Diff" button to the page edit form.
  3. package IkiWiki::Plugin::editdiff;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.00;
  7. use HTML::Entities;
  8. use IPC::Open2;
  9. sub import { #{{{
  10. hook(type => "formbuilder_setup", id => "editdiff",
  11. call => \&formbuilder_setup);
  12. } #}}}
  13. sub diff ($$) { #{{{
  14. my $orig=shift;
  15. my $content=shift;
  16. my $sigpipe=0;
  17. $SIG{PIPE} = sub { $sigpipe=1; };
  18. my $pid = open2(*DIFFOUT, *DIFFIN, 'diff', '-u', $orig, '-');
  19. binmode($_, ':utf8') foreach (*DIFFIN, *DIFFOUT);
  20. print DIFFIN $content;
  21. close DIFFIN;
  22. my $ret;
  23. while (<DIFFOUT>) {
  24. if (defined $ret) {
  25. $ret.=$_;
  26. }
  27. elsif (/^\@\@/) {
  28. $ret=$_;
  29. }
  30. }
  31. close DIFFOUT;
  32. waitpid $pid, 0;
  33. $SIG{PIPE}="default";
  34. return "couldn't run diff\n" if $sigpipe;
  35. return "<pre>".encode_entities($ret)."</pre>";
  36. } #}}}
  37. sub formbuilder_setup { #{{{
  38. my %params=@_;
  39. my $form=$params{form};
  40. my $page=$form->field("page");
  41. return if $form->title ne "editpage"
  42. || $form->field("do") ne "edit";
  43. $page = IkiWiki::titlepage(IkiWiki::possibly_foolish_untaint($page));
  44. return unless exists $pagesources{$page};
  45. push @{$params{buttons}}, "Diff";
  46. if ($form->submitted eq "Diff") {
  47. my $content=$form->field('editcontent');
  48. $content=~s/\r\n/\n/g;
  49. $content=~s/\r/\n/g;
  50. my $diff = diff(srcfile($pagesources{$page}), $content);
  51. $form->tmpl_param("page_preview", $diff);
  52. }
  53. } #}}}
  54. 1