Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Formatter/YouTube.pm: Allow to embed youtube video player
- Loading branch information
Showing
2 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); |