File Coverage

blib/lib/Dotiac/DTL/Tag/cycle.pm
Criterion Covered Total %
statement 145 152 95.3
branch 59 70 84.2
condition 11 18 61.1
subroutine 14 16 87.5
pod 11 11 100.0
total 240 267 89.8


line stmt bran cond sub pod time code
1             #cycle.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             #cycle.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            
17            
18             package Dotiac::DTL::Tag::cycle;
19 11     11   57 use base qw/Dotiac::DTL::Tag/;
  11         21  
  11         878  
20 11     11   57 use strict;
  11         27  
  11         355  
21 11     11   57 use warnings;
  11         22  
  11         22631  
22            
23             our $VERSION = 0.8;
24            
25             sub new {
26 20     20 1 30 my $class=shift;
27 20         62 my $self={p=>shift()};
28 20         32 my $name=shift;
29 20         25 my $list;
30 20         61 my %list = Dotiac::DTL::get_variables($name,"as");
31 20 50 66     36 die "Can't create cycle without arguments" unless @{$list{""}} or @{$list{as}};
  20         71  
  1         6  
32             #warn '"',@{$list[0]},"--",@{$list[1]},'"' if @list == 2;
33             #warn '"',@{$list[0]},'"' if @list == 1;
34 20 100 66     70 if ($list{""} and @{$list{""}} == 1 and $list{""}->[0] =~ /,/) {
  20   100     132  
35 2         7 @{$list{""}}=map {Dotiac::DTL::escap($_)} split /,/,$list{""}->[0];
  2         10  
  7         17  
36             }
37 20 100       64 if ($list{as}) {
38 8         28 $self->{name}=$list{as}->[0];
39 8 100 66     30 if ($list{""} and @{$list{""}}) {
  8         37  
40 7         14 $self->{cycle}=[@{$list{""}}];
  7         27  
41             }
42             else {
43 1         4 $self->{res}=1;
44             }
45            
46             }
47             else {
48 12         17 my @cycle = @{$list{""}};
  12         29  
49 12 100       32 if ($#cycle) {
50 3         15 $self->{cycle}=[@cycle];
51             }
52             else {
53 9         24 $self->{name}=$cycle[0];
54             }
55             }
56 20         60 bless $self,$class;
57 20         229 return $self;
58             }
59             sub print {
60 51     51 1 73 my $self=shift;
61 51         105 print $self->{p};
62 51 100 33     148 if ($self->{name}) {
    50          
63 40 100       107 if ($self->{cycle}) {
    100          
64 19 100       53 if ($Dotiac::DTL::cycle{$self->{name}}) {
65 13         15 $Dotiac::DTL::cycle{$self->{name}}->[1]=scalar @{$self->{cycle}};
  13         50  
66 13         34 $Dotiac::DTL::cycle{$self->{name}}->[2]=$self->{cycle};
67             }
68             else {
69 6         19 $Dotiac::DTL::cycle{$self->{name}}=[0,scalar @{$self->{cycle}},$self->{cycle}];
  6         32  
70             }
71             }
72             elsif ($self->{res}) {
73 1 50       7 $Dotiac::DTL::cycle{$self->{name}}->[0]=0 if $Dotiac::DTL::cycle{$self->{name}};
74             }
75 40 50       273 print Dotiac::DTL::devar($Dotiac::DTL::cycle{$self->{name}}->[2]->[$Dotiac::DTL::cycle{$self->{name}}->[0]++ % $Dotiac::DTL::cycle{$self->{name}}->[1]],@_) if $Dotiac::DTL::cycle{$self->{name}};
76             }
77             elsif ($self->{cycle} or $Dotiac::DTL::cycle{$self}) {
78 11         15 my $l;
79 11 100       47 unless ($Dotiac::DTL::cycle{$self}) {
80 3         5 $Dotiac::DTL::cycle{$self}=[0,scalar @{$self->{cycle}},$self->{cycle}];
  3         17  
81             }
82             #TODO: Find out how Django deals with cycle in for in for.
83             #if ($_[0]->{forloop}->{counter0}) {
84             # $l=$_[0]->{forloop}->{counter0};
85             #}
86             #else {
87 11         29 $l = $Dotiac::DTL::cycle{$self}->[0]++;
88             #}
89 11         49 print Dotiac::DTL::devar($Dotiac::DTL::cycle{$self}->[2]->[$l % $Dotiac::DTL::cycle{$self}->[1]],@_);
90             }
91 51         215 $self->{n}->print(@_);
92             }
93             sub string {
94 51     51 1 83 my $self=shift;
95 51         71 my $r=undef;
96 51 100 33     138 if ($self->{name}) {
    50          
97 40 100       111 if ($self->{cycle}) {
    100          
98 19 100       55 if ($Dotiac::DTL::cycle{$self->{name}}) {
99 13         17 $Dotiac::DTL::cycle{$self->{name}}->[1]=scalar @{$self->{cycle}};
  13         33  
100 13         33 $Dotiac::DTL::cycle{$self->{name}}->[2]=$self->{cycle};
101             }
102             else {
103 6         15 $Dotiac::DTL::cycle{$self->{name}}=[0,scalar @{$self->{cycle}},$self->{cycle}];
  6         33  
104             }
105             }
106             elsif ($self->{res}) {
107 1 50       8 $Dotiac::DTL::cycle{$self->{name}}->[0]=0 if $Dotiac::DTL::cycle{$self->{name}};
108             }
109 40 50       226 $r=$Dotiac::DTL::cycle{$self->{name}}->[2]->[$Dotiac::DTL::cycle{$self->{name}}->[0]++ % $Dotiac::DTL::cycle{$self->{name}}->[1]] if $Dotiac::DTL::cycle{$self->{name}};
110             }
111             elsif ($self->{cycle} or $Dotiac::DTL::cycle{$self}) {
112 11         16 my $l;
113 11 100       37 unless ($Dotiac::DTL::cycle{$self}) {
114 3         6 $Dotiac::DTL::cycle{$self}=[0,scalar @{$self->{cycle}},$self->{cycle}];
  3         14  
115             }
116             #if ($_[0]->{forloop}->{counter0}) {
117             # $l=$_[0]->{forloop}->{counter0};
118             #}
119             #else {
120 11         26 $l = $Dotiac::DTL::cycle{$self}->[0]++;
121             #}
122 11         46 $r=$Dotiac::DTL::cycle{$self}->[2]->[$l % $Dotiac::DTL::cycle{$self}->[1]];
123             }
124 51         166 return $self->{p}.Dotiac::DTL::devar($r,@_).$self->{n}->string(@_);
125            
126             }
127             sub perl {
128 20     20 1 39 my $self=shift;
129 20         31 my $fh=shift;
130 20         31 my $id=shift;
131 20         90 $self->SUPER::perl($fh,$id,@_);
132 20 100       70 print $fh "my " if $self->{name};
133 20 100       124 print $fh (Data::Dumper->Dump([$self->{name}],["\$name$id"])) if $self->{name};
134 20 100       669 print $fh "my " if $self->{cycle};
135 20 100       95 print $fh (Data::Dumper->Dump([$self->{cycle}],["\$cycle$id"])) if $self->{cycle};
136 20         612 return $self->{n}->perl($fh,$id+1,@_);
137             }
138             sub perlprint {
139 20     20 1 38 my $self=shift;
140 20         23 my $fh=shift;
141 20         26 my $id=shift;
142 20         27 my $level=shift;
143 20         31 my $digest=shift;
144 11 50   11   85 use Carp; confess unless $digest; #TODO;
  11         35  
  11         5212  
  20         46  
145 20         38 my $in="\t"x$level;
146 20         80 $self->SUPER::perlprint($fh,$id,$level,@_);
147 20 100       60 if ($self->{name}) {
148 17 100       58 if ($self->{cycle}) {
    100          
149 7         25 print $fh $in,"if (\$Dotiac::DTL::cycle{\$name$id}) {\n";
150 7         29 print $fh $in,"\t\$Dotiac::DTL::cycle{\$name$id}->[1]=scalar \@{\$cycle$id};\n";
151 7         20 print $fh $in,"\t\$Dotiac::DTL::cycle{\$name$id}->[2]=\$cycle$id;\n";
152 7         13 print $fh $in,"}\n";
153 7         12 print $fh $in,"else {\n";
154 7         21 print $fh $in,"\t\$Dotiac::DTL::cycle{\$name$id}=[0,scalar \@{\$cycle$id},\$cycle$id];\n";
155 7         22 print $fh $in,"}\n";
156             }
157             elsif ($self->{res}) {
158 1         5 print $fh "$in\$Dotiac::DTL::cycle{\$name$id}->[0]=0 if \$Dotiac::DTL::cycle{\$name$id};\n";
159             }
160 17         67 print $fh $in,"print Dotiac::DTL::devar(\$Dotiac::DTL::cycle{\$name$id}->[2]->[\$Dotiac::DTL::cycle{\$name$id}->[0]++ % \$Dotiac::DTL::cycle{\$name$id}->[1]],\$vars,\$escape,\@_) if \$Dotiac::DTL::cycle{\$name$id};\n";
161             }
162             else {
163 3 50       10 if ($self->{cycle}) {
164 3         23 print $fh $in,"\$Dotiac::DTL::cycle{'$digest-$id'}=[0,scalar \@{\$cycle$id},\$cycle$id] unless \$Dotiac::DTL::cycle{'$digest-$id'};\n";
165 3         21 print $fh $in,"print Dotiac::DTL::devar(\$Dotiac::DTL::cycle{'$digest-$id'}->[2]->[\$Dotiac::DTL::cycle{'$digest-$id'}->[0]++ % \$Dotiac::DTL::cycle{'$digest-$id'}->[1]],\$vars,\$escape,\@_);\n";
166             }
167             else {
168 0         0 print $fh $in,"print Dotiac::DTL::devar(\$Dotiac::DTL::cycle{'$digest-$id'}->[2]->[\$Dotiac::DTL::cycle{'$digest-$id'}->[0]++ % \$Dotiac::DTL::cycle{'$digest-$id'}->[1]],\$vars,\$escape,\@_) if \$Dotiac::DTL::cycle{'$digest-$id'};\n";
169             }
170             }
171 20         139 return $self->{n}->perlprint($fh,$id+1,$level,$digest,@_);
172             }
173             sub perlstring {
174 20     20 1 30 my $self=shift;
175 20         24 my $fh=shift;
176 20         24 my $id=shift;
177 20         34 my $level=shift;
178 20         26 my $digest=shift;
179 11 50   11   77 use Carp; confess unless $digest; #TODO;
  11         30  
  11         7970  
  20         57  
180 20         41 my $in="\t"x$level;
181 20         77 $self->SUPER::perlstring($fh,$id,$level,@_);
182 20 100       53 if ($self->{name}) {
183 17 100       65 if ($self->{cycle}) {
    100          
184 7         22 print $fh $in,"if (\$Dotiac::DTL::cycle{\$name$id}) {\n";
185 7         21 print $fh $in,"\t\$Dotiac::DTL::cycle{\$name$id}->[1]=scalar \@{\$cycle$id};\n";
186 7         20 print $fh $in,"\t\$Dotiac::DTL::cycle{\$name$id}->[2]=\$cycle$id;\n";
187 7         17 print $fh $in,"}\n";
188 7         11 print $fh $in,"else {\n";
189 7         22 print $fh $in,"\t\$Dotiac::DTL::cycle{\$name$id}=[0,scalar \@{\$cycle$id},\$cycle$id];\n";
190 7         34 print $fh $in,"}\n";
191             }
192             elsif ($self->{res}) {
193 1         6 print $fh "$in\$Dotiac::DTL::cycle{\$name$id}->[0]=0 if \$Dotiac::DTL::cycle{\$name$id};\n";
194             }
195 17         58 print $fh $in,"\$r.=Dotiac::DTL::devar(\$Dotiac::DTL::cycle{\$name$id}->[2]->[\$Dotiac::DTL::cycle{\$name$id}->[0]++ % \$Dotiac::DTL::cycle{\$name$id}->[1]],\$vars,\$escape,\@_) if \$Dotiac::DTL::cycle{\$name$id};\n";
196             }
197             else {
198 3 50       12 if ($self->{cycle}) {
199 3         16 print $fh $in,"\$Dotiac::DTL::cycle{'$digest-$id'}=[0,scalar \@{\$cycle$id},\$cycle$id] unless \$Dotiac::DTL::cycle{'$digest-$id'};\n";
200 3         22 print $fh $in,"\$r.=Dotiac::DTL::devar(\$Dotiac::DTL::cycle{'$digest-$id'}->[2]->[\$Dotiac::DTL::cycle{'$digest-$id'}->[0]++ % \$Dotiac::DTL::cycle{'$digest-$id'}->[1]],\$vars,\$escape,\@_);\n";
201             }
202             else {
203 0         0 print $fh $in,"\$r.=Dotiac::DTL::devar(\$Dotiac::DTL::cycle{'$digest-$id'}->[2]->[\$Dotiac::DTL::cycle{'$digest-$id'}->[0]++ % \$Dotiac::DTL::cycle{'$digest-$id'}->[1]],\$vars,\$escape,\@_) if \$Dotiac::DTL::cycle{'$digest-$id'};\n";
204             }
205             }
206 20         148 return $self->{n}->perlstring($fh,$id+1,$level,$digest,@_);
207             }
208             sub perleval {
209 20     20 1 35 my $self=shift;
210 20         24 my $fh=shift;
211 20         21 my $id=shift;
212 20         97 $self->{n}->perleval($fh,$id+1,@_);
213             }
214             sub perlinit {
215 20     20 1 29 my $self=shift;
216 20         24 my $fh=shift;
217 20         27 my $id=shift;
218 20         81 $self->{n}->perlinit($fh,$id+1,@_);
219             }
220             sub perlcount {
221 0     0 1 0 my $self=shift;
222 0         0 my $id=shift;
223 0         0 return $self->{n}->perlcount($id+1);
224             }
225             sub next {
226 20     20 1 31 my $self=shift;
227 20         86 $self->{n}=shift;
228             }
229             sub eval {
230 0     0 1   my $self=shift;
231 0           $self->{n}->eval(@_);
232             }
233             1;
234            
235             __END__