File Coverage

blib/lib/Dotiac/DTL/Addon/html_template.pm
Criterion Covered Total %
statement 96 107 89.7
branch 38 52 73.0
condition 20 25 80.0
subroutine 8 8 100.0
pod 2 2 100.0
total 164 194 84.5


line stmt bran cond sub pod time code
1             ###############################################################################
2             #html_template.pm
3             #Last Change: 2009-01-21
4             #Copyright (c) 2009 Marc-Seabstian "Maluku" Lucksch
5             #Version 0.4
6             ####################
7             #This file is an addon to the Dotiac::DTL project.
8             #http://search.cpan.org/perldoc?Dotiac::DTL
9             #
10             #html_template.pm is published under the terms of the MIT license, which
11             #basically means "Do with it whatever you want". For more information, see the
12             #license.txt file that should be enclosed with the distributions. A copy of
13             #the license is (at the time of writing) also available at
14             #http://www.opensource.org/licenses/mit-license.php .
15             ###############################################################################
16            
17            
18             package Dotiac::DTL::Addon::html_template;
19 9     9   319666 use strict;
  9         31  
  9         354  
20 9     9   46 use warnings;
  9         19  
  9         286  
21 9     9   52 use base qw/Dotiac::DTL::Parser Dotiac::DTL::Addon::html_template_pure/;
  9         16  
  9         9977  
22 9     9   51 use Dotiac::DTL::Core;
  9         17  
  9         14608  
23             require Dotiac::DTL::Tag;
24             require Dotiac::DTL::Addon;
25             require Dotiac::DTL::Addon::html_template::Variable;
26             require Dotiac::DTL::Tag::importloop;
27            
28             our $VERSION = 0.4;
29            
30             my @oldparser;
31             my $first;
32            
33            
34             sub import {
35 14     14   70844 push @oldparser,$Dotiac::DTL::PARSER;
36 14         35 $Dotiac::DTL::PARSER="Dotiac::DTL::Addon::html_template";
37 14         41 $Dotiac::DTL::Addon::NOCOMPILE{'Dotiac::DTL::Addon::html_template'}=1;
38 14         33 my $class=shift;
39 14         76 while (my $a=shift @_) {
40 0         0 my $o=shift @_;
41 0 0 0     0 if (defined $o and exists $Dotiac::DTL::Addon::html_template_pure::OPTIONS{$a}) {
42 0         0 $Dotiac::DTL::Addon::html_template_pure::OPTIONS{$a}=$o;
43             }
44             }
45 14         13846 $first=1;
46             }
47            
48             sub unimport {
49 5     5   10075 $Dotiac::DTL::PARSER=pop @oldparser;
50 5         17 $first=0;
51             }
52            
53            
54             sub new {
55 76     76 1 1405249 my $class=shift;
56 76         235 my $self={};
57 76         312 bless $self,$class;
58 76         238 return $self;
59             }
60            
61            
62             sub parse {
63 494     494 1 8490 my $self=shift;
64 494         649 my $template=shift;
65 494         624 my $pos=shift;
66 494         628 my $start=$$pos;
67 494         851 my @end = @_;
68 494         532 my $found;
69 494 100       1219 $found=shift @end if @end;
70 494         630 local $_;
71 494         1315 while ($Dotiac::DTL::PARSER eq __PACKAGE__) {
72 609         1696 pos($$template) = $$pos;
73 609 100       2726 if ($$template=~m/[^<\{]*([<\{])/g) {
74 533         1154 my $dtag=$1; #Decisive tag :)
75 533         1285 my $pre = substr $$template,$start,pos($$template)-$start-1;
76 533 100       1263 if ($dtag eq "{") {
77 321         602 my $n = substr $$template,pos($$template),1;
78 321         686 $$pos=pos($$template)+1;
79 321 100       710 if ($n eq "%") {
    100          
    50          
80 255         593 my $npos = index($$template,"%}",$$pos);
81 255 50       514 die "Missing closing %} at char $$pos" if $npos < 0;
82 255         541 my $cont=substr $$template,$$pos,$npos-$$pos;
83 255         301 $$pos=$npos+2;
84 255         1088 $cont=~s/^\s+//;
85 255         898 $cont=~s/\s+$//;
86 255         1111 my ($tagname,$param) = split /\s+/,$cont,2;
87 255         418 $tagname=lc $tagname;
88 255 100 50     794 $$found = $tagname and return Dotiac::DTL::Tag->new($pre) if $found and grep {$_ eq $tagname} @end;
  233   100     1346  
89 137         166 my $r;
90 137         321 eval {$r="Dotiac::DTL::Tag::$tagname"->new($pre,$param,$self,$template,$pos);};
  137         1585  
91 137 50       38425 if ($@) {
92 0         0 die "Error while loading Tag '$tagname' from Dotiac::DTL::Tag::$tagname. If this is an endtag (like endif) then your template is unbalanced\n$@";
93             }
94 137 100       346 if ($$pos >= length $$template) {
95 2         12 $r->next(Dotiac::DTL::Tag->new(""));
96             }
97             else {
98 135         631 $r->next($self->parse($template,$pos,@_));
99             }
100 137 100 100     2399 if (not $start or $first) {
101 52         86 $first=0;
102 52 50       216 return $r if lc($Dotiac::DTL::Addon::html_template_pure::OPTIONS{default_escape}) eq "html";
103 52         198 return bless {p=>"",n=>Dotiac::DTL::Tag->new(""),escape=>0,content=>$r},"Dotiac::DTL::Tag::autoescape"; #Autoescape off unless default_escape == html
104             }
105 85         467 return $r;
106            
107             }
108             elsif ($n eq "{") {
109 63         143 my $npos = index($$template,"}}",$$pos);
110 63 50       152 die "Missing closing }} at char $$pos" if $npos < 0;
111 63         163 my $cont=substr $$template,$$pos,$npos-$$pos;
112 63         129 $$pos=$npos+2;
113 63         275 return Dotiac::DTL::Variable->new($pre,$cont,$self->parse($template,$pos,@_));
114             }
115             elsif ($n eq "#") {
116 3         4 my $npos = index($$template,"#}",$$pos);
117 3 50       8 die "Missing closing #} at char $$pos" if $npos < 0;
118 3         5 my $cont=substr $$template,$$pos,$npos-$$pos;
119 3         4 $$pos=$npos+2;
120 3         6 return Dotiac::DTL::Comment->new($pre,$cont,$self->parse($template,$pos,@_));
121             }
122             }
123             else {
124             #my $p=
125 212 100       1272 if($$template=~m/\G(?:!--\s*)?
126             ([\/]?)\s*
127             [Tt][Mm][Pp][Ll]_((?:[Vv][Aa][Rr])|(?:[Ii][Ff])|(?:[Ee][Ll][Ss][Ee])|(?:[Uu][Nn][Ll][Ee][Ss][Ss])|(?:[Ll][Oo][Oo][Pp])|(?:[Ii][Nn][Cc][Ll][Uu][Dd][Ee]))
128             \s*(
129             (?:
130             (?:
131             (?:(?:[Dd][Ee][Ff][Aa][Uu][Ll][Tt])|(?:[Ee][Ss][Cc][Aa][Pp][Ee])|(?:[Nn][Aa][Mm][Ee]))
132             \s*=\s*
133             )?
134             (?!-->)(?:(?:"[^">]*")|(?:'[^'>]*')|(?:[^\s=>]*))\s*
135             )*
136             )
137             (?:--)?>/sgx) {
138 97         193 my $end=$1;
139 97         181 my $tag=lc($2);
140 97         156 my $content=$3;
141 97         148 $$pos=pos($$template);
142 97         119 my $tagname=$tag;
143 97 100       202 $tagname="end$tag" if $end;
144 97 100 100     291 $tagname="endimportloop" if $end and $tag eq "loop";
145 97 100 100     311 $tagname="endif" if $end and $tag eq "unless";
146 97 100 50     302 $$found = $tagname and return Dotiac::DTL::Tag->new($pre) if $found and grep {$_ eq $tagname} @end;
  80   100     408  
147 69         396 my $t=$self->maketag($tag,$content,$pre,$template,$pos);
148 69 50       332 if ($t) {
149 69 50       147 if ($$pos >= length $$template) {
150 0         0 $t->next(Dotiac::DTL::Tag->new(""));
151             }
152             else {
153 69         346 $t->next($self->parse($template,$pos,@_));
154             }
155 69 100 100     728 if (not $start or $first) {
156 24         34 $first=0;
157 24 100       116 return $t if lc($Dotiac::DTL::Addon::html_template_pure::OPTIONS{default_escape}) eq "html";
158 21         86 return bless {p=>"",n=>Dotiac::DTL::Tag->new(""),escape=>0,content=>$t},"Dotiac::DTL::Tag::autoescape"; #Autoescape off unless default_escape == html
159             }
160 45         212 return $t;
161             }
162             else {
163 0         0 warn "Couldn't make anything with $tag,$tagname, maybe your template is unbalanced";
164             }
165             }
166             else {
167             #die;# $p;
168 115         280 $$pos++;
169             }
170             }
171             }
172             else {
173 76         151 $$pos=length $$template;
174 76         491 return Dotiac::DTL::Tag->new(substr $$template,$start);
175             }
176            
177             }
178 0           my $parser=$Dotiac::DTL::PARSER->new();
179 0           my @args=($template,$pos);
180 0 0         push @args,$found if $found;
181 0 0         push @args,@end if @end;
182 0           return $parser->parse(@args);
183             }
184            
185            
186             1;
187             __END__