/[pub]/suikawiki/script/lib/SuikaWiki/View/Implementation.pm
Suika

Contents of /suikawiki/script/lib/SuikaWiki/View/Implementation.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations) (download)
Fri Jan 16 08:02:05 2004 UTC (22 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.7: +38 -4 lines
(__report_error): New function

1 wakaba 1.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 wakaba 1.8 our $VERSION = do{my @r=(q$Revision: 1.7 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
11 wakaba 1.1 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 wakaba 1.8 $mode =~ s/(?<=.)-/_/g;
33 wakaba 1.1 my $views = $self->{definition}->{$mode} || [];
34     my @views;
35     VIEW: for my $view (@$views) {
36     my $point = 0;
37 wakaba 1.2 COND: for (keys %{$view->{condition}}) {
38 wakaba 1.1 next if $_ eq 'mode';
39 wakaba 1.2 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 wakaba 1.1 $point++;
49     } else {
50 wakaba 1.2 next VIEW; # no match
51 wakaba 1.1 }
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 wakaba 1.2 our %TemplateFragment;
108     sub assemble_template ($$) {
109     my ($self, $name) = @_;
110 wakaba 1.4 $name =~ tr/-/_/;
111 wakaba 1.5 my $template = join '',
112 wakaba 1.2 map {$_->{Main}}
113     sort {$a->{Order} <=> $b->{Order}}
114 wakaba 1.3 @{$TemplateFragment{$name}||[]};
115 wakaba 1.5 $template;
116 wakaba 1.2 }
117    
118 wakaba 1.8 sub ___report_error ($$) {
119     my ($view, $err) = @_;
120     $view->{wiki}->___raise_event ('view_error', $err, argv_name => 'error')
121     and $err->throw;;
122     }
123    
124 wakaba 1.7 sub exit ($) {
125     my $self = shift;
126     delete $self->{wiki};
127     delete $self->{definition};
128     $self->{exited} = 1;
129     1;
130     }
131    
132     sub DESTROY ($) {
133     my $self = shift;
134     $self->exit unless $self->{exited};
135     }
136    
137 wakaba 1.1 =item $v->{definition}->{ $mode } = [{ condition => {mode => $mode,...}, ... },...]
138    
139     =item $v->{wiki}
140    
141     Instance of wiki implementation
142    
143     =back
144    
145     =cut
146    
147     package SuikaWiki::View::template;
148    
149     sub new ($%) {
150     my $class = shift;
151     bless {@_}, $class;
152     }
153    
154     sub main ($$) {
155     my ($self, $opt) = @_;
156 wakaba 1.2 my $opt2 = {};
157     $self->main_pre ($opt, $opt2)
158     &&
159     1
160     &&
161     $self->main_post ($opt, $opt2);
162     }
163    
164     sub main_pre ($$$) {
165     1;
166     }
167    
168     sub main_post ($$$) {
169     1;
170 wakaba 1.1 }
171    
172 wakaba 1.6 package SuikaWiki::View::Implementation::error;
173     require Message::Util::Error;
174     our @ISA = 'Message::Util::Error';
175    
176 wakaba 1.8 sub ___error_def () {+{
177     VIEW_NOT_DEFINED => {
178     description => q(View (%condition (separator => ", ");): View not defined),
179     level => 'fatal',
180     },
181     WARN_VIEW_NOT_DEFINED => {
182     description => q(View (%condition (separator => ", ");): View not defined),
183     },
184 wakaba 1.6 ERROR_REPORTED => {
185 wakaba 1.8 level => 'fatal',
186     },
187     }}
188    
189     sub _FORMATTER_PACKAGE_ () { 'SuikaWiki::View::Implementation::error::formatter' }
190    
191     package SuikaWiki::View::Implementation::error::formatter;
192     push our @ISA, 'Message::Util::Error::formatter';
193    
194     sub ___rule_def () {+{
195     condition => {
196     after => sub {
197     my ($f, $name, $p, $o) = @_;
198     if ($p->{name}) {
199     $p->{-result} .= $o->{condition}->{$p->{name}};
200     } else {
201     $p->{-result} .= join $p->{separator} || '; ',
202     map {$_.($p->{vi}||'=').$o->{condition}->{$_}}
203     keys %{$o->{condition}};
204     }
205     },
206 wakaba 1.6 },
207     }}
208    
209 wakaba 1.1 =head1 LICENSE
210    
211     Copyright 2003 Wakaba <[email protected]>
212    
213     This program is free software; you can redistribute it and/or
214     modify it under the same terms as Perl itself.
215    
216     =cut
217    
218 wakaba 1.8 1; # $Date: 2003/12/06 05:31:58 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24