# JAM22 TRACKS # Copyright (c) 2004 Denis Petrov # $Id: magic.pl,v 1.4 2005/01/26 14:03:39 Owner Exp $ # Distributed under the terms of the GNU General Public License # # Template Processor use strict; use Data::Dumper; package main; sub Magic { my %labels; my @parsed = $_[0] =~ /(<%.+?(?=%>)%>)(.*?)(?=<%|$)/gs; # print Dumper(\@parsed); my $i = 0; my $len = scalar(@parsed); for ( $i = 0; $i < $len; $i++ ) { my $l = $parsed[$i]; if ( $l =~ /<%(\w+)%>/ ) { $labels{$1} = $i; } elsif ( $l =~ /<%(.+)%>/ ) { my $result = eval($1); if ( !defined($result) ) { warn("$@\n TEMPLATE CODE:\n$l\nEND TEMPLATE CODE\n"); } elsif ( $result =~ /\w+/ ) { if ( exists $labels{$result} ) { $i = $labels{$result}; } else { #trace labels forward my $j; for ( $j = $i+1; $j < $len; $j++ ) { if ( $parsed[$j] =~ /<%(\w+)%>/ ) { $labels{$1} = $j; if ( $1 eq $result ) { $i = $j; last; } } } } } } else { $l =~ s/\"/\\\"/g; my $result = eval("\"".$l."\""); if ( !defined($result) ) { warn("$@\n TEMPLATE TEXT:\n$l\nEND TEMPLATE TEXT\n"); } else { $Q->print( $result ); } } } } 1;