1 |
|
2 |
=head1 NAME |
3 |
|
4 |
SuikaWiki::View::Implementation --- SuikaWiki: WikiView implementation |
5 |
|
6 |
=cut |
7 |
|
8 |
package SuikaWiki::View::Implementation; |
9 |
use strict; |
10 |
our $VERSION = do{my @r=(q$Revision: 1.8 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
11 |
our @CommonViewDefs; |
12 |
|
13 |
=head1 METHODS |
14 |
|
15 |
=over 4 |
16 |
|
17 |
=item $v = SuikaWiki::View->new |
18 |
|
19 |
Constructs new instance of WikiView implementation |
20 |
|
21 |
=cut |
22 |
|
23 |
sub new ($%) { |
24 |
my $class = shift; |
25 |
my $self = bless {@_, definition => {}}, $class; |
26 |
|
27 |
$self; |
28 |
} |
29 |
|
30 |
sub select ($$$) { |
31 |
my ($self, $mode, $opt) = @_; |
32 |
$mode =~ s/(?<=.)-/_/g; |
33 |
my $views = $self->{definition}->{$mode} || []; |
34 |
my @views; |
35 |
VIEW: for my $view (@$views) { |
36 |
my $point = 0; |
37 |
COND: for (keys %{$view->{condition}}) { |
38 |
next if $_ eq 'mode'; |
39 |
if (ref ($view->{condition}->{$_}) eq 'ARRAY') { |
40 |
for my $item (@{$view->{condition}->{$_}}) { |
41 |
if ($item eq $opt->{condition}->{$_}) { |
42 |
$point++; |
43 |
next COND; |
44 |
} |
45 |
} |
46 |
next VIEW; # no match |
47 |
} elsif ($opt->{condition}->{$_} eq $view->{condition}->{$_}) { |
48 |
$point++; |
49 |
} else { |
50 |
next VIEW; # no match |
51 |
} |
52 |
} |
53 |
$views[$point] ||= []; |
54 |
push @{$views[$point]}, $view; |
55 |
} |
56 |
|
57 |
for (reverse 0..$#views) { |
58 |
for my $view (@{$views[$_]||[]}) { |
59 |
return $view if !$view->{check} || &{$view->{check}} ($self, $opt); |
60 |
} |
61 |
} |
62 |
|
63 |
if ($mode ne '#default') { |
64 |
return $self->select ('#default', $opt); |
65 |
} else { |
66 |
return undef; |
67 |
} |
68 |
} |
69 |
|
70 |
sub instantiate ($$$) { |
71 |
my ($self, $mode, $opt) = @_; |
72 |
my $view = $self->select ($mode, $opt) or return undef; |
73 |
unless (ref $view->{object}) { |
74 |
if ($view->{object_class}) { |
75 |
$view->{object} = $view->{object_class}->new (view => $self, |
76 |
viewdef => $view, |
77 |
opt => $opt); |
78 |
} else { |
79 |
$view->{object} = &{$view->{object_init}} ($self, $opt, $view); |
80 |
} |
81 |
} |
82 |
$view->{object}->{viewdef} = $view; |
83 |
return $view->{object}; |
84 |
} |
85 |
|
86 |
|
87 |
sub register_mode ($$$) { |
88 |
my ($self, $mode, $view) = @_; |
89 |
$view->{condition}->{mode} ||= $mode; |
90 |
$self->{definition}->{$mode} ||= []; |
91 |
push @{$self->{definition}->{$mode}}, $view; |
92 |
} |
93 |
|
94 |
|
95 |
sub init_db ($$) { |
96 |
my ($self, $opt) = @_; |
97 |
$self->{wiki}->init_db; |
98 |
} |
99 |
|
100 |
sub register_common_modes ($) { |
101 |
my ($self) = @_; |
102 |
for (@CommonViewDefs) { |
103 |
$self->register_mode ($_->{condition}->{mode} => $_); |
104 |
} |
105 |
} |
106 |
|
107 |
our %TemplateFragment; |
108 |
sub assemble_template ($$) { |
109 |
my ($self, $name) = @_; |
110 |
$name =~ tr/-/_/; |
111 |
my $template = join '', |
112 |
map {$_->{Main}} |
113 |
sort {$a->{Order} <=> $b->{Order}} |
114 |
@{$TemplateFragment{$name}||[]}; |
115 |
$template; |
116 |
} |
117 |
|
118 |
sub ___report_error ($$) { |
119 |
my ($view, $err) = @_; |
120 |
$view->{wiki}->___raise_event (name => 'view_error', |
121 |
argv => $err, argv_name => 'error') |
122 |
and $err->throw;; |
123 |
} |
124 |
|
125 |
sub exit ($) { |
126 |
my $self = shift; |
127 |
delete $self->{wiki}; |
128 |
delete $self->{definition}; |
129 |
$self->{exited} = 1; |
130 |
1; |
131 |
} |
132 |
|
133 |
sub DESTROY ($) { |
134 |
my $self = shift; |
135 |
$self->exit unless $self->{exited}; |
136 |
} |
137 |
|
138 |
=item $v->{definition}->{ $mode } = [{ condition => {mode => $mode,...}, ... },...] |
139 |
|
140 |
=item $v->{wiki} |
141 |
|
142 |
Instance of wiki implementation |
143 |
|
144 |
=back |
145 |
|
146 |
=cut |
147 |
|
148 |
package SuikaWiki::View::template; |
149 |
|
150 |
sub new ($%) { |
151 |
my $class = shift; |
152 |
bless {@_}, $class; |
153 |
} |
154 |
|
155 |
sub main ($$) { |
156 |
my ($self, $opt) = @_; |
157 |
my $opt2 = {}; |
158 |
$self->main_pre ($opt, $opt2) |
159 |
&& |
160 |
1 |
161 |
&& |
162 |
$self->main_post ($opt, $opt2); |
163 |
} |
164 |
|
165 |
sub main_pre ($$$) { |
166 |
1; |
167 |
} |
168 |
|
169 |
sub main_post ($$$) { |
170 |
1; |
171 |
} |
172 |
|
173 |
package SuikaWiki::View::Implementation::error; |
174 |
require Message::Util::Error; |
175 |
our @ISA = 'Message::Util::Error'; |
176 |
|
177 |
sub ___error_def () {+{ |
178 |
VIEW_NOT_DEFINED => { |
179 |
description => q(View (%condition (separator => ", ");): View not defined), |
180 |
level => 'fatal', |
181 |
}, |
182 |
WARN_VIEW_NOT_DEFINED => { |
183 |
description => q(View (%condition (separator => ", ");): View not defined), |
184 |
}, |
185 |
ERROR_REPORTED => { |
186 |
level => 'fatal', |
187 |
}, |
188 |
}} |
189 |
|
190 |
sub _FORMATTER_PACKAGE_ () { 'SuikaWiki::View::Implementation::error::formatter' } |
191 |
|
192 |
package SuikaWiki::View::Implementation::error::formatter; |
193 |
push our @ISA, 'Message::Util::Error::formatter'; |
194 |
|
195 |
sub ___rule_def () {+{ |
196 |
condition => { |
197 |
after => sub { |
198 |
my ($f, $name, $p, $o) = @_; |
199 |
if ($p->{name}) { |
200 |
$p->{-result} .= $o->{condition}->{$p->{name}}; |
201 |
} else { |
202 |
$p->{-result} .= join $p->{separator} || '; ', |
203 |
map {$_.($p->{vi}||'=').$o->{condition}->{$_}} |
204 |
keys %{$o->{condition}}; |
205 |
} |
206 |
}, |
207 |
}, |
208 |
}} |
209 |
|
210 |
=head1 LICENSE |
211 |
|
212 |
Copyright 2003 Wakaba <w@suika.fam.cx> |
213 |
|
214 |
This program is free software; you can redistribute it and/or |
215 |
modify it under the same terms as Perl itself. |
216 |
|
217 |
=cut |
218 |
|
219 |
1; # $Date: 2004/01/16 08:02:05 $ |