| 1 | package TemplateM::GaloreWin32; # $Id: GaloreWin32.pm 2 2013-04-02 10:51:49Z abalama $ | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | use base qw/Exporter/; | ||
| 5 | use vars qw($VERSION); | ||
| 6 | our $VERSION = 2.21; | ||
| 7 | |||
| 8 | use TemplateM::Util; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | our @EXPORT = qw/html/; | ||
| 12 | |||
| 13 | sub start { | ||
| 14 | my $self = shift; | ||
| 15 | my $label = shift; | ||
| 16 | |||
| 17 | croak("[start] Incorrect call of method \"start\"") unless (defined($label)); | ||
| 18 | |||
| 19 | my $tpl = ''; | ||
| 20 | my $wrk = ''; | ||
| 21 | my $stk = []; | ||
| 22 | |||
| 23 | $tpl = $2 if $self->{work} =~ m/<!--\s*do:\s*($label)\s*-->(.*?)<!--\s*loop:\s*\1\s*-->/s; | ||
| 24 | |||
| 25 | return bless { | ||
| 26 | template => $tpl, | ||
| 27 | work => $wrk, | ||
| 28 | stackout => $stk, | ||
| 29 | label => $label, | ||
| 30 | pobj => $self, | ||
| 31 | tf => 1 | ||
| 32 | }; | ||
| 33 | } | ||
| 34 | sub loop { | ||
| 35 | my $self = shift; | ||
| 36 | my $hr = $_[0]; | ||
| 37 | |||
| 38 | croak("[loop] Incorrect call of method \"loop\"") unless (defined($hr)); | ||
| 39 | |||
| 40 | if (defined($hr) && (ref($hr) ne "HASH")) { | ||
| 41 | if (ref($hr) eq "ARRAY") { | ||
| 42 | $hr = {@$hr}; | ||
| 43 | } else { | ||
| 44 | $hr = {@_}; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | push @{$self->{stackout}}, $self->{work} if $self->{work} ne ''; | ||
| 49 | |||
| 50 | my $wrk = $self->{template}; | ||
| 51 | |||
| 52 | $wrk =~ s/<!--\s*val:\s*(\S+?)\s*-->/_exec_directive($hr,$1,'val')/ieg if defined($hr); | ||
| 53 | |||
| 54 | $self->{work} = $wrk | ||
| 55 | } | ||
| 56 | sub finish { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | push @{$self->{stackout}}, $self->{work} if defined $self->{work} && $self->{work} ne ''; | ||
| 60 | |||
| 61 | $self->{work} = ''; | ||
| 62 | |||
| 63 | my $label = $self->{label}; | ||
| 64 | my $stacklink = $self->{stackout}; | ||
| 65 | |||
| 66 | if ($self->{pobj}->{tf}) { | ||
| 67 | my $stackdata = join "", @$stacklink; | ||
| 68 | $self->{pobj}->{work} =~ s/<!--\s*do:\s*($label)\s*-->(.*?)<!--\s*loop:\s*\1\s*-->/$stackdata/s | ||
| 69 | } else { | ||
| 70 | $self->{pobj}->{looparr}->{$self->{label}} = $stacklink | ||
| 71 | } | ||
| 72 | } | ||
| 73 | sub finalize { goto &finish } | ||
| 74 | sub cast { | ||
| 75 | my $self = shift; | ||
| 76 | my $hr = $_[0]; | ||
| 77 | |||
| 78 | croak("[cast] Incorrect call of method \"cast\"") unless (defined $hr); | ||
| 79 | |||
| 80 | unless (ref($hr) eq "HASH") { | ||
| 81 | $hr = {@_}; | ||
| 82 | } | ||
| 83 | |||
| 84 | $self->{work} =~ s/<!--\s*cgi:\s*(\S+?)\s*-->/_exec_directive($hr, $1, 'cgi')/ieg; | ||
| 85 | } | ||
| 86 | sub stash { goto &cast } | ||
| 87 | sub ifelse { | ||
| 88 | my $self = shift; | ||
| 89 | my $label = shift || ''; | ||
| 90 | my $predicate = shift || 0; | ||
| 91 | |||
| 92 | croak("[efelse] Incorrect call of method \"ifelse\"") unless (defined($label)); | ||
| 93 | |||
| 94 | if ($predicate) { | ||
| 95 | $self->{work} =~ s/<!--\s*if:\s*($label)\s*-->(.*?)<!--\s*end_?if:\s*\1\s*-->/$2/igs; | ||
| 96 | $self->{work} =~ s/<!--\s*else:\s*($label)\s*-->.*?<!--\s*end_?else:\s*\1\s*-->//igs; | ||
| 97 | } else { | ||
| 98 | $self->{work} =~ s/<!--\s*else:\s*($label)\s*-->(.*?)<!--\s*end_?else:\s*\1\s*-->/$2/igs; | ||
| 99 | $self->{work} =~ s/<!--\s*if:\s*($label)\s*-->.*?<!--\s*end_?if:\s*\1\s*-->//igs; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | sub cast_if { goto &ifelse } | ||
| 103 | sub output { | ||
| 104 | my $self = shift; | ||
| 105 | my $property = shift || 'stackout'; | ||
| 106 | |||
| 107 | if (! $self->{tf} and $property eq 'stackout') { | ||
| 108 | $self->{work} =~ s/<!--\s*do:\s*(\S+?)\s*-->(.*?)<!--\s*loop:\s*\1\s*-->/_analize($self->{looparr},$1)/egs; | ||
| 109 | $self->{stackout} = [$self->{work}]; | ||
| 110 | } | ||
| 111 | if ($property eq 'stackout') { | ||
| 112 | return join "", @{$self->{stackout}} | ||
| 113 | } else { | ||
| 114 | return defined ($self->{$property}) ? $self->{$property} : '' | ||
| 115 | } | ||
| 116 | } | ||
| 117 | sub html { | ||
| 118 | my $self = shift; | ||
| 119 | my $header = $self->{header} || ''; | ||
| 120 | ($header) = read_attributes([['HEAD','HEADER']],@_) if (defined $_[0]); | ||
| 121 | |||
| 122 | return $header . $self->output() | ||
| 123 | } | ||
| 124 | sub _exec_directive { | ||
| 125 | my ($hr, $directive, $sig) = @_; | ||
| 126 | |||
| 127 | if (defined($hr->{$directive})) { | ||
| 128 | return $hr->{$directive}; | ||
| 129 | } else { | ||
| 130 | return $sig?('<!-- '.$sig.': '.$directive.' -->'):''; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | sub _analize { | ||
| 134 | my ($hr, $directive) = @_; | ||
| 135 | if (defined($hr->{$directive})) { | ||
| 136 | my $strd = $hr->{$directive}; | ||
| 137 | return join "", @$strd; | ||
| 138 | } | ||
| 139 | return '' | ||
| 140 | |||
| 141 | } | ||
| 142 | 1; | ||
| 143 |