From fc5d3d99ad1ba653acd5ca656c712ec2e3276167 Mon Sep 17 00:00:00 2001 From: Robert Litwiniec Date: Sun, 19 Apr 2009 16:27:34 +0200 Subject: [PATCH] Formatter/YouTube.pm: Allow to embed youtube video player --- lib/MojoMojo/Formatter/YouTube.pm | 95 +++++++++++++++++++++++++++++++ t/formatter_youtube.t | 79 +++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 lib/MojoMojo/Formatter/YouTube.pm create mode 100644 t/formatter_youtube.t diff --git a/lib/MojoMojo/Formatter/YouTube.pm b/lib/MojoMojo/Formatter/YouTube.pm new file mode 100644 index 00000000..f9f237da --- /dev/null +++ b/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 . + +=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!
$youtube
$url
!; + } else { + $line =~ s!$re!!; + } + return $line; +} + +=back + +=head1 SEE ALSO + +L,L,L + +=head1 AUTHORS + +Robert 'LiNiO' Litwiniec + +=head1 License + +This module is licensed under the same terms as Perl itself. + +=cut + +1; diff --git a/t/formatter_youtube.t b/t/formatter_youtube.t new file mode 100644 index 00000000..9699aa72 --- /dev/null +++ b/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(\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( 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(\n));