blib/lib/Mojolicious/Plugin/PPI.pm

filter: Statementstmt 99.0% 107 / 108 Branchbran 88.6% 39 / 44 Conditioncond 57.1% 4 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 95.0% 19 / 20 Podpod 14.2% 1 / 7 totaltotal 89.4% 170 / 190 CC 29 SCAR 17.2
CC
29
Cov
94%
process_converter_opts
12.2
convert
6.0
check_file
5.1
CRAP 5.6
↑ Summary
1 package Mojolicious::Plugin::PPI;
2  
3 5 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Mojo::ByteStream 'b';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use PPI::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our $VERSION = '0.07';
14 5 $VERSION = eval $VERSION;
15  
16 5 has 'line_numbers' => 1;
17 5 has 'no_check_file' => 0;
18 3 ▶has 'ppi_html_on' => sub { PPI::HTML->new( line_numbers => 1 ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
19 2 ▶has 'ppi_html_off' => sub { PPI::HTML->new( line_numbers => 0 ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
20 5 has 'src_folder';
21  
22 5 has style => <<'END';
23 .ppi-code {
24 display: inline-block;
25 min-width: 400px;
26 background-color: #F8F8F8;
27 border-radius: 10px;
28 padding: 15px;
29 }
30 END
31  
32 0 ▶has class_style => sub {{
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 line_number_on => { display => 'inline' },
34 line_number_off => { display => 'none' },
35  
36 cast => '#339999',
37 comment => '#008080',
38 core => '#FF0000',
39 double => '#999999',
40 heredoc_content => '#FF0000',
41 interpolate => '#999999',
42 keyword => '#BD2E2A',
43 line_number => '#666666',
44 literal => '#999999',
45 magic => '#0099FF',
46 match => '#9900FF',
47 number => '#990000',
48 operator => '#DD7700',
49 pod => '#008080',
50 pragma => '#A33AF7',
51 regex => '#9900FF',
52 single => '#FF33FF',
53 substitute => '#9900FF',
54 symbol => '#389A7D',
55 transliterate => '#9900FF',
56 word => '#999999',
57 5 }};
58  
59 5 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
60 5 my ($plugin, $app) = (shift, shift);
61 5 $plugin->initialize($app, @_);
62  
63 5 push @{$app->static->classes}, __PACKAGE__;
64 5 push @{$app->renderer->classes}, __PACKAGE__;
65  
66 8 ▶ $app->helper( ppi => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
67 8 ▶ return $plugin if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 3 5 8
68 5 return $plugin->convert(@_);
69 5 });
70 5 ▶ $app->helper( ppi_css => sub { $_[0]->ppi->generate_css(@_) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
71 }
72  
73 5 ▶sub initialize {
Subroutine initialize: called CC=5 SCAR=16.1
Pod undocumented
74 5 my ($plugin, $app) = (shift, shift);
75 5 ▶ my %opts = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 5 0 5
76 5 my @unknown;
77 5 foreach my $key (keys %opts) {
78 2 my $code = $plugin->can($key);
79 2 ▶ unless ($code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 1 1 2
80 1 push @unknown, $key;
81 1 next;
82 }
83 1 $plugin->$code($opts{$key});
84 }
85  
86 5 ▶ if ( @unknown ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown) 1 4 5
87 1 warn "Unknown option(s): " . join(", ", @unknown) . "\n";
88 }
89 }
90  
91 5 ▶sub convert {
Subroutine convert: called CC=6 SCAR=17.9
Pod undocumented
92 5 my $plugin = shift;
93 5 my $c = shift;
94  
95 5 my %opts = $plugin->process_converter_opts(@_);
96  
97 my $converter =
98 $opts{line_numbers}
99 5 ▶ ? $plugin->ppi_html_on
Branch: 1 branch
BranchTrueFalseTotal
$opts{'line_numbers'} ? : 3 2 5
100 : $plugin->ppi_html_off;
101  
102 5 my $id = $plugin->generate_id($c);
103  
104 my @tag = (
105 $opts{inline} ? 'code' : 'pre',
106 id => $id,
107 5 ▶ class => 'ppi-code ' . ($opts{inline} ? 'ppi-inline' : 'ppi-block'),
Branch: 2 branches
BranchTrueFalseTotal
$opts{'inline'} ? : 2 3 5
$opts{'inline'} ? : 2 3 5
108 );
109  
110 5 ▶ if ($opts{line_numbers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"line_numbers"}) 3 2 5
111 3 push @tag, ondblclick => "ppi_toggleLineNumbers('$id')";
112 3 $c->stash('ppi.js.required' => 1);
113 }
114  
115 my %render_opts = (
116 5 ▶ 'ppi.code' => $converter->html( $opts{file} ? $opts{file} : \$opts{string} ),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'file'} ? : 2 3 5
117 'ppi.tag' => \@tag,
118 );
119  
120 #TODO use render_to_string once Mojo 5.00 is required
121 5 return $c->include('ppi_template', %render_opts);
122 }
123  
124 5 ▶sub generate_id {
Subroutine generate_id: called CC=1 SCAR=0.0
Pod undocumented
125 5 my ($plugin, $c) = @_;
126 5 return 'ppi' . $c->stash->{'ppi.id'}++;
127 }
128  
129 3 ▶sub check_file {
Subroutine check_file: called CC=5 SCAR=16.2
Pod undocumented
130 3 my ($self, $file) = @_;
131 3 ▶ return undef if $self->no_check_file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->no_check_file 0 3 3
132  
133 3 ▶ if ( my $folder = $self->src_folder ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $folder = $self->src_folder) 1 2 3
134 1 ▶ die "Could not find folder $folder\n" unless -d $folder;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $folder 0 1 1
135 1 $file = File::Spec->catfile( $folder, $file );
136 }
137  
138 3 ▶ return -e $file ? $file : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e $file ? : 2 1 3
139 }
140  
141 5 ▶sub process_converter_opts {
Subroutine process_converter_opts: called CC=12 SCAR=25.0
Pod undocumented
142 5 my $plugin = shift;
143  
144 5 my $string = do {
145 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
146 5 ▶ if (ref $_[-1] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] eq "CODE") 2 3 5
147 2 Mojo::Util::trim pop->();
148 }
149 };
150  
151 5 my %opts;
152 5 ▶ if (ref $_[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1]) 1 4 5
153 1 %opts = %{ pop() };
154 }
155  
156 5 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 3 2 5
157 3 ▶ die "Cannot specify both a string and a block\n" if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 0 3 3
158  
159 3 $string = shift;
160 3 $opts{file} = $plugin->check_file($string);
161 3 ▶ unless ( $opts{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"file"}) 1 2 3
162 1 ▶ $opts{inline} //= 1;
Condition50%1/2
$opts{"inline"} //= 1
l!l
01
MC/DC0%0/1
$opts{"inline"} //= 1
$opts{"inline"}
Truth table50%1/2
A
A= $opts{"inline"}
Aresult
11
00
163 }
164  
165 }
166  
167 5 ▶ %opts = (%opts, @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 5 5
168  
169 5 ▶ $opts{string} = $string unless defined $opts{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"file"} 3 2 5
170  
171 5 ▶ $opts{line_numbers} //= 0 if $opts{inline};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"inline"} 2 3 5
Condition50%1/2
$opts{"line_numbers"} //= 0
l!l
02
MC/DC0%0/1
$opts{"line_numbers"} //= 0
$opts{"line_numbers"}
Truth table50%1/2
A
A= $opts{"line_numbers"}
Aresult
11
00
172 5 ▶ $opts{line_numbers} //= $plugin->line_numbers;
Condition66%2/3
$opts{"line_numbers"} //= $plugin->line_numbers
l!l&&r!l&&!r
230
MC/DC0%0/2
$opts{"line_numbers"} //= $plugin->line_numbers
$opts{"line_numbers"} $plugin->line_numbers
Truth table66%2/3
A //= B
A= $opts{"line_numbers"}B= $plugin->line_numbers
ABresult
1X1
011
000
173  
174 5 return %opts;
175 }
176  
177 1 ▶sub generate_css {
Subroutine generate_css: called CC=4 SCAR=13.9
Pod undocumented
178 1 my ($plugin, $c) = @_;
179 1 my $sheet = b("pre.ppi-code br { display: none; }\n");
180 1 $$sheet .= $plugin->style."\n";
181 1 my $cs = $plugin->class_style;
182 1 foreach my $key (sort keys %$cs) {
183 2 my $value = $cs->{$key};
184 2 ▶ $value = { color => $value } unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 1 1 2
185 2 $$sheet .= ".ppi-code .$key { ";
186 2 foreach my $prop ( sort keys %$value ) {
187 3 $$sheet .= "$prop: $value->{$prop}; ";
188 }
189 2 $$sheet .= "}\n";
190 }
191 1 ▶ return $c->stylesheet(sub{$sheet});
Subroutine __ANON__: called CC=1 SCAR=0.0
192 }
193  
194 5 1;
195  
196 __DATA__
↑ Summary