/[pub]/suikawiki/script/lib/SuikaWiki/Plugin.pm
Suika

Contents of /suikawiki/script/lib/SuikaWiki/Plugin.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (hide annotations) (download)
Fri Mar 12 04:57:39 2004 UTC (21 years, 1 month ago) by wakaba
Branch: MAIN
Branch point for: paragraph-200404
Changes since 1.14: +16 -2 lines
(load_file): New method

1 wakaba 1.10 =head1 NAME
2    
3     SuikaWiki::Plugin - SuikaWiki: WikiPlugin common interface
4    
5     =head1 DESCRIPTION
6    
7     This module provides WikiPlugin interface implementation.
8 wakaba 1.1
9 wakaba 1.10 There are two interface: class methods collection and WikiPlugin object.
10     Class methods collection provides some useful facilities to be
11     called by WikiPlugin module and/or by contexts using WikiPlugin.
12 wakaba 1.1
13 wakaba 1.10 This module is part of SuikaWiki.
14 wakaba 1.1
15     =cut
16    
17     package SuikaWiki::Plugin;
18     use strict;
19 wakaba 1.15 our $VERSION = do{my @r=(q$Revision: 1.14 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
20 wakaba 1.1
21 wakaba 1.10 =head1 CLASS METHODS
22 wakaba 1.1
23     =over 4
24    
25 wakaba 1.10 =item $formatter = SuikaWiki::Plugin->formatter ($category_name)
26    
27     Returns an instance of formatter whose category (context) is
28     $category_name.
29    
30     =cut
31 wakaba 1.1
32 w 1.5 {my %Formatter;
33     sub formatter ($$) {
34     my $t = $_[1]; $t =~ tr/-/_/;
35 wakaba 1.7 unless ($Formatter{$t}) {
36 wakaba 1.8 $Formatter{$t} = new SuikaWiki::Plugin::xml_formatter
37     -category_name => $t;
38 wakaba 1.7 }
39 w 1.5 return $Formatter{$t};
40     }}
41    
42 wakaba 1.12 {my %Formatter;
43     sub text_formatter ($$) {
44     my $t = $_[1]; $t =~ tr/-/_/;
45     unless ($Formatter{$t}) {
46     $Formatter{$t} = new SuikaWiki::Plugin::text_formatter
47     -category_name => $t;
48     }
49     return $Formatter{$t};
50     }}
51    
52 wakaba 1.14 {my %Formatter;
53     sub boolean_formatter ($$) {
54     my $t = $_[1]; $t =~ tr/-/_/;
55     unless ($Formatter{$t}) {
56     $Formatter{$t} = new SuikaWiki::Plugin::boolean_formatter
57     -category_name => $t;
58     }
59     return $Formatter{$t};
60     }}
61    
62 wakaba 1.10 =item $package = SuikaWiki::Plugin->module_package ($plugin_name)
63    
64     Returns package name of the plugin module named $plugin_name.
65    
66     =cut
67    
68 wakaba 1.8 sub module_package ($$) {
69     my ($self, $module) = @_;
70     my $pack = $SuikaWiki::Plugin::Registry::Info{$module}->{module_name};
71     if ($pack) {
72     return $pack;
73     } else {
74 wakaba 1.10 throw SuikaWiki::Plugin::error
75     -type => 'PLUGIN_NOT_FOUND',
76     module => $module,
77     -object => $self, method => 'module_package';
78 wakaba 1.8 }
79     }
80    
81 wakaba 1.10 =item SuikaWiki::Plugin->load_directory ($dir1, $dir2,...)
82    
83     Loads plugin modules in specified directories.
84    
85     =cut
86 wakaba 1.6
87 wakaba 1.10 sub load_directory ($;@) {
88     my $self = shift;
89     for my $dir (@_) {
90     opendir PDIR, $dir or throw SuikaWiki::Plugin::error
91     -type => 'PLUGIN_DIRECTORY_CANT_OPEN',
92     file => $dir,
93     message => $!,
94     -object => $self, method => 'load_directory';
95 wakaba 1.13 for (grep {substr ($_, -3) eq '.pm'} readdir PDIR) {
96 wakaba 1.10 eval { require $dir.'/'.$_ };
97     if ($@) {
98     throw SuikaWiki::Plugin::error
99     -type => 'PLUGIN_COMPILE_ERROR',
100     file => $dir.'/'.$_,
101     message => $@,
102     method => 'load_directory', -object => $self;
103     }
104     }
105     closedir PDIR;
106     }
107 wakaba 1.1 }
108    
109 wakaba 1.15 sub load_file ($@) {
110     my $self = shift;
111     for (@_) {
112     eval { require $_.'.pm' };
113     if ($@) {
114     throw SuikaWiki::Plugin::error
115     -type => 'PLUGIN_COMPILE_ERROR',
116     file => $_.'.pm',
117     message => $@,
118     method => 'load_file', -object => $self;
119     }
120     }
121     }
122    
123 wakaba 1.1 =back
124    
125 wakaba 1.6 =head1 WIKIPLUGIN MANAGER OBJECT
126    
127     This part implements WikiPlugin manager object of SuikaWiki WikiPlugin
128     implementation model, second edition.
129    
130 wakaba 1.10 Note that class methods such as C<< ->load_directory >>
131     and C<< ->formatter >> as also available as methods of WikiPlugin object,
132     although result of those operation is still "global".
133    
134     WikiPlugin manager object is usually instantiated by C<init_plugins>
135     method of Wiki implementation object (C<SuikaWiki::Implementation> module).
136    
137     $wiki = <SuikaWiki::Implementation instance>;
138     $wiki->init_plugins;
139     $plugin_manager = $wiki->{plugin};
140    
141 wakaba 1.6 =over 4
142    
143     =item $p = SuikaWiki::Plugin->new (wiki => $WIKI)
144    
145     Constructs new instance of WikiPlugin manager
146    
147     =cut
148    
149 wakaba 1.10 sub new ($;%) {
150 wakaba 1.6 my $class = shift;
151 wakaba 1.10 bless {@_}, $class;
152 wakaba 1.6 }
153    
154 wakaba 1.11 sub exit ($) {
155     my $self = shift;
156     delete $self->{wiki};
157     $self->{exited} = 1;
158     }
159    
160     sub DESTROY ($) {
161     my $self = shift;
162     $self->exit unless $self->{exited};
163     }
164    
165 wakaba 1.6 =item $p->{wiki}
166    
167     Wiki implementation $p is associated with
168    
169     =back
170    
171 wakaba 1.8 =cut
172    
173     package SuikaWiki::Plugin::xml_formatter;
174     require Message::Util::Formatter::Node;
175     our @ISA = 'Message::Util::Formatter::Node';
176     require Message::Markup::XML::Node;
177    
178 wakaba 1.11 sub ___rule_def ($) {
179 wakaba 1.12 $SuikaWiki::Plugin::Rule{$_[0]->{-category_name}} || {};
180 wakaba 1.8 }
181    
182 wakaba 1.10 sub replace_option () {+{
183 wakaba 1.8 -class => 'Message::Markup::XML::Node',
184 wakaba 1.10 }}
185    
186 wakaba 1.12 package SuikaWiki::Plugin::text_formatter;
187     require Message::Util::Formatter::Text;
188     our @ISA = 'Message::Util::Formatter::Text';
189    
190     sub ___rule_def ($) {
191     $SuikaWiki::Plugin::Rule{$_[0]->{-category_name}} || {};
192     }
193    
194 wakaba 1.14 package SuikaWiki::Plugin::boolean_formatter;
195     require Message::Util::Formatter::Boolean;
196     our @ISA = 'Message::Util::Formatter::Boolean';
197    
198     sub ___rule_def ($) {
199     $SuikaWiki::Plugin::Rule{$_[0]->{-category_name}} || {};
200     }
201    
202 wakaba 1.10 package SuikaWiki::Plugin::error;
203     require Message::Util::Error;
204     our @ISA = 'Message::Util::Error';
205    
206     =head1 EXCEPTIONS
207    
208     =over 4
209    
210     =item PLUGIN_COMPILE_ERROR
211    
212     Something wrong while loading WikiPlugin module file C<file>,
213     because of C<message>.
214    
215     =item PLUGIN_DIRECTORY_CANT_OPEN
216    
217     WikiPlugin directory C<file> cannot be opened, because of C<message>.
218    
219     =item PLUGIN_NOT_FOUND
220    
221     WikiPlugin module C<module> not found.
222    
223     =cut
224    
225 wakaba 1.11 sub ___error_def () {+{
226 wakaba 1.10 PLUGIN_COMPILE_ERROR => {
227     description => q(%t(name => method);: %t(name => file);: %t(name => message);),
228     },
229     PLUGIN_DIRECTORY_CANT_OPEN => {
230     description => q(%t(name => method);: %t(name => file);: %t(name => message);),
231     },
232     PLUGIN_NOT_FOUND => {
233     description => q(%t(name => method);: WikiPlugin module "%t(name => module);" not loaded),
234     },
235     }}
236    
237     =back
238 wakaba 1.8
239 wakaba 1.1 =head1 SEE ALSO
240    
241 wakaba 1.10 <IW:SuikaWiki:SuikaWiki:WikiPlugin>
242    
243     C<lib/SuikaWiki/Plugin/*>,
244     C<misc/pluging/*>
245 wakaba 1.1
246     =head1 LICENSE
247    
248     Copyright 2002-2003 Wakaba <w@suika.fam.cx>
249    
250     This program is free software; you can redistribute it and/or
251     modify it under the same terms as Perl itself.
252    
253     =cut
254    
255 wakaba 1.15 1; # $Date: 2004/03/10 06:58:10 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24