~jamesmikedupont/introspectorreader/wikipedia-strategy

« back to all changes in this revision

Viewing changes to PDF/PDF/FromHTML/Template/Container/Conditional.pm

  • Committer: Michael DuPont
  • Date: 2009-10-10 16:42:24 UTC
  • Revision ID: mdupont@mdupontdesktop2-20091010164224-9vcctg96e5opjry0
update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package PDF::FromHTML::Template::Container::Conditional;
 
2
 
 
3
#GGG Convert <conditional> to be a special case of <switch>?
 
4
 
 
5
use strict;
 
6
 
 
7
BEGIN {
 
8
    use vars qw(@ISA);
 
9
    @ISA = qw(PDF::FromHTML::Template::Container);
 
10
 
 
11
    use PDF::FromHTML::Template::Container;
 
12
}
 
13
 
 
14
my %isOp = (
 
15
    '='  => '==',
 
16
    (map { $_ => $_ } ( '>', '<', '==', '!=', '>=', '<=' )),
 
17
    (map { $_ => $_ } ( 'gt', 'lt', 'eq', 'ne', 'ge', 'le' )),
 
18
);
 
19
 
 
20
# This cannot be within a should_render() function because the conditional needs
 
21
# to return true even if the conditional is false. We are indicating that this
 
22
# branch has done everything it needs to do, not that this branch is calling for
 
23
# a pagebreak.
 
24
 
 
25
sub conditional_passes
 
26
{
 
27
    my $self = shift;
 
28
    my ($context) = @_;
 
29
 
 
30
    my $name = $context->get($self, 'NAME');
 
31
    return 0 unless $name =~ /\S/;
 
32
 
 
33
    my $val = $context->param($name);
 
34
    $val = @{$val} while UNIVERSAL::isa($val, 'ARRAY');
 
35
    $val = ${$val} while UNIVERSAL::isa($val, 'SCALAR');
 
36
 
 
37
    my $istrue = (defined $val && $val) ? 1 : 0;
 
38
    my $value = $context->get($self, 'VALUE');
 
39
    if (defined $value)
 
40
    {
 
41
        my $op = $context->get($self, 'OP');
 
42
        $op = defined $op && exists $isOp{$op}
 
43
            ? $isOp{$op}
 
44
            : '==';
 
45
 
 
46
        my $res;
 
47
        for ($op)
 
48
        {
 
49
            /^>$/  && do { $res = ($val > $value);  last };
 
50
            /^<$/  && do { $res = ($val < $value);  last };
 
51
            /^==$/ && do { $res = ($val == $value); last };
 
52
            /^!=$/ && do { $res = ($val != $value); last };
 
53
            /^>=$/ && do { $res = ($val >= $value); last };
 
54
            /^<=$/ && do { $res = ($val <= $value); last };
 
55
            /^gt$/ && do { $res = ($val gt $value); last };
 
56
            /^lt$/ && do { $res = ($val lt $value); last };
 
57
            /^eq$/ && do { $res = ($val eq $value); last };
 
58
            /^ne$/ && do { $res = ($val ne $value); last };
 
59
            /^ge$/ && do { $res = ($val ge $value); last };
 
60
            /^le$/ && do { $res = ($val le $value); last };
 
61
 
 
62
            die "Unknown operator '$op' in conditional resolve", $/;
 
63
        }
 
64
 
 
65
        return 1;
 
66
    }
 
67
    elsif (my $is = uc $context->get($self, 'IS'))
 
68
    {
 
69
        if ($is eq 'TRUE')
 
70
        {
 
71
            return $istrue;
 
72
        }
 
73
        else
 
74
        {
 
75
            warn "Conditional 'is' value was [$is], defaulting to 'FALSE'" . $/
 
76
                if $is ne 'FALSE';
 
77
 
 
78
            return !$istrue;
 
79
        }
 
80
    }
 
81
 
 
82
    return $istrue;
 
83
}
 
84
 
 
85
sub render
 
