<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package IkiWiki::Plugin::embed;

use warnings;
use strict;
use IkiWiki 3.00;

my $attribr=qr/[^&lt;&gt;"]+/;

# regexp matching known-safe html
my $safehtml=qr{(
	# google maps
	&lt;\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*&gt;\s*&lt;/iframe&gt;

	|

	# youtube
	&lt;\s*object\s+width="\d+"\s+height="\d+"\s*&gt;\s*
	&lt;\s*param\s+name="movie"\s+value="http://www.youtube.com/v/$attribr"\s*&gt;\s*
	&lt;/param&gt;\s*
	&lt;\s*param\s+name="wmode"\s+value="transparent"\s*&gt;\s*&lt;/param&gt;\s*
	&lt;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*&gt;\s*&lt;/embed&gt;\s*&lt;/object&gt;

	|

	# google video
	&lt;\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*&gt;\s*&lt;/embed&gt;

	|

	# google calendar
	&lt;\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*&gt;\s*&lt;/iframe&gt;
)}sx;

my @embedded;

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

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; undef,
		},
}

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

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

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

1
</pre></body></html>