blib/lib/Templ/Parser.pm

filter: Statementstmt 47.1% 49 / 104 Branchbran 28.9% 11 / 38 Conditioncond 21.4% 3 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 63.1% 12 / 19 Podpod 0.0% 0 / 12 totaltotal 38.4% 75 / 195 SCAR 52.0
CC
27
Cov
40%
prettify_lines
90.0
parse
27.8
new
7.6
CRAP 181.5
↑ Summary blib/lib/Templ/Template.pm »
1 package Templ::Parser;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(cluck croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Templ::Util qw(quote attempt default number_lines);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶my $can_tidy = attempt { require Perl::Tidy; require File::Temp };
Subroutine __ANON__: called CC=1 SCAR=0.4
11 1 ▶my $can_say = attempt { require v5.10; };
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 sub new {
14 4 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=20.3
Pod undocumented
15 4 ▶ if ( not defined $class || ref $class || $class !~ m/^(\w+\:\:)*\w+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/) 0 4 4
Condition33%2/6
defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/
l!l&&r!l&&!r
400
defined $class or ref $class
l!l&&r!l&&!r
400
MC/DC0%0/3
defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/
defined $class ref $class not $class =~ /^(\w+\:\:)*\w+$/
Truth table25%1/4
A or B or not(C)
A= defined $classB= ref $classC= not $class =~ /^(\w+\:\:)*\w+$/
ABCresult
1XX1
01X1
0011
0000
16 0 croak "Can only be called as Templ::Parser::...->new";
17 }
18 4 ▶ if ($class eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 0 4 4
19 0 croak "__PACKAGE__ cannot be instantiated directly, use a subclass";
20 }
21 4 my $self = bless {@_}, $class;
22 4 return $self;
23 }
24  
25 sub parse {
26 4 ▶ my $self = shift;
Subroutine parse: called CC=10 SCAR=33.2
Pod undocumented
27 4 my $templ = shift;
28  
29 # The template is assumed to be starting as printing output, so
30 # wrap the whole template in a header/footer, escaping the contents
31 4 my $perl = '';
32 4 ▶ if ($self->prettify) { $perl .= $self->pretty_header; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prettify) 0 4 4
33 4 $perl .= $templ->header;
34 4 $perl .= $self->header;
35 4 $perl .= "'".quote($templ->templ_code)."'";
36 4 $perl .= $self->footer;
37  
38 # Loop over all of the remaining <* ... *> tag types, performing the
39 # prescribed replacements... run in reverse to process latest / deepest
40 # subclass tags first
41 #
42 # $self->debug && print Data::Dumper->Dump([[$templ->tags]],['tags']);
43 # $self->debug && print Data::Dumper->Dump([$templ],['templ']);
44 4 foreach my $tag ( $templ->tags ) {
45 21 $perl = $tag->process( $perl, $self );
46 }
47  
48 4 ▶ if ( $self->prettify ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prettify) 0 4 4
49 # Change any standalone single-quote print statements with
50 # literal newlines in them to a series of individual print
51 # or say statements for readability
52 0 $perl =~ s{
53 (?:
54 # $1 = Previous statement separator
55 ( (?: ^ | \; | \{ | \} ) [ \t]*? (?:\r?\n)? )
56 # $2 = Print statement indentation
57 ( \s*? )
58 # $3 = Double quote contents
59 print \s* '(.*?)(?<!\\)'
60 # $4 = Closing brace or semicolon
61 ( \s* (?: (?:\;|\}) (?:\r?\n)? | $ ) )
62 )
63 }
64 0 { $self->prettify_lines($1, $2, $3, $4) }egsx;
65 }
66  
67 4 ▶ if ( $self->tidy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tidy) 0 4 4
68 0 ▶ if ($can_tidy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_tidy) { } 0 0 0
69 0 require File::Temp;
70 0 require Perl::Tidy;
71  
72 # Using a temp file because the output is weird when we don't
73 0 ( undef, my $tmp ) = File::Temp::tempfile();
74 0 Perl::Tidy::perltidy(
75 'source' => \$perl,
76 'destination' => $tmp,
77 'argv' => [ split /\s+/, $self->tidy_options ],
78 );
79 0 ▶ open my $FH, '<', $tmp
Condition0%0/2
$tmp || die("Unable to open file for reading ${tmp}: $!")
l!l
00
MC/DC0%0/1
$tmp || die("Unable to open file for reading ${tmp}: $!")
$tmp
Truth table0%0/2
A
A= $tmp
Aresult
11
00
80 || die "Unable to open file for reading $tmp: $!";
81 0 local $/ = undef;
82 0 $perl = <$FH>;
83 0 close $FH;
84 }
85 else {
86 0 warn "Unable to load Perl::Tidy and/or File::Temp\n";
87 }
88 }
89  
90 4 ▶ if ( $self->tidy || $self->prettify ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tidy or $self->prettify) 0 4 4
Condition33%1/3
$self->tidy or $self->prettify
l!l&&r!l&&!r
004
MC/DC0%0/2
$self->tidy or $self->prettify
$self->tidy $self->prettify
Truth table33%1/3
A or B
A= $self->tidyB= $self->prettify
ABresult
1X1
011
000
91  
92 # Remove blank append statements
93 0 my $append = $self->append;
94 0 $perl =~ s/(?:^|(?<=\n)[ \t]*)\Q$append\E'';[ \t]*(?:\r?\n|$)//;
95 }
96  
97 4 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 4 4
98 0 print STDERR number_lines($perl);
99 }
100  
101 4 return $perl;
102 }
103  
104 # Breaks a print statement with newlines in it into multiple statements
105 # Helps with formatting code to preserve indentation (used when prettify is
106 # enabled)
107 sub prettify_lines {
108 0 ▶ my $self = shift;
Subroutine prettify_lines: not called CC=9 SCAR=45.0
Pod undocumented
109 0 my $pre = shift; # Previous opening brace or semicolon
110 0 my $indent = shift; # Indentation spacing of the print statement
111 0 my $contents = shift; # Contents of the single quotes of the print
112 0 my $post = shift; # Closing brace or semicolon
113  
114 0 my $out = $pre;
115  
116 # Create a list of lines (and the ending partial line) in the print
117 0 my @chunks = split /(.*?\n)/, $contents;
118  
119 # print "CHUNK <<$_>>\n" foreach @chunks;
120 0 foreach ( 0 .. $#chunks ) {
121 0 my $is_last_chunk = ( $_ == $#chunks );
122 0 my $chunk = $chunks[$_];
123 0 ▶ next if ( ( $chunk eq '' ) && ( not $is_last_chunk ) );
Branch: 1 branch
BranchTrueFalseTotal
if $chunk eq "" and not $is_last_chunk 0 0 0
Condition0%0/3
$chunk eq "" and not $is_last_chunk
!ll&&!rl&&r
000
MC/DC0%0/2
$chunk eq "" and not $is_last_chunk
$chunk eq "" not $is_last_chunk
Truth table0%0/3
A and not(B)
A= $chunk eq ""B= not $is_last_chunk
ABresult
0X0
100
111
124  
125 0 my $nl = '';
126 0 ▶ if ( $chunk =~ s/\r\n$// ) { $nl = '\r\n'; }
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk =~ s/\r\n$//) { } 0 0 0
elsif ($chunk =~ s/\n$//) { } 0 0 0
127 0 elsif ( $chunk =~ s/\n$// ) { $nl = '\n'; }
128  
129 0 my $statement;
130 0 ▶ if ($nl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl) { } 0 0 0
131 0 ▶ if ($self->append_pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->append_pretty) { } 0 0 0
132 0 $statement = $self->append_pretty . "'$chunk'";
133 }
134 else {
135 0 $statement = $self->append . "'$chunk'" . '."$nl"';
136 }
137 }
138 else {
139 0 $statement = $self->append . "'$chunk'";
140 }
141 0 ▶ $statement .= $is_last_chunk ? $post : ";\n";
Branch: 1 branch
BranchTrueFalseTotal
$is_last_chunk ? : 0 0 0
142  
143 0 $out .= $statement;
144 }
145  
146 0 return $out;
147 }
148  
149 ##############################################################################
150 # Some override-able functions for subclasses
151  
152 0 ▶sub pretty_header { return ''; }
Subroutine pretty_header: not called CC=1 SCAR=6.9
Pod undocumented
153 0 ▶sub header { die "Subclass must override Templ::Parser->header"; }
Subroutine header: not called CC=1 SCAR=6.9
Pod undocumented
154 0 ▶sub append { die "Subclass must override Templ::Parser->append"; }
Subroutine append: not called CC=1 SCAR=6.9
Pod undocumented
155 0 ▶sub append_pretty { return ''; }
Subroutine append_pretty: not called CC=1 SCAR=6.9
Pod undocumented
156 0 ▶sub footer { die "Subclass must override Templ::Parser->footer"; }
Subroutine footer: not called CC=1 SCAR=6.9
Pod undocumented
157  
158 ##############################################################################
159 # Accessors...
160 sub debug {
161 4 ▶ my $self = shift;
Subroutine debug: called CC=2 SCAR=7.6
Pod undocumented
162 4 ▶ if ( defined $_[0] ) { $self->{'debug'} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 4 4
163 4 return default $self->{'debug'}, $Templ::Parser::debug, 0;
164 }
165  
166 sub tidy {
167 20 ▶ my $self = shift;
Subroutine tidy: called CC=2 SCAR=7.6
Pod undocumented
168 20 ▶ if ( defined $_[0] ) { $self->{'tidy'} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 20 20
169 20 return default $self->{'tidy'}, $Templ::Parser::tidy, 0;
170 }
171  
172 sub tidy_options {
173 0 ▶ my $self = shift;
Subroutine tidy_options: not called CC=2 SCAR=17.9
Pod undocumented
174 0 ▶ if ( defined $_[0] ) { $self->{'tidy_options'} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
175 0 return default $self->{'tidy_options'}, $Templ::Parser::tidy_options,
176 '-pbp -nst -b -aws -dws -dsm -nbbc -kbl=0 -asc -npro -sbl';
177 }
178  
179 sub prettify {
180 12 ▶ my $self = shift;
Subroutine prettify: called CC=3 SCAR=12.5
Pod undocumented
181 12 ▶ if ( defined $_[0] ) { $self->{'prettify'} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 12 12
182 12 ▶ return default $self->{'prettify'}, $Templ::Parser::prettify,
Branch: 1 branch
BranchTrueFalseTotal
$self->tidy ? : 0 12 12
183 ( $self->tidy ? 1 : 0 );
184 }
185  
186 1 1;
↑ Summary blib/lib/Templ/Template.pm »