google plugin: Use google.com to search the local site.
[ikiwiki.git] / IkiWiki / Plugin / google.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::google;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7 use URI;
8
9 sub import { #{{{
10         hook(type => "getsetup", id => "google", call => \&getsetup);
11         hook(type => "checkconfig", id => "google", call => \&checkconfig);
12         hook(type => "pagetemplate", id => "google", call => \&pagetemplate);
13 } # }}}
14
15 sub getsetup () { #{{{
16         return
17                 plugin => {
18                         safe => 1,
19                         rebuild => 1,
20                 },
21 } #}}}
22
23 sub checkconfig () { #{{{
24         foreach my $required (qw(url)) {
25                 if (! length $config{$required}) {
26                         error(sprintf(gettext("Must specify %s when using the google search plugin"), $required));
27                 }
28         }
29 } #}}}
30
31 my $form;
32 sub pagetemplate (@) { #{{{
33         my %params=@_;
34         my $page=$params{page};
35         my $template=$params{template};
36
37         # Add search box to page header.
38         if ($template->query(name => "searchform")) {
39                 if (! defined $form) {
40                         my $searchform = template("googleform.tmpl", blind_cache => 1);
41                         $searchform->param(sitefqdn => URI->new($config{url})->host);
42                         $form=$searchform->output;
43                 }
44
45                 $template->param(searchform => $form);
46         }
47 } #}}}
48
49 1