change plugin interface to use named parameters for flexability

master
joey 2006-05-03 19:58:58 +00:00
parent 4ba45ec3fc
commit a44bfb158d
7 changed files with 21 additions and 14 deletions

View File

@ -7,7 +7,7 @@ use File::Spec;
use HTML::Template;
use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
%renderedfiles %pagesources %depends %plugins};
%renderedfiles %pagesources %depends %hooks};
sub checkconfig () { #{{{
if ($config{cgi} && ! length $config{url}) {
@ -387,12 +387,14 @@ sub globlist_match ($$) { #{{{
return 0;
} #}}}
sub register_plugin ($$$) { # {{{
my $type=shift;
my $command=shift;
my $function=shift;
sub hook (@) { # {{{
my %param=@_;
$plugins{$type}{$command}=$function;
if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
error "hook requires type, call, and id parameters";
}
$hooks{$param{type}}{$param{id}}=\%param;
} # }}}
1

View File

@ -7,7 +7,8 @@ use strict;
use IkiWiki;
sub import { #{{{
IkiWiki::register_plugin("preprocess", "brokenlinks", \&preprocess);
IkiWiki::hook(type => "preprocess", id => "brokenlinks",
call => \&preprocess);
} # }}}
sub preprocess (@) { #{{{

View File

@ -7,7 +7,8 @@ use strict;
use IkiWiki;
sub import { #{{{
IkiWiki::register_plugin("preprocess", "inline", \&IkiWiki::preprocess_inline);
IkiWiki::hook(type => "preprocess", id => "inline",
call => \&IkiWiki::preprocess_inline);
} # }}}
# Back to ikiwiki namespace for the rest, this code is very much

View File

@ -7,7 +7,8 @@ use strict;
use IkiWiki;
sub import { #{{{
IkiWiki::register_plugin("preprocess", "orphans", \&preprocess);
IkiWiki::hook(type => "preprocess", id => "orphans",
call => \&preprocess);
} # }}}
sub preprocess (@) { #{{{

View File

@ -7,7 +7,8 @@ use strict;
use IkiWiki;
sub import { #{{{
IkiWiki::register_plugin("preprocess", "pagecount", \&preprocess);
IkiWiki::hook(type => "preprocess", id => "pagecount",
call => \&preprocess);
} # }}}
sub preprocess (@) { #{{{

View File

@ -1,6 +1,6 @@
#!/usr/bin/perl
# Ikiwiki skeleton plugin. Replace "skeleton" with the name of your plugin
# in the lines below, and flesh out the methods to make it do something.
# in the lines below, and flesh out the code to make it do something.
package IkiWiki::Plugin::skeleton;
use warnings;
@ -8,7 +8,8 @@ use strict;
use IkiWiki;
sub import { #{{{
IkiWiki::register_plugin("preprocess", "skeleton", \&preprocess);
IkiWiki::hook(type => "preprocess", id => "skeleton",
call => \&preprocess);
} # }}}
sub preprocess (@) { #{{{

View File

@ -134,12 +134,12 @@ sub preprocess ($$) { #{{{
if (length $escape) {
return "[[$command $params]]";
}
elsif (exists $plugins{preprocess}{$command}) {
elsif (exists $hooks{preprocess}{$command}) {
my %params;
while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
$params{$1}=$2;
}
return $plugins{preprocess}{$command}->(page => $page, %params);
return $hooks{preprocess}{$command}{call}->(page => $page, %params);
}
else {
return "[[$command not processed]]";