Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Formatter/YouTube.pm: Allow to embed youtube video player

  • Loading branch information...
commit fc5d3d99ad1ba653acd5ca656c712ec2e3276167 1 parent 67ee674
@LiNiO LiNiO authored
Showing with 174 additions and 0 deletions.
  1. +95 −0 lib/MojoMojo/Formatter/YouTube.pm
  2. +79 −0 t/formatter_youtube.t
View
95 lib/MojoMojo/Formatter/YouTube.pm
@@ -0,0 +1,95 @@
+package MojoMojo::Formatter::YouTube;
+
+use base qw/MojoMojo::Formatter/;
+use URI::Fetch;
+
+=head1 NAME
+
+MojoMojo::Formatter::YouTube - Embed YouTube player
+
+=head1 DESCRIPTION
+
+Embed Youtube video player for given video by writing =youtube <url>.
+
+=head1 METHODS
+
+=over 4
+
+=item format_content_order
+
+Format order can be 1-99. The YouTube formatter runs on 6
+
+=cut
+
+sub format_content_order { 6 }
+
+=item format_content
+
+calls the formatter. Takes a ref to the content as well as the
+context object.
+
+=cut
+
+sub format_content {
+ my ( $class, $content, $c ) = @_;
+
+ my @lines = split /\n/, $$content;
+ $$content = "";
+ my $re=$class->gen_re(qr/youtube\s+(.*?)/);
+ my $lang=$c->session->{lang} || $c->pref('default_lang') || 'en';
+
+ foreach my $line (@lines) {
+ if ( $line =~ m/$re/){
+ $line=$class->process($c,$line,$re,$lang);
+ }
+ $$content .= $line . "\n";
+ }
+
+}
+
+
+sub process {
+ my ( $class, $c, $line, $re, $lang) = @_;
+
+ my $youtube=$c->loc('YouTube Video');
+ my $video_id;
+ $line =~ m/$re/;
+ $url = URI->new($1);
+
+ unless ($url){
+ $line =~ s/$re/"$youtube: $url ".$c->loc('is not a valid url')/e;
+ return $line;
+ }
+
+ if ($url =~ m!youtube.com/.*?v=([A-Za-z0-9_]+)!){
+ $video_id=$1;
+ } else {
+ $line =~ s/$re/"$youtube: $url ".$c->loc('is not a valid link to youtube video')/e;
+ return $line;
+ }
+
+ if ( ($c->action->reverse eq 'pageadmin/edit') || ($c->action->reverse eq 'jsrpc/render') ){
+ $line =~ s!$re!<div style='width: 425px;height: 344px; border: 1px black dotted;'>$youtube<br /><a href="$url">$url</a></div>!;
+ } else {
+ $line =~ s!$re!<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/$video_id&hl=$lang"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$video_id&hl=$lang" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>!;
+ }
+ return $line;
+}
+
+=back
+
+=head1 SEE ALSO
+
+L<MojoMojo>,L<Module::Pluggable::Ordered>,L<URI::Fetch>
+
+=head1 AUTHORS
+
+Robert 'LiNiO' Litwiniec <linio@wonder.pl>
+
+=head1 License
+
+This module is licensed under the same terms as Perl itself.
+
+=cut
+
+1;
View
79 t/formatter_youtube.t
@@ -0,0 +1,79 @@
+#!/usr/bin/perl -w
+package Dummy;
+sub new {
+ my $class = shift;
+ bless {}, $class;
+}
+
+sub loc {
+ return $_[1];
+}
+
+sub action {
+ return $_[0];
+}
+
+sub reverse {
+ return $reverse;
+}
+
+sub set_reverse {
+ $reverse=$_[1];
+}
+
+sub cache {
+ my ($self,$c)=@_;
+ return undef;
+}
+
+sub session {
+ my ($self,$c)=@_;
+ return "";
+}
+
+sub pref {
+ my ($self,$c)=@_;
+ return "";
+}
+
+
+
+package main;
+
+use MojoMojo::Formatter::YouTube;
+use Test::More;
+
+plan tests => 5;
+
+my ($content,$exist,$new);
+
+my $fake_c=Dummy->new;
+
+$content = " youtube http://www.youtube.com/abc";
+MojoMojo::Formatter::YouTube->format_content(\$content, $fake_c, undef);
+#warn("Content is $content");
+is($content, " youtube http://www.youtube.com/abc\n","no youtube formatter line");
+
+$fake_c->set_reverse('pageadmin/edit');
+$content = "{{youtube http://www.youtube.com/v=abcABC0}}\n";
+MojoMojo::Formatter::YouTube->format_content(\$content, $fake_c, undef);
+#warn("Content is $content");
+is($content, qq(<div style='width: 425px;height: 344px; border: 1px black dotted;'>YouTube Video<br /><a href="http://www.youtube.com/v=abcABC0">http://www.youtube.com/v=abcABC0</a></div>\n));
+
+$fake_c->set_reverse('jsrpc/render');
+$content = "{{youtube http://www.youtube.com/v=abcABC0}} xx\n";
+MojoMojo::Formatter::YouTube->format_content(\$content, $fake_c, undef);
+#warn("Content is $content");
+is($content, qq(<div style='width: 425px;height: 344px; border: 1px black dotted;'>YouTube Video<br /><a href="http://www.youtube.com/v=abcABC0">http://www.youtube.com/v=abcABC0</a></div> xx\n));
+
+$content = "{{youtube http://wwwwwwww.youtube.com/abc}}";
+MojoMojo::Formatter::YouTube->format_content(\$content, $fake_c, undef);
+#warn("Content is $content");
+is($content, "YouTube Video: http://wwwwwwww.youtube.com/abc is not a valid link to youtube video\n","no youtube link");
+
+$fake_c->set_reverse('');
+
+$content = "{{youtube http://www.youtube.com/watch?v=ABC_abc_09}}";
+MojoMojo::Formatter::YouTube->format_content(\$content, $fake_c, undef);
+#warn("Content is $content");
+is($content, qq(<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/ABC_abc_09&hl=en"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ABC_abc_09&hl=en" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>\n));
Please sign in to comment.
Something went wrong with that request. Please try again.