]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/cutpaste.pm
Coding style change: Remove explcit vim folding markers.
[git.ikiwiki.info.git] / IkiWiki / Plugin / cutpaste.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::cutpaste;
4 use warnings;
5 use strict;
6 use IkiWiki 2.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);
15 }
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => undef,
22                 },
23 }
25 sub preprocess_cut (@) {
26         my %params=@_;
28         foreach my $param (qw{id text}) {
29                 if (! exists $params{$param}) {
30                         error sprintf(gettext('%s parameter is required'), $param);
31                 }
32         }
34         $savedtext{$params{page}} = {} if not exists $savedtext{$params{"page"}};
35         $savedtext{$params{page}}->{$params{id}} = $params{text};
37         return "" if defined wantarray;
38 }
40 sub preprocess_copy (@) {
41         my %params=@_;
43         foreach my $param (qw{id text}) {
44                 if (! exists $params{$param}) {
45                         error sprintf(gettext('%s parameter is required'), $param);
46                 }
47         }
49         $savedtext{$params{page}} = {} if not exists $savedtext{$params{"page"}};
50         $savedtext{$params{page}}->{$params{id}} = $params{text};
52         return IkiWiki::preprocess($params{page}, $params{destpage}, 
53                 IkiWiki::filter($params{page}, $params{destpage}, $params{text})) if defined wantarray;
54 }
56 sub preprocess_paste (@) {
57         my %params=@_;
59         foreach my $param (qw{id}) {
60                 if (! exists $params{$param}) {
61                         error sprintf(gettext('%s parameter is required'), $param);
62                 }
63         }
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         }
72         return IkiWiki::preprocess($params{page}, $params{destpage}, 
73                 IkiWiki::filter($params{page}, $params{destpage}, $savedtext{$params{page}}->{$params{id}}));
74 }
76 1;