2 # Demo external plugin. Kinda pointless, since it's a perl script, but
3 # useful for testing or as an hint of how to write an external plugin in
8 print STDERR "externaldemo plugin running as pid $$\n";
17 # Used to build up RPC calls as they're read from stdin.
21 # Read stdin, a line at a time, until a whole RPC call is accumulated.
22 # Parse to XML::RPC object and return.
26 # Kinda hackish approch to parse a single XML RPC out of the
27 # accumulated input. Relies on calls always ending with a
28 # newline, which ikiwiki's protocol requires be true.
29 if ($accum =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
32 # Now parse the XML RPC.
33 my $r = RPC::XML::Parser->new->parse($1);
35 die "error: XML RPC parse failure $r";
45 # Handle an incoming XML RPC command.
50 if ($r->isa("RPC::XML::request")) {
52 my @args=map { $_->value } @{$r->args};
53 # Dispatch the requested function. This could be
54 # done with a switch statement on the name, or
55 # whatever. I'll use eval to call the function with
57 my $ret = eval $name.'(@args)';
60 # Now send the repsonse from the function back,
61 # followed by a newline.
62 my $resp=RPC::XML::response->new($ret);
63 $resp->serialize(\*STDOUT);
65 # stdout needs to be flushed here. If it isn't,
66 # things will deadlock. Perl flushes it
67 # automatically when $| is set.
70 elsif ($r->isa("RPC::XML::response")) {
71 die "protocol error; got a response when expecting a request";
76 # Make an XML RPC call and return the result.
80 my $req=RPC::XML::request->new($command, @params);
81 $req->serialize(\*STDOUT);
83 # stdout needs to be flushed here to prevent deadlock. Perl does it
84 # automatically when $| is set.
87 if ($r->isa("RPC::XML::response")) {
88 return $r->value->value;
91 die "protocol error; got a request when expecting a response";
95 # Now on with the actual plugin. Let's do a simple preprocessor plugin.
98 # The import function will be called by ikiwiki when the plugin is
99 # loaded. When it's imported, it needs to hook into the preprocessor
101 rpc_call("hook", type => "preprocess", id => "externaldemo", call => "preprocess");
103 # Here's an example of how to inject an arbitrary function into
104 # ikiwiki. Ikiwiki will be able to call bob() just like any other
106 rpc_call("inject", name => "IkiWiki::bob", call => "bob");
108 # Here's an exmaple of how to access values in %IkiWiki::config.
109 print STDERR "url is set to: ".
110 rpc_call("getvar", "config", "url")."\n";
112 print STDERR "externaldemo plugin successfully imported\n";
116 # This function will be called when ikiwiki wants to preprocess
120 # Let's use IkiWiki's pagetitle function to turn the page name into
122 my $title=rpc_call("pagetitle", $params{page});
124 return "externaldemo plugin preprocessing on $title!";
128 print STDERR "externaldemo plugin's bob called via RPC";
131 # Now all that's left to do is loop and handle each incoming RPC request.
132 while (rpc_handle()) { print STDERR "externaldemo plugin handled RPC request\n" }