summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/cutpaste.pm
blob: 0f6ea0b1f3798650219f7a08a66fa77f85151401 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::cutpaste;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. sub import {
  7. hook(type => "getsetup", id => "cutpaste", call => \&getsetup);
  8. hook(type => "needsbuild", id => "cutpaste", call => \&needsbuild);
  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 needsbuild (@) {
  22. my $needsbuild=shift;
  23. foreach my $page (keys %pagestate) {
  24. if (exists $pagestate{$page}{cutpaste}) {
  25. if (exists $pagesources{$page} &&
  26. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  27. # remove state, will be re-added if
  28. # the cut/copy directive is still present
  29. # on rebuild.
  30. delete $pagestate{$page}{cutpaste};
  31. }
  32. }
  33. }
  34. return $needsbuild;
  35. }
  36. sub preprocess_cut (@) {
  37. my %params=@_;
  38. foreach my $param (qw{id text}) {
  39. if (! exists $params{$param}) {
  40. error sprintf(gettext('%s parameter is required'), $param);
  41. }
  42. }
  43. $pagestate{$params{page}}{cutpaste}{$params{id}} = $params{text};
  44. return "" if defined wantarray;
  45. }
  46. sub preprocess_copy (@) {
  47. my %params=@_;
  48. foreach my $param (qw{id text}) {
  49. if (! exists $params{$param}) {
  50. error sprintf(gettext('%s parameter is required'), $param);
  51. }
  52. }
  53. $pagestate{$params{page}}{cutpaste}{$params{id}} = $params{text};
  54. return IkiWiki::preprocess($params{page}, $params{destpage}, $params{text})
  55. if defined wantarray;
  56. }
  57. sub preprocess_paste (@) {
  58. my %params=@_;
  59. foreach my $param (qw{id}) {
  60. if (! exists $params{$param}) {
  61. error sprintf(gettext('%s parameter is required'), $param);
  62. }
  63. }
  64. if (! exists $pagestate{$params{page}}{cutpaste}) {
  65. error gettext('no text was copied in this page');
  66. }
  67. if (! exists $pagestate{$params{page}}{cutpaste}{$params{id}}) {
  68. error sprintf(gettext('no text was copied in this page with id %s'), $params{id});
  69. }
  70. return IkiWiki::preprocess($params{page}, $params{destpage},
  71. $pagestate{$params{page}}{cutpaste}{$params{id}});
  72. }
  73. 1;