blib/lib/Mojolicious/Plugin/PetalTinyRenderer.pm

filter: Statementstmt 80.2% 77 / 96 Branchbran 58.3% 21 / 36 Conditioncond 66.6% 6 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 86.6% 13 / 15 Podpod 100% 1 / 1 totaltotal 74.6% 121 / 162 CC 23 SCAR 18.4
CC
23
Cov
74%
__ANON__
20.0
_petal
8.6
__ANON__
6.0
CRAP 6.3
↑ Summary
1 package Mojolicious::Plugin::PetalTinyRenderer;
2 2 $Mojolicious::Plugin::PetalTinyRenderer::VERSION = '0.06';
3 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 my $tal_ns = q{xmlns:tal="http://purl.org/petal/1.0/"};
7  
8 2 __PACKAGE__->attr('config');
9  
10 2 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
Pod documented
11 2 my ($self, $app, $conf) = @_;
12 2 $self->config($conf);
13  
14 2 ▶ $app->renderer->add_handler($conf->{name} || 'tal' => sub { $self->_petal(@_) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%2/2
$conf->{'name'} || 'tal'
l!l
11
MC/DC100%1/1
$conf->{'name'} || 'tal'
$conf->{'name'}
Truth table100%2/2
A
A= $conf->{'name'}
Aresult
11
00
15 }
16  
17 8 ▶sub _petal {
Subroutine _petal: called CC=8 SCAR=21.6
18 8 my ($self, $renderer, $c, $output, $options) = @_;
19  
20 8 my $inline = $options->{inline};
21 8 ▶ my $name = defined $inline ? "inline" : $renderer->template_name($options);
Branch: 1 branch
BranchTrueFalseTotal
defined $inline ? : 1 7 8
22 8 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 8 8
23  
24 8 $$output = '';
25  
26 8 my $log = $c->app->log;
27  
28 8 ▶ if (defined $inline) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inline) { } 1 7 8
29 1 $log->debug(qq{Rendering inline template "$name".});
30 1 $$output = $self->_render_xml($inline, $c, $name);
31 }
32 else {
33 7 ▶ if (defined(my $path = $renderer->template_path($options))) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $path = $renderer->template_path($options))) { } 1 6 7
elsif (my $d = $renderer->get_data_template($options)) { } 6 0 6
34 1 $log->debug(qq{Rendering template "$name".});
35  
36 1 ▶ my $encoding = $self->config->{encoding} // ":encoding(UTF-8)";
Condition50%1/2
$self->config->{"encoding"} // ":encoding(UTF-8)"
l!l
01
MC/DC0%0/1
$self->config->{"encoding"} // ":encoding(UTF-8)"
$self->config->{"encoding"}
Truth table50%1/2
A
A= $self->config->{"encoding"}
Aresult
11
00
37  
38 1 ▶ if (open my $file, "<$encoding", $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $file, "<$encoding", $path) { } 1 0 1
39 1 my $xml = join "", <$file>;
40 1 $$output = $self->_render_xml($xml, $c, $name);
41 1 close $file;
42 }
43 else {
44 0 $log->debug(qq{Template "$name" ($path) not readable.});
45 0 return undef;
46 }
47 }
48 elsif (my $d = $renderer->get_data_template($options)) {
49 6 $log->debug(qq{Rendering template "$name" from DATA section.});
50 6 $$output = $self->_render_xml($d, $c, $name);
51 }
52 else {
53 0 $log->debug(qq{Template "$name" not found.});
54 0 return undef;
55 }
56 }
57  
58 8 return 1;
59 }
60  
61 8 ▶sub _render_xml {
Subroutine _render_xml: called CC=3 SCAR=13.5
62 8 my ($self, $xml, $c, $name) = @_;
63  
64 8 my $deldiv = 0;
65 8 ▶ if ($xml !~ /\bxmlns:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xml =~ /\bxmlns:/u) 7 1 8
66 7 $xml = "<div $tal_ns>$xml</div>";
67 7 $deldiv = 1;
68 }
69  
70 8 my $template = Petal::Tiny::_Mojo->new($xml);
71  
72 8 my $helper = Mojolicious::Plugin::PetalTinyRenderer::Helper->new(ctx => $c);
73  
74 8 my $html;
75 8 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 8 $html = $template->process(%{$c->stash}, c => $c, h => $helper);
77 }
78 0 ▶ catch {
Subroutine __ANON__: not called CC=4 SCAR=30.0
79 0 my $validator;
80 0 eval "use XML::Validate; \$validator = XML::Validate->new(Type => 'LibXML');";
81 0 ▶ if ($validator) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator) { } 0 0 0
82 0 $xml =~ s/<!DOCTYPE.*?>//;
83 0 ▶ if ($validator->validate($xml)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator->validate($xml)) { } 0 0 0
84 0 die "Petal::Tiny blew up handling '$name', and XML::Validate reports the XML is fine.\n\n$_";
85 }
86 else {
87 0 my $e = $validator->last_error;
88 0 ▶ my $message = $e->{message} // "";
Condition0%0/2
$e->{"message"} // ""
l!l
00
MC/DC0%0/1
$e->{"message"} // ""
$e->{"message"}
Truth table0%0/2
A
A= $e->{"message"}
Aresult
11
00
89 0 die "Petal::Tiny blew up handling '$name', and XML::Validate reports:\n\n$message";
90 }
91 }
92 else {
93 0 die "Petal::Tiny blew up handling '$name'. Install XML::Validate and XML::LibXML for better diagnostics.\n\n$_";
94 }
95 8 };
96  
97 8 ▶ if ($deldiv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deldiv) 7 1 8
98 7 $html =~ s,\A<div>,,;
99 7 $html =~ s,</div>\z,,;
100 }
101  
102 8 return $html;
103 }
104  
105 2 1;
106  
107 package
108 Petal::Tiny::_Mojo;
109  
110 2 ▶use Mojo::Base 'Petal::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
111 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 26 ▶sub reftype {
Subroutine reftype: called CC=3 SCAR=11.0
114 26 my ($self, $obj) = @_;
115 26 ▶ return 'ARRAY' if blessed $obj and $obj->isa('Mojo::Collection');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($obj) and $obj->isa("Mojo::Collection") 1 25 26
Condition100%3/3
builtin::blessed($obj) and $obj->isa("Mojo::Collection")
!ll&&!rl&&r
2141
MC/DC100%2/2
builtin::blessed($obj) and $obj->isa("Mojo::Collection")
builtin::blessed($obj) $obj->isa("Mojo::Collection")
Truth table100%3/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Mojo::Collection")
ABresult
0X0
100
111
116 25 return $self->SUPER::reftype($obj);
117 }
118  
119 2 1;
120  
121 package
122 Mojolicious::Plugin::PetalTinyRenderer::Helper;
123  
124 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 2 our $AUTOLOAD;
127  
128 2 __PACKAGE__->attr('ctx');
129  
130 # stolen from Mojolicious::Plugin::TtRenderer::Helper
131 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=16.8
132 1 my $self = shift;
133  
134 1 my $method = $AUTOLOAD;
135  
136 1 ▶ return if $method =~ /^[A-Z]+?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /^[A-Z]+?$/u 0 1 1
137 1 ▶ return if $method =~ /^_/;
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /^_/u 0 1 1
138 1 ▶ return if $method =~ /(?:\:*?)DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /(?:\:*?)DESTROY$/u 0 1 1
139  
140 1 $method = (split '::' => $method)[-1];
141  
142 1 ▶ die qq/Unknown helper: $method/ unless $self->ctx->app->renderer->helpers->{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ctx->app->renderer->helpers->{$method} 0 1 1
143  
144 1 return $self->ctx->$method(@_);
145 }
146  
147 # lifted from http://www.perlmonks.org/?node_id=44911
148 1 ▶sub can {
Subroutine can: called CC=3 SCAR=15.3
149 1 my ($self, $method) = @_;
150 1 my $subref = $self->SUPER::can($method);
151 1 ▶ return $subref if $subref; # can found it; it's a real method
Branch: 1 branch
BranchTrueFalseTotal
if $subref 0 1 1
152  
153 # Method doesn't currently exist; should it, though?
154 1 ▶ return unless exists $self->ctx->app->renderer->helpers->{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->ctx->app->renderer->helpers->{$method} 0 1 1
155  
156 # Return an anon sub that will work when it's eventually called
157 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
158 0 my $self = $_[0];
159  
160 # The method is being called. The real method may have been
161 # created in the meantime; if so, don't call AUTOLOAD again
162 0 my $subref = $self->SUPER::can($method);
163 0 ▶ goto &$subref if $subref;
Branch: 1 branch
BranchTrueFalseTotal
if $subref 0 0 0
164  
165 0 $AUTOLOAD=$method;
166 0 goto &AUTOLOAD;
167 1 };
168 }
169  
170 2 1;
171 __END__
↑ Summary