blib/lib/Templ/Parser.pm

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