]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/google.pm
move dedup code into function, simplify
[git.ikiwiki.info.git] / IkiWiki / Plugin / google.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::google;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use URI;
9 my $host;
11 sub import {
12         hook(type => "getsetup", id => "google", call => \&getsetup);
13         hook(type => "checkconfig", id => "google", call => \&checkconfig);
14         hook(type => "pagetemplate", id => "google", call => \&pagetemplate);
15 }
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => 1,
22                 },
23 }
25 sub checkconfig () {
26         if (! length $config{url}) {
27                 error(sprintf(gettext("Must specify %s when using the %s plugin"), "url", 'google'));
28         }
29         my $uri=URI->new($config{url});
30         if (! $uri || ! defined $uri->host) {
31                 error(gettext("Failed to parse url, cannot determine domain name"));
32         }
33         $host=$uri->host;
34 }
36 my $form;
37 sub pagetemplate (@) {
38         my %params=@_;
39         my $page=$params{page};
40         my $template=$params{template};
42         # Add search box to page header.
43         if ($template->query(name => "searchform")) {
44                 if (! defined $form) {
45                         my $searchform = template("googleform.tmpl", blind_cache => 1);
46                         $searchform->param(sitefqdn => $host);
47                         $form=$searchform->output;
48                 }
50                 $template->param(searchform => $form);
51         }
52 }
54 1