| 1 | package Template::Qtpl; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | ########################################################################### | ||||||||||||||||||||||||||||
| 4 | # Copyright (c) 2000 barnabÓs debreceni [cranx@scene.hu] | ||||||||||||||||||||||||||||
| 5 | # Original PHP version. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Copyright(c) 2000-2001 Alexey Presnyakov [alexey_pres@sourceforge.net] | ||||||||||||||||||||||||||||
| 8 | # Perl port and extension. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # This library is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 11 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # $Author: alexey_pres $, $Date: 2001/10/18 09:39:24 $ | ||||||||||||||||||||||||||||
| 14 | # $Revision: 1.2 $ | ||||||||||||||||||||||||||||
| 15 | ########################################################################### | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | $Template::Qtpl::VERSION = '0.5'; | |||||||||||||||||||||||||||
| 23 | 1 | $Template::Qtpl::file_delim = "\{FILE\s*\"(.+?)\"\s*\}"; | |||||||||||||||||||||||||||
| 24 | 1 | $Template::Qtpl::block_start_delim = '<!-- '; | |||||||||||||||||||||||||||
| 25 | 1 | $Template::Qtpl::block_end_delim = '-->'; | |||||||||||||||||||||||||||
| 26 | 1 | $Template::Qtpl::block_start_word = 'BEGIN:'; | |||||||||||||||||||||||||||
| 27 | 1 | $Template::Qtpl::block_end_word = 'END:'; | |||||||||||||||||||||||||||
| 28 | 1 | $Template::Qtpl::var_begin = '{'; | |||||||||||||||||||||||||||
| 29 | 1 | $Template::Qtpl::var_end = '}'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | ############################ variable set functions ################ | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub block_start_delim { | ||||||||||||||||||||||||||
|
Subroutine
block_start_delim: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 0 | my ($v) = shift; | |||||||||||||||||||||||||||
| 35 | 0 | my $ret; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | $v = shift if ((ref $v) eq 'Template::Qtpl'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | $ret = $Template::Qtpl::block_start_delim; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | $Template::Qtpl::block_start_delim = $v if (length $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | return $ret; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub block_end_delim { | ||||||||||||||||||||||||||
|
Subroutine
block_end_delim: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 0 | my ($v) = shift; | |||||||||||||||||||||||||||
| 44 | 0 | my $ret; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | $v = shift if ((ref $v) eq 'Template::Qtpl'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | $ret = $Template::Qtpl::block_end_delim; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | $Template::Qtpl::block_end_delim = $v if (length $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | return $ret; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub block_start_word { | ||||||||||||||||||||||||||
|
Subroutine
block_start_word: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | my ($v) = shift; | |||||||||||||||||||||||||||
| 53 | 0 | my $ret; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | $v = shift if ((ref $v) eq 'Template::Qtpl'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $ret = $Template::Qtpl::block_start_word; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | $Template::Qtpl::block_start_word = $v if (length $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | return $ret; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub block_end_word { | ||||||||||||||||||||||||||
|
Subroutine
block_end_word: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 0 | my ($v) = shift; | |||||||||||||||||||||||||||
| 62 | 0 | my $ret; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | $v = shift if ((ref $v) eq 'Template::Qtpl'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | $ret = $Template::Qtpl::block_end_word; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | $Template::Qtpl::block_end_word = $v if (length $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | return $ret; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub var_begin { | ||||||||||||||||||||||||||
|
Subroutine
var_begin: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | my ($v) = shift; | |||||||||||||||||||||||||||
| 71 | 0 | my $ret; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | $v = shift if ((ref $v) eq 'Template::Qtpl'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $ret = $Template::Qtpl::var_begin; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | $Template::Qtpl::block_start_word = $v if (length $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | return $ret; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub var_end { | ||||||||||||||||||||||||||
|
Subroutine
var_end: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 0 | my ($v) = shift; | |||||||||||||||||||||||||||
| 80 | 0 | my $ret; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | $v = shift if ((ref $v) eq 'Template::Qtpl'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $ret = $Template::Qtpl::var_end; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | $Template::Qtpl::block_end_word = $v if (length $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | return $ret; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub file_delim { | ||||||||||||||||||||||||||
|
Subroutine
file_delim: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 0 | my ($v) = shift; | |||||||||||||||||||||||||||
| 89 | 0 | my $ret; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | $v = shift if ((ref $v) eq 'Template::Qtpl'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $ret = $Template::Qtpl::file_delim; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | $Template::Qtpl::file_delim = $v if (length $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | return $ret; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | ############################ new ################################### | ||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 99 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 100 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 101 | 1 | $self->init(@_); | |||||||||||||||||||||||||||
| 102 | 1 | return $self; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | ############################## init ################################ | ||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 107 | 1 | my ($filename, $mainblock) = @_; | |||||||||||||||||||||||||||
| 108 | ##### set vars | ||||||||||||||||||||||||||||
| 109 | 1 | $self->{filecontents} = ''; | |||||||||||||||||||||||||||
| 110 | 1 | $self->{blocks} = {}; | |||||||||||||||||||||||||||
| 111 | 1 | $self->{parsed_blocks} = {}; | |||||||||||||||||||||||||||
| 112 | 1 | $self->{block_parse_order} = []; | |||||||||||||||||||||||||||
| 113 | 1 | $self->{sub_blocks} = {}; | |||||||||||||||||||||||||||
| 114 | 1 | $self->{VARS} = {}; | |||||||||||||||||||||||||||
| 115 | #this makes the delimiters look like: <!-- BEGIN: block_name --> if you use my syntax. | ||||||||||||||||||||||||||||
| 116 | 1 | $self->{NULL_STRING} = {''=>''}; | |||||||||||||||||||||||||||
| 117 | 1 | $self->{NULL_BLOCK} = {''=>''}; | |||||||||||||||||||||||||||
| 118 | 1 | $self->{mainblock} = ''; | |||||||||||||||||||||||||||
| 119 | 1 | $self->{ERROR} = ''; | |||||||||||||||||||||||||||
| 120 | 1 | $self->{AUTORESET} = 1; | |||||||||||||||||||||||||||
| 121 | ####### init variables | ||||||||||||||||||||||||||||
| 122 | 1 | $self->{mainblock} = $mainblock; | |||||||||||||||||||||||||||
| 123 | 1 | $self->{FILENAME} = $filename; | |||||||||||||||||||||||||||
| 124 | 1 | $self->{filecontents} = $self->r_getfile($filename); | |||||||||||||||||||||||||||
| 125 | 1 | $self->{blocks} = $self->maketree($self->{filecontents}, $mainblock); | |||||||||||||||||||||||||||
| 126 | 1 | $self->scan_globals(); | |||||||||||||||||||||||||||
| 127 | 1 | return $self; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | ▶ | sub assign { | ||||||||||||||||||||||||||
|
Subroutine
assign: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 132 | 3 | my ($k, $v); | |||||||||||||||||||||||||||
| 133 | 3 | ▶ | while (scalar(@_)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 3 | $k = shift; | |||||||||||||||||||||||||||
| 135 | 3 | $v = shift; | |||||||||||||||||||||||||||
| 136 | 3 | $self->{VARS}{$k} = $v; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 6 | ▶ | sub get_var{ | ||||||||||||||||||||||||||
|
Subroutine
get_var: called CC=6 SCAR=18.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 141 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 6 | my $v = shift; #variable name for subst | |||||||||||||||||||||||||||
| 143 | 6 | my $need_encode = 0; | |||||||||||||||||||||||||||
| 144 | 6 | ▶ | if ($v =~ /^\~(.+)$/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | $v = $1; | |||||||||||||||||||||||||||
| 146 | 0 | $need_encode = 1; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 6 | my @sub = split(/\./,$v); | |||||||||||||||||||||||||||
| 149 | 6 | my $var; | |||||||||||||||||||||||||||
| 150 | 6 | ▶ | if ($sub[0] =~ /^main::(.+)$/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 1 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | 2 | my $varname = $sub[0]; | |||||||||||||||||||||||||||
| 153 | 2 | ▶ | if (scalar(@sub)>1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 2 | $var = \%$varname; | |||||||||||||||||||||||||||
| 155 | 2 | shift @sub; | |||||||||||||||||||||||||||
| 156 | 2 | goto FROM_GLOBALS; | |||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||
| 158 | 0 | $var = $$varname; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } else { | ||||||||||||||||||||||||||||
| 161 | 4 | $var=$self->{VARS}; | |||||||||||||||||||||||||||
| 162 | 6 | FROM_GLOBALS: | |||||||||||||||||||||||||||
| 163 | my $sv; | ||||||||||||||||||||||||||||
| 164 | 6 | foreach $sv (@sub){ | |||||||||||||||||||||||||||
| 165 | 9 | $var = $var->{$sv}; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 6 | ▶ | if ($need_encode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | $var =~ s/\"/\"/g; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 6 | return $var; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub _if_subst { | ||||||||||||||||||||||||||
|
Subroutine
_if_subst: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 175 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 176 | 0 | my ($if, $body) = @_; | |||||||||||||||||||||||||||
| 177 | 0 | my $if_val = $self->get_var($if); | |||||||||||||||||||||||||||
| 178 | 0 | my @parts = split(/$Template::Qtpl::var_begin[E]LSE$Template::Qtpl::var_end/, $body); | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | if (length $if_val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | return $parts[0]; | |||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||
| 182 | 0 | return $parts[1]; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 4 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=15 SCAR=28.1
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 188 | 4 | my ($bname) = @_; | |||||||||||||||||||||||||||
| 189 | 4 | my $copy = $self->{blocks}->{$bname}; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | $self->set_error("parse: blockname [$bname] does not exist") | ||||||||||||||||||||||||||||
| 192 | 4 | ▶ | unless (defined($self->{blocks}->{$bname})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 4 | ▶ | while ($self->{blocks}->{$bname} =~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | /$Template::Qtpl::var_begin([\~\w\.\:]+)$Template::Qtpl::var_end/g) { | ||||||||||||||||||||||||||||
| 196 | 9 | my $v = $1; | |||||||||||||||||||||||||||
| 197 | 9 | ▶ | next if ($v =~ /^IF\s+/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 9 | ▶ | next if ($v =~ /^(ELSE|ENDIF)$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 9 | ▶ | if ($v =~ /^_BLOCK_\.(.+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 3 | my $bname2=$1; | |||||||||||||||||||||||||||
| 201 | 3 | my $var=$self->{parsed_blocks}->{$bname2}; | |||||||||||||||||||||||||||
| 202 | my $nul=(!exists($self->{NULL_BLOCK}->{$bname2})) ? | ||||||||||||||||||||||||||||
| 203 | $self->{NULL_BLOCK}{''} : | ||||||||||||||||||||||||||||
| 204 | 3 | ▶ | $self->{NULL_BLOCK}->{$bname2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 3 | ▶ | $var=(!defined($var))?$nul:$var; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 3 | $copy =~ s/$Template::Qtpl::var_begin$v$Template::Qtpl::var_end/$var/eg; | |||||||||||||||||||||||||||
| 207 | } else { | ||||||||||||||||||||||||||||
| 208 | 6 | my $var = $self->get_var($v); | |||||||||||||||||||||||||||
| 209 | my $nul=(!exists($self->{NULL_STRING}{$v})) ? | ||||||||||||||||||||||||||||
| 210 | $self->{NULL_STRING}{''} : | ||||||||||||||||||||||||||||
| 211 | 6 | ▶ | $self->{NULL_STRING}{$v}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 6 | ▶ | $var=(!length($var))?$nul:$var; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 6 | ▶ | $copy =~ s/$Template::Qtpl::var_begin$v$Template::Qtpl::var_end/$var/eg | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | unless ($v =~ /^\d/); | ||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | # parse if tags | ||||||||||||||||||||||||||||
| 218 | 0 | $copy =~ s/$Template::Qtpl::var_begin[I]F\s+(.+?)\s*$Template::Qtpl::var_end(.*?)$Template::Qtpl::var_begin[E]NDIF$Template::Qtpl::var_end/$self->_if_subst($1, $2)/egs; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 4 | my ($bname_new) = split(/\:/, $bname); | |||||||||||||||||||||||||||
| 221 | #save as to parsed | ||||||||||||||||||||||||||||
| 222 | 4 | $self->{parsed_blocks}->{$bname_new}.=$copy; | |||||||||||||||||||||||||||
| 223 | #reset sub-blocks | ||||||||||||||||||||||||||||
| 224 | 4 | my ($bname3); | |||||||||||||||||||||||||||
| 225 | 4 | ▶ | if ($self->{AUTORESET}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 4 | ▶ | if (exists($self->{sub_blocks}->{$bname_new})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 4 | foreach $bname3 (@{$self->{sub_blocks}->{$bname_new}}){ | |||||||||||||||||||||||||||
| 228 | 7 | ▶ | next unless length ($bname_new); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 7 | $self->reset($bname3); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | #***[ rparse ]**************************************************************/ | ||||||||||||||||||||||||||||
| 236 | #* returns the parsed text for a block, including all sub-blocks. | ||||||||||||||||||||||||||||
| 237 | 0 | ▶ | sub rparse { | ||||||||||||||||||||||||||
|
Subroutine
rparse: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 238 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 239 | 0 | my $bname = shift; | |||||||||||||||||||||||||||
| 240 | 0 | ▶ | if (exists($self->{sub_blocks}->{$bname})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 0 | my ($bname3); | |||||||||||||||||||||||||||
| 242 | 0 | foreach $bname3 (@{$self->{sub_blocks}->{$bname}}){ | |||||||||||||||||||||||||||
| 243 | 0 | ▶ | next if (!length $bname3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 0 | $self->rparse($bname3); | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 0 | $self->parse($bname); | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | #***[ text ]****************************************************************/ | ||||||||||||||||||||||||||||
| 251 | #* returns the parsed text for a block | ||||||||||||||||||||||||||||
| 252 | 0 | ▶ | sub text { | ||||||||||||||||||||||||||
|
Subroutine
text: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 253 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 254 | 0 | my ($bname) = @_; | |||||||||||||||||||||||||||
| 255 | 0 | ▶ | if (!length($bname)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 0 | $bname=$self->{mainblock}; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | 0 | return $self->{parsed_blocks}->{$bname}; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | #/***[ out ]*****************************************************************/ | ||||||||||||||||||||||||||||
| 262 | #/* prints the parsed text | ||||||||||||||||||||||||||||
| 263 | 0 | ▶ | sub out { | ||||||||||||||||||||||||||
|
Subroutine
out: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 264 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 265 | 0 | my ($bname) = @_; | |||||||||||||||||||||||||||
| 266 | 0 | print $self->text($bname); | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | #/***[ reset ]***************************************************************/ | ||||||||||||||||||||||||||||
| 270 | #/* resets the parsed text | ||||||||||||||||||||||||||||
| 271 | 7 | ▶ | sub reset { | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 272 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 273 | 7 | my ($bname) = @_; | |||||||||||||||||||||||||||
| 274 | 7 | $self->{parsed_blocks}->{$bname}=''; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | #***[ parsed ]**************************************************************/ | ||||||||||||||||||||||||||||
| 278 | #* returns true if block was parsed, false if not | ||||||||||||||||||||||||||||
| 279 | 0 | ▶ | sub parsed { | ||||||||||||||||||||||||||
|
Subroutine
parsed: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 280 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 281 | 0 | my ($bname) = @_; | |||||||||||||||||||||||||||
| 282 | 0 | return (defined($self->{parsed_blocks}->{$bname})); | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | #***[ SetNullString ]*******************************************************/ | ||||||||||||||||||||||||||||
| 286 | #* sets the string to replace in case the var was not assigned | ||||||||||||||||||||||||||||
| 287 | 0 | ▶ | sub SetNullString { | ||||||||||||||||||||||||||
|
Subroutine
SetNullString: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 288 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 289 | 0 | my ($str, $varname) = @_; | |||||||||||||||||||||||||||
| 290 | 0 | $self->{NULL_STRING}{$varname}=$str; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | #***[ SetNullBlock ]********************************************************/ | ||||||||||||||||||||||||||||
| 294 | #* sets the string to replace in case the block was not parsed | ||||||||||||||||||||||||||||
| 295 | 0 | ▶ | sub SetNullBlock { | ||||||||||||||||||||||||||
|
Subroutine
SetNullBlock: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 296 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 297 | 0 | my ($str, $bname) = @_; | |||||||||||||||||||||||||||
| 298 | 0 | $self->{NULL_BLOCK}{$bname}=$str; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | #***[ set_autoreset ]*******************************************************/ | ||||||||||||||||||||||||||||
| 302 | #* sets AUTORESET to 1. (default is 1) | ||||||||||||||||||||||||||||
| 303 | # if set to 1, parse() automatically resets the parsed blocks' sub blocks | ||||||||||||||||||||||||||||
| 304 | # (for multiple level blocks) | ||||||||||||||||||||||||||||
| 305 | 0 | ▶ | sub set_autoreset { | ||||||||||||||||||||||||||
|
Subroutine
set_autoreset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 306 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 307 | 0 | $self->{AUTORESET}=1; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | #/***[ clear_autoreset ]*****************************************************/ | ||||||||||||||||||||||||||||
| 311 | #/* | ||||||||||||||||||||||||||||
| 312 | # sets AUTORESET to 0. (default is 1) | ||||||||||||||||||||||||||||
| 313 | # if set to 1, parse() automatically resets the parsed blocks' sub blocks | ||||||||||||||||||||||||||||
| 314 | # (for multiple level blocks) | ||||||||||||||||||||||||||||
| 315 | 0 | ▶ | sub clear_autoreset { | ||||||||||||||||||||||||||
|
Subroutine
clear_autoreset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 316 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 317 | 0 | $self->{AUTORESET}=0; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | #/***[ scan_globals ]********************************************************/ | ||||||||||||||||||||||||||||
| 321 | #/* | ||||||||||||||||||||||||||||
| 322 | # scans global variables | ||||||||||||||||||||||||||||
| 323 | #*/ | ||||||||||||||||||||||||||||
| 324 | 1 | ▶ | sub scan_globals { | ||||||||||||||||||||||||||
|
Subroutine
scan_globals: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 325 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 326 | 1 | $self->assign("PHP",\%ENV); | |||||||||||||||||||||||||||
| 327 | #* access global variables as {ENV.HTTP_HOST} in your template! */ | ||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | #/****** | ||||||||||||||||||||||||||||
| 331 | # | ||||||||||||||||||||||||||||
| 332 | # WARNING | ||||||||||||||||||||||||||||
| 333 | # PUBLIC FUNCTIONS BELOW THIS LINE DIDN'T GET TESTED | ||||||||||||||||||||||||||||
| 334 | # | ||||||||||||||||||||||||||||
| 335 | #******/ | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | #/***************************************************************************/ | ||||||||||||||||||||||||||||
| 339 | #/***[ private stuff ]*******************************************************/ | ||||||||||||||||||||||||||||
| 340 | #/***************************************************************************/ | ||||||||||||||||||||||||||||
| 341 | #/***[ maketree ]************************************************************/ | ||||||||||||||||||||||||||||
| 342 | #/* | ||||||||||||||||||||||||||||
| 343 | # generates the array containing to-be-parsed stuff: | ||||||||||||||||||||||||||||
| 344 | # $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc. | ||||||||||||||||||||||||||||
| 345 | # also builds the reverse parse order. | ||||||||||||||||||||||||||||
| 346 | #*/ | ||||||||||||||||||||||||||||
| 347 | 1 | ▶ | sub maketree { | ||||||||||||||||||||||||||
|
Subroutine
maketree: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 348 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 349 | 1 | my ($con, $block) = @_; | |||||||||||||||||||||||||||
| 350 | 1 | my @con2=split($Template::Qtpl::block_start_delim,$con); | |||||||||||||||||||||||||||
| 351 | 1 | my $level=0; | |||||||||||||||||||||||||||
| 352 | 1 | my @block_names = (''); | |||||||||||||||||||||||||||
| 353 | 1 | my %blocks=(); | |||||||||||||||||||||||||||
| 354 | 1 | my $parent_name; | |||||||||||||||||||||||||||
| 355 | 1 | my ($k,$v, @res); | |||||||||||||||||||||||||||
| 356 | 1 | my %added_to_parent = (); | |||||||||||||||||||||||||||
| 357 | 1 | foreach $v (@con2){ | |||||||||||||||||||||||||||
| 358 | 9 | my $patt="($Template::Qtpl::block_start_word|$Template::Qtpl::block_end_word)\\s*([\\w\\.\\:]+)\\s*$Template::Qtpl::block_end_delim(.*)"; | |||||||||||||||||||||||||||
| 359 | 9 | ▶ | if ($v =~ /$patt/is) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | # $res[1] = BEGIN or END | ||||||||||||||||||||||||||||
| 361 | # $res[2] = block name | ||||||||||||||||||||||||||||
| 362 | # $res[3] = kinda content | ||||||||||||||||||||||||||||
| 363 | 8 | $res[1] = $1; | |||||||||||||||||||||||||||
| 364 | 8 | $res[2] = $2; | |||||||||||||||||||||||||||
| 365 | 8 | $res[3] = $3; | |||||||||||||||||||||||||||
| 366 | 8 | ▶ | if ($res[1] eq $Template::Qtpl::block_start_word) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 367 | 4 | ▶ | $parent_name=@block_names ? join(".", @block_names) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 4 | $parent_name =~ s/^\.//; #hack | |||||||||||||||||||||||||||
| 369 | 4 | $block_names[++$level ] = $res[2]; | |||||||||||||||||||||||||||
| 370 | #/* add one level - array("main","table","row")*/ | ||||||||||||||||||||||||||||
| 371 | 4 | my @block_nm = @block_names; | |||||||||||||||||||||||||||
| 372 | 4 | shift @block_nm; | |||||||||||||||||||||||||||
| 373 | 4 | my $cur_block_name=join(".",@block_nm); | |||||||||||||||||||||||||||
| 374 | #/* make block name (main.table.row) */ | ||||||||||||||||||||||||||||
| 375 | 4 | push @{$self->{block_parse_order}}, $cur_block_name; | |||||||||||||||||||||||||||
| 376 | #/* build block parsing order (reverse) */ | ||||||||||||||||||||||||||||
| 377 | 4 | $blocks{$cur_block_name}.=$res[3]; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 4 | my ($cur_block_name_new) = split(/\:/, $cur_block_name); | |||||||||||||||||||||||||||
| 380 | #/* add contents */ | ||||||||||||||||||||||||||||
| 381 | #/* add {_BLOCK_.blockname} string to parent block */ | ||||||||||||||||||||||||||||
| 382 | 4 | ▶ | if (!$added_to_parent{$cur_block_name_new}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | 4 | $blocks{$parent_name}.=$Template::Qtpl::var_begin."_BLOCK_.$cur_block_name_new".$Template::Qtpl::var_end; | |||||||||||||||||||||||||||
| 384 | #/* store sub block names for autoresetting | ||||||||||||||||||||||||||||
| 385 | # and recursive parsing */ | ||||||||||||||||||||||||||||
| 386 | 4 | push @{$self->{sub_blocks}->{$parent_name}}, | |||||||||||||||||||||||||||
| 387 | $cur_block_name_new; | ||||||||||||||||||||||||||||
| 388 | #/* store sub block names for autoresetting */ | ||||||||||||||||||||||||||||
| 389 | 4 | push @{$self->{sub_blocks}->{$cur_block_name_new}},''; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | 4 | $added_to_parent{$cur_block_name_new}++; | |||||||||||||||||||||||||||
| 392 | } elsif ($res[1] eq $Template::Qtpl::block_end_word) { | ||||||||||||||||||||||||||||
| 393 | 4 | splice(@block_names, $level--, 1); | |||||||||||||||||||||||||||
| 394 | 4 | $parent_name=join(".",@block_names); | |||||||||||||||||||||||||||
| 395 | 4 | $parent_name =~ s/^\.//; #hack | |||||||||||||||||||||||||||
| 396 | 4 | $blocks{$parent_name}.=$res[3]; | |||||||||||||||||||||||||||
| 397 | #/* add rest of block to parent block */ | ||||||||||||||||||||||||||||
| 398 | } else { #if there is not block | ||||||||||||||||||||||||||||
| 399 | 0 | $parent_name=join(".",@block_names); | |||||||||||||||||||||||||||
| 400 | 0 | $parent_name =~ s/^\.//; #hack | |||||||||||||||||||||||||||
| 401 | 0 | $blocks{$parent_name}.=$Template::Qtpl::block_start_delim . $v; | |||||||||||||||||||||||||||
| 402 | } } | ||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | 1 | return \%blocks; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | #/***[ error stuff ]*********************************************************/ | ||||||||||||||||||||||||||||
| 410 | #/* | ||||||||||||||||||||||||||||
| 411 | # sets and gets error | ||||||||||||||||||||||||||||
| 412 | #*/ | ||||||||||||||||||||||||||||
| 413 | 0 | ▶ | sub get_error { | ||||||||||||||||||||||||||
|
Subroutine
get_error: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 414 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 415 | 0 | ▶ | return ($self->{ERROR} eq '') ? 0 : $self->{ERROR}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | 0 | ▶ | sub set_error { | ||||||||||||||||||||||||||
|
Subroutine
set_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 420 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 421 | 0 | my $str = shift; | |||||||||||||||||||||||||||
| 422 | 0 | $self->{ERROR}=$str; | |||||||||||||||||||||||||||
| 423 | 0 | die "$self->{ERROR}\n"; | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | #/***[ getfile ]*************************************************************/ | ||||||||||||||||||||||||||||
| 427 | #/* returns the contents of a file | ||||||||||||||||||||||||||||
| 428 | 1 | ▶ | sub getfile { | ||||||||||||||||||||||||||
|
Subroutine
getfile: called CC=5 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 429 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 430 | 1 | my $file = shift; | |||||||||||||||||||||||||||
| 431 | 1 | ▶ | if (!length($file)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | 0 | $self->set_error("Empty file name!"); | |||||||||||||||||||||||||||
| 433 | 0 | return ''; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | #find path of original template | ||||||||||||||||||||||||||||
| 436 | 1 | ▶ | if (($self->{FILENAME} ne $file) && dirname($self->{FILENAME})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"FILENAME"} ne $file and dirname($self->{"FILENAME"})
MC/DC0%0/2
$self->{"FILENAME"} ne $file and dirname($self->{"FILENAME"})
$self->{"FILENAME"} ne $file
dirname($self->{"FILENAME"})
Truth table33%1/3
A and B
A= $self->{"FILENAME"} ne $fileB= dirname($self->{"FILENAME"})
| |||||||||||||||||||||||||||||
| 437 | 0 | $file = dirname($self->{FILENAME}) . '/' .$file; | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | 1 | ▶ | unless (open(XTPLFILE, $file)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 440 | 0 | $self->set_error("Cannot open file: $file or file not exists"); | |||||||||||||||||||||||||||
| 441 | 0 | return ''; | |||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | 1 | my @file_text = <XTPLFILE>; | |||||||||||||||||||||||||||
| 444 | 1 | close(XTPLFILE); | |||||||||||||||||||||||||||
| 445 | 1 | return join('',@file_text); | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | #/***[ r_getfile ]***********************************************************/ | ||||||||||||||||||||||||||||
| 449 | #/* | ||||||||||||||||||||||||||||
| 450 | # recursively gets the content of a file with {FILE "filename.tpl"} directives | ||||||||||||||||||||||||||||
| 451 | #*/ | ||||||||||||||||||||||||||||
| 452 | 1 | ▶ | sub r_getfile($file) { | ||||||||||||||||||||||||||
|
Subroutine
r_getfile: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 453 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 454 | 1 | my $file = shift; | |||||||||||||||||||||||||||
| 455 | 1 | my $text=$self->getfile($file); | |||||||||||||||||||||||||||
| 456 | 1 | ▶ | while ($text =~ /(\{FILE\s*")(.+?)(\"\s*\})/g){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 457 | 0 | my $full = $1.$2.$3; | |||||||||||||||||||||||||||
| 458 | 0 | my $text2=$self->getfile($2); | |||||||||||||||||||||||||||
| 459 | 0 | $text =~ s/$full/$text2/gi; | |||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||
| 461 | 1 | return $text; | |||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | 1 | 1; | |||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | __END__ | ||||||||||||||||||||||||||||