| 1 | # Bilstone html template parser | ||
| 2 | # | ||
| 3 | # September 1999 | ||
| 4 | # Version 1.33 production | ||
| 5 | # Author Ian Steel (ian@bilstone.co.uk) | ||
| 6 | # | ||
| 7 | # For latest releases, documentation, faqs, etc see the homepage at | ||
| 8 | # http://www.bilstone.co.uk/parser | ||
| 9 | # | ||
| 10 | # History: | ||
| 11 | # | ||
| 12 | # April 1999 - First cut | ||
| 13 | # September 1999 - Added strict. | ||
| 14 | # March 2000 - 1.20 - Allow for invisible templates. | ||
| 15 | # March 2001 - 1.30 - Added to CPAN | ||
| 16 | # March 2001 - 1.31 - Added getAllTokens, and srcString methods. | ||
| 17 | # Sept. 2001 - 1.32 - Reset $/ to prior value (courtesy) | ||
| 18 | # Oct. 2001 - 1.34 - Corrected operation of Invisible blocks. | ||
| 19 | # | ||
| 20 | |||
| 21 | package HTMLTMPL; | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use vars qw($AUTOLOAD); | ||
| 25 | use Carp; | ||
| 26 | |||
| 27 | no strict 'refs'; | ||
| 28 | |||
| 29 | $HTMLTMPL::VERSION = '1.34'; | ||
| 30 | |||
| 31 | sub new() | ||
| 32 | { | ||
| 33 | my $class = shift; | ||
| 34 | my %baseHtml = (); | ||
| 35 | bless \%baseHtml, $class; | ||
| 36 | return \%baseHtml | ||
| 37 | } | ||
| 38 | |||
| 39 | sub src() | ||
| 40 | { | ||
| 41 | if ($#_ != 1) | ||
| 42 | { | ||
| 43 | bailout("Error! template function requires a single parameter\n"); | ||
| 44 | } | ||
| 45 | |||
| 46 | my $self = shift; | ||
| 47 | my $src = shift; | ||
| 48 | |||
| 49 | my $tmp = $/; | ||
| 50 | undef $/; | ||
| 51 | open(HTML, "<$src") || bailout("Cannot open html template file!<br>$src"); | ||
| 52 | my $tmplString = <HTML>; | ||
| 53 | close HTML; | ||
| 54 | |||
| 55 | srcString($self, $tmplString); | ||
| 56 | $/ = $tmp; | ||
| 57 | } | ||
| 58 | |||
| 59 | # If the template is within a string rather than a file, use this method to | ||
| 60 | # populate the template object. | ||
| 61 | # | ||
| 62 | sub srcString() | ||
| 63 | { | ||
| 64 | my $self = shift; | ||
| 65 | my $src = shift; | ||
| 66 | |||
| 67 | $self->{'_C__REPEAT__'} = 0; # Not a repeating block as this is the | ||
| 68 | # main html page. | ||
| 69 | |||
| 70 | my $allToks = []; | ||
| 71 | |||
| 72 | parseSegment($self, $allToks, $src); | ||
| 73 | |||
| 74 | $self->{'_C__TOKLIST__'} = $allToks; # Array of all tokens. | ||
| 75 | } | ||
| 76 | |||
| 77 | # Processes html given as third parameter. | ||
| 78 | # Adds segments to array within the given hash parameter. | ||
| 79 | # | ||
| 80 | sub parseSegment() | ||
| 81 | { | ||
| 82 | my ($segHash, $allToks, $tmplString) = @_; | ||
| 83 | |||
| 84 | my @segments = (); | ||
| 85 | my $repeating = $segHash->{'_C__REPEAT__'}; | ||
| 86 | my $pos = 0; | ||
| 87 | |||
| 88 | my ($padding, $token, $remainder); | ||
| 89 | while ($tmplString =~ /(.*?)((__[ix]_.+?__\n)|(__.+?__))/sg) | ||
| 90 | { | ||
| 91 | $padding = $1; | ||
| 92 | $token = $2; | ||
| 93 | $remainder = $'; | ||
| 94 | |||
| 95 | $token =~ s/\n$//g; # chomp $token (chomp bust as $/ undef'd) | ||
| 96 | |||
| 97 | push @segments, $padding; | ||
| 98 | |||
| 99 | $pos = pos $tmplString; | ||
| 100 | |||
| 101 | if ($token =~ /__[ix]_.+__/) | ||
| 102 | { | ||
| 103 | # The start of a repeating block | ||
| 104 | # Create a hash which is named after the repeating block. | ||
| 105 | # Pass a reference to the hash into parseSegment (us) so that | ||
| 106 | # the block of repeating html is picked up and associated | ||
| 107 | # with this hash. | ||
| 108 | # | ||
| 109 | # We need to restart the search processing after the end of | ||
| 110 | # the repeating block. | ||
| 111 | |||
| 112 | my $tokId = $token; | ||
| 113 | $tokId =~ s/__[ix]_(.+)__/$1/; | ||
| 114 | |||
| 115 | %$token = (); | ||
| 116 | |||
| 117 | %$token->{'_C__ID__'} = $tokId; | ||
| 118 | %$token->{'_C__REPEAT__'} = 1; | ||
| 119 | |||
| 120 | my $block = $remainder; | ||
| 121 | |||
| 122 | $block =~ /(.+)${token}/s; | ||
| 123 | $block = $1; | ||
| 124 | |||
| 125 | # Create an entry in the list of tokens for this block if we have | ||
| 126 | # just found an 'invisible block'. | ||
| 127 | # | ||
| 128 | $segHash->{$token} = [] if $token =~ /^__i_/; | ||
| 129 | |||
| 130 | push @segments, $token; | ||
| 131 | parseSegment(\%$token, $allToks, $block); | ||
| 132 | |||
| 133 | $pos += length($block) + length($token) + 1; | ||
| 134 | pos $tmplString = $pos; | ||
| 135 | |||
| 136 | next; | ||
| 137 | } | ||
| 138 | |||
| 139 | if (defined $token) | ||
| 140 | { | ||
| 141 | # This line contains a token. | ||
| 142 | # Break up line to get out tokens, and store in array. | ||
| 143 | # | ||
| 144 | # my $padding = $1; | ||
| 145 | # my $token = $2; | ||
| 146 | my $tokId = $token; | ||
| 147 | $tokId =~ s/__(.+)__/$1/; | ||
| 148 | |||
| 149 | # For a repeating block, each token will have to hold an array | ||
| 150 | # of values. For non-repeating, just a scalar. | ||
| 151 | # | ||
| 152 | $segHash->{$token} = ($repeating ? [] : ''); | ||
| 153 | |||
| 154 | push @segments, $token; | ||
| 155 | |||
| 156 | push @$allToks, ($repeating ? | ||
| 157 | $segHash->{'_C__ID__'} . ":" . $tokId : $tokId); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | pos $tmplString = $pos; | ||
| 162 | $tmplString =~ m/(.*)/sg; | ||
| 163 | |||
| 164 | push @segments, $1; | ||
| 165 | |||
| 166 | $segHash->{'_C__HTML__'} = [ @segments ]; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub AUTOLOAD | ||
| 170 | { | ||
| 171 | my $token = $AUTOLOAD; | ||
| 172 | my ($self, $value, $block) = @_; | ||
| 173 | |||
| 174 | if (defined $block) | ||
| 175 | { | ||
| 176 | # Self needs to refer to the repeating block now rather than this | ||
| 177 | # object because the user wants to store a repeating block value. | ||
| 178 | # | ||
| 179 | my $tmp = '__' . $block . '__'; | ||
| 180 | $self = \%$tmp; | ||
| 181 | } | ||
| 182 | |||
| 183 | $token =~ s/.*:://; | ||
| 184 | my $tok = '__' . $token . '__'; | ||
| 185 | |||
| 186 | if (exists $self->{$tok}) | ||
| 187 | { | ||
| 188 | if ($self->{"_C__REPEAT__"} == 1) | ||
| 189 | { | ||
| 190 | # within a block so add to array rather than storing raw value. | ||
| 191 | # | ||
| 192 | my $tmp = $self->{$tok}; | ||
| 193 | push @$tmp, $value; | ||
| 194 | $self->{$tok} = [ @$tmp ]; | ||
| 195 | } | ||
| 196 | else | ||
| 197 | { | ||
| 198 | $self->{$tok} = $value; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | else | ||
| 202 | { | ||
| 203 | bailout("Invalid token '$token'.<br>Maybe it needs qualifying within a " . | ||
| 204 | "block?"); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | # | ||
| 209 | # Returns a blessed hash (the name is given as a parameter) which is in all | ||
| 210 | # essence the invisible template object. | ||
| 211 | # | ||
| 212 | sub getBlock($) | ||
| 213 | { | ||
| 214 | my ($self, $iBlock) = @_; | ||
| 215 | $iBlock = "__" . $iBlock . "__"; | ||
| 216 | my $ret = bless \%$iBlock; | ||
| 217 | |||
| 218 | # All none control entries in the hash, should be initialised. | ||
| 219 | # | ||
| 220 | my $repeating = $ret->{"_C__REPEAT__"}; | ||
| 221 | |||
| 222 | map { | ||
| 223 | if (!/^_C__/) | ||
| 224 | { | ||
| 225 | $ret->{$_} = ($repeating ? [] : ''); | ||
| 226 | } | ||
| 227 | } keys %$ret; | ||
| 228 | |||
| 229 | return $ret; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub output() | ||
| 233 | { | ||
| 234 | my $self = shift; | ||
| 235 | my $hdr; | ||
| 236 | |||
| 237 | foreach $hdr (@_) | ||
| 238 | { | ||
| 239 | print "$hdr\n"; | ||
| 240 | } | ||
| 241 | |||
| 242 | print "\n"; | ||
| 243 | |||
| 244 | print mergeData($self); | ||
| 245 | } | ||
| 246 | |||
| 247 | sub htmlString() | ||
| 248 | { | ||
| 249 | my $self = shift; | ||
| 250 | return mergeData($self); | ||
| 251 | } | ||
| 252 | |||
| 253 | sub mergeData() | ||
| 254 | { | ||
| 255 | my ($leg) = @_; | ||
| 256 | |||
| 257 | my $segs = $leg->{'_C__HTML__'}; | ||
| 258 | my $repeating = $leg->{'_C__REPEAT__'}; | ||
| 259 | my $entries = 1; | ||
| 260 | |||
| 261 | my $htmlGen = ''; # Generated html to be output. | ||
| 262 | |||
| 263 | if ($repeating) | ||
| 264 | { | ||
| 265 | # determine number of times we need to repeat putting out this | ||
| 266 | # segment. | ||
| 267 | # | ||
| 268 | my $key; | ||
| 269 | foreach $key (keys %$leg) | ||
| 270 | { | ||
| 271 | next if ($key =~ /^_C__/); # ignore control entries. | ||
| 272 | |||
| 273 | my $tmp = $leg->{$key}; | ||
| 274 | $entries = $#$tmp; | ||
| 275 | $entries++; | ||
| 276 | last; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | my $ix; | ||
| 281 | for ($ix=0; $ix < $entries; $ix++) | ||
| 282 | { | ||
| 283 | # Walk the array of html segments. | ||
| 284 | # | ||
| 285 | my $seg; | ||
| 286 | foreach $seg (@$segs) | ||
| 287 | { | ||
| 288 | if ($seg =~ /__.*__/) # Is it a token? | ||
| 289 | { | ||
| 290 | if ($seg =~ /^__x_.+__/) # Repeating? | ||
| 291 | { | ||
| 292 | chomp($seg); | ||
| 293 | # This is a repeating block rather than an individual token, | ||
| 294 | # so process it seperately. | ||
| 295 | # | ||
| 296 | $htmlGen .= mergeData(\%$seg); | ||
| 297 | } | ||
| 298 | else | ||
| 299 | { | ||
| 300 | if ($repeating) | ||
| 301 | { | ||
| 302 | # Output next value from array for this token. | ||
| 303 | # | ||
| 304 | my $temp = $leg->{$seg}; | ||
| 305 | $htmlGen .= @$temp[$ix]; | ||
| 306 | } | ||
| 307 | else | ||
| 308 | { | ||
| 309 | # Output token value. | ||
| 310 | # | ||
| 311 | $htmlGen .= $leg->{$seg}; | ||
| 312 | } | ||
| 313 | } | ||
| 314 | } | ||
| 315 | else | ||
| 316 | { | ||
| 317 | # Straight html | ||
| 318 | # | ||
| 319 | $htmlGen .= $seg; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | return $htmlGen; | ||
| 325 | } | ||
| 326 | |||
| 327 | # Returns a ref to an array. Each element of the array contains the name of a | ||
| 328 | # token within the template. Tokens within repeating blocks are prefixed | ||
| 329 | # with 'block_name:'. | ||
| 330 | # | ||
| 331 | sub listAllTokens() | ||
| 332 | { | ||
| 333 | my $self = shift; | ||
| 334 | return $self->{'_C__TOKLIST__'}; | ||
| 335 | } | ||
| 336 | |||
| 337 | # If called in a scalar context, returns a comma seperated list of tokens | ||
| 338 | # found within the template. | ||
| 339 | # If called in array context, returns array of tokens found within the | ||
| 340 | # template. | ||
| 341 | # | ||
| 342 | sub getAllTokens() | ||
| 343 | { | ||
| 344 | my $self = shift; | ||
| 345 | |||
| 346 | if (wantarray) { | ||
| 347 | return @{$self->{'_C__TOKLIST__'}}; | ||
| 348 | } | ||
| 349 | |||
| 350 | my $ret = ","; | ||
| 351 | map { $ret .= "$_,"; } @{$self->{'_C__TOKLIST__'}}; | ||
| 352 | return $ret; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub dumpAll() | ||
| 356 | { | ||
| 357 | print << "EOHTML"; | ||
| 358 | Content-type: text/html | ||
| 359 | |||
| 360 | <html><head><title>Dump of tokens and values</title></head> | ||
| 361 | <body bgcolor=beige> | ||
| 362 | <h3 align=center>Dump of tokens and values</h3> | ||
| 363 | <p> | ||
| 364 | <table border=1 align=center> | ||
| 365 | <tr align=center><th bgcolor=lightblue>Token</th><th colspan=2 bgcolor=lightblue>Value</th></tr> | ||
| 366 | EOHTML | ||
| 367 | dumpit(@_); | ||
| 368 | |||
| 369 | print "</table></body></html>"; | ||
| 370 | } | ||
| 371 | |||
| 372 | sub dumpit() | ||
| 373 | { | ||
| 374 | my ($self, $block) = @_; | ||
| 375 | |||
| 376 | if (defined $block) | ||
| 377 | { | ||
| 378 | $self = \%$block if (defined $block); | ||
| 379 | } | ||
| 380 | |||
| 381 | my $repeating = $self->{'_C__REPEAT__'}; | ||
| 382 | |||
| 383 | if ($repeating) | ||
| 384 | { | ||
| 385 | my($entries); | ||
| 386 | |||
| 387 | # Determine number of entries in block values. | ||
| 388 | # | ||
| 389 | my($key); | ||
| 390 | foreach $key (keys %$self) | ||
| 391 | { | ||
| 392 | next if ($key =~ /^_C__/); # ignore control entries. | ||
| 393 | |||
| 394 | my $tmp = $self->{$key}; | ||
| 395 | $entries = $#$tmp; | ||
| 396 | $entries++; | ||
| 397 | last; | ||
| 398 | } | ||
| 399 | |||
| 400 | my($ix); | ||
| 401 | for ($ix=0; $ix < $entries; $ix++) | ||
| 402 | { | ||
| 403 | my($key); | ||
| 404 | foreach $key (keys %$self) | ||
| 405 | { | ||
| 406 | next if ($key =~ /^_C__/); # ignore control entries. | ||
| 407 | |||
| 408 | my $tmp = $self->{$key}; | ||
| 409 | print "<tr><td>$key" . "</td><td>[$ix]</td><td>@$tmp[$ix]</td><tr>\n"; | ||
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | else | ||
| 414 | { | ||
| 415 | # Walk the array of html segments. | ||
| 416 | # | ||
| 417 | my $segs = $self->{'_C__HTML__'}; | ||
| 418 | my($seg); | ||
| 419 | foreach $seg (@$segs) | ||
| 420 | { | ||
| 421 | if ($seg =~ /^__.+__$/) | ||
| 422 | { | ||
| 423 | # dump only the tokens | ||
| 424 | # | ||
| 425 | if ($seg =~ /^__[ix]_.+__$/) | ||
| 426 | { | ||
| 427 | # repeating block so treat as seperate section | ||
| 428 | # | ||
| 429 | chomp $seg; | ||
| 430 | print "<tr><td colspan=3 align=center>$seg</td></tr>\n"; | ||
| 431 | dumpit(\%$seg); | ||
| 432 | print "<tr><td colspan=3 align=center> </td></tr>\n"; | ||
| 433 | } | ||
| 434 | else | ||
| 435 | { | ||
| 436 | print "<tr><td>$seg</td><td colspan=2>" . $self->{$seg} . "</td></tr>\n"; | ||
| 437 | } | ||
| 438 | } | ||
| 439 | } | ||
| 440 | } | ||
| 441 | |||
| 442 | } | ||
| 443 | |||
| 444 | sub bailout() | ||
| 445 | { | ||
| 446 | my $mess = splice @_; | ||
| 447 | |||
| 448 | my($retVal) =<<HTML10; | ||
| 449 | content-type: text/html | ||
| 450 | |||
| 451 | <html><head></head> | ||
| 452 | <body bgcolor=red> | ||
| 453 | <p> | ||
| 454 | <font color=white> | ||
| 455 | <h3>Template Error!</h3> | ||
| 456 | <center> | ||
| 457 | $mess | ||
| 458 | </center> | ||
| 459 | </p> | ||
| 460 | <hr> | ||
| 461 | </body></html> | ||
| 462 | HTML10 | ||
| 463 | |||
| 464 | print $retVal; | ||
| 465 | croak "Template Error : $mess"; | ||
| 466 | } | ||
| 467 | |||
| 468 | sub DESTROY() | ||
| 469 | { | ||
| 470 | } | ||
| 471 | 1; | ||
| 472 | |||
| 473 | __END__ |