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