/[pub]/suikawiki/script/bin/mkplugin2.pl
Suika

Contents of /suikawiki/script/bin/mkplugin2.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (hide annotations) (download)
Fri Jan 16 08:06:06 2004 UTC (20 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.9: +74 -35 lines
File MIME type: text/plain
Format/WikiForm, Format/NextIndex, FormattingRule/After, etc. is supported

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3 wakaba 1.10 our $VERSION = do{my @r=(q$Revision: 1.9 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4 wakaba 1.5 require Message::Markup::SuikaWikiConfig20::Parser;
5 wakaba 1.1
6 wakaba 1.2 {
7 wakaba 1.1 my $src = '';
8     my $srcfile = shift;
9     open SRC, $srcfile or die "$0: $!"; {
10     local $/ = undef;
11     $src = <SRC>;
12     } close SRC;
13    
14     sub literal ($) {
15     my $s = shift;
16 wakaba 1.2 if (ref ($s) eq 'ARRAY') {
17     q<[> . list (@$s) . q<]>;
18     } elsif (ref ($s) eq 'HASH') {
19     q<{> . hash (%$s) . q<}>;
20     } elsif (ref ($s) eq 'bare') {
21     $$s;
22     } else {
23     $s =~ s/([#\\])/\\$1/g;
24     q<q#> . $s . q<#>;
25     }
26     }
27     sub list (@) {
28     join ', ', map {literal $_} @_;
29     }
30     sub hash (%) {
31     my $i = 0;
32     list map {($i++ % 2) ? $_ : do {my $s = $_; $s =~ s/(?<=.)-/_/; $s}} @_;
33 wakaba 1.1 }
34     sub n11n ($) {
35     my $s = shift;
36     $s =~ s/\s+/ /g;
37     $s;
38     }
39 wakaba 1.2 sub m13ed_val_list ($$) {
40     my ($src, $key) = @_;
41     my @r;
42     for (@{$src->child_nodes}) {
43     if ($_->local_name eq $key) {
44     push @r, [scalar $_->inner_text,
45     scalar $_->get_attribute ('lang', make_new_node => 1)
46     ->inner_text,
47     scalar $_->get_attribute ('script', make_new_node => 1)
48     ->inner_text];
49     }
50     }
51     list @r;
52     }
53     sub barecode ($) {
54     bless \$_[0], 'bare';
55     }
56     sub code ($$) {
57     my ($Info, $code) = @_;
58     for (keys %{$Info->{const}}) {
59     $code =~ s/\$$_\b/literal $Info->{const}->{$_}/ge;
60     }
61     $code =~ s/__FUNCPACK__/$Info->{module_name}/g;
62     $code;
63     }
64 wakaba 1.5 sub change_package ($$) {
65     my ($Info, $pack) = @_;
66     unless ($Info->{current_package} eq $pack) {
67     $Info->{current_package} = $pack;
68     return qq{package $pack;\n\n};
69     } else {
70     return '';
71     }
72     }
73     sub quoted_string ($) {
74     my $s = shift;
75     $s =~ s/([\\"])/\\$1/g;
76     '"'.$s.'"';
77     }
78 wakaba 1.6 sub line ($;%) {
79     my ($Info, %opt) = @_;
80    
81     unless ($opt{file}) {
82     if ($opt{reset}) {
83     $opt{file} = sprintf '(WikiPlugin module %s, chunk %d)',
84     $Info->{Name},
85     ++$Info->{chunk_count};
86     } elsif ($opt{realfile}) {
87     $opt{file} = sprintf '(WikiPlugin module %s, chunk from %s)',
88     $Info->{Name},
89     $opt{realfile};
90     } else {
91 wakaba 1.7 $opt{file} = sprintf '(WikiPlugin module source %s, block %s)',
92     $Info->{source_file},
93 wakaba 1.6 $opt{node_path};
94     }
95     }
96    
97     $opt{file} =~ s/"/''/g;
98     sprintf '%s#line %d "%s"%s', "\n", $opt{line_no} || 1, $opt{file}, "\n";
99     }
100 wakaba 1.9 sub literal_or_code ($$) {
101     my ($Info, $s) = @_;
102     substr ($s, 0, 1) ne '{' ? literal ($s)
103     : code ($Info, substr ($s, 1, length ($s) - 2));
104     }
105 wakaba 1.1
106 wakaba 1.5 my $parser = Message::Markup::SuikaWikiConfig20::Parser->new;
107 wakaba 1.1 my $plugins = $parser->parse_text ($src);
108     my $meta = $plugins->get_attribute ('Plugin')
109     or die "$0: Required 'Plugin' section not found";
110 wakaba 1.2 my %Info = (provide => {},
111     Name => n11n $meta->get_attribute ('Name')->value);
112 wakaba 1.7 $Info{source_file} = $srcfile;
113 wakaba 1.1 $Info{name_literal} = literal $Info{Name};
114     my @date = gmtime;
115     $Info{LastModified} = sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00',
116     $date[5] + 1900, $date[4] + 1, @date[3,2,1,0];
117     $Info{Version} = sprintf '%04d.%02d%02d.%02d%02d',
118     $date[5] + 1900, $date[4] + 1, @date[3,2,1];
119 wakaba 1.2 $Info{InterfaceVersion} = '2.9.1';
120     $Info{mkpluginVersion} = '2.'.$VERSION;
121 wakaba 1.1 $Info{module_name} = q#SuikaWiki::Plugin::plugin#;
122     $Info{module_name} = random_module_name (\%Info, $Info{Name});
123    
124     print <<EOH;
125     use strict;
126 wakaba 1.5 @{[change_package \%Info, 'SuikaWiki::Plugin::Registry']}
127 wakaba 1.1 our \%Info;
128     \$Info{$Info{name_literal}}->{Name} = $Info{name_literal};
129     EOH
130 wakaba 1.5 for (qw/Version InterfaceVersion mkpluginVersion module_name/) {
131     print qq{\$Info{$Info{name_literal}}->{$_} = @{[literal $Info{$_}]};\n};
132 wakaba 1.2 }
133     for (qw/LastModified/) {
134 wakaba 1.1 $Info{$_} = n11n $meta->get_attribute ($_,make_new_node=>1)->value;
135     next unless length $Info{$_};
136     print qq{\$Info{$Info{name_literal}}->{$_} = } . literal $Info{$_};
137     print ";\n";
138     }
139 wakaba 1.2 for (qw/RequiredPlugin RequiredModule/) {
140 wakaba 1.1 $Info{$_} = $meta->get_attribute ($_);
141     next unless ref $Info{$_}; $Info{$_} = $Info{$_}->value;
142     print qq{\$Info{$Info{name_literal}}->{$_} = [};
143     print join ', ', map {literal $_} @{$Info{$_}};
144     print "];\n";
145     }
146 wakaba 1.2 for (qw/Description License RelatedWikiPage RelatedURI/) {
147     my $r = m13ed_val_list $meta, $_;
148     next unless $r;
149     print qq{\$Info{$Info{name_literal}}->{$_} = [$r];\n};
150     }
151    
152     print qq{\$Info{$Info{name_literal}}->{Author} = [} .( list map {
153     [
154     [ barecode m13ed_val_list ($_, 'Name') ],
155     [ $_->get_attribute ('Mail', make_new_node => 1)->value ],
156     [ $_->get_attribute ('URI', make_new_node => 1)->value ],
157     ]
158     } grep { $_->local_name eq 'Author' } @{$meta->child_nodes}
159     ). qq{];\n};
160 wakaba 1.1
161 wakaba 1.5 my $use = $meta->get_attribute ('Use');
162     if (ref $use) {
163     print change_package \%Info, $Info{module_name};
164 wakaba 1.6 print line \%Info, node_path => 'Plugin/Use';
165     print $use->inner_text, "\n";
166     print line \%Info, reset => 1;
167 wakaba 1.5 }
168    
169 wakaba 1.1 for (@{$plugins->child_nodes}) {
170 wakaba 1.2 if ($_->local_name eq 'FormattingRule') {
171     print "\n", make_rule ($_, \%Info);
172     } elsif ($_->local_name eq 'ViewDefinition') {
173 wakaba 1.1 print "\n", make_viewdef ($_, \%Info);
174     } elsif ($_->local_name eq 'ViewFragment') {
175     print "\n", make_viewfragment ($_, \%Info);
176 wakaba 1.2 } elsif ($_->local_name eq 'Function') {
177     print "\n", make_function ($_, \%Info);
178     } elsif ($_->local_name eq 'Resource') {
179     print "\n", make_resdef ($_, \%Info);
180     } elsif ($_->local_name eq 'PluginConst') {
181     register_plugin_const ($_, \%Info);
182 wakaba 1.5 } elsif ($_->local_name eq 'Format') {
183     print "\n", make_format ($_, \%Info);
184 wakaba 1.9 # Parameter
185     # PluginCategory
186 wakaba 1.1 }
187     }
188    
189 wakaba 1.5 print change_package \%Info, q(SuikaWiki::Plugin::Registry);
190 wakaba 1.2 print qq{\$Info{$Info{name_literal}}->{provide} = } . literal $Info{provide};
191     print qq{;\n};
192    
193 wakaba 1.1 print "\n1;\n";
194     exit;
195 wakaba 1.2 }
196    
197 wakaba 1.5 sub make_format ($$) {
198     my ($src, $Info) = @_;
199     my $module_name = 'SuikaWiki::Format::Definition::'.$src->get_attribute_value ('ModuleName');
200     my $r = change_package $Info, $module_name;
201     $r .= qq{our \@ISA;\n};
202     if (my $isa = $src->get_attribute_value ('Inherit')) {
203     for (@$isa) {
204     $r .= qq{push \@ISA, @{[literal 'SuikaWiki::Format::Definition::'.$_]};\n};
205     }
206     } else {
207     $r .= qq{push \@ISA, 'SuikaWiki::Format::Definition::template';\n};
208     }
209     if (my $name = $src->get_attribute_value ('Name')) {
210     $r .= qq{\$SuikaWiki::Format::Definition::Class{@{[literal '/'.$name.'/'.$src->get_attribute_value ('Version', default => '').'//']}} = '$module_name';\n};
211     }
212     if (my $type = $src->get_attribute_value ('Type')) {
213 wakaba 1.10 $type .= join '', map {
214     ';'. $_->local_name .'='. quoted_string $_->inner_text
215     } sort {
216     $a->local_name cmp $b->local_name
217     } @{$src->get_attribute ('Type')->child_nodes};
218 wakaba 1.5 $r .= qq{\$SuikaWiki::Format::Definition::Class{@{[literal $type.'//']}} = '$module_name';\n};
219     }
220    
221 wakaba 1.8 my $convert = line $Info, line_no => __LINE__ + 2, realfile => __FILE__;
222     $convert .= <<'EOH';
223 wakaba 1.5 our $Converter;
224     sub convert ($$;%) {
225     my ($self, $source, %opt) = @_;
226     my $converter;
227     my $flag = '//';
228     $flag .= 'f' if $opt{IsFragment};
229     $flag .= 'p' if $opt{IsPlaceholder};
230     if ($Converter->{$opt{Type}.$flag}) {
231     $converter = $Converter->{$opt{Type}.$flag};
232     } elsif ($Converter->{$opt{Name}.'/'.$opt{Version}.$flag}) {
233     $converter = $Converter->{'/'.$opt{Name}.'/'.$opt{Version}.$flag};
234     }
235     return $converter->{Main}->($self, $source, \%opt) if $converter;
236     $self->SUPER::convert ($source, %opt);
237     }
238     EOH
239    
240     for (@{$src->child_nodes}) {
241     if ($_->local_name eq 'Converter') {
242 wakaba 1.8 if ($convert) {
243     $r .= $convert;
244     $r .= line $Info, reset => 1;
245     undef $convert;
246     }
247 wakaba 1.5 $r .= make_format_converter ($_, $Info);
248 wakaba 1.10 } elsif ($_->local_name eq 'WikiForm') {
249     $r .= q(sub wikiform {)."\n".q(my ($self, $source, %opt) = @_;);
250     $r .= line $Info, node_path => qq(Format[module-name()=$module_name]/WikiForm);
251     $r .= code $Info, $_->get_attribute_value ('Main');
252     $r .= line $Info, reset => 1;
253     $r .= qq(}\n);
254     } elsif ($_->local_name eq 'NextIndex') {
255     my $name = $_->get_attribute_value ('Name', default => '');
256     $r .= q(sub next_index_for_).$name
257     . q( {)."\n".q(my ($self, $source, %opt) = @_;)
258     . line $Info, node_path => qq(Format[module-name()=$module_name]/NextIndex[Name=$name]);
259     $r .= code $Info, $_->get_attribute_value ('Main');
260     $r .= line $Info, reset => 1;
261     $r .= qq(}\n);
262 wakaba 1.5 } elsif ($_->local_name eq 'Use') {
263 wakaba 1.6 $r .= line $Info, node_path => qq(Format[module-name()=$module_name]/Use);
264 wakaba 1.5 $r .= $_->inner_text;
265     }
266     }
267     $r;
268     }
269    
270     sub make_format_converter ($$) {
271     my ($src, $Info) = @_;
272     my %def;
273     $def{Type} = $src->get_attribute ('Type');
274     if (ref $def{Type}) {
275     $def{Type} = $def{Type}->inner_text
276     . join '', map {
277     ';'. $_->local_name .'='. quoted_string $_->inner_text
278     } sort {
279     $a->local_name cmp $b->local_name
280     } @{$def{Type}->child_nodes};
281     } else {
282     delete $def{Type};
283     }
284     $def{Name} = $src->get_attribute_value ('Name');
285     delete $def{Name} unless defined $def{Name};
286     $def{Version} = $src->get_attribute_value ('Version');
287     delete $def{Version} if not defined $def{Version} or
288     not defined $def{Name};
289    
290     my $flag = '//';
291     $flag .= 'f' and $def{IsFragment} = 1
292     if $src->get_attribute_value ('IsFragment');
293     $flag .= 'p' and $def{IsPlaceholder} = 1
294     if $src->get_attribute_value ('IsPlaceholder');
295    
296     $def{Main} = $src->get_attribute_value ('Main');
297 wakaba 1.6 $def{Main} = line ($Info, node_path => '//Converter/Main')
298     . $def{Main}
299     . line ($Info, reset => 1);
300     if ($def{Main} =~ /\$r\b/) {
301     $def{Main} = 'my $r;'."\n".$def{Main}."\n".'$r';
302     }
303 wakaba 1.5 $def{Main} = barecode code $Info,
304     'sub {my ($self, $source, $opt) = @_;'
305     . $def{Main} . '}';
306    
307     my $r = list %def;
308     if ($def{Type}) {
309     $r = qq{\$Converter->{@{[literal $def{Type}.$flag]}} = {$r};\n};
310     $r .= qq{\$Converter->{@{[literal '/'.$def{Name}.'/'.$def{Version}.$flag]}} = \$Converter->{@{[literal $def{Type}.$flag]}};\n}
311     if $def{Name};
312     } elsif ($def{Name}) {
313     $r = qq{\$Converter->{@{[literal '/'.$def{Name}.'/'.$def{Version}.$flag]}} = {$r};\n};
314 wakaba 1.9 } else {
315     $r = 'BEGIN { die "Invalid Syntax of Converter: Type or Name property required" }';
316 wakaba 1.5 }
317     $r;
318     }
319    
320 wakaba 1.2 sub make_function ($$) {
321     my ($src, $Info) = @_;
322     ## TODO: support of ARGV property
323 wakaba 1.6 my $name;
324 wakaba 1.2 my $r = <<EOH;
325 wakaba 1.5 @{[change_package $Info, $Info->{module_name}]}
326 wakaba 1.6 sub @{[$name = $src->get_attribute_value ('Name')]} {
327     @{[line $Info, node_path => "Function[Name='$name']/Main"]}@{[
328     code $Info, $src->get_attribute_value ('Main')
329 wakaba 1.9 ]}
330 wakaba 1.2 }
331 wakaba 1.9 @{[line $Info, reset => 1]}
332 wakaba 1.2 EOH
333     }
334    
335     sub register_plugin_const ($$) {
336     my ($src, $Info) = @_;
337     for (@{$src->child_nodes}) {
338     $Info->{const}->{$_->local_name} = $_->value;
339     }
340     }
341    
342     sub make_resdef ($$) {
343     my ($src, $Info) = @_;
344 wakaba 1.5 my $r = change_package $Info, 'SuikaWiki::Plugin::Resource';
345     $r .= qq{our \$BaseResource;\n};
346 wakaba 1.2 for (@{$src->child_nodes}) {
347     if ($_->node_type eq '#element') {
348     my $lang = literal ($_->get_attribute_value ('lang') || 'und');
349     my $script = literal $_->get_attribute_value ('script');
350     my $name = literal $_->local_name;
351     my $val = literal n11n $_->value;
352     $r .= qq{\$BaseResource->{$lang}->{$script}->{$name} = $val;\n};
353     }
354     }
355     $r;
356     }
357 wakaba 1.1
358     sub make_viewfragment ($$) {
359     my ($src, $Info) = @_;
360     my $r = '';
361 wakaba 1.4 my $body = <<EOH;
362     {
363 wakaba 1.2 Main => @{[literal $src->get_attribute_value ('Formatting')]},
364     Order => @{[0+$src->get_attribute_value ('Order')]},
365     Description => [@{[m13ed_val_list $src, 'Description']}],
366     };
367     EOH
368 wakaba 1.4 ## Recommended format
369     my $name = $src->get_attribute_value ('Template');
370     if (ref ($name) and @$name > 1) {
371     $r .= qq({my \$def = $body;\n);
372     for (@$name) {
373     my $name = $_; $name =~ tr/-/_/;
374     $r .= qq(push \@{\$SuikaWiki::View::Implementation::TemplateFragment{@{[literal $name]}}}, \$def;\n);
375     push @{$Info->{provide}->{viewfragment}}, {Name => $name};
376     }
377     $r .= qq(}\n);
378     } else { ## Obsoleted format
379     $name = ref $name ? $name->[0] : $src->get_attribute_value ('Name');
380     $name =~ tr/-/_/;
381     $r .= qq(push \@{\$SuikaWiki::View::Implementation::TemplateFragment{@{[literal $name]}}}, $body);
382     push @{$Info->{provide}->{viewfragment}}, {Name => $name};
383     }
384 wakaba 1.1 $r;
385     }
386    
387     sub make_viewdef ($$) {
388     my ($src, $Info) = @_;
389     my $ViewProp = {};
390     my $r = '';
391 wakaba 1.6 $ViewProp->{Name} = n11n $src->get_attribute_value ('Mode');
392 wakaba 1.10 $ViewProp->{Name} =~ s/(?<=.)-/_/g;
393 wakaba 1.1 $ViewProp->{pack_name} = random_module_name ($Info, $ViewProp->{Name});
394    
395 wakaba 1.2 $ViewProp->{condition_stringified} = hash
396 wakaba 1.1 mode => $ViewProp->{Name},
397     map {($_->local_name => $_->value)}
398     @{$src->get_attribute ('Condition',make_new_node=>1)->child_nodes};
399    
400     $r .= <<EOH;
401     push \@SuikaWiki::View::Implementation::CommonViewDefs, {
402     condition => {$ViewProp->{condition_stringified}},
403     object_class => q#$ViewProp->{pack_name}#,
404     };
405 wakaba 1.5 @{[change_package $Info, $ViewProp->{pack_name}]}
406 wakaba 1.1 our \@ISA = q#SuikaWiki::View::template#;
407     EOH
408     for (@{$src->child_nodes}) {
409 wakaba 1.2 if ($_->local_name eq 'template') {
410 wakaba 1.8 $r .= make_view_template_method ($_, $Info, $ViewProp);
411 wakaba 1.2 } elsif ($_->local_name eq 'method') {
412 wakaba 1.6 my $method_name = $_->get_attribute_value ('Name');
413 wakaba 1.1 $r .= ({
414 wakaba 1.8 main => q(sub main ($$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",
415 wakaba 1.1 main_pre => q(sub main_pre ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",
416     main_post => q(sub main_post ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",
417 wakaba 1.6 }->{$method_name}
418     ||qq(sub @{[$method_name]} {\n))
419     . line ($Info, node_path => "ViewDefinition[Mode='$ViewProp->{Name}']/method[Name='$method_name']")
420 wakaba 1.2 . code ($Info, $_->value)
421 wakaba 1.9 . qq(}\n)
422     . line ($Info, reset => 1);
423 wakaba 1.1 }
424     }
425 wakaba 1.2 my $prop = {Name => $ViewProp->{Name},
426     Description => barecode m13ed_val_list $_, 'Description'};
427     push @{$Info->{provide}->{viewdef}}, $prop;
428     $r;
429     }
430    
431     sub make_view_template_method ($$) {
432 wakaba 1.8 my ($src, $Info, $ViewProp) = @_;
433 wakaba 1.2 my $r = <<EOH;
434    
435     sub main (\$\$\$) {
436     my (\$self, \$opt, \$opt2) = \@_;
437     require SuikaWiki::Output::HTTP;
438     \$opt2->{output} = SuikaWiki::Output::HTTP->new
439     (wiki => \$self->{view}->{wiki},
440     view => \$self->{view}, viewobj => \$self);
441     for (\@{\$self->{view}->{wiki}->{var}->{client}->{used_for_negotiate}},
442     'Accept-Language') {
443     \$opt2->{output}->add_negotiate_header_field (\$_);
444     }
445    
446     \$opt2->{template} = @{[literal $src->get_attribute ('body', make_new_node => 1)->inner_text]};
447     \$opt2->{o} = bless {
448     ## Compatible options for SuikaWiki 2 WikiPlugin interface
449     param => \\\%main::form,
450     page => \$main::form{mypage},
451 wakaba 1.8 #toc => [],
452 wakaba 1.2 #magic
453     #content
454     #use_anchor_name
455     media => {@{[hash
456     type => ($src->get_attribute ('media-type',make_new_node=>1)->inner_text
457     || 'application/octet-stream'),
458     charset => ($src->get_attribute ('use-media-type-charset',make_new_node=>1)
459     ->inner_text || 0),
460     ## In fact, this value is not referred from any SuikaWiki 2 WikiPlugin rule.
461     #expires => ($src->get_attribute ('expires',make_new_node=>1)->inner_text
462     # || 0)
463     ]}},
464     ## SuikaWiki 3 WikiPlugin interface
465     wiki => \$self->{view}->{wiki},
466     plugin => \$self->{view}->{wiki}->{plugin},
467     var => {},
468     }, 'SuikaWiki::Plugin';
469     @{[do{my $x=$src->get_attribute('http-status-code',make_new_node=>1)->inner_text;
470     $x?q{$opt2->{output}->{status_code} = }.(0 + $x).q{;}:q{}}]}
471     @{[do{my $x=$src->get_attribute('http-status-phrase',make_new_node=>1)->inner_text;
472 wakaba 1.8 $x?q{$opt2->{output}->{status_phrase} = }.literal($x).q{;}:q{}}]}
473 wakaba 1.2 \$opt2->{output}->{entity}->{media_type} = @{[literal
474     $src->get_attribute ('media-type',make_new_node=>1)
475     ->inner_text || 'application/octet-stream']};
476     @{[($src->get_attribute ('use-media-type-charset',make_new_node=>1)
477     ->inner_text || 0) ?
478     q{$opt2->{output}->{entity}->{charset} = $self->{view}->{wiki}->{config}->{charset}->{output};}:
479     q{}]}
480     @{[do{my $x = $src->get_attribute ('expires',make_new_node=>1)->inner_text;
481     if ($x =~ /%%(\w+)%%/) {
482     qq{\$opt2->{output}->set_expires (%{\$self->{view}->{wiki}->{config}->{entity}->{expires}->{@{[literal $1]}}});};
483     } else {
484     qq{\$opt2->{output}->set_expires (delta => @{[0 + $x]});};
485     }
486     }]}
487    
488     \$self->{view}->{wiki}->init_db;
489     \$self->main_pre (\$opt, \$opt2);
490    
491 wakaba 1.8 use Message::Util::Error;
492     try {
493     \$opt2->{output}->{entity}->{body}
494     = SuikaWiki::Plugin->formatter ('view')
495     ->replace (\$opt2->{template}, param => \$opt2->{o});
496     } \$self->{view}->{wiki}->{config}->{catch}->{ @{[
497     $ViewProp->{Name} eq '-error' ? 'formatter_view_error'
498     : 'formatter_view' ]} };
499 wakaba 1.2 \$opt2->{output}->output (output => 'http-cgi');
500    
501     \$self->main_post (\$opt, \$opt2);
502     }
503     EOH
504     }
505    
506     sub make_rule ($$) {
507     my ($src, $Info) = @_;
508     my $type = $src->get_attribute ('Category', make_new_node => 1)->value || [];
509     my $name = $src->get_attribute ('Name', make_new_node => 1)->value;
510 wakaba 1.5 $name =~ s/(?<=.)-/_/g;
511    
512     my $reg_block;
513     $reg_block = qr/[^{}]*(?>[^{}]+|{(??{$reg_block})})*/;
514 wakaba 1.10 my %code;
515     for my $codename ([qw/Formatting main/], [qw/After after/],
516     [qw/Before before/], [qw/Pre pre/], [qw/Post post/],
517     [qw/Attribute attr/]) {
518     my $main = code $Info, $src->get_attribute_value ($codename->[0]);
519     next unless $main;
520     $main = line ($Info, node_path =>
521     "FormattingRule[name()='@{[list $type]}/$name']/".$codename->[0])
522     . $main;
523    
524     $main = q{my ($f, $rule_name, $p, $o, %opt) = @_;}."\n".$main
525     if $main =~ /\$f\b/
526     or $main =~ /\$rule_name\b/
527     or $main =~ /\$[opr]\b/
528     or $main =~ /[%\$]opt\b/;
529     if ($main =~ /\$r\b/) {
530     warn qq(Rule @{[list $type]}/$name: Use of \$r is deprecated);
531     $main = q{my $r = '';} . "\n" . $main . "\n"
532     . q{$p->{-parent}->append_node ($r, node_or_text => 1);};
533     }
534     $main =~ s{__ATTR(TEXT|NODE)?:%(\w+|{$reg_block})(?:->{($reg_block)})?__;}
535     {($1 eq 'TEXT' ? '$p->{'.literal_or_code($Info, $2)
536     .'} = do { my $r = ' : '')
537     .'$f->parse_attr ($p=>'.literal_or_code($Info, $2).', $o, '
538     .($3?'-parent => '.$3.', ':'')
539     .($1?'-non_parsed_to_node => 1, ':'')
540     .'%opt)'
541     .($1 eq 'TEXT' ? '; ref $r?$r->inner_text:$r}'
542     : '')
543     .';'}ge;
544     $code{$codename->[1]} = barecode "sub {$main}";
545     }
546    
547     my $main = literal {
548     Description => [barecode m13ed_val_list $src, 'Description'],
549     Parameter => {do {
550     my @r;
551     for (@{$src->child_nodes}) {
552     if ($_->local_name eq 'Parameter') {
553     push @r, $_->get_attribute_value ('Name')
554     => {Type => $_->get_attribute_value ('Type'),
555     Default => $_->get_attribute_value ('Default'),
556     Description => [barecode m13ed_val_list $_, 'Description']};
557     }
558     }
559     @r;
560     }},
561     %code,
562     };
563     $main .= line $Info, reset => 1;
564    
565    
566     my $amain = <<EOH;
567 wakaba 1.2 {
568 wakaba 1.5 main => sub {$main},
569 wakaba 1.9 @{[line ($Info, reset => 1)]}
570 wakaba 1.2 Description => [@{[m13ed_val_list $src, 'Description']}],
571     Parameter => {@{[do{
572     }]}},
573     }
574     EOH
575 wakaba 1.5 my $r = change_package $Info, $Info->{module_name};
576 wakaba 1.2 if (@$type == 1) {
577     $type->[0] =~ tr/-/_/;
578 wakaba 1.5 $r .= qq{\$SuikaWiki::Plugin::Rule{$type->[0]}->{$name} = $main;\n};
579 wakaba 1.2 push @{$Info->{provide}->{rule}->{$type->[0]}}, $name;
580     } else {
581 wakaba 1.5 $r .= qq({my \$def = $main;\n);
582 wakaba 1.2 for my $type (@$type) {
583     $type =~ tr/-/_/;
584     $r .= qq{\$SuikaWiki::Plugin::Rule{$type}->{$name} = \$def;\n};
585     push @{$Info->{provide}->{rule}->{$type}}, $name;
586     }
587     $r .= qq(};\n);
588     }
589 wakaba 1.1 $r;
590     }
591    
592    
593     sub random_module_name ($;$) {
594     my ($Info, $subname) = @_;
595     $subname =~ s/[^0-9A-Za-z_:]//g;
596     my @date = gmtime;
597     my @rand = ('A'..'Z','a'..'z',0..9,'_');
598 wakaba 1.2 sprintf '%s::%s%s%s', $Info->{module_name}, $subname,
599 wakaba 1.1 sprintf ('%02d%02d%02d%02d%02d%02d', @date[5,4,3,2,1,0]),
600     join ('', @rand[rand@rand,rand@rand,rand@rand,rand@rand]);
601     }

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24