]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/cutpaste.pm
fix targetpage replacement to support 3 argument form
[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 3.00;
8 sub import {
9         hook(type => "getsetup", id => "cutpaste", call => \&getsetup);
10         hook(type => "needsbuild", id => "cutpaste", call => \&needsbuild);
11         hook(type => "preprocess", id => "cut", call => \&preprocess_cut, scan => 1);
12         hook(type => "preprocess", id => "copy", call => \&preprocess_copy, scan => 1);
13         hook(type => "preprocess", id => "paste", call => \&preprocess_paste);
14 }
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => undef,
21                         section => "widget",
22                 },
23 }
25 sub needsbuild (@) {
26         my $needsbuild=shift;
27         foreach my $page (keys %pagestate) {
28                 if (exists $pagestate{$page}{cutpaste}) {
29                         if (exists $pagesources{$page} &&
30                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
31                                 # remove state, will be re-added if
32                                 # the cut/copy directive is still present
33                                 # on rebuild.
34                                 delete $pagestate{$page}{cutpaste};
35                         }
36                 }
37         }
38         return $needsbuild;
39 }
41 sub preprocess_cut (@) {
42         my %params=@_;
44         foreach my $param (qw{id text}) {
45                 if (! exists $params{$param}) {
46                         error sprintf(gettext('%s parameter is required'), $param);
47                 }
48         }
50         $pagestate{$params{page}}{cutpaste}{$params{id}} = $params{text};
52         return "" if defined wantarray;
53 }
55 sub preprocess_copy (@) {
56         my %params=@_;
58         foreach my $param (qw{id text}) {
59                 if (! exists $params{$param}) {
60                         error sprintf(gettext('%s parameter is required'), $param);
61                 }
62         }
64         $pagestate{$params{page}}{cutpaste}{$params{id}} = $params{text};
66         return IkiWiki::preprocess($params{page}, $params{destpage}, $params{text})
67                 if defined wantarray;
68 }
70 sub preprocess_paste (@) {
71         my %params=@_;
73         foreach my $param (qw{id}) {
74                 if (! exists $params{$param}) {
75                         error sprintf(gettext('%s parameter is required'), $param);
76                 }
77         }
79         if (! exists $pagestate{$params{page}}{cutpaste}) {
80                 error gettext('no text was copied in this page');
81         }
82         if (! exists $pagestate{$params{page}}{cutpaste}{$params{id}}) {
83                 error sprintf(gettext('no text was copied in this page with id %s'), $params{id});
84         }
86         return IkiWiki::preprocess($params{page}, $params{destpage},
87                 $pagestate{$params{page}}{cutpaste}{$params{id}});
88 }
90 1;