]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/cutpaste.pm
Merge branch 'master' into staging
[ikiwiki.git] / IkiWiki / Plugin / cutpaste.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::cutpaste;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 my %savedtext;
9
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);
15 }
16
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => undef,
22                 },
23 }
24
25 sub preprocess_cut (@) {
26         my %params=@_;
27
28         foreach my $param (qw{id text}) {
29                 if (! exists $params{$param}) {
30                         error sprintf(gettext('%s parameter is required'), $param);
31                 }
32         }
33
34         $savedtext{$params{page}} = {} if not exists $savedtext{$params{"page"}};
35         $savedtext{$params{page}}->{$params{id}} = $params{text};
36
37         return "" if defined wantarray;
38 }
39
40 sub preprocess_copy (@) {
41         my %params=@_;
42
43         foreach my $param (qw{id text}) {
44                 if (! exists $params{$param}) {
45                         error sprintf(gettext('%s parameter is required'), $param);
46                 }
47         }
48
49         $savedtext{$params{page}} = {} if not exists $savedtext{$params{"page"}};
50         $savedtext{$params{page}}->{$params{id}} = $params{text};
51
52         return IkiWiki::preprocess($params{page}, $params{destpage}, 
53                 IkiWiki::filter($params{page}, $params{destpage}, $params{text})) if defined wantarray;
54 }
55
56 sub preprocess_paste (@) {
57         my %params=@_;
58
59         foreach my $param (qw{id}) {
60                 if (! exists $params{$param}) {
61                         error sprintf(gettext('%s parameter is required'), $param);
62                 }
63         }
64
65         if (! exists $savedtext{$params{page}}) {
66                 error gettext('no text was copied in this page');
67         }
68         if (! exists $savedtext{$params{page}}->{$params{id}}) {
69                 error sprintf(gettext('no text was copied in this page with id %s'), $params{id});
70         }
71
72         return IkiWiki::preprocess($params{page}, $params{destpage}, 
73                 IkiWiki::filter($params{page}, $params{destpage}, $savedtext{$params{page}}->{$params{id}}));
74 }
75
76 1;