Skip to content
Browse files

added Gist Formatter

  • Loading branch information...
1 parent f6cbb18 commit d2bb2dbaa25ac04fb182294d6ec5b5e9057e3308 @bayashi bayashi committed Feb 12, 2011
Showing with 173 additions and 0 deletions.
  1. +105 −0 lib/MojoMojo/Formatter/Gist.pm
  2. +68 −0 t/formatter_gist.t
View
105 lib/MojoMojo/Formatter/Gist.pm
@@ -0,0 +1,105 @@
+package MojoMojo::Formatter::Gist;
+use strict;
+use warnings;
+use parent qw/MojoMojo::Formatter/;
+
+=head1 NAME
+
+MojoMojo::Formatter::Gist - Embed Gist script
+
+=head1 DESCRIPTION
+
+Embed Gist script by writing {{gist <id>}}.
+
+if you write:
+
+ {{gist 618402}}
+
+it will be formatted, like this
+
+ <script src="https://gist.github.com/618402.js"></script>
+
+then you can see the syntax highlighted source code.
+
+=head1 METHODS
+
+=head2 format_content_order
+
+The Gist 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/gist\s+(\d+)/ );
+
+ 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, $id) = @_;
+
+ my $gist = $c->loc('Gist Script');
+
+ if (!$id || $id !~ /^\d+$/){
+ $line =~ s/$re/"$gist: $id ". $c->loc('is not a valid id')/e;
+ return $line;
+ }
+
+ my $url = "https://gist.github.com/$id";
+
+ my $ar = $c->action->reverse;
+ if ( $ar && ($ar eq 'pageadmin/edit' || $ar eq 'jsrpc/render') ){
+ $line =~ s!$re!<div style='width: 95%;height: 90px; border: 1px black dotted;'>$gist - <a href="$url">gist:$id</a></div>!;
+ $c->stash->{precompile_off} = 1;
+ } else {
+ $line =~ s!$re!<script src="$url.js"></script>!;
+ }
+
+ return $line;
+}
+
+
+=head1 SEE ALSO
+
+L<MojoMojo> and L<Module::Pluggable::Ordered>.
+Gist is <https://gist.github.com/>.
+
+=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;
View
68 t/formatter_gist.t
@@ -0,0 +1,68 @@
+#!/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::Gist';
+}
+
+my $fake_c = FakeCatalystObject->new;
+
+{
+ my $content = "see {{gist }}";
+ MojoMojo::Formatter::Gist->format_content(\$content, $fake_c);
+ is(
+ $content,
+ qq|see {{gist }}\n|,
+ "blank (no format)",
+ );
+}
+
+{
+ my $content = "see {{gist 618402}}";
+ MojoMojo::Formatter::Gist->format_content(\$content, $fake_c);
+ is(
+ $content,
+ qq|see <script src="https://gist.github.com/618402.js"></script>\n|,
+ "normal",
+ );
+}
+
+$fake_c->set_reverse('pageadmin/edit');
+{
+ my $content = "see {{gist 618402}}";
+ MojoMojo::Formatter::Gist->format_content(\$content, $fake_c);
+ is(
+ $content,
+ qq|see <div style='width: 95%;height: 90px; border: 1px black dotted;'>Faking localization... Gist Script ...fake complete. - <a href="https://gist.github.com/618402">gist:618402</a></div>\n|,
+ "edit / valid tag",
+ );
+}
+
+$fake_c->set_reverse('jsrpc/render');
+{
+ my $content = "see {{gist 618402}}";
+ MojoMojo::Formatter::Gist->format_content(\$content, $fake_c);
+ is(
+ $content,
+ qq|see <div style='width: 95%;height: 90px; border: 1px black dotted;'>Faking localization... Gist Script ...fake complete. - <a href="https://gist.github.com/618402">gist:618402</a></div>\n|,
+ "jsrpc/render / valid tag",
+ );
+}
+
+$fake_c->set_reverse('');
+{
+ my $content = "see {{gist 123invalid123}}";
+ MojoMojo::Formatter::Gist->format_content(\$content, $fake_c);
+ is(
+ $content,
+ qq|see {{gist 123invalid123}}\n|,
+ "invalid ID",
+ );
+}
+

0 comments on commit d2bb2db

Please sign in to comment.
Something went wrong with that request. Please try again.