aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/embed.pm
blob: a7d38358fe3ab4177c2cf6a7817f3e037f2c8e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
package IkiWiki::Plugin::embed;

use warnings;
use strict;
use IkiWiki 3.00;

my $attribr=qr/[^<>"]+/;

# regexp matching known-safe html
my $safehtml=qr{(
	# google maps
	<\s*iframe\s+width="\d+"\s+height="\d+"\s+frameborder="$attribr"\s+
	scrolling="$attribr"\s+marginheight="\d+"\s+marginwidth="\d+"\s+
	src="http://maps.google.com/\?$attribr"\s*>\s*</iframe>

	|

	# youtube
	<\s*object\s+width="\d+"\s+height="\d+"\s*>\s*
	<\s*param\s+name="movie"\s+value="http://www.youtube.com/v/$attribr"\s*>\s*
	</param>\s*
	<\s*param\s+name="wmode"\s+value="transparent"\s*>\s*</param>\s*
	<embed\s+src="http://www.youtube.com/v/$attribr"\s+
	type="application/x-shockwave-flash"\s+wmode="transparent"\s+
	width="\d+"\s+height="\d+"\s*>\s*</embed>\s*</object>

	|

	# google video
	<\s*embed\s+style="\s*width:\d+px;\s+height:\d+px;\s*"\s+id="$attribr"\s+
	type="application/x-shockwave-flash"\s+
	src="http://video.google.com/googleplayer.swf\?$attribr"\s+
	flashvars=""\s*>\s*</embed>

	|

	# google calendar
	<\s*iframe\s+src="http://www.google.com/calendar/embed\?src=$attribr"\s+
	style="\s*border-width:\d+\s*"\s+width="\d+"\s+frameborder="\d+"\s*
	height="\d+"\s*>\s*</iframe>
)}sx;

my @embedded;

sub import {
	hook(type => "getsetup", id => "embed", call => \&getsetup);
	hook(type => "filter", id => "embed", call => \&filter);
}

sub getsetup () {
	return
		plugin => {
			safe => 1,
			rebuild => undef,
		},
}

sub embed ($) {
	hook(type => "format", id => "embed", call => \&format) unless @embedded;
	push @embedded, shift;
	return "<div class=\"embed$#embedded\"></div>";
}

sub filter (@) {
	my %params=@_;
	$params{content} =~ s/$safehtml/embed($1)/eg;
	return $params{content};
}

sub format (@) {
        my %params=@_;
	$params{content} =~ s/<div class="embed(\d+)"><\/div>/$embedded[$1]/eg;
        return $params{content};
}

1