summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/cutpaste.pm
blob: 01e9ce043c90f2eb059f7abc6eb8093de717fb18 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::cutpaste;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.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. section => "widget",
  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;