blib/lib/HTML/Expander.pm

filter: Statementstmt 79.8% 127 / 159 Branchbran 57.6% 30 / 52 Conditioncond 66.6% 30 / 45 MC/DCmcdc 45.8% 11 / 24 Subroutinesub 81.2% 13 / 16 Podpod 0.0% 0 / 11 totaltotal 68.7% 211 / 307 CC 50 SCAR 29.0
CC
50
Cov
73%
mode_load
42.0
expand_tag
30.7
warn
12.0
CRAP 18.2
↑ Summary
1 #########################################################################
2 #
3 # HTML::Expander
4 # Vladi Belperchinov-Shabanski "Cade"
5 # <cade@biscom.net> <cade@datamax.bg> <cade@cpan.org>
6 # http://cade.datamax.bg
7 #
8 #########################################################################
9 package HTML::Expander;
10 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 @ISA = qw( Exporter );
12  
13 1 our $VERSION = '2.7';
14  
15 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 #########################################################################
19  
20 sub new
21 1 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
22 1 my $pack = shift;
23 1 ▶ my $class = ref( $pack ) || $pack;
Condition33%1/3
ref $pack || $pack
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $pack || $pack
ref $pack $pack
Truth table33%1/3
A || B
A= ref $packB= $pack
ABresult
1X1
011
000
24  
25 1 my $self = {};
26  
27 1 $self->{ 'TAGS' } = {}; # tag tables
28 1 $self->{ 'VARS' } = {}; # var tables
29 1 $self->{ 'INC' } = {}; # include directories
30 1 $self->{ 'ENV' } = {}; # local environment, this is free for use
31  
32 1 $self->{ 'MODE' } = []; # mode stack
33  
34 1 $self->{ 'WARNINGS' } = 0; # set to 1 for debug
35  
36 1 bless $self, $class;
37 1 return $self;
38 }
39  
40 sub DESTROY
41 1 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
42 1 my $self = shift;
43 # nothing
44 }
45  
46 sub define_tag
47 7 ▶{
Subroutine define_tag: called CC=4 SCAR=14.0
Pod undocumented
48 7 my $self = shift;
49  
50 7 ▶ confess "invalid arguments count, need MODENAME first" unless @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 0 7 7
51  
52 7 ▶ my $mode = shift || 'main';
Condition50%1/2
shift() || 'main'
l!l
70
MC/DC0%0/1
shift() || 'main'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
53 7 my %tags = @_;
54  
55 7 ▶ $self->{ 'TAGS' }{ $mode } ||= {};
Condition100%2/2
$self->{"TAGS"}{$mode} ||= {}
l!l
52
MC/DC100%1/1
$self->{"TAGS"}{$mode} ||= {}
$self->{"TAGS"}{$mode}
Truth table100%2/2
A
A= $self->{"TAGS"}{$mode}
Aresult
11
00
56 7 %{ $self->{ 'TAGS' }{ $mode } } = ( %{ $self->{ 'TAGS' }{ $mode } }, %tags );
57 }
58  
59 sub define_var
60 3 ▶{
Subroutine define_var: called CC=4 SCAR=14.2
Pod undocumented
61 3 my $self = shift;
62  
63 3 ▶ confess "invalid arguments count, need MODENAME first" unless @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 0 3 3
64  
65 3 ▶ my $mode = shift || 'main';
Condition50%1/2
shift() || 'main'
l!l
30
MC/DC0%0/1
shift() || 'main'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
66 3 my %vars = @_;
67  
68 3 ▶ $self->{ 'VARS' }{ $mode } ||= {};
Condition50%1/2
$self->{"VARS"}{$mode} ||= {}
l!l
30
MC/DC0%0/1
$self->{"VARS"}{$mode} ||= {}
$self->{"VARS"}{$mode}
Truth table50%1/2
A
A= $self->{"VARS"}{$mode}
Aresult
11
00
69 3 %{ $self->{ 'VARS' }{ $mode } } = ( %{ $self->{ 'VARS' }{ $mode } }, %vars );
70 }
71  
72 sub add_inc_paths
73 1 ▶{
Subroutine add_inc_paths: called CC=2 SCAR=6.9
Pod undocumented
74 1 my $self = shift;
75 1 $self->{ 'INC' }{ $_ }++ for @_;
76 }
77  
78 sub del_inc_paths
79 0 ▶{
Subroutine del_inc_paths: not called CC=2 SCAR=17.9
Pod undocumented
80 0 my $self = shift;
81 0 delete $self->{ 'INC' }{ $_ } for @_;
82 }
83  
84 sub mode_copy
85 1 ▶{
Subroutine mode_copy: called CC=4 SCAR=14.0
Pod undocumented
86 1 my $self = shift;
87 1 my $mode = shift; # destination mode
88  
89 1 for my $s ( @_ ) # for each source modes
90 {
91 # print "DEBUG: mode copy: [$mode] <- [$s]\n";
92 1 ▶ while( my ( $k, $v ) = each %{ $self->{ 'TAGS' }{ $s } } )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"TAGS"}{$s};}) 2 1 3
93 {
94 # print "DEBUG: TAG ($k) = ($v)\n";
95 2 $self->define_tag( $mode, $k, $v );
96 }
97 1 ▶ while( my ( $k, $v ) = each %{ $self->{ 'VARS' }{ $s } } )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"VARS"}{$s};}) 0 1 1
98 {
99 # print "DEBUG: VAR ($k) = ($v)\n";
100 0 $self->define_var( $mode, $k, $v );
101 }
102 }
103 }
104  
105 sub mode_load
106 0 ▶{
Subroutine mode_load: not called CC=6 SCAR=37.4
Pod undocumented
107 0 my $self = shift;
108 0 my $file = shift;
109  
110 0 my $target = 'main';
111 0 open my $i, $file;
112 0 ▶ while(<$i>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $i)) 0 0 0
113 {
114 0 ▶ next if /^\s*[#;]/; # comments
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*[#;]/ 0 0 0
115 0 chomp;
116 0 ▶ if ( /^\s*MODE/i )
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*MODE/i) { } 0 0 0
117 {
118 0 $_ = lc $_;
119 0 s/\s+//g; # get rid of whitespace
120 0 my @a = split /[:,]/;
121 0 shift @a; # skip `mode' keyword
122 0 $target = shift @a;
123 0 $self->mode_copy( $target, @a );
124 }
125 else
126 {
127 0 ▶ $self->define_tag( $target, lc $1, $2 ) if /^\s*(<\S+)\s+(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(<\S+)\s+(.*)$/ 0 0 0
128 0 ▶ $self->define_var( $target, lc $2, $3 ) if /^\s*(%(\S+))\s+(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(%(\S+))\s+(.*)$/ 0 0 0
129 }
130 }
131 0 close $i;
132 }
133  
134 sub expand
135 62 ▶{
Subroutine expand: called CC=3 SCAR=11.0
Pod undocumented
136 62 my $self = shift;
137 62 my $text = shift;
138 62 ▶ my $level = shift || 0;
Condition100%2/2
shift() || 0
l!l
593
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
139 62 ▶ my $visited_arg = shift || {};
Condition100%2/2
shift() || {}
l!l
593
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
140  
141 62 my $visited = { %$visited_arg };
142  
143 #print "DEBUG: expand (level=$level) [text=$text]\n";
144  
145 62 $text =~ s/\(\%([^\(\)]+)\)/$self->expand_var( $1, $level+1, $visited )/gie;
146 #print "DEBUG: ----------------------\n";
147 62 $text =~ s/<([^<>]+)>/$self->expand_tag( $1, $level+1, $visited )/gie;
148 #print "DEBUG: expand result: [text=$text]\n";
149 62 return $text;
150 }
151  
152 sub expand_var
153 20 ▶{
Subroutine expand_var: called CC=4 SCAR=13.9
Pod undocumented
154 20 my $self = shift;
155 20 my $var = shift;
156 20 my $level = shift;
157 20 my $visited_arg = shift;
158  
159 20 my $visited = { %$visited_arg };
160  
161 20 ▶ return '***'.undef if $visited->{ "VAR::$var" }++; # avoids recursion
Branch: 1 branch
BranchTrueFalseTotal
if $$visited{"VAR::$var"}++ 0 20 20
162 20 ▶ my $mode = $self->{ 'MODE' }[0] || 'main';
Condition100%2/2
$self->{'MODE'}[0] || 'main'
l!l
218
MC/DC100%1/1
$self->{'MODE'}[0] || 'main'
$self->{'MODE'}[0]
Truth table100%2/2
A
A= $self->{'MODE'}[0]
Aresult
11
00
163 my $value = $self->{ 'VARS' }{ $mode }{ $var }
164 20 ▶ || $self->{ 'ENV' }{ $var };
Condition100%3/3
$self->{'VARS'}{$mode}{$var} || $self->{'ENV'}{$var}
l!l&&r!l&&!r
1037
MC/DC100%2/2
$self->{'VARS'}{$mode}{$var} || $self->{'ENV'}{$var}
$self->{'VARS'}{$mode}{$var} $self->{'ENV'}{$var}
Truth table100%3/3
A || B
A= $self->{'VARS'}{$mode}{$var}B= $self->{'ENV'}{$var}
ABresult
1X1
011
000
165 1 ▶ use Data::Dumper; # DEBUG
Subroutine BEGIN: called CC=1 SCAR=0.0
166 20 print "DEBUG: expand_var: [$var] = ($value)\n".Dumper($visited)."\n";
167 20 return $self->expand( $value, $level + 1, $visited );
168 }
169  
170 sub expand_tag
171 133 ▶{
Subroutine expand_tag: called CC=26 SCAR=34.3
Pod undocumented
172 133 my $self = shift;
173 133 my $tag_org = shift;
174 133 my $level = shift;
175 133 my $visited_arg = shift;
176  
177 133 my $visited = { %$visited_arg };
178  
179 133 my %args;
180 133 my ( $tag, $args ) = split /\s+/, $tag_org, 2;
181 # print "DEBUG: expand_tag: [$tag] -- ($args)\n";
182 133 my $tag_lc = lc $tag;
183 133 ▶ while( $args =~ /\s*([^=]+)(=('([^']*)'|"([^"]*)"|(\S*)))?/g ) # "' # fix string colorization
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /\s*([^=]+)(=('([^']*)'|"([^"]*)"|(\S*)))?/g) 43 133 176
184 {
185 43 my $k = lc $1;
186 43 ▶ my $v = $4 || $5 || $6 || 1;
Condition62%5/8
$4 || $5 || $6 || 1
l!l
385
$4 or $5 or $6
l!l&&r!l&&!r
0385
$4 or $5
l!l&&r!l&&!r
0043
MC/DC33%1/3
$4 || $5 || $6 || 1
$4 $5 $6
Truth table50%2/4
A or B or C
A= $4B= $5C= $6
ABCresult
1XX1
01X1
0011
0000
187 43 $args{ $k } = $v;
188 # print "DEBUG: [$k] = ($v)\n";
189 }
190  
191 133 ▶ if ( $tag_lc eq 'mode' )
Branch: 2 branches
BranchTrueFalseTotal
if ($tag_lc eq 'mode') { } 2 131 133
elsif ($tag_lc eq '/mode') { } 2 129 131
192 {
193 2 ▶ unshift @{ $self->{ 'MODE' } }, ( $args{ 'name' } || 'main' );
Condition50%1/2
$args{'name'} || 'main'
l!l
20
MC/DC0%0/1
$args{'name'} || 'main'
$args{'name'}
Truth table50%1/2
A
A= $args{'name'}
Aresult
11
00
194 2 ▶ $self->{ 'ENV' }{ '!MODE' } = $self->{ 'MODE' }[0] || 'main';
Condition50%1/2
$self->{'MODE'}[0] || 'main'
l!l
20
MC/DC0%0/1
$self->{'MODE'}[0] || 'main'
$self->{'MODE'}[0]
Truth table50%1/2
A
A= $self->{'MODE'}[0]
Aresult
11
00
195 2 return undef;
196 }
197 elsif ( $tag_lc eq '/mode' )
198 {
199 2 shift @{ $self->{ 'MODE' } };
200 2 ▶ $self->{ 'ENV' }{ '!MODE' } = $self->{ 'MODE' }[0] || 'main';
Condition50%1/2
$self->{'MODE'}[0] || 'main'
l!l
02
MC/DC0%0/1
$self->{'MODE'}[0] || 'main'
$self->{'MODE'}[0]
Truth table50%1/2
A
A= $self->{'MODE'}[0]
Aresult
11
00
201 2 return undef;
202 }
203 129 ▶ if ( $tag_lc eq 'var' )
Branch: 3 branches
BranchTrueFalseTotal
if ($tag_lc eq 'var') { } 20 109 129
elsif ($tag_lc eq 'include' or $tag_lc eq 'inc') { } 2 107 109
elsif ($tag_lc eq 'exec') { } 2 105 107
204 {
205 20 ▶ if( $args{ 'set' } eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'set'} eq '') { } 18 2 20
206 {
207 18 return $self->expand_var( $args{ 'name' }, $level + 1, $visited );
208 }
209 else
210 {
211 2 $self->{ 'ENV' }{ uc $args{ 'name' } } = $args{ 'set' };
212 2 ▶ return $args{ 'echo' } ? $args{ 'set' } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$args{'echo'} ? : 2 0 2
213 }
214 }
215 ▶ elsif ( $tag_lc eq 'include' or $tag_lc eq 'inc' )
Condition66%2/3
$tag_lc eq "include" or $tag_lc eq "inc"
l!l&&r!l&&!r
02107
MC/DC50%1/2
$tag_lc eq "include" or $tag_lc eq "inc"
$tag_lc eq "include" $tag_lc eq "inc"
Truth table66%2/3
A or B
A= $tag_lc eq "include"B= $tag_lc eq "inc"
ABresult
1X1
011
000
216 {
217 2 my $file_arg = $args{ 'file' };
218 2 ▶ if( $file_arg !~ /^[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]*)?$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_arg =~ /^[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]*)?$/) 0 2 2
219 {
220 0 $self->warn( "forbidden include file name `$file_arg'" );
221 return undef
222 0 }
223 2 my $file;
224 2 for( keys %{ $self->{ 'INC' } } )
225 {
226 2 $file = $_ . '/' . $file_arg;
227 2 ▶ last if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 2 0 2
228 0 $file = undef;
229 }
230 2 ▶ return undef if $visited->{ "INC::$file" }++; # avoids recursion
Branch: 1 branch
BranchTrueFalseTotal
if $$visited{"INC::$file"}++ 1 1 2
231 open( my $i, $file ) || do
232 1 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
open my $i, $file or do { $self->warn("cannot open file `${file}'"); return undef } 1 0 1
233 0 $self->warn( "cannot open file `$file'" );
234 0 return undef;
235 };
236 1 my $data = $self->expand( join( '', <$i> ), $level + 1, $visited );
237 1 close( $i );
238 1 return $data;
239 }
240 elsif ( $tag_lc eq 'exec' )
241 {
242 2 my $cmd = $args{ 'cmd' };
243 2 ▶ if( ! $self->{ 'EXEC_TAG_ALLOWED' } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"EXEC_TAG_ALLOWED"}) 0 2 2
244 {
245 0 $self->warn( "exec is forbidden `$cmd'" );
246 0 return undef;
247 }
248  
249 open( my $i, $cmd . '|' ) || do
250 2 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
open my $i, $cmd . '|' or do { $self->warn("exec failed `${cmd}'"); return undef } 2 0 2
251 0 $self->warn( "exec failed `$cmd'" );
252 0 return undef;
253 };
254 2 my $data = $self->expand( join( '', <$i> ), $level + 1, $visited );
255 2 close $i;
256 2 return $data;
257 }
258 else
259 {
260 105 $tag = "<$tag>";
261  
262 105 ▶ my $mode = $self->{ 'MODE' }[0] || 'main';
Condition100%2/2
$self->{'MODE'}[0] || 'main'
l!l
5055
MC/DC100%1/1
$self->{'MODE'}[0] || 'main'
$self->{'MODE'}[0]
Truth table100%2/2
A
A= $self->{'MODE'}[0]
Aresult
11
00
263 105 my $value = $self->{ 'TAGS' }{ $mode }{ $tag };
264 # print "DEBUG: mode name {$mode}, tag: $tag -> ($value)\n" if defined $value;
265 105 ▶ if ( $value and ! $visited->{ "$mode::$tag" } )
Branch: 1 branch
BranchTrueFalseTotal
if ($value and not $$visited{"$main::mode::$tag"}) { } 18 87 105
Condition100%3/3
$value and not $$visited{"$main::mode::$tag"}
!ll&&!rl&&r
79818
MC/DC100%2/2
$value and not $$visited{"$main::mode::$tag"}
$value not $$visited{"$main::mode::$tag"}
Truth table100%3/3
A and not(B)
A= $valueB= not $$visited{"$main::mode::$tag"}
ABresult
0X0
100
111
266 {
267 # print "DEBUG: ---> ($value)\n";
268 18 $visited->{ "$mode::$tag" }++; # avoids recursion
269 18 $value = $self->expand( $value, $level + 1, $visited );
270 18 $value =~ s/\%([a-z_0-9]+)/$args{ lc $1 }/gi;
271 18 my $ret = $self->expand( $value, $level + 1, $visited );
272 # print "DEBUG: expand_tag return: [$ret]\n";
273 18 return $ret;
274 }
275 else
276 {
277 # print "DEBUG: expand_tag original: [$tag_org]\n";
278 87 return "<$tag_org>";
279 }
280 }
281 }
282  
283 sub warn
284 0 ▶{
Subroutine warn: not called CC=3 SCAR=24.8
Pod undocumented
285 0 my $self = shift;
286 0 ▶ return unless $self->{ 'WARNINGS' } || $self->{ 'WARN' };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"WARNINGS"} or $self->{"WARN"} 0 0 0
Condition0%0/3
$self->{"WARNINGS"} or $self->{"WARN"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"WARNINGS"} or $self->{"WARN"}
$self->{"WARNINGS"} $self->{"WARN"}
Truth table0%0/3
A or B
A= $self->{"WARNINGS"}B= $self->{"WARN"}
ABresult
1X1
011
000
287  
288 0 carp __PACKAGE__ . ": " . join( ' ', @_ );
289 }
290  
291 =pod
292  
293 =head1 NAME
294  
295 HTML::Expander - html tag expander with inheritable tag definitions (modes)
296  
297 =head1 SYNOPSIS
298  
299 use HTML::Expander;
300  
301 # get new HTML::Expander object;
302 my $ex = new HTML::Expander;
303  
304 # load mode (tags) definitions
305 $ex->mode_load( "/path/to/mode.def.txt" );
306  
307 # define some more tags
308 $ex->define_tag( 'main', '<name>', '<h1><font color=%c>' );
309 $ex->define_tag( 'main', '</name>', '</font></h1>' );
310  
311 # copy `main' into `new' mode
312 $ex->mode_copy( 'new', 'main' );
313  
314 # define one more tag
315 $ex->define_tag( 'new', '<h1>', '<p><h1>' );
316 $ex->define_tag( 'new', '<box>', '<pre>' );
317 $ex->define_tag( 'new', '</box>', '</pre>' );
318  
319 # expand!
320 print $ex->expand( "<mode name=new>
321 (current mode is '<var name=!MODE>')
322 <name c=#fff>This is me</name>
323 </mode>
324 (current mode is '<var name=!MODE>')
325 <name>empty</name>
326 1.<var name=TEST>
327 2.<var name=TEST set=opala! echo>
328 3.<var name=TEST>
329 \n" );
330 # the result will be:
331 # <pre>(current mode is 'new')</pre>
332 # <p><h1><font color=#fff>This is me</font></h1>
333 #
334 # <box>(cyrrent mode is 'main')</box>
335 # <h1><font color=>empty</font></h1>
336 # 1.
337 # 2.opala!
338 # 3.opala!
339  
340 # this should print current date
341 $self->{ 'EXEC_TAG_ALLOWED' } = 1; # allow execution of programs
342 print $ex->expand( '<exec cmd=date>' ), "\n";
343 $self->{ 'EXEC_TAG_ALLOWED' } = 0; # forbid execution of programs (default)
344  
345 # add include paths
346 $ex->add_inc_paths( '/usr/html/inc', '/opt/test' );
347 $ex->del_inc_paths( '.' );
348 $ex->{ 'INC' }{ '.' } = 1;
349 $ex->{ 'INC' }{ '/usr/html/inc' } = 1;
350 $ex->{ 'INC' }{ '/opt/test' } = 1;
351  
352 # remove path
353 delete $ex->{ 'INC' }{ '/usr/html/inc' };
354  
355 # include some file (avoiding recursion if required)
356 print $ex->expand( '<inc file=test.pl>' ), "\n";
357  
358 =head1 DESCRIPTION
359  
360 HTML::Expander replaces html tags with other text (more tags, so it 'expands':))
361 with optional arguments. HTML::Expander uses tag tables which are called modes.
362 Modes can inherit other modes (several ones if needed). The goal is to have
363 as simple input html document as you need and have multiple different outputs.
364 For example you may want <box> tag to render either as <pre> or as
365 <table><tr><td> in two separated modes.
366  
367 Essentially HTML::Expander works as preprocessor.
368  
369 The mode file syntax is:
370  
371 tag tag-replacement-string
372  
373 MODE: mode-name: inherited-modes-list
374  
375 tag tag-replacement-string
376  
377 etc...
378  
379 inherited-modes-list is comma or semicolon-separated list of modes that
380 should be copied (inherited) in this mode
381  
382 The mode file example:
383  
384 ### begin mode
385  
386 # top-level mode is called `main' and is silently defined by default
387 # mode: main
388  
389 <head1> <h1>
390 </head1> </h1>
391  
392 <head2> <h1><font color=#ff0000>
393 </head2> </h1></font>
394  
395 MODE: page: main
396  
397 <head2> <h1><font color=#00ff00>
398  
399 MODE: edit: page, main
400  
401 # actually `page' inherits `main' so it is not really
402 # required here to list `main'
403  
404 <head2> <h1><font color=#0000ff><u>
405  
406 This is not exhaustive example but it is just for example...
407  
408 =head1 TAG ARGUMENTS
409  
410 Inside the tag you can define arguments that can be used later during the
411 interpolation or as argument to the special tags etc.
412  
413 Arguments cannot contain whitespace unless enclosed in " or ':
414  
415 <mytag arg=value> # correct
416 <mytag arg=this is long value> # incorrect!
417 <mytag arg='the second try'> # correct
418 <mytag arg="cade's third try"> # correct
419  
420 There is no way to mix " and ':
421  
422 <mytag arg='cade\'s third try'> # incorrect! there is no escape syntax
423  
424 You can have unary arguments (without value) which, if used, have '1' value.
425  
426 <mytag echo> is the same as <mytag echo=1>
427  
428 =head1 SPECIAL TAGS
429  
430 There are several tags with special purposes:
431  
432 <mode name=name>
433  
434 Sets current mode to `name' (saves it on the top of the mode stack).
435  
436 </mode>
437  
438 Removes last used mode from the stack (if stack is empty `main' is used).
439 Both <mode> and </mode> are replaced with empty strings.
440  
441 <exec cmd=command>
442  
443 This tag is replaced with `command's output. 'exec' is forbidden by default.
444 Using it will lead to empty string returned. To allow it you need to:
445  
446 $ex->{ 'EXEC_TAG_ALLOWED' } = 1; # allow execution of programs
447  
448 ($ex is the HTML::Expander object you want to allow execution)
449 exec must be used only if you produce html pages from static source, i.e. NOT
450 from end-user source like html forms etc.! To avoid unwanted execution the
451 program which uses HTML::Expander must encode all <>'s into html special
452 chars:
453  
454 > must be converted to &gt;
455 < must be converted to &lt;
456  
457 Rule of thumb is: do not use exec! :)
458  
459 <include file=incfile>
460 or
461 <inc file=incfile>
462  
463 This tag is replaced with `incfile' file's content (which will be
464 HTML::Expanded recursively).
465  
466 =head1 VARIABLES/ENVIRONMENT
467  
468 HTML::Expander object have own 'environment' which is accessed this way:
469  
470 $ex->{'ENV'}{ 'var-name' } = 'var-value';
471  
472 i.e. $ex->{'ENV'} is hash reference to the local environment. There is no
473 special access policy.
474  
475 There is syntax for variables interpolation. Values are taken from internal
476 environment table:
477  
478 (%VARNAME)
479  
480 All variables are replaced before tag expansion! This helps to handle this:
481  
482 <tag argument=(%VAR) etc.>
483  
484 If you need to interpolate variable in the tag expansion process (after the
485 variables interpolation) you need to:
486  
487 <var name=VARNAME>
488  
489 If you need to set variable name during tag interpolation you should:
490  
491 <var name=VARNAME set=VALUE>
492  
493 If you want to set variable and return its value at the same time you have to
494 use unary 'echo' argument:
495  
496 <var name=VARNAME set=VALUE echo>
497  
498 (%VAR) variables are interpolated before %arg interpolation, so it is safe to
499 use this:
500  
501 <img src=(%WWWROOT)/%src>
502  
503 =head1 BUGS
504  
505 Unknown tags are left as-is, this is not bug but if you write non-html tag
506 which is not defined in mode tables it will passed into the output text.
507 (see <box> example above for 'main' mode)
508  
509 If you find bug please contact me, thank you.
510  
511 =head1 DIAGNOSTICS
512  
513 HTML::Expander can report warnings if something wrong is going on. This is
514 assumed to be debugging or diagnostic tool so it is disabled by default.
515 To enable warnings:
516  
517 $ex->{ 'WARNINGS' } = 1;
518  
519 =head1 TODO
520  
521 <empty>
522  
523 =head1 AUTHOR
524  
525 Vladi Belperchinov-Shabanski "Cade"
526  
527 <cade@biscom.net> <cade@datamax.bg> <cade@cpan.org>
528  
529 http://cade.datamax.bg
530  
531 =head1 VERSION
532  
533 $Id: Expander.pm,v 1.18 2006/04/30 00:30:00 cade Exp $
534  
535 =cut
536  
537 #########################################################################
538 # eof
539 #########################################################################
540 1 1;
↑ Summary