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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by wakaba, Thu Apr 3 01:08:16 2003 UTC revision 1.14 by wakaba, Wed Mar 10 06:58:10 2004 UTC
# Line 1  Line 1 
 # -*- perl -*-  
   
1  =head1 NAME  =head1 NAME
2    
3  SuikaWiki::Plugin --- SuikaWiki: Plugin manager and the interface to the Wiki database from plugins  SuikaWiki::Plugin - SuikaWiki: WikiPlugin common interface
4    
5    =head1 DESCRIPTION
6    
7    This module provides WikiPlugin interface implementation.
8    
9    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    
13    This module is part of SuikaWiki.
14    
15  =cut  =cut
16    
17  package SuikaWiki::Plugin;  package SuikaWiki::Plugin;
18  use strict;  use strict;
19  our $VERSION = do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION = do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
 our ($plugin_directory, %List, %Index, %Cache);  
 push @main::INC, $plugin_directory.'/../..';  
20    
21  =head1 PLUGIN INTERFACE  =head1 CLASS METHODS
22    
23  =over 4  =over 4
24    
25  =item $o->new_index ($index_name)  =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    
32    {my %Formatter;
33    sub formatter ($$) {
34      my $t = $_[1]; $t =~ tr/-/_/;
35      unless ($Formatter{$t}) {
36        $Formatter{$t} = new SuikaWiki::Plugin::xml_formatter
37                           -category_name => $t;
38      }
39      return $Formatter{$t};
40    }}
41    
42    {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    {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    =item $package = SuikaWiki::Plugin->module_package ($plugin_name)
63    
64  Increments the index number and returns the new index number.  Returns package name of the plugin module named $plugin_name.
65    
66  =cut  =cut
67    
68  sub new_index ($$) { ++$Index{$_[1]} }  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        throw SuikaWiki::Plugin::error
75          -type => 'PLUGIN_NOT_FOUND',
76          module => $module,
77          -object => $self, method => 'module_package';
78      }
79    }
80    
81  =item $o->magic_and_conten ($document)  =item SuikaWiki::Plugin->load_directory ($dir1, $dir2,...)
82    
83  Splits the documents into the magic line and content part.  Loads plugin modules in specified directories.
84    
85  =cut  =cut
86    
87  sub magic_and_content ($$) {  sub load_directory ($;@) {
88    my ($magic, $page) = ('', $_[1]);    my $self = shift;
89    $magic = $1 if $page =~ s!^((?:\#\?|/\*|<\?)[^\x02\x0A\x0D]+)[\x02\x0A\x0D]+!!s;    for my $dir (@_) {
90    ($magic, $page);      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        for (grep {substr ($_, -3) eq '.pm'} readdir PDIR) {
96          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  }  }
108    
109  =head1 PLUGIN MANAGER  =back
110    
111    =head1 WIKIPLUGIN MANAGER OBJECT
112    
113    This part implements WikiPlugin manager object of SuikaWiki WikiPlugin
114    implementation model, second edition.
115    
116    Note that class methods such as C<< ->load_directory >>
117    and C<< ->formatter >> as also available as methods of WikiPlugin object,
118    although result of those operation is still "global".
119    
120    WikiPlugin manager object is usually instantiated by C<init_plugins>
121    method of Wiki implementation object (C<SuikaWiki::Implementation> module).
122    
123      $wiki = <SuikaWiki::Implementation instance>;
124      $wiki->init_plugins;
125      $plugin_manager = $wiki->{plugin};
126    
127  =over 4  =over 4
128    
129  =item SuikaWiki::Plugin::import_plugins  =item $p = SuikaWiki::Plugin->new (wiki => $WIKI)
130    
131  Searchs plugins into $plugin_directory and loads all plugins found.  Constructs new instance of WikiPlugin manager
 This method should be called at least one time before plugin-required-features  
 are used.  
132    
133  =cut  =cut
134    
135  sub import_plugins () {  sub new ($;%) {
136      opendir PDIR, $plugin_directory;    my $class = shift;
137      my @plugin = grep {s/\.pm$//} readdir (PDIR);    bless {@_}, $class;
     closedir PDIR;  
     for (@plugin) {  
         eval qq{ use SuikaWiki::Plugin::$_ } unless /[^A-Za-z0-9_]/;  
         push @{$List{_all}}, qq(SuikaWiki::Plugin::$_);  
     }  
138  }  }
139    
140  =item SuikaWiki::Plugin::regist ($plugin_package, @categories)  sub exit ($) {
141      my $self = shift;
142      delete $self->{wiki};
143      $self->{exited} = 1;
144    }
145    
146  Regists the plugin categories provided by the plugin package.  sub DESTROY ($) {
147  This method should be called from plugin packages.    my $self = shift;
148      $self->exit unless $self->{exited};
149    }
150    
151  Example:  =item $p->{wiki}
152    
153    sub import {  Wiki implementation $p is associated with
154      my $self = shift;  
155      $self->SuikaWiki::Plugin::regist (qw/wikiform_input/);  =back
   }  
156    
157  =cut  =cut
158    
159  sub regist ($@) {  package SuikaWiki::Plugin::xml_formatter;
160      my $pack = shift;  require Message::Util::Formatter::Node;
161      for (@_) {  our @ISA = 'Message::Util::Formatter::Node';
162          push @{$List{$_}}, $pack;  require Message::Markup::XML::Node;
163      }  
164    sub ___rule_def ($) {
165      $SuikaWiki::Plugin::Rule{$_[0]->{-category_name}} || {};
166  }  }
167    
168    sub replace_option () {+{
169        -class => 'Message::Markup::XML::Node',
170    }}
171    
172    package SuikaWiki::Plugin::text_formatter;
173    require Message::Util::Formatter::Text;
174    our @ISA = 'Message::Util::Formatter::Text';
175    
176    sub ___rule_def ($) {
177      $SuikaWiki::Plugin::Rule{$_[0]->{-category_name}} || {};
178    }
179    
180    package SuikaWiki::Plugin::boolean_formatter;
181    require Message::Util::Formatter::Boolean;
182    our @ISA = 'Message::Util::Formatter::Boolean';
183    
184    sub ___rule_def ($) {
185      $SuikaWiki::Plugin::Rule{$_[0]->{-category_name}} || {};
186    }
187    
188    package SuikaWiki::Plugin::error;
189    require Message::Util::Error;
190    our @ISA = 'Message::Util::Error';
191    
192    =head1 EXCEPTIONS
193    
194    =over 4
195    
196    =item PLUGIN_COMPILE_ERROR
197    
198    Something wrong while loading WikiPlugin module file C<file>,
199    because of C<message>.
200    
201    =item PLUGIN_DIRECTORY_CANT_OPEN
202    
203    WikiPlugin directory C<file> cannot be opened, because of C<message>.
204    
205    =item PLUGIN_NOT_FOUND
206    
207    WikiPlugin module C<module> not found.
208    
209    =cut
210    
211    sub ___error_def () {+{
212      PLUGIN_COMPILE_ERROR => {
213        description => q(%t(name => method);: %t(name => file);: %t(name => message);),
214      },
215      PLUGIN_DIRECTORY_CANT_OPEN => {
216        description => q(%t(name => method);: %t(name => file);: %t(name => message);),
217      },
218      PLUGIN_NOT_FOUND => {
219        description => q(%t(name => method);: WikiPlugin module "%t(name => module);" not loaded),
220      },
221    }}
222    
223  =back  =back
224    
225  =head1 SEE ALSO  =head1 SEE ALSO
226    
227  suikawiki.pl, suikawiki-config.ph, <IW:SuikaWiki:SuikaWiki>  <IW:SuikaWiki:SuikaWiki:WikiPlugin>
228    
229    C<lib/SuikaWiki/Plugin/*>,
230    C<misc/pluging/*>
231    
232  =head1 LICENSE  =head1 LICENSE
233    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.14

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24