Skip to content

Commit

Permalink
Formatter/YouTube.pm: Allow to embed youtube video player
Browse files Browse the repository at this point in the history
  • Loading branch information
LiNiO committed Apr 19, 2009
1 parent 67ee674 commit fc5d3d9
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
95 changes: 95 additions & 0 deletions 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;
79 changes: 79 additions & 0 deletions 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));

0 comments on commit fc5d3d9

Please sign in to comment.