Skip to content

Instantly share code, notes, and snippets.

@bayashi
Created October 9, 2010 17:40
Show Gist options
  • Save bayashi/618402 to your computer and use it in GitHub Desktop.
Save bayashi/618402 to your computer and use it in GitHub Desktop.
MojoMojo::Formatter::NicoVideo
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 7;
use lib 't/lib';
use HTTP::Request::Common;
use FakeCatalystObject;
BEGIN {
use_ok 'Catalyst::Test', 'MojoMojo';
use_ok 'MojoMojo::Formatter::NicoVideo';
}
my $fake_c = FakeCatalystObject->new;
my $NicoVideo_CONF = $MojoMojo::Formatter::NicoVideo::CONF;
{
my $content = "see {{nico }}";
MojoMojo::Formatter::NicoVideo->format_content(\$content, $fake_c);
is(
$content,
qq|see {{nico }}\n|,
"blank (no format)",
);
}
{
my $content = "see {{nico http://www.nicovideo.jp/watch/sm4483132}}";
MojoMojo::Formatter::NicoVideo->format_content(\$content, $fake_c);
is(
$content,
qq|see <script type="text/javascript" src="http://ext.nicovideo.jp/thumb_watch/sm4483132?w=$NicoVideo_CONF->{x}&h=$NicoVideo_CONF->{y}"></script><noscript><a href="http://www.nicovideo.jp/watch/sm4483132">Faking localization... Nico Nico Doga ...fake complete. [4483132]</a></noscript>\n|,
"normal",
);
}
$fake_c->set_reverse('pageadmin/edit');
{
my $content = "see {{nico http://www.nicovideo.jp/watch/sm4483132}}";
MojoMojo::Formatter::NicoVideo->format_content(\$content, $fake_c);
is(
$content,
qq|see <div style='width: $NicoVideo_CONF->{x}px;height: $NicoVideo_CONF->{y}px; border: 1px black dotted;'>Faking localization... Nico Nico Doga ...fake complete.<br /><a href="http://www.nicovideo.jp/watch/sm4483132">http://www.nicovideo.jp/watch/sm4483132</a></div>\n|,
"edit / valid tag",
);
}
$fake_c->set_reverse('jsrpc/render');
{
my $content = "see {{nico http://www.nicovideo.jp/watch/sm4483132}}";
MojoMojo::Formatter::NicoVideo->format_content(\$content, $fake_c);
is(
$content,
qq|see <div style='width: $NicoVideo_CONF->{x}px;height: $NicoVideo_CONF->{y}px; border: 1px black dotted;'>Faking localization... Nico Nico Doga ...fake complete.<br /><a href="http://www.nicovideo.jp/watch/sm4483132">http://www.nicovideo.jp/watch/sm4483132</a></div>\n|,
"jsrpc/render / valid tag",
);
}
$fake_c->set_reverse('');
{
my $content = "see {{nico http://www.nicovideo.jp/watch/invalid4483132}}";
MojoMojo::Formatter::NicoVideo->format_content(\$content, $fake_c);
is(
$content,
qq|see Faking localization... Nico Nico Doga ...fake complete.: http://www.nicovideo.jp/watch/invalid4483132 Faking localization... is not a valid link to nico nico doga ...fake complete.\n|,
"invalid URL",
);
}
package MojoMojo::Formatter::NicoVideo;
use strict;
use warnings;
use parent qw/MojoMojo::Formatter/;
=head1 NAME
MojoMojo::Formatter::NicoVideo - Embed Nico Nico Doga player
=cut
our $CONF = MojoMojo->config->{'Formatter::NicoVideo'} || { x=>440, y=>360 };
=head1 DESCRIPTION
Embed Nico Nico Doga player for given video by writing {{nico <URL>}}.
=head1 METHODS
=head2 format_content_order
The NicoVideo formatter has no special requirements
in terms of the order it gets run in, so it has a priority of 17.
=cut
sub format_content_order { 17 }
=head2 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 ) = @_;
return unless $$content;
my @lines = split /\n/, $$content;
$$content = '';
my $re = $class->gen_re( qr/nico\s+(.+)/ );
for my $line (@lines) {
if ( $line =~ m/$re/ ) {
$line = $class->process($c, $line, $re, $1);
}
$$content .= $line . "\n";
}
}
=head2 process
Here the actual formatting is done.
=cut
sub process {
my ( $class, $c, $line, $re, $url) = @_;
my $nico = $c->loc('Nico Nico Doga');
my $video_id;
my $uri = URI->new($url);
unless ($uri){
$line =~ s/$re/"$nico: $uri ". $c->loc('is not a valid url')/e;
return $line;
}
if ( $uri =~ m!www.nicovideo.jp/watch/sm([0-9]+)! ){
$video_id = $1;
} else {
$line =~ s/$re/"$nico: $uri ". $c->loc('is not a valid link to nico nico doga')/e;
return $line;
}
my $ar = $c->action->reverse;
if ( $ar && ($ar eq 'pageadmin/edit' || $ar eq 'jsrpc/render') ){
$line =~ s!$re!<div style='width: $CONF->{x}px;height: $CONF->{y}px; border: 1px black dotted;'>$nico<br /><a href="$uri">$uri</a></div>!;
$c->stash->{precompile_off} = 1;
} else {
$line =~ s!$re!<script type="text/javascript" src="http://ext.nicovideo.jp/thumb_watch/sm$video_id?w=$CONF->{x}&h=$CONF->{y}"></script><noscript><a href="$uri">$nico [$video_id]</a></noscript>!;
}
return $line;
}
=head1 SEE ALSO
L<MojoMojo> and L<Module::Pluggable::Ordered>.
=head1 AUTHORS
Dai Okabayashi, L<bayashi at cpan . org>
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment