public
Last active

MojoMojo::Formatter::NicoVideo

  • Download Gist
NicoVideo.pm
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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;
formatter-nicovideo.t
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#!/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",
);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.