86
{
 
87
    my $self = shift;
 
88
    my ($context) = @_;
 
89
 
 
90
    return 0 unless $self->should_render($context);
 
91
 
 
92
    return 1 unless $self->conditional_passes($context);
 
93
 
 
94
    return $self->iterate_over_children($context);
 
95
}
 
96
 
 
97
sub max_of
 
98
{
 
99
    my $self = shift;
 
100
    my ($context, $attr) = @_;
 
101
 
 
102
    return 0 unless $self->conditional_passes($context);
 
103
 
 
104
    return $self->SUPER::max_of($context, $attr);
 
105
}
 
106
 
 
107
sub total_of
 
108
{
 
109
    my $self = shift;
 
110
    my ($context, $attr) = @_;
 
111
 
 
112
    return 0 unless $self->conditional_passes($context);
 
113
 
 
114
    return $self->SUPER::total_of($context, $attr);
 
115
}
 
116
 
 
117
sub _do_page
 
118
{
 
119
    my $self = shift;
 
120
    return unless $self->conditional_passes(@_);
 
121
    return $self->SUPER::_do_page( @_ );
 
122
}
 
123
 
 
124
sub begin_page
 
125
{
 
126
    _do_page(@_,'begin_page');
 
127
}
 
128
 
 
129
sub end_page
 
130
{
 
131
    _do_page(@_,'end_page');
 
132
}
 
133
 
 
134
1;
 
135
__END__
 
136
 
 
137
=head1 NAME
 
138
 
 
139
PDF::FromHTML::Template::Container::Conditional
 
140
 
 
141
=head1 PURPOSE
 
142
 
 
143
To conditionally allow children to render
 
144
 
 
145
=head1 NODE NAME
 
146
 
 
147
CONDITIONAL
 
148
IF (an alias for CONDITIONAL)
 
149
 
 
150
=head1 INHERITANCE
 
151
 
 
152
PDF::FromHTML::Template::Container
 
153
 
 
154
=head1 ATTRIBUTES
 
155
 
 
156
=over 4
 
157
 
 
158
=item * NAME - Required. This is a parameter name, whose value will determine
 
159
if the conditional passed or fails. If NAME is not specified, the conditional
 
160
will consider to always fail.
 
161
 
 
162
=item * OP - defaults to == (numeric equality). If VALUE is specified, this will
 
163
be how NAME and VALUE are compared. OP can be any of the 6 numeric comparision
 
164
operators or the 6 string comparision operators.
 
165
 
 
166
=item * VALUE - if this is specified, OP will be checked. This is a standard
 
167
attribute, so if you want a parameter, prepend it with '$'.
 
168
 
 
169
=item * IS - If there is no VALUE attribute, this will be checked. IS can be
 
170
either 'FALSE' or 'TRUE'. The boolean of NAME will be compared and the
 
171
conditional will branch appropriately. If NAME has no value, this will fail.
 
172
 
 
173
=item * NONE - If there is no IS and no VALUE, then an attempt will be made to
 
174
find the variable defined by NAME. If it exists and is true, the condition
 
175
will succeed. Otherwise, it will fail.
 
176
 
 
177
=back
 
178
 
 
179
=head1 CHILDREN
 
180
 
 
181
None
 
182
 
 
183
=head1 AFFECTS
 
184
 
 
185
Nothing
 
186
 
 
187
=head1 DEPENDENCIES
 
188
 
 
189
None
 
190
 
 
191
=head1 USAGE
 
192
 
 
193
  <if name="__PAGE__" OP="!=" VALUE="__LAST_PAGE__">
 
194
    ... Children execute if the current page is not the last page ...
 
195
  </if>
 
196
 
 
197
  <if name="Param1" OP="eq" VALUE="$Param2">
 
198
    ... Children execute if Param1 is string-wise equals to Param2 ...
 
199
  </if>
 
200
 
 
201
=head1 AUTHOR
 
202
 
 
203
Rob Kinyon (rkinyon@columbus.rr.com)
 
204
 
 
205
=head1 SEE ALSO
 
206
 
 
207
=cut