(no commit message)
[ikiwiki] / 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 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 }
15
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => undef,
21                         section => "widget",
22                 },
23 }
24
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 }
40
41 sub preprocess_cut (@) {
42         my %params=@_;
43
44         foreach my $param (qw{id text}) {
45                 if (! exists $params{$param}) {
46                         error sprintf(gettext('%s parameter is required'), $param);
47                 }
48         }
49
50         $pagestate{$params{page}}{cutpaste}{$params{id}} = $params{text};
51
52         return "" if defined wantarray;
53 }
54
55 sub preprocess_copy (@) {
56         my %params=@_;
57
58         foreach my $param (qw{id text}) {
59                 if (! exists $params{$param}) {
60                         error sprintf(gettext('%s parameter is required'), $param);
61                 }
62         }
63
64         $pagestate{$params{page}}{cutpaste}{$params{id}} = $params{text};
65
66         return IkiWiki::preprocess($params{page}, $params{destpage}, $params{text})
67                 if defined wantarray;
68 }
69
70 sub preprocess_paste (@) {
71         my %params=@_;
72
73         foreach my $param (qw{id}) {
74                 if (! exists $params{$param}) {
75                         error sprintf(gettext('%s parameter is required'), $param);
76                 }
77         }
78
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         }
85
86         return IkiWiki::preprocess($params{page}, $params{destpage},
87                 $pagestate{$params{page}}{cutpaste}{$params{id}});
88 }
89
90 1;