save the full path to the setup file
[ikiwiki] / IkiWiki / Plugin / googlecalendar.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::googlecalendar;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "getsetup", id => "googlecalendar",
10                 call => \&getsetup);
11         hook(type => "preprocess", id => "googlecalendar",
12                 call => \&preprocess);
13         hook(type => "format", id => "googlecalendar",
14                 call => \&format);
15 } # }}}
16
17 sub getsetup () { #{{{
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => undef,
22                 },
23 } #}}}
24
25 sub preprocess (@) { #{{{
26         my %params=@_;
27
28         # Parse the html, looking for the url to embed for the calendar.
29         # Avoid XSS attacks..
30         my ($url)=$params{html}=~m#iframe\s+src="http://www\.google\.com/calendar/embed\?([^"<>]+)"#;
31         if (! defined $url || ! length $url) {
32                 error gettext("failed to find url in html")
33         }
34         my ($height)=$params{html}=~m#height="(\d+)"#;
35         my ($width)=$params{html}=~m#width="(\d+)"#;
36
37         return "<div class=\"googlecalendar\" src=\"$url\" height=\"$height\" width=\"$width\"></div>";
38 } # }}}
39
40 sub format (@) { #{{{
41         my %params=@_;
42
43         $params{content}=~s/<div class=\"googlecalendar" src="([^"]+)" height="([^"]+)" width="([^"]+)"><\/div>/gencal($1,$2,$3)/eg;
44
45         return $params{content};
46 } # }}}
47
48 sub gencal ($$$) { #{{{
49         my $url=shift;
50         my $height=shift;
51         my $width=shift;
52         return qq{<iframe src="http://www.google.com/calendar/embed?$url" style=" border-width:0 " width="$width" frameborder="0" height="$height"></iframe>};
53 } #}}}
54
55 1