summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/cutpaste.pm
blob: b1161ba063c64ca8ecd8e08ec711d43682de64d5 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::cutpaste;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. use UNIVERSAL;
  7. my %savedtext;
  8. sub import { #{{{
  9. hook(type => "preprocess", id => "cut", call => \&preprocess_cut, scan => 1);
  10. hook(type => "preprocess", id => "copy", call => \&preprocess_copy, scan => 1);
  11. hook(type => "preprocess", id => "paste", call => \&preprocess_paste);
  12. } # }}}
  13. sub preprocess_cut (@) { #{{{
  14. my %params=@_;
  15. foreach my $param (qw{id text}) {
  16. if (! exists $params{$param}) {
  17. error sprintf(gettext('%s parameter is required'), $param);
  18. }
  19. }
  20. $savedtext{$params{page}} = {} if not exists $savedtext{$params{"page"}};
  21. $savedtext{$params{page}}->{$params{id}} = $params{text};
  22. return "" if defined wantarray;
  23. } # }}}
  24. sub preprocess_copy (@) { #{{{
  25. my %params=@_;
  26. foreach my $param (qw{id text}) {
  27. if (! exists $params{$param}) {
  28. error sprintf(gettext('%s parameter is required'), $param);
  29. }
  30. }
  31. $savedtext{$params{page}} = {} if not exists $savedtext{$params{"page"}};
  32. $savedtext{$params{page}}->{$params{id}} = $params{text};
  33. return IkiWiki::preprocess($params{page}, $params{destpage},
  34. IkiWiki::filter($params{page}, $params{destpage}, $params{text})) if defined wantarray;
  35. } # }}}
  36. sub preprocess_paste (@) { #{{{
  37. my %params=@_;
  38. foreach my $param (qw{id}) {
  39. if (! exists $params{$param}) {
  40. error sprintf(gettext('%s parameter is required'), $param);
  41. }
  42. }
  43. if (! exists $savedtext{$params{page}}) {
  44. error gettext('no text was copied in this page');
  45. }
  46. if (! exists $savedtext{$params{page}}->{$params{id}}) {
  47. error sprintf(gettext('no text was copied in this page with id %s'), $params{id});
  48. }
  49. return IkiWiki::preprocess($params{page}, $params{destpage},
  50. IkiWiki::filter($params{page}, $params{destpage}, $savedtext{$params{page}}->{$params{id}}));
  51. } # }}}
  52. 1;