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