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