ready for review, I think
[ikiwiki.git] / IkiWiki / Plugin / cutpaste.pm
blob01e9ce043c90f2eb059f7abc6eb8093de717fb18
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::cutpaste;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
8 my %savedtext;
10 sub import {
11 hook(type => "getsetup", id => "cutpaste", call => \&getsetup);
12 hook(type => "preprocess", id => "cut", call => \&preprocess_cut, scan => 1);
13 hook(type => "preprocess", id => "copy", call => \&preprocess_copy, scan => 1);
14 hook(type => "preprocess", id => "paste", call => \&preprocess_paste);
17 sub getsetup () {
18 return
19 plugin => {
20 safe => 1,
21 rebuild => undef,
22 section => "widget",
26 sub preprocess_cut (@) {
27 my %params=@_;
29 foreach my $param (qw{id text}) {
30 if (! exists $params{$param}) {
31 error sprintf(gettext('%s parameter is required'), $param);
35 $savedtext{$params{page}} = {} if not exists $savedtext{$params{"page"}};
36 $savedtext{$params{page}}->{$params{id}} = $params{text};
38 return "" if defined wantarray;
41 sub preprocess_copy (@) {
42 my %params=@_;
44 foreach my $param (qw{id text}) {
45 if (! exists $params{$param}) {
46 error sprintf(gettext('%s parameter is required'), $param);
50 $savedtext{$params{page}} = {} if not exists $savedtext{$params{"page"}};
51 $savedtext{$params{page}}->{$params{id}} = $params{text};
53 return IkiWiki::preprocess($params{page}, $params{destpage},
54 IkiWiki::filter($params{page}, $params{destpage}, $params{text})) if defined wantarray;
57 sub preprocess_paste (@) {
58 my %params=@_;
60 foreach my $param (qw{id}) {
61 if (! exists $params{$param}) {
62 error sprintf(gettext('%s parameter is required'), $param);
66 if (! exists $savedtext{$params{page}}) {
67 error gettext('no text was copied in this page');
69 if (! exists $savedtext{$params{page}}->{$params{id}}) {
70 error sprintf(gettext('no text was copied in this page with id %s'), $params{id});
73 return IkiWiki::preprocess($params{page}, $params{destpage},
74 IkiWiki::filter($params{page}, $params{destpage}, $savedtext{$params{page}}->{$params{id}}));