summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/embed.pm
blob: a7d38358fe3ab4177c2cf6a7817f3e037f2c8e4a (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::embed;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.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 => "getsetup", id => "embed", call => \&getsetup);
  37. hook(type => "filter", id => "embed", call => \&filter);
  38. }
  39. sub getsetup () {
  40. return
  41. plugin => {
  42. safe => 1,
  43. rebuild => undef,
  44. },
  45. }
  46. sub embed ($) {
  47. hook(type => "format", id => "embed", call => \&format) unless @embedded;
  48. push @embedded, shift;
  49. return "<div class=\"embed$#embedded\"></div>";
  50. }
  51. sub filter (@) {
  52. my %params=@_;
  53. $params{content} =~ s/$safehtml/embed($1)/eg;
  54. return $params{content};
  55. }
  56. sub format (@) {
  57. my %params=@_;
  58. $params{content} =~ s/<div class="embed(\d+)"><\/div>/$embedded[$1]/eg;
  59. return $params{content};
  60. }
  61. 1