make message a bit more generic
[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 => "preprocess", id => "googlecalendar",
10                 call => \&preprocess);
11         hook(type => "format", id => "googlecalendar",
12                 call => \&format);
13 } # }}}
14
15 sub preprocess (@) { #{{{
16         my %params=@_;
17
18         # Parse the html, looking for the url to embed for the calendar.
19         # Avoid XSS attacks..
20         my ($url)=$params{html}=~m#iframe\s+src="http://www\.google\.com/calendar/embed\?([^"<>]+)"#;
21         if (! defined $url || ! length $url) {
22                 error gettext("failed to find url in html")
23         }
24         my ($height)=$params{html}=~m#height="(\d+)"#;
25         my ($width)=$params{html}=~m#width="(\d+)"#;
26
27         return "<div class=\"googlecalendar\" src=\"$url\" height=\"$height\" width=\"$width\"></div>";
28 } # }}}
29
30 sub format (@) { #{{{
31         my %params=@_;
32
33         $params{content}=~s/<div class=\"googlecalendar" src="([^"]+)" height="([^"]+)" width="([^"]+)"><\/div>/gencal($1,$2,$3)/eg;
34
35         return $params{content};
36 } # }}}
37
38 sub gencal ($$$) { #{{{
39         my $url=shift;
40         my $height=shift;
41         my $width=shift;
42         return qq{<iframe src="http://www.google.com/calendar/embed?$url" style=" border-width:0 " width="$width" frameborder="0" height="$height"></iframe>};
43 } #}}}
44
45 1