File Coverage

blib/lib/MojoMojo/Formatter/Gist.pm
Criterion Covered Total %
statement 29 32 90.6
branch 6 8 75.0
condition 5 9 55.5
subroutine 6 6 100.0
pod 3 3 100.0
total 49 58 84.4


line stmt bran cond sub pod time code
1             package MojoMojo::Formatter::Gist;
2 27     27   16754 use strict;
  27         82  
  27         728  
3 27     27   139 use warnings;
  27         67  
  27         1041  
4 27     27   136 use parent qw/MojoMojo::Formatter/;
  27         60  
  27         146  
5              
6             =head1 NAME
7              
8             MojoMojo::Formatter::Gist - Embed Gist script
9              
10             =head1 DESCRIPTION
11              
12             Embed Gist script by writing {{gist <id>}}.
13              
14             if you write:
15              
16             {{gist 618402}}
17              
18             it will be formatted, like this
19              
20             <script src="https://gist.github.com/618402.js"></script>
21              
22             then you can see the syntax highlighted source code.
23              
24             =head1 METHODS
25              
26             =head2 format_content_order
27              
28             The Gist formatter has no special requirements
29             in terms of the order it gets run in, so it has a priority of 17.
30              
31             =cut
32              
33 744     744 1 2549 sub format_content_order { 17 }
34              
35             =head2 format_content
36              
37             Calls the formatter. Takes a ref to the content as well as the context object.
38              
39             =cut
40              
41             sub format_content {
42 129     129 1 4225 my ( $class, $content, $c ) = @_;
43              
44 129 50       482 return unless $$content;
45              
46 129         735 my @lines = split /\n/, $$content;
47 129         346 $$content = '';
48              
49 129         830 my $re = $class->gen_re( qr/gist\s+(\d+)/ );
50              
51 129         502 for my $line (@lines) {
52 735 100       2489 if ( $line =~ m/$re/ ) {
53 3         13 $line = $class->process($c, $line, $re, $1);
54             }
55 735         1740 $$content .= $line . "\n";
56             }
57              
58             }
59              
60             =head2 process
61              
62             Here the actual formatting is done.
63              
64             =cut
65             sub process {
66 3     3 1 14 my ( $class, $c, $line, $re, $id) = @_;
67              
68 3         13 my $gist = $c->loc('Gist Script');
69              
70 3 50 33     39 if (!$id || $id !~ /^\d+$/){
71 0         0 $line =~ s/$re/"$gist: $id ". $c->loc('is not a valid id')/e;
  0         0  
72 0         0 return $line;
73             }
74              
75 3         11 my $url = "https://gist.github.com/$id";
76              
77 3         11 my $ar = $c->action->reverse;
78 3 100 66     35 if ( $ar && ($ar eq 'pageadmin/edit' || $ar eq 'jsrpc/render') ){
      66        
79 2         25 $line =~ s!$re!<div style='width: 95%;height: 90px; border: 1px black dotted;'>$gist - <a href="$url">gist:$id</a></div>!;
80 2         10 $c->stash->{precompile_off} = 1;
81             } else {
82 1         10 $line =~ s!$re!<script src="$url.js"></script>!;
83             }
84              
85 3         24 return $line;
86             }
87              
88              
89             =head1 SEE ALSO
90              
91             L<MojoMojo> and L<Module::Pluggable::Ordered>.
92             Gist is <https://gist.github.com/>.
93              
94             =head1 AUTHORS
95              
96             Dai Okabayashi, L<bayashi at cpan . org>
97              
98             =head1 LICENSE
99              
100             This library is free software. You can redistribute it and/or modify
101             it under the same terms as Perl itself.
102              
103             =cut
104              
105             1;