Teeny.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 218 CC 23 SCAR 43.8
CC
23
Cov
0%
compile
156.0
parse
42.0
_optimize
12.0
CRAP 79.8
1 package Template::Teeny;
2  
3 our $VERSION = '0.00_002';
4  
5 our $CODE_START = <<'END';
6 sub {
7 my ($stash_a, $out) = @_;
8 END
9  
10 our $CODE_END = <<'END';
11 }
12 END
13  
14 use Moose;
15  
16 has include_path => (
17 is => 'rw',
18 isa => 'ArrayRef[Str]',
19 required => 1,
20 lazy => 1,
21 default => sub { [qw(.)] },
22 );
23  
24 my ($START,$END) = map { qr{\Q$_\E} } qw([% %]);
25 my $DECLARATION = qr{$START (?:.+?) $END}x;
26 my $TEXT = qr{
27 (?:\A|(?<=$END)) # Start matching from the start of the file or end of a declaration
28 .*? # everything in between
29 (?:\Z|(?=$START)) # Finish at the end of the file or start of another declaration
30 }msx;
31 my $CHUNKS = qr{
32 ($TEXT)?
33 ($DECLARATION)?
34 }msx;
35  
36 my $IDENT = qr{
37 [a-z][a-z0-9_]+ # any alphanumeric characters and underscores, but must start
38 # with a letter; everything must be lower case
39 }x;
40  
41 my $SECTION = qr{
42 SECTION \s+ ($IDENT)
43 }x;
44 my $INCLUDE = qr{
45 INCLUDE \s+ ["']? ([^"']+) ["']?
46 }x;
47  
48 my $VARS = qr{
49 (?: \s* \| \s* )?
50 ( $IDENT )
51 }x;
52  
53 my $DIRECTIVE = qr{
54 $START
55 \s*?
56 (END
57 | $SECTION
58 | $INCLUDE
59 | [a-z0-9_\s\|]+
60 )
61 \s*?
62 $END
63 }x;
64  
65 sub parse {
66 my ($self, $tpl) = @_;
67 my (@chunks) = grep { defined $_ && $_ } ($tpl =~ m{$CHUNKS}g);
68
69 my @AST;
70 while(my $chunk = shift @chunks){
71 if(my ($dir) = $chunk =~ $DIRECTIVE){
72 if(my ($name) = $dir =~ $SECTION){
73 $name =~ s/['"]//g;
74 push @AST, [SECTION => $name];
75 }elsif(my ($nm) = $dir =~ $INCLUDE){
76 $nm =~ s/['"]//g;
77 push @AST, [INCLUDE => $nm];
78 }elsif($dir =~ m{END}){
79 push @AST, ['END'];
80 }elsif(my (@items) = $dir =~ m{$VARS}g){
81 push @AST, [VARS => [@items]];
82 }
83 } else {
84 push @AST, [TEXT => $chunk];
85 }
86 }
87  
88 return [@AST];
89 }
90  
91 # XXX - Tests should be added for this
92 sub _optimize {
93 my (undef, $AST) = @_;
94  
95 my @OPT;
96  
97 while (my $item = shift @$AST){
98 my ($type, $val) = @$item;
99  
100 if($type eq 'TEXT' || $type eq 'VARS'){
101 my @long = ($item);
102
103 # lets see what the next statement is to see if we can concat
104 while( $AST->[0] && ($AST->[0][0] eq 'TEXT' || $AST->[0][0] eq 'VARS') ){
105  
106 # move this
107 push @long, shift @$AST;
108 }
109  
110 # if there's only one statement, not much point in concat-ing.
111 if(@long > 1){
112 @long = [ CONCAT => [@long] ];
113 }
114  
115 push @OPT, @long;
116 } else {
117 push @OPT, $item;
118 }
119 }
120  
121 return [@OPT];
122 }
123  
124 sub compile {
125 my ($self, $AST) = @_;
126  
127 my $current_level ||= 0;
128  
129 my $code = '';
130 if(!$current_level){
131 $code .= $CODE_START;
132 }
133  
134 my @names = ('a'..'z');
135  
136 while(my $item = shift @$AST){
137 my ($type, $val) = @$item;
138  
139 if($type eq 'TEXT'){
140 $val =~ s{'}{\\'};
141 $code .= q{ print {$out} '}.$val.qq{';\n};
142  
143 } elsif ($type eq 'VARS') {
144 $code .= q{ print {$out} $stash_}
145 . $names[$current_level]
146 . q{->get(qw(}
147 . join(' ', @$val)
148 . qq{));\n};
149  
150 } elsif ($type eq 'END'){
151 $code .= " }\n";
152 $current_level--;
153  
154 } elsif ($type eq 'SECTION') {
155 my $old = $names[$current_level];
156 my $new = $names[++$current_level];
157  
158 $code .= " for my \$stash_$new ( \$stash_$old\->sections('$val') ) {\n";
159 } elsif ($type eq 'CONCAT') {
160 my ($t,$v) = @{ shift @$val };
161  
162 if($t eq 'TEXT'){
163 $v =~ s{'}{\\'};
164 $code .= q{ print {$out} '}.$v.qq{'\n};
165 }elsif($t eq 'VARS'){
166 $code .= q{ print {$out} $stash_}
167 . $names[$current_level]
168 . q{->get(qw(}
169 . join(' ', @$v)
170 . qq{))};
171 }
172  
173 for my $concat (@$val){
174 my ($ct,$cv) = @$concat;
175
176 if($ct eq 'TEXT'){
177 $cv =~ s{'}{\\'};
178 $code .= qq{\n . '}.$cv.q{'};
179 }elsif($ct eq 'VARS'){
180 $code .= qq{\n . \$stash_}
181 . $names[$current_level]
182 . q{->get(qw(}
183 . join(' ', @$cv)
184 . qq{))};
185 }
186 }
187  
188 $code .= ";\n";
189  
190 } else {
191 die "Could not understand type '$type'";
192 }
193 }
194  
195 if(!$current_level){
196 $code .= $CODE_END;
197 }
198  
199 return $code;
200 }
201  
202  
203 my $compiled_tpls = {};
204 sub process {
205 my ($self, $tpl, $stash, $fh) = @_;
206  
207 if(!$fh){
208 $fh = \*STDOUT;
209 }
210  
211 my $tpl_str = '';
212
213 # XXX - This should really take the full name
214 my $compile = $compiled_tpls->{ $tpl } ||= do {
215 if(!ref $tpl){
216 $tpl_str .= $self->_get_tpl_str($tpl);
217 }
218  
219 my $AST = $self->parse($tpl_str);
220 $AST = $self->_optimize($AST);
221 my $code_str = $self->compile($AST);
222  
223 my $coderef = eval($code_str) or die "Could not compile template: $@";
224 };
225 $compile->($stash, $fh);
226  
227 return;
228 }
229  
230 sub _get_tpl_str {
231 my ($self, $tpl) = @_;
232  
233 my $tpl_str = '';
234 my @dirs_to_try = @{ $self->include_path };
235  
236 my $file;
237 while(my $dir = shift @dirs_to_try){
238 my $tmp = $dir . '/' . $tpl;
239 if(-e $tmp){
240 $file = $tmp;
241 last;
242 }
243 }
244
245 die "Could not find $tpl" if(!$file);
246  
247 open my $fh, $file or die "Could not open '$file': $!";
248 $tpl_str .= do { local $/; <$fh>; };
249 close $fh or die "Could not close '$file': $!";
250  
251 return $tpl_str;
252 }
253  
254 1;
255  
256 __END__