XPP/PreParse.pm

filter: Statementstmt 53.3% 71 / 133 Branchbran 7.6% 2 / 26 Conditioncond 21.4% 9 / 42 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 69.5% 16 / 23 Podpod 100% 14 / 14 totaltotal 42.5% 112 / 263 CC 29 SCAR 26.2
CC
29
Cov
41%
parse_stag
42.0
parse_tag
30.0
__ANON__
30.0
CRAP 13.8
« XPP/Cache/Expiry/Duration.pm ↑ Summary XPP/Inline.pm »
1 =head1 NAME
2  
3 Apache::XPP::PreParse - XPP TAG Parser
4  
5 =cut
6  
7 =head1 SYNOPSIS
8  
9 use Apache::XPP::PreParse;
10 $preparsers = Apache::XPP::PreParse->parsers;
11 foreach (@{ $preparsers }) {
12 $preparsers->( \$text );
13 }
14  
15 =head1 REQUIRES
16  
17 Nothing
18  
19 =head1 EXPORTS
20  
21 Nothing
22  
23 =cut
24  
25 package Apache::XPP::PreParse;
26  
27 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use vars qw($AUTOLOAD @parsers $debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 $Apache::XPP::PreParse::REVISION = (qw$Revision: 1.19 $)[-1];
33 3 $Apache::XPP::PreParse::VERSION = '2.01';
34  
35 3 $debug = undef;
36 3 @parsers = qw( parser_xppcomment parser_xppcache parser_xppxinclude
37 parser_print parser_appmethod parser_xppforeach parser_xppshift
38 parser_xppwhile );
39 }
40  
41 =head1 DESCRIPTION
42  
43 Apache::XPP::PreParse handles pre parsing of an xpp page to convert 'tags' into valid
44 XPML. Tags are meant as a shortcut for code that might otherwise be burdensome or
45 confusing for and xpml author, such as ref checking before calling a method:
46 <xpp method="cities_popup_menu" obj="$app"> might be converted to:
47 <?xpp if (ref($app)) { print $app->cities_popup_menu; } ?>.
48  
49 =head1 METHODS
50  
51 =over
52  
53 =item C<parsers> ( )
54  
55 Returns a new parser object
56  
57 =cut
58 5 ▶sub parsers {
Subroutine parsers: called CC=2 SCAR=7.1
Pod documented
59 5 my $self = shift;
60 5 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
61 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 5 return \@{"${class}::parsers"};
63 } # END method parsers
64  
65  
66 =item C<add_parser> ( \&parser )
67  
68 Adds a parser to the list of registered pre-parsers.
69  
70 =cut
71 0 ▶sub add_parser {
Subroutine add_parser: not called CC=2 SCAR=17.9
Pod documented
72 0 my $proto = shift;
73 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
74 0 my $parser = shift;
75 0 push(@parsers, $parser);
76 } # END method add_parser
77  
78 =item C<parse_tag> ( $tag )
79  
80 Given an xpp TAG $tag, will return an array with two elements.
81 The first element is an array reference of the order of keys in
82 the tag. The second element is a hash reference to the key-value
83 pairs.
84  
85 =cut
86 0 ▶sub parse_tag {
Subroutine parse_tag: not called CC=5 SCAR=34.0
Pod documented
87 0 my $proto = shift;
88 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
89 0 my $tag = shift;
90 0 ▶ if ($tag =~ m/\<(.+)?\>/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /\<(.+)?\>/s) { } 0 0 0
91 0 my $tagcontent = $1;
92 0 ▶ warn "CONTENT: $tagcontent" if ($debug >= 2);
Branch: 1 branch
BranchTrueFalseTotal
if $Apache::XPP::PreParse::debug >= 2 0 0 0
93 0 my (@keys, %data);
94 0 ▶ while ($tagcontent =~ s/^(?:\s*)([^=\s]+)(?:\="(.*?)")?//so) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagcontent =~ s/^(?:\s*)([^=\s]+)(?:\="(.*?)")?//os) 0 0 0
95 0 push(@keys, $1);
96 0 $data{ $1 } = $2;
97 }
98
99 0 return (\@keys, \%data);
100 } else {
101 0 return undef;
102 }
103 } # END method parse_tag
104  
105 =item C<dtag> ( \$text, $tag, $subref )
106  
107 This is for processing tags with a start tag and seperate end tag.
108 All the data in between is sent to $subref->( $params, $data) where
109 $params is a hashref containing all the key value pairs in the
110 start tag and $data is the data contained in between the opening and
111 closing tag.
112  
113 =cut
114  
115 20 ▶sub dtag {
Subroutine dtag: called CC=6 SCAR=21.5
Pod documented
116 20 my $proto = shift;
117 20 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0200
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
118 20 my $text = shift;
119 20 my $tag = shift;
120 20 my $subref = shift;
121  
122 warn "Invalid paramaters passed!"
123 20 ▶ unless (defined(${ $text }) && defined($tag) && defined($subref));
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$text;} and defined $tag and defined $subref 0 20 20
Condition33%2/6
defined ${$text;} and defined $tag and defined $subref
!ll&&!rl&&r
0020
defined ${$text;} and defined $tag
!ll&&!rl&&r
0020
MC/DC0%0/3
defined ${$text;} and defined $tag and defined $subref
defined ${$text;} defined $tag defined $subref
Truth table25%1/4
A and B and C
A= defined ${$text;}B= defined $tagC= defined $subref
ABCresult
0XX0
10X0
1100
1111
124  
125 0 ▶ while ( ${ $text } =~ s/(<$tag(?:\s*\w+?=\".+?\")*>)((?:(?!<$tag|<\/$tag).)*(?:<$tag(?:\s*\w+?=\".+?\")*>.*?<\/$tag>)?(?:(?!<\/$tag>).)*?)<\/$tag>/my ($keys, $params) = $proto->parse_tag( $1 ); $subref->( $params, $2, @_ );/se ) {}
Branch: 1 branch
BranchTrueFalseTotal
if (${$text;} =~ s[(<$tag(?:\s*\w+?=\".+?\")*>)((?:(?!<$tag|</$tag).)*(?:<$tag(?:\s*\w+?=\".+?\")*>.*?</$tag>)?(?:(?!</$tag>).)*?)</$tag>][() = $proto->parse_tag($1); &$subref($params, $2, @_);]es) 0 20 20
126 }
127  
128 =item C<parse_stag>
129  
130 Called by stag to parse the matched tag with the supplied rules.
131  
132 =cut
133  
134 0 ▶sub parse_stag {
Subroutine parse_stag: not called CC=6 SCAR=37.4
Pod documented
135 0 my $proto = shift;
136 0 my ($keys, $data, $rrules) = @_;
137 0 my $replace;
138 0 ▶ foreach my $rule (ref($rrules->[0]) ? @{ $rrules } : ($rrules)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $rrules->[0] ? : 0 0 0
139 0 my ($template, $fields);
140 0 ▶ if ($#{ $rule } == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#{$rule;} == 2) { } 0 0 0
elsif ($#{$rule;} == 1) { } 0 0 0
141 0 my $opt = $rule->[0];
142 0 ($template, $fields) = @{ $rule }[1,2];
143 0 ▶ unless (exists $data->{ $opt }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $data->{$opt}) 0 0 0
144 0 next;
145 }
146 0 } elsif ($#{ $rule } == 1) {
147 0 ($template, $fields) = @{ $rule };
148 }
149
150 0 $replace = sprintf( $template, map { $data->{$_} } @{ $fields } );
151  
152 0 last;
153 }
154 0 return $replace;
155  
156 }
157  
158 =item C<stag> ( \$text, $match, \@replace_rules )
159  
160 Given a string to match, uses the replace rules to replace tags that contain
161 the $match with it's equivalent. See examples below for syntax of replace rules.
162  
163 =cut
164  
165 20 ▶sub stag {
Subroutine stag: called CC=2 SCAR=7.3
Pod documented
166 20 my $proto = shift;
167 20 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0200
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
168 20 my $text = shift;
169 20 my $match = shift;
170 20 my $rrules = shift;
171  
172 0 ${ $text } =~ s/($match(?:\s*\w+?(?:=\".+?\")?)*\s*>)/$proto->parse_stag($proto->parse_tag( $1 ), $rrules);/seg;
173
174 } # END method stag
175  
176 ###################### This is the old code. Until the above regex is fully tested lets
177 ###################### keep it.
178 # my ($spos, $epos);
179 # my @matches;
180 # my $i = -1;
181 # my $tagflag = 0;
182 # my $valflag = 0;
183 # my $tagmatch = 0;
184 # while (my $chr = substr( ${ $text }, ++$i, 1 )) {
185 # if (($chr eq '<') && !($valflag)) {
186 # warn "*** begin at [$i]\n" if ($debug >= 2);
187 # warn "\t<<< " . substr(${ $text }, $i, index(${ $text }, '>', $i)-$i+1) . " >>>\n" if ($debug >= 3);
188 # $tagflag ^= 1;
189 # $tagmatch = 0;
190 # $spos = $i;
191 # } elsif (($chr eq '"') && ($tagflag)) {
192 # if ($valflag) {
193 # warn "*** endVAL" if ($debug >= 3);
194 # } else {
195 # warn "*** beginVAL" if ($debug >= 3);
196 # }
197 # $valflag ^= 1;
198 # } elsif (($chr eq '>') && !($valflag)) {
199 # warn " end\n" if ($debug >= 2);
200 # $tagflag = 0;
201 # $epos = $i + 1;
202 # push(@matches, [$spos, $epos]) if ($tagmatch);
203 # }
204 #
205 # my $tmp = substr( ${ $text }, $i, length($match) );
206 # $tmp =~ s/\n/\\n/g;
207 # warn "\t(CHR, TAG, VAL, NEXTLEN, NEXT) ($chr, $tagflag, $valflag, " . length($match) . ", $tmp\n" if ($debug >= 3);
208 #
209 # if (($tagflag) && !($valflag) && (lc(substr( ${ $text }, $i, length($match) )) eq lc($match))) {
210 # warn "*** MATCHING TAG" if ($debug >= 2);
211 # $tagmatch = 1;
212 # }
213 # }
214 #
215 # @matches = sort { $b->[0] <=> $a->[0] } @matches;
216 # foreach (@matches) {
217 # my ($st,$en) = @{ $_ };
218 # my $replace = \substr( ${ $text }, $st, ($en - $st) );
219 # my ($keys, $data) = $proto->parse_tag( ${ $replace } );
220 # foreach my $rule (ref($rrules->[0]) ? @{ $rrules } : ($rrules)) {
221 # warn "RULE: " . Dumper($rule) if ($debug >= 2);
222 # my ($template, $fields);
223 # if ($#{ $rule } == 2) {
224 # my $opt = $rule->[0];
225 # ($template, $fields) = @{ $rule }[1,2];
226 # unless (exists $data->{ $opt }) {
227 # next;
228 # }
229 # } elsif ($#{ $rule } == 1) {
230 # ($template, $fields) = @{ $rule };
231 # }
232 #
233 # ${ $replace } = sprintf( $template, map { $data->{ $_ } } @{ $fields } );
234 # last;
235 # }
236 # }
237 ################################
238  
239 3 *AUTOLOAD = \&{ "Apache::XPP::AUTOLOAD" };
240  
241 ##################################################
242  
243 =item C<parser_xppcomment>
244  
245 Used to comment out blocks of xpml code.
246  
247 Ex:
248 <XPPCOMMENT>
249 <title><?= $obj->title() ?></title>
250 </XPPCOMMENT>
251  
252 =cut
253  
254 5 ▶sub parser_xppcomment {
Subroutine parser_xppcomment: called CC=2 SCAR=7.6
Pod documented
255 5 my $self = shift;
256 5 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
257 5 my $text = shift;
258 0 ▶ my $subref = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 0 return "";
260 5 };
261 5 $self->dtag($text, "XPPCOMMENT", $subref);
262 }
263  
264 =item C<parser_xppcache>
265  
266 Cache a block of code using a specific expire/store module. Pass
267 the caches name, group, expire, and store. See L<xppcachetut.pod>
268 for more information.
269  
270 Ex:
271 <XPPCACHE name="mycache" group="cachegroup" store="File" expire="Duration, 10s">
272 ....
273 </XPPCACHE>
274  
275 =cut
276  
277 5 ▶sub parser_xppcache {
Subroutine parser_xppcache: called CC=2 SCAR=13.4
Pod documented
278 5 my $self = shift;
279 5 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
280 5 my $text = shift;
281 0 ▶ my $subref = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
282 0 my $params = shift;
283 0 my $data = shift;
284 0 ▶ unless ( (defined ($params->{'name'})) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params->{"name"} and defined $params->{"group"} and defined $params->{"store"} and defined $params->{"expire"}) 0 0 0
Condition0%0/9
defined $params->{"name"} and defined $params->{"group"} and defined $params->{"store"} and defined $params->{"expire"}
!ll&&!rl&&r
000
defined $params->{"name"} and defined $params->{"group"} and defined $params->{"store"}
!ll&&!rl&&r
000
defined $params->{"name"} and defined $params->{"group"}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $params->{"name"} and defined $params->{"group"} and defined $params->{"store"} and defined $params->{"expire"}
defined $params->{"name"} defined $params->{"group"} defined $params->{"store"} defined $params->{"expire"}
Truth table0%0/5
A and B and C and D
A= defined $params->{"name"}B= defined $params->{"group"}C= defined $params->{"store"}D= defined $params->{"expire"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
285 (defined ($params->{'group'})) &&
286 (defined ($params->{'store'})) &&
287 (defined ($params->{'expire'})) ) {
288 0 warn "<XPPCACHE> tags must contain the following params: "
289 ."name, group, store and expiretype.";
290 0 return undef;
291 }
292 my $cache = Apache::XPP::Cache->new(
293 $params->{'name'}, $params->{'group'}, {},
294 [ split( /,\s*/, $params->{'store'} ), \$data ],
295 0 [ split( /,\s*/, $params->{'expire'} ) ]);
296 0 return $cache->content;
297 5 };
298 5 $self->dtag($text, "XPPCACHE", $subref);
299 }
300  
301 =item C<parser_appmethod>
302  
303 Assign or print an object method. Ref checks the object before using
304 it. Assigns the result to the value of the as tag parameter. If this
305 param isn't present the result is printed.
306  
307 Ex:
308 <XPP app app="$obj" attr="bleh" as="$var">
309 <XPP app app="$obj" attr="bleh">
310  
311 =cut
312  
313 5 ▶sub parser_appmethod {
Subroutine parser_appmethod: called CC=2 SCAR=7.4
Pod documented
314 5 my $self = shift;
315 5 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
316 5 my $text = shift;
317 5 $self->stag(
318 $text,
319
320 #if the tag begins with '<xpp app' ...
321 q{<XPP app},
322 [
323 # and contains an 'as' attribute - replace with:
324 # sprintf('<?xpp my %s = ref(%s) ? %s->%s : ""; ?>', @attributes{ qw(as app app attr) });
325 [ 'as', '<?xpp my %s = ref(%s) ? %s->%s : ""; ?>', [qw(as app app attr)] ],
326  
327 # otherwise - replace with:
328 # sprintf('<?xpp= %s->%s; ?>', @attributes{ qw(app attr) });
329 [ '<?xpp= %s->%s; ?>', [qw(app attr)] ],
330 ]
331 );
332 }
333  
334 =item C<parser_print>
335  
336 Prints the result of an object method.
337  
338 Ex:
339 <XPP print obj="$obj" attr="bleh">
340  
341 =cut
342  
343 5 ▶sub parser_print {
Subroutine parser_print: called CC=2 SCAR=7.4
Pod documented
344 5 my $self = shift;
345 5 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
346 5 my $text = shift;
347 5 $self->stag(
348 $text,
349
350 # if the tag begins with '<XPP print' ...
351 q{<XPP print},
352
353 # replace it with:
354 # sprintf('<?xpp print ref(%s) ? %s->%s() : ""; ?>', @attributes{ qw(obj obj attr) });
355 [ '<?xpp print ref(%s) ? %s->%s() : ""; ?>', [qw(obj obj attr)] ]
356 );
357 }
358  
359 =item C<parser_xppxinclude>
360  
361 Calls xinclude with the passed filename and options.
362  
363 Ex:
364 <XPP xinclude filename="include.xmi" options="$obj1, $obj2">
365  
366 =cut
367  
368 5 ▶sub parser_xppxinclude {
Subroutine parser_xppxinclude: called CC=1 SCAR=0.0
Pod documented
369 5 my $self = shift;
370 5 my $text = shift;
371 5 $self->stag(
372 $text,
373  
374 #if the tag begins with <XPPXINCLUDE
375 q{<XPP xinclude},
376 [ '<?xpp xinclude("%s", [ split(/,\s*/, %s) ]); ?>', [qw(filename options)] ]
377 );
378  
379 }
380  
381 =item C<parser_xppforeach>
382  
383 Places the included block within a foreach loop. Assigning each element
384 of the array to 'as.' If 'as' is not present it uses $_.
385  
386 Ex:
387 <XPPFOREACH array="@ary" as="$val">
388 <XPPFOREACH array="@ary">
389  
390 =cut
391  
392 5 ▶sub parser_xppforeach {
Subroutine parser_xppforeach: called CC=1 SCAR=2.3
Pod documented
393 5 my $self = shift;
394 5 my $text = shift;
395 0 ▶ my $subref = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
396 0 my $params = shift;
397 0 my $data = shift;
398 0 ▶ my $as = $params->{'as'}?'my '.$params->{'as'}:'';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'as'} ? : 0 0 0
399 0 my $xppstring = "<?xpp foreach $as (".$params->{'array'}.") { ?>\n"
400 .$data ."\n<?xpp } ?>\n";
401 0 return $xppstring;
402 5 };
403 5 $self->dtag($text,"XPPFOREACH",$subref);
404 }
405  
406 =item C<parser_xppwhile>
407  
408 Places the included block within a while block, looping on the condition
409 specified in 'condition'. If the option 'counter' is specified, the number
410 of loops performed will be assigned to that scalar.
411  
412 Ex:
413 <XPPWHILE condition="my $bar = shift @foo" counter="$count">
414 Shifted off <?= $bar ?>.<BR>
415 </XPPWHILE>
416 I looped <?= $count ?> times.
417  
418 =cut
419  
420 5 ▶sub parser_xppwhile {
Subroutine parser_xppwhile: called CC=1 SCAR=3.7
Pod documented
421 5 my $self = shift;
422 5 my $text = shift;
423 0 ▶ my $subref = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
424 0 my $params = shift;
425 0 my $data = shift;
426 0 my $xppstring = '<?xpp ';
427 0 my $counter = $params->{counter};
428 0 ▶ $xppstring .= "my $counter = 0;\n" if $counter;
Branch: 1 branch
BranchTrueFalseTotal
if $counter 0 0 0
429 0 $xppstring .= "while ($params->{condition}) { ";
430 0 ▶ $xppstring .= "\n$counter++;\n" if $counter;
Branch: 1 branch
BranchTrueFalseTotal
if $counter 0 0 0
431 0 $xppstring .= "?>\n$data\n<?xpp } ?>\n";
432 0 return $xppstring;
433 5 };
434 5 $self->dtag($text,"XPPWHILE",$subref);
435 }
436  
437 =item C<parser_xppshift>
438  
439 Shifts one value off the given array (C<@_> if none is specified) and assigns
440 the value to the specified scalar, scoped lexically (using C<my()>).
441  
442 Ex:
443 <XPPSHIFT array="@ary" as="$val">
444  
445 =cut
446  
447 5 ▶sub parser_xppshift {
Subroutine parser_xppshift: called CC=1 SCAR=0.0
Pod documented
448 5 my $self = shift;
449 5 my $text = shift;
450 5 $self->stag( $text, '<XPPSHIFT',
451 [
452 [ 'array', '<?xpp my %s = shift(%s); ?>', [ 'as', 'array' ] ],
453 [ '<?xpp my %s = shift; ?>', [ 'as' ] ],
454 ]
455 );
456 }
457  
458 3 1;
459  
460 __END__
« XPP/Cache/Expiry/Duration.pm ↑ Summary XPP/Inline.pm »