add news item for ikiwiki 2.7
[ikiwiki] / IkiWiki / Plugin / embed.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::embed;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 my $attribr=qr/[^<>"]+/;
9
10 # regexp matching known-safe html
11 my $safehtml=qr{(
12         # google maps
13         <\s*iframe\s+width="\d+"\s+height="\d+"\s+frameborder="$attribr"\s+
14         scrolling="$attribr"\s+marginheight="\d+"\s+marginwidth="\d+"\s+
15         src="http://maps.google.com/\?$attribr"\s*>\s*</iframe>
16
17         |
18
19         # youtube
20         <\s*object\s+width="\d+"\s+height="\d+"\s*>\s*
21         <\s*param\s+name="movie"\s+value="http://www.youtube.com/v/$attribr"\s*>\s*
22         </param>\s*
23         <\s*param\s+name="wmode"\s+value="transparent"\s*>\s*</param>\s*
24         <embed\s+src="http://www.youtube.com/v/$attribr"\s+
25         type="application/x-shockwave-flash"\s+wmode="transparent"\s+
26         width="\d+"\s+height="\d+"\s*>\s*</embed>\s*</object>
27
28         |
29
30         # google video
31         <\s*embed\s+style="\s*width:\d+px;\s+height:\d+px;\s*"\s+id="$attribr"\s+
32         type="application/x-shockwave-flash"\s+
33         src="http://video.google.com/googleplayer.swf\?$attribr"\s+
34         flashvars=""\s*>\s*</embed>
35
36         |
37
38         # google calendar
39         <\s*iframe\s+src="http://www.google.com/calendar/embed\?src=$attribr"\s+
40         style="\s*border-width:\d+\s*"\s+width="\d+"\s+frameborder="\d+"\s*
41         height="\d+"\s*>\s*</iframe>
42 )}sx;
43
44 my @embedded;
45
46 sub import { #{{{
47         hook(type => "filter", id => "embed", call => \&filter);
48 } # }}}
49
50 sub embed ($) { #{{{
51         hook(type => "format", id => "embed", call => \&format) unless @embedded;
52         push @embedded, shift;
53         return "<div class=\"embed$#embedded\"></div>";
54 } #}}}
55
56 sub filter (@) { #{{{
57         my %params=@_;
58         $params{content} =~ s/$safehtml/embed($1)/eg;
59         return $params{content};
60 } # }}}
61
62 sub format (@) { #{{{
63         my %params=@_;
64         $params{content} =~ s/<div class="embed(\d+)"><\/div>/$embedded[$1]/eg;
65         return $params{content};
66 } # }}}
67
68 1