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 HTML::Template;
use vars qw{%config %links %oldlinks %oldpagemtime %pagectime use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
%renderedfiles %pagesources %depends %plugins}; %renderedfiles %pagesources %depends %hooks};
sub checkconfig () { #{{{ sub checkconfig () { #{{{
if ($config{cgi} && ! length $config{url}) { if ($config{cgi} && ! length $config{url}) {
@ -387,12 +387,14 @@ sub globlist_match ($$) { #{{{
return 0; return 0;
} #}}} } #}}}
sub register_plugin ($$$) { # {{{ sub hook (@) { # {{{
my $type=shift; my %param=@_;
my $command=shift;
my $function=shift;
$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 1

View File

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

View File

@ -7,7 +7,8 @@ use strict;
use IkiWiki; use IkiWiki;
sub import { #{{{ 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 # Back to ikiwiki namespace for the rest, this code is very much

View File

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

View File

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

View File

@ -1,6 +1,6 @@
#!/usr/bin/perl #!/usr/bin/perl
# Ikiwiki skeleton plugin. Replace "skeleton" with the name of your plugin # 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; package IkiWiki::Plugin::skeleton;
use warnings; use warnings;
@ -8,7 +8,8 @@ use strict;
use IkiWiki; use IkiWiki;
sub import { #{{{ sub import { #{{{
IkiWiki::register_plugin("preprocess", "skeleton", \&preprocess); IkiWiki::hook(type => "preprocess", id => "skeleton",
call => \&preprocess);
} # }}} } # }}}
sub preprocess (@) { #{{{ sub preprocess (@) { #{{{

View File

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