blib/lib/CGI/okTemplate.pm

filter: Statementstmt 91.3% 137 / 150 Branchbran 59.6% 31 / 52 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 81.2% 190 / 234 CC 33 SCAR 18.3
CC
33
Cov
82%
___parse_data
11.2
___clean_up_dirs
7.3
___read_includes
6.9
CRAP 6.3
↑ Summary
1 package CGI::okTemplate;
2  
3 1 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11  
12 1 our @ISA = qw(Exporter);
13  
14 # Items to export into callers namespace by default. Note: do not export
15 # names by default without a very good reason. Use EXPORT_OK instead.
16 # Do not simply export all your public functions/methods/constants.
17  
18 # This allows declaration use CGI::okTemplate ':all';
19 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
20 # will save memory.
21 1 our %EXPORT_TAGS = ( 'all' => [ qw(
22
23 ) ] );
24  
25 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26  
27 1 our @EXPORT = qw(
28
29 );
30  
31 1 our $VERSION = '0.03';
32  
33 1 our $debug=0;
34  
35 # Preloaded methods go here.
36  
37 1 ▶sub new {
Subroutine new: called CC=6 SCAR=18.9
Pod documented
38 1 my $class = shift;
39 1 my $self = {};
40  
41 1 bless($self,$class);
42 1 $self->{___params} = {@_};
43 1 ▶ $debug = 1 if $self->{___params}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"___params"}{"Debug"} 0 1 1
44 1 ▶ $self->{___params}->{BlockTag} = 'TemplateBlock' unless $self->{___params}->{BlockTag};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"___params"}{"BlockTag"} 1 0 1
45 1 ▶ if($self->{___params}->{RootDir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'___params'}{'RootDir'}) { } 0 1 1
46 0 $self->{___params}->{RootDir} = File::Spec->rel2abs($self->{___params}->{RootDir});
47 } else {
48 1 $self->{___params}->{RootDir} = File::Spec->rel2abs(File::Spec->curdir());
49 }
50 1 $self->{___params}->{RootDir} = ___clean_up_dirs(File::Spec->canonpath($self->{___params}->{RootDir}));
51 1 ▶ confess "Root Dir '$self->{___params}->{RootDir}' does not exists\n" unless (-d $self->{___params}->{RootDir});
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"___params"}{"RootDir"} 0 1 1
52 1 ▶ $self->read_template($self->{___params}->{File}) if($self->{___params}->{File});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"___params"}{"File"} 0 1 1
53  
54 1 return $self;
55 }
56  
57 1 ▶sub read_template {
Subroutine read_template: called CC=5 SCAR=16.7
Pod documented
58 1 my $self = shift;
59 1 my $file = shift;
60  
61 1 ▶ unless($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 1 1
62 0 confess "Parameter 'File' undefined in function 'read_template.'\n";
63 }
64  
65 1 ▶ unless(File::Spec->file_name_is_absolute( $file )) { # make absolute path to file
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($file)) 1 0 1
66 1 $file = File::Spec->catfile($self->{___params}->{RootDir},$file);
67 }
68  
69 1 $file = ___clean_up_dirs(File::Spec->canonpath($file)); # delete up dirs from path
70 1 ▶ confess "File '$file' does not exists\n" unless (-r $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 1 1
71  
72 1 ▶ unless(___under_root($self->{___params}->{RootDir},$file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (___under_root($self->{"___params"}{"RootDir"}, $file)) 0 1 1
73 0 confess "File '$file' does not under root dir '$self->{___params}->{RootDir}'\n";
74 }
75
76 1 $self->{___params}->{File} = $file;
77  
78 1 local($/) = undef;
79 1 open IN, $file;
80 1 my $in = <IN>;
81 1 close IN;
82  
83 1 my $cur_path = ___get_dir($file);
84 1 $in = ___read_includes($in,$cur_path,$self->{___params}->{RootDir});
85  
86 1 $self->{___template___} = ___parse_template($in,$self->{___params}->{BlockTag},$cur_path,$self->{___params}->{RootDir});
87 }
88  
89 5 ▶sub ___read_includes {
Subroutine ___read_includes: called CC=6 SCAR=19.3
90 5 my $text = shift;
91 5 my $cur_path = shift;
92 5 my $root_path = shift;
93 5 ▶ while($text =~ m/<!--Include\s+(.+?)-->/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /<!--Include\s+(.+?)-->/) 4 5 9
94 4 my $pre_inc = $PREMATCH; # text before include
95 4 my $post_inc = $POSTMATCH; # text after include
96 4 my $include_filename = $1; # got include filename
97 4 ▶ unless(File::Spec->file_name_is_absolute( $include_filename )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($include_filename)) 4 0 4
98 4 $include_filename = File::Spec->catfile($cur_path,$include_filename);
99 }
100 4 $include_filename = ___clean_up_dirs(File::Spec->canonpath($include_filename)); # delete up dirs from path
101 4 ▶ unless(___under_root($root_path,$include_filename) && (-r $include_filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (___under_root($root_path, $include_filename) and -r $include_filename) { } 4 0 4
Condition33%1/3
___under_root($root_path, $include_filename) and -r $include_filename
!ll&&!rl&&r
004
MC/DC0%0/2
___under_root($root_path, $include_filename) and -r $include_filename
___under_root($root_path, $include_filename) -r $include_filename
Truth table33%1/3
A and B
A= ___under_root($root_path, $include_filename)B= -r $include_filename
ABresult
0X0
100
111
102 0 $text = $pre_inc;
103 0 ▶ if($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::okTemplate::debug) 0 0 0
104 0 $text .=
105 "File '$include_filename' can't be included" .
106 " in this document because of wrong file path";
107 }
108 0 $text .= $post_inc;
109 } else {
110 4 my $in = '';
111 4 local($/) = undef;
112 4 open IN, "< $include_filename";
113 4 $in = <IN>;
114 4 close IN;
115 4 my $new_cur_path = ___get_dir($include_filename);
116 4 $text = $pre_inc . ___read_includes($in,$new_cur_path,$root_path) . $post_inc;
117 }
118 }
119 5 return $text;
120 }
121  
122 6 ▶sub ___clean_up_dirs {
Subroutine ___clean_up_dirs: called CC=5 SCAR=19.9
123 6 my $path = shift;
124 6 my ($vol, $dirs, $file) = File::Spec->splitpath( $path, 1 );
125 6 my @path = File::Spec->splitdir( $dirs );
126 6 my $i;
127  
128 6 ▶ for($i = 0; $i<$#path; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#path) 44 6 50
129 44 ▶ if($path[$i] eq File::Spec->updir()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path[$i] eq "File::Spec"->updir) 0 44 44
130 0 ▶ if($i) { # not first folder
Branch: 1 branch
BranchTrueFalseTotal
if ($i) { } 0 0 0
131 0 $i--; # go 1 step up
132 } else { # first folder
133 0 shift @path;
134 }
135 0 redo;
136 }
137 44 ▶ if($path[$i+1] eq File::Spec->updir()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path[$i + 1] eq "File::Spec"->updir) 0 44 44
138 0 splice @path, $i, 2;
139 0 redo;
140 }
141 }
142  
143 6 return File::Spec->catpath($vol,File::Spec->catdir(@path),$file);
144 }
145  
146  
147 5 ▶sub ___under_root {
Subroutine ___under_root: called CC=1 SCAR=0.0
148 5 my $root = shift;
149 5 my $file = shift;
150  
151 5 return $file =~ /^($root)/;
152 }
153  
154 5 ▶sub ___get_dir {
Subroutine ___get_dir: called CC=1 SCAR=0.0
155 5 my $path = shift;
156 5 my ($vol, $dirs, $file) = File::Spec->splitpath( $path );
157  
158 5 return File::Spec->catpath($vol,$dirs,''); # remove file
159 }
160  
161  
162  
163 1 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.2
Pod documented
164 1 my $self = shift;
165 1 ▶ my $data = shift || {};
Condition50%1/2
shift() || {}
l!l
10
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
166 1 my $res;
167  
168 1 $res = ___parse_data($self->{___template___},$data,$self->{___params}->{BlockTag});
169 1 ▶ $res =~ s/<%.*?%>//g unless $self->{___params}->{NoClean};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"___params"}{"NoClean"} 1 0 1
170  
171 1 return $res;
172 }
173  
174 5 ▶sub ___parse_template {
Subroutine ___parse_template: called CC=3 SCAR=11.0
175 5 my $text = shift;
176 5 my $block_tag = shift;
177 5 my $cur_path = shift;
178 5 my $root_path = shift;
179 5 my $tmp = {___text___=>'',___blocks___=>{}};
180 5 ▶ while($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 9 5 14
181 9 ▶ if($text =~ m/<!--($block_tag)\s+(.+?)-->(.*?)<!--\/\1\s+?\2-->/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ m[<!--($block_tag)\s+(.+?)-->(.*?)<!--/\1\s+?\2-->]s) { } 4 5 9
182 4 my $block_name = $2;
183 4 my $block_text = $3;
184 4 $tmp->{___text___} .= $PREMATCH;
185 4 $text = $POSTMATCH;
186 4 $tmp->{___text___} .= "<!--${block_tag}_parsed $block_name-->";
187 4 $tmp->{___blocks___}->{$block_name} = ___parse_template($block_text,$block_tag);
188 } else {
189 5 $tmp->{___text___} .= $text;
190 5 $text = undef;
191 }
192 }
193 5 return $tmp;
194 }
195  
196 6 ▶sub ___parse_data {
Subroutine ___parse_data: called CC=11 SCAR=24.1
197 6 ▶ my $template = shift || {};
Condition50%1/2
shift() || {}
l!l
60
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
198 6 ▶ my $data = shift || {};
Condition50%1/2
shift() || {}
l!l
60
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
199 6 my $block_tag = shift;
200 6 my $text_level = $template->{___text___};
201 6 my $text_result = '';
202 6 my %data = ();
203 6 my %blocks = ();
204 6 my $key;
205 6 foreach $key (keys %$data) {
206 11 ▶ if(ref $data->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{$key}) { } 4 7 11
207 4 $blocks{$key} = $data->{$key};
208 } else {
209 7 $data{$key} = $data->{$key};
210 }
211 }
212 6 ▶ while($text_level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_level) 10 6 16
213 10 ▶ if($text_level =~ m/<!--(${block_tag}_parsed) (.+?)-->/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_level =~ /<!--(${block_tag}_parsed) (.+?)-->/s) { } 4 6 10
214 4 my $block_name = $2;
215 4 my $block;
216 4 $text_result .= $PREMATCH;
217 4 $text_level = $POSTMATCH;
218 4 foreach $block (@{$blocks{$block_name}}) {
219 5 ▶ $text_result .= "<!--BlockParsed $block_name-->" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::okTemplate::debug 0 5 5
220 5 $text_result .= ___parse_data($template->{___blocks___}->{$block_name},$block,$block_tag);
221 5 ▶ $text_result .= "<!--/BlockParsed $block_name-->" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::okTemplate::debug 0 5 5
222 }
223 } else {
224 6 $text_result .= $text_level;
225 6 $text_level = undef;
226 }
227 }
228  
229 # put local macro value or leave for global value changes
230 6 ▶ $text_result =~ s/<%\s*(.+?)\s*%>/(exists $data{$1}) ? $data{$1} : "<% $1 %>"/ge;
Branch: 1 branch
BranchTrueFalseTotal
exists $data{$1} ? : 7 0 7
231 6 return $text_result;
232 }
233  
234 1 1;
235 __END__
↑ Summary