File Coverage

blib/lib/Dotiac/DTL/Tag/ssi.pm
Criterion Covered Total %
statement 150 167 89.8
branch 55 72 76.3
condition 4 6 66.6
subroutine 12 14 85.7
pod 11 11 100.0
total 232 270 85.9


line stmt bran cond sub pod time code
1             #ssi.pm
2             #Last Change: 2009-01-19
3             #Copyright (c) 2009 Marc-Seabstian "Maluku" Lucksch
4             #Version 0.8
5             ####################
6             #This file is part of the Dotiac::DTL project.
7             #http://search.cpan.org/perldoc?Dotiac::DTL
8             #
9             #ssi.pm is published under the terms of the MIT license, which basically
10             #means "Do with it whatever you want". For more information, see the
11             #license.txt file that should be enclosed with libsofu distributions. A copy of
12             #the license is (at the time of writing) also available at
13             #http://www.opensource.org/licenses/mit-license.php .
14             ###############################################################################
15            
16             package Dotiac::DTL::Tag::ssi;
17 11     11   57 use base qw/Dotiac::DTL::Tag/;
  11         18  
  11         921  
18 11     11   54 use strict;
  11         24  
  11         345  
19 11     11   54 use warnings;
  11         17  
  11         31565  
20            
21             our $VERSION = 0.8;
22            
23             sub new {
24 6     6 1 8 my $class=shift;
25 6         14 my $self={p=>shift()};
26 6         10 my $name=shift;
27 6 50       16 die "This ssi needs a filename or variable" unless $name;
28 6 50       12 die "\$Dotiac::DTL::ALLOWED_INCLUDE_ROOTS is not set, can't use this tag" unless $Dotiac::DTL::ALLOWED_INCLUDE_ROOTS;
29             #my $parsed=$name=~s/\s+parsed$//;
30 6         20 my @name=Dotiac::DTL::get_variables($name);
31 6 100 66     37 $self->{parsed}=1 if $name[-1] and $name[-1] eq "parsed";
32 6         10 my $f=substr $name[0],0,1;
33 6         12 my $e=substr $name[0],-1,1;
34 6 100 66     24 if ($f eq "`" and $e eq "`") {
35 2         10 $self->{load}=Dotiac::DTL::devar($name[0],{},0);
36 2 100       8 if ($self->{parsed}) {
37 1         7 my $tem = Dotiac::DTL->safenew($self->{load});
38 1         10 $self->{content}=$tem->{first};
39             }
40             else {
41 1 50       34 open my $fh,"<",substr($name,1,-1) or die "Can't open ssi include $name";
42 1         3 $self->{content}=Dotiac::DTL::Tag->new(do {local $/;<$fh>});
  1         3  
  1         27  
43 1         12 close $fh;
44             }
45             }
46             else {
47 4         9 $self->{var}=$name[0];
48             }
49 6         16 bless $self,$class;
50 6         29 return $self;
51             }
52             sub print {
53 6     6 1 7 my $self=shift;
54 6         32 print $self->{p};
55 6 100       15 if ($self->{content}) {
56 2         9 $self->{content}->print(@_);
57             }
58             else {
59 4         13 my $tem = Dotiac::DTL::devar_repr($self->{var},@_);
60 4 100       11 if ($self->{parsed}) {
61 2         7 my $tem = Dotiac::DTL->safenew($tem);
62 2         9 $tem->{first}->print(@_);
63             }
64             else {
65 2 50       54 open my $fh,"<",$tem or die "Can't open ssi include from var :\"$tem\"";
66 2         3 print do {local $/;<$fh>};
  2         7  
  2         55  
67 2         21 close $fh;
68             }
69             }
70 6 50       34 $self->{n}->print(@_) if $self->{n};
71             }
72             sub string {
73 6     6 1 8 my $self=shift;
74 6 100       18 if ($self->{content}) {
75 2 50       12 return $self->{p}.$self->{content}->string(@_).($self->{n}?$self->{n}->string(@_):"");
76             }
77 4         13 my $tem = Dotiac::DTL::devar_repr($self->{var},@_);
78 4 100       11 if ($self->{parsed}) {
79 2         10 $tem = Dotiac::DTL->safenew($tem);
80 2 50       10 return $self->{p}.$tem->{first}->string(@_).($self->{n}?$self->{n}->string(@_):"");
81             }
82             else {
83 2 50       64 open my $fh,"<",$tem or die "Can't open ssi include from var :\"$tem\"";
84 2 50       4 return $self->{p}.do {local $/;<$fh>}.($self->{n}?$self->{n}->string(@_):"");
  2         6  
  2         58  
85 0         0 close $fh;
86             }
87             }
88             sub perl {
89 6     6 1 11 my $self=shift;
90 6         7 my $fh=shift;
91 6         7 my $id=shift;
92 6         27 $self->SUPER::perl($fh,$id,@_);
93 6 100       1181 if ($self->{content}) {
94 2         7 print $fh "my ";
95 2         15 print $fh (Data::Dumper->Dump([$self->{load}],["\$name$id"]));
96 2 100       85 if ($self->{parsed}) {
97 1         5 print $fh "my \$template$id=Dotiac::DTL->safenew(\$name$id);\n";
98             }
99             else {
100 1         5 print $fh "open my \$fh$id,'<',\$name$id or die \"Can't SSI \$name$id: \$!\";\n";
101 1         3 print $fh "my \$template$id=do {local \$/;<\$fh$id>};\n";
102 1         3 print $fh "close \$fh$id;\n";
103             }
104             }
105             else {
106 4         7 print $fh "my ";
107 4         23 print $fh (Data::Dumper->Dump([$self->{var}],["\$name$id"]));
108             }
109 6         160 return $self->{n}->perl($fh,$id+1,@_);
110             }
111             sub perlprint {
112 6     6 1 8 my $self=shift;
113 6         10 my $fh=shift;
114 6         4 my $id=shift;
115 6         9 my $level=shift;
116 6         34 $self->SUPER::perlprint($fh,$id,$level,@_);
117 6         18 my $in="\t" x $level;
118 6 100       14 if ($self->{content}) {
119 2 100       10 print $fh $in,"die \"Cyclic include detected \" if \$Dotiac::DTL::included{\$template$id}++;\n" if $self->{parsed};
120 2 100       14 print $fh "$in\$template$id"."->{first}->print(\$vars,\$escape,\@_);\n" if $self->{parsed};
121 2 100       16 print $fh $in,"print \$template$id;\n" unless $self->{parsed};
122 2 100       12 print $fh $in,"\$Dotiac::DTL::included{\$template$id}=0;\n" if $self->{parsed};
123             }
124             else {
125 4 100       9 if ($self->{parsed}) {
126 2         6 print $fh $in,"my \$template$id = Dotiac::DTL::devar_repr(\$name$id,\$vars,\$escape,\@_);\n";
127 2         6 print $fh $in,"my \$s$id=\$template$id;\n";
128 2         4 print $fh $in,"if (not ref \$template$id) {\n";
129 2         5 print $fh $in,"\t\$template$id = Dotiac::DTL->new(\$template$id);\n";
130 2         2 print $fh $in,"} else {\n";
131 2         5 print $fh $in,"\tdie \"Can't ssi with \\\"\$template$id\\\"\";\n";
132 2         4 print $fh $in,"}\n";
133 2         4 print $fh $in,"die \"Cyclic include detected \" if \$Dotiac::DTL::included{\$s$id}++;\n";
134 2         5 print $fh $in,"\$template$id"."->{first}->print(\$vars,\$escape,\@_);\n";
135 2         5 print $fh $in,"\$Dotiac::DTL::included{\$s$id}=0;\n";
136             }
137             else {
138 2         7 print $fh $in,"open my \$fh$id,'<',Dotiac::DTL::devar_repr(\$name$id,\$vars,\$escape,\@_) or die \"Can't SSI \$name$id: \$!\";\n";
139 2         5 print $fh $in,"print do {local \$/;<\$fh$id>};\n";
140 2         4 print $fh $in,"close \$fh$id;\n";
141             }
142             }
143 6         34 return $self->{n}->perlprint($fh,$id+1,$level,@_);
144             }
145             sub perlstring {
146 6     6 1 8 my $self=shift;
147 6         8 my $fh=shift;
148 6         9 my $id=shift;
149 6         8 my $level=shift;
150 6         34 $self->SUPER::perlstring($fh,$id,$level,@_);
151 6         9 my $in="\t" x $level;
152 6 100       21 if ($self->{content}) {
153 2 100       10 print $fh $in,"die \"Cyclic include detected \" if \$Dotiac::DTL::included{\$template$id}++;\n" if $self->{parsed};
154 2 100       10 print $fh "$in\$r.=\$template$id"."->{first}->string(\$vars,\$escape,\@_);\n" if $self->{parsed};
155 2 100       7 print $fh "$in\$r.=\$template$id;\n" unless $self->{parsed};
156 2 100       8 print $fh $in,"\$Dotiac::DTL::included{\$template$id}=0;\n" if $self->{parsed};
157             }
158             else {
159 4 100       11 if ($self->{parsed}) {
160 2         7 print $fh $in,"my \$template$id = Dotiac::DTL::devar_repr(\$name$id,\$vars,\$escape,\@_);\n";
161 2         6 print $fh $in,"my \$s$id=\$template$id;\n";
162 2         4 print $fh $in,"if (not ref \$template$id) {\n";
163 2         6 print $fh $in,"\t\$template$id = Dotiac::DTL->safenew(\$template$id);\n";
164 2         8 print $fh $in,"} else {\n";
165 2         5 print $fh $in,"\tdie \"Can't ssi with \\\"\$template$id\\\"\";\n";
166 2         236 print $fh $in,"}\n";
167 2         3133 print $fh $in,"die \"Cyclic include detected \" if \$Dotiac::DTL::included{\$s$id}++;\n";
168 2         9 print $fh $in,"\$r.=\$template$id"."->{first}->string(\$vars,\$escape,\@_);\n";
169 2         5 print $fh $in,"\$Dotiac::DTL::included{\$s$id}=0;\n";
170             }
171             else {
172 2         8 print $fh $in,"open my \$fh$id,'<',Dotiac::DTL::devar_repr(\$name$id,\$vars,\$escape,\@_) or die \"Can't SSI \$name$id: \$!\";\n";
173 2         5 print $fh $in,"\$r.=do {local \$/;<\$fh$id>};\n";
174 2         5 print $fh $in,"close \$fh$id;\n";
175             }
176             }
177 6         42 return $self->{n}->perlstring($fh,$id+1,$level,@_);
178             }
179             sub perlcount {
180 0     0 1 0 my $self=shift;
181 0         0 my $id=shift;
182 0         0 return $self->{n}->perlcount($id+1);
183             }
184             sub perleval {
185 6     6 1 9 my $self=shift;
186 6         7 my $fh=shift;
187 6         7 my $id=shift;
188 6         7 my $level=shift;
189 6         12 my $in="\t" x $level;
190 6 100       15 if ($self->{parsed}) {
191 3 100       9 if ($self->{content}) {
192 1         3 print $fh $in,"die \"Cyclic include detected \" if \$Dotiac::DTL::included{\$template$id}++;\n";
193 1         3 print $fh "$in\$template$id"."->{first}->eval(\$vars,\$escape,\@_);\n";
194 1         3 print $fh $in,"\$Dotiac::DTL::included{\$template$id}=0;\n";
195             }
196             else {
197 2         6 print $fh $in,"my \$template$id = Dotiac::DTL::devar_repr(\$name$id,\$vars,\$escape,\@_);\n";
198 2         6 print $fh $in,"my \$s$id=\$template$id;\n";
199 2         11 print $fh $in,"if (not ref \$template$id) {\n";
200 2         4 print $fh $in,"\t\$template$id = Dotiac::DTL->safenew(\$template$id);\n";
201 2         3 print $fh $in,"} else {\n";
202 2         5 print $fh $in,"\tdie \"Can't ssi with \\\"\$template$id\\\"\";\n";
203 2         2 print $fh $in,"}\n";
204 2         5 print $fh $in,"die \"Cyclic include detected \" if \$Dotiac::DTL::included{\$s$id}++;\n";
205 2         6 print $fh $in,"\$template$id"."->{first}->eval(\$vars,\$escape,\@_);\n";
206 2         4 print $fh $in,"\$Dotiac::DTL::included{\$s$id}=0;\n";
207             }
208             }
209 6         32 return $self->{n}->perleval($fh,$id+1,$level,@_);
210             }
211             sub perlinit {
212 6     6 1 9 my $self=shift;
213 6         8 my $fh=shift;
214 6         8 my $id=shift;
215 6         21 return $self->{n}->perlinit($fh,$id+1,@_);
216             }
217             sub next {
218 6     6 1 9 my $self=shift;
219 6         21 $self->{n}=shift;
220             }
221             sub eval {
222 0     0 1   my $self=shift;
223 0 0         if ($self->{parsed}) {
224 0 0         if ($self->{content}) {
225 0           $self->{content}->eval(@_) ;
226             }
227             else {
228 0           my $tem = Dotiac::DTL::devar($self->{var},@_);
229 0           my $s=$tem;
230 0 0         if (not ref $tem) {
231 0           $tem = Dotiac::DTL->safenew($tem);
232             }
233             else {
234 0           die "Can't ssi with \"$tem\"";
235             }
236 0 0         die "Cyclic include detected " if $Dotiac::DTL::included{$s}++;
237 0           $tem->{first}->eval(@_);
238 0           $Dotiac::DTL::included{$s}=0;
239             }
240             }
241 0           $self->{n}->eval(@_);
242             }
243             1;
244            
245             __END__