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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download)
Wed Nov 26 09:11:01 2003 UTC (20 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.5: +54 -10 lines
File MIME type: text/plain
Output #line directive

1 #!/usr/bin/perl
2 use strict;
3 our $VERSION = do{my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4 require Message::Markup::SuikaWikiConfig20::Parser;
5
6 {
7 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 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 }
34 sub n11n ($) {
35 my $s = shift;
36 $s =~ s/\s+/ /g;
37 $s;
38 }
39 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 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 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 $opt{file} = sprintf '(WikiPlugin module %s, block %s)',
92 $Info->{Name},
93 $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
101 my $parser = Message::Markup::SuikaWikiConfig20::Parser->new;
102 my $plugins = $parser->parse_text ($src);
103 my $meta = $plugins->get_attribute ('Plugin')
104 or die "$0: Required 'Plugin' section not found";
105 my %Info = (provide => {},
106 Name => n11n $meta->get_attribute ('Name')->value);
107 $Info{name_literal} = literal $Info{Name};
108 my @date = gmtime;
109 $Info{LastModified} = sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00',
110 $date[5] + 1900, $date[4] + 1, @date[3,2,1,0];
111 $Info{Version} = sprintf '%04d.%02d%02d.%02d%02d',
112 $date[5] + 1900, $date[4] + 1, @date[3,2,1];
113 $Info{InterfaceVersion} = '2.9.1';
114 $Info{mkpluginVersion} = '2.'.$VERSION;
115 $Info{module_name} = q#SuikaWiki::Plugin::plugin#;
116 $Info{module_name} = random_module_name (\%Info, $Info{Name});
117
118 print <<EOH;
119 use strict;
120 @{[change_package \%Info, 'SuikaWiki::Plugin::Registry']}
121 our \%Info;
122 \$Info{$Info{name_literal}}->{Name} = $Info{name_literal};
123 EOH
124 for (qw/Version InterfaceVersion mkpluginVersion module_name/) {
125 print qq{\$Info{$Info{name_literal}}->{$_} = @{[literal $Info{$_}]};\n};
126 }
127 for (qw/LastModified/) {
128 $Info{$_} = n11n $meta->get_attribute ($_,make_new_node=>1)->value;
129 next unless length $Info{$_};
130 print qq{\$Info{$Info{name_literal}}->{$_} = } . literal $Info{$_};
131 print ";\n";
132 }
133 for (qw/RequiredPlugin RequiredModule/) {
134 $Info{$_} = $meta->get_attribute ($_);
135 next unless ref $Info{$_}; $Info{$_} = $Info{$_}->value;
136 print qq{\$Info{$Info{name_literal}}->{$_} = [};
137 print join ', ', map {literal $_} @{$Info{$_}};
138 print "];\n";
139 }
140 for (qw/Description License RelatedWikiPage RelatedURI/) {
141 my $r = m13ed_val_list $meta, $_;
142 next unless $r;
143 print qq{\$Info{$Info{name_literal}}->{$_} = [$r];\n};
144 }
145
146 print qq{\$Info{$Info{name_literal}}->{Author} = [} .( list map {
147 [
148 [ barecode m13ed_val_list ($_, 'Name') ],
149 [ $_->get_attribute ('Mail', make_new_node => 1)->value ],
150 [ $_->get_attribute ('URI', make_new_node => 1)->value ],
151 ]
152 } grep { $_->local_name eq 'Author' } @{$meta->child_nodes}
153 ). qq{];\n};
154
155 my $use = $meta->get_attribute ('Use');
156 if (ref $use) {
157 print change_package \%Info, $Info{module_name};
158 print line \%Info, node_path => 'Plugin/Use';
159 print $use->inner_text, "\n";
160 print line \%Info, reset => 1;
161 }
162
163 for (@{$plugins->child_nodes}) {
164 if ($_->local_name eq 'FormattingRule') {
165 print "\n", make_rule ($_, \%Info);
166 } elsif ($_->local_name eq 'ViewDefinition') {
167 print "\n", make_viewdef ($_, \%Info);
168 } elsif ($_->local_name eq 'ViewFragment') {
169 print "\n", make_viewfragment ($_, \%Info);
170 } elsif ($_->local_name eq 'Function') {
171 print "\n", make_function ($_, \%Info);
172 } elsif ($_->local_name eq 'Resource') {
173 print "\n", make_resdef ($_, \%Info);
174 } elsif ($_->local_name eq 'PluginConst') {
175 register_plugin_const ($_, \%Info);
176 } elsif ($_->local_name eq 'Format') {
177 print "\n", make_format ($_, \%Info);
178 }
179 }
180
181 print change_package \%Info, q(SuikaWiki::Plugin::Registry);
182 print qq{\$Info{$Info{name_literal}}->{provide} = } . literal $Info{provide};
183 print qq{;\n};
184
185 print "\n1;\n";
186 exit;
187 }
188
189 sub make_format ($$) {
190 my ($src, $Info) = @_;
191 my $module_name = 'SuikaWiki::Format::Definition::'.$src->get_attribute_value ('ModuleName');
192 my $r = change_package $Info, $module_name;
193 $r .= qq{our \@ISA;\n};
194 if (my $isa = $src->get_attribute_value ('Inherit')) {
195 for (@$isa) {
196 $r .= qq{push \@ISA, @{[literal 'SuikaWiki::Format::Definition::'.$_]};\n};
197 }
198 } else {
199 $r .= qq{push \@ISA, 'SuikaWiki::Format::Definition::template';\n};
200 }
201 if (my $name = $src->get_attribute_value ('Name')) {
202 $r .= qq{\$SuikaWiki::Format::Definition::Class{@{[literal '/'.$name.'/'.$src->get_attribute_value ('Version', default => '').'//']}} = '$module_name';\n};
203 }
204 if (my $type = $src->get_attribute_value ('Type')) {
205 $r .= qq{\$SuikaWiki::Format::Definition::Class{@{[literal $type.'//']}} = '$module_name';\n};
206 }
207
208 $r .= line $Info, line_no => __LINE__ + 2, realfile => __FILE__;
209 $r .= <<'EOH';
210 our $Converter;
211 sub convert ($$;%) {
212 my ($self, $source, %opt) = @_;
213 my $converter;
214 my $flag = '//';
215 $flag .= 'f' if $opt{IsFragment};
216 $flag .= 'p' if $opt{IsPlaceholder};
217 if ($Converter->{$opt{Type}.$flag}) {
218 $converter = $Converter->{$opt{Type}.$flag};
219 } elsif ($Converter->{$opt{Name}.'/'.$opt{Version}.$flag}) {
220 $converter = $Converter->{'/'.$opt{Name}.'/'.$opt{Version}.$flag};
221 }
222 return $converter->{Main}->($self, $source, \%opt) if $converter;
223 $self->SUPER::convert ($source, %opt);
224 }
225 EOH
226
227 my $reset = 0;
228 for (@{$src->child_nodes}) {
229 if ($_->local_name eq 'Converter') {
230 $r .= line $Info, reset => 1 unless $reset;
231 $r .= make_format_converter ($_, $Info);
232 $reset = 1;
233 } elsif ($_->local_name eq 'Use') {
234 $r .= line $Info, node_path => qq(Format[module-name()=$module_name]/Use);
235 $r .= $_->inner_text;
236 $reset = 0;
237 }
238 }
239 $r .= line $Info, reset => 1 unless $reset;
240
241 $r;
242 }
243
244 sub make_format_converter ($$) {
245 my ($src, $Info) = @_;
246 my %def;
247 $def{Type} = $src->get_attribute ('Type');
248 if (ref $def{Type}) {
249 $def{Type} = $def{Type}->inner_text
250 . join '', map {
251 ';'. $_->local_name .'='. quoted_string $_->inner_text
252 } sort {
253 $a->local_name cmp $b->local_name
254 } @{$def{Type}->child_nodes};
255 } else {
256 delete $def{Type};
257 }
258 $def{Name} = $src->get_attribute_value ('Name');
259 delete $def{Name} unless defined $def{Name};
260 $def{Version} = $src->get_attribute_value ('Version');
261 delete $def{Version} if not defined $def{Version} or
262 not defined $def{Name};
263
264 my $flag = '//';
265 $flag .= 'f' and $def{IsFragment} = 1
266 if $src->get_attribute_value ('IsFragment');
267 $flag .= 'p' and $def{IsPlaceholder} = 1
268 if $src->get_attribute_value ('IsPlaceholder');
269
270 $def{Main} = $src->get_attribute_value ('Main');
271 $def{Main} = line ($Info, node_path => '//Converter/Main')
272 . $def{Main}
273 . line ($Info, reset => 1);
274 if ($def{Main} =~ /\$r\b/) {
275 $def{Main} = 'my $r;'."\n".$def{Main}."\n".'$r';
276 }
277 $def{Main} = barecode code $Info,
278 'sub {my ($self, $source, $opt) = @_;'
279 . $def{Main} . '}';
280
281 my $r = list %def;
282 if ($def{Type}) {
283 $r = qq{\$Converter->{@{[literal $def{Type}.$flag]}} = {$r};\n};
284 $r .= qq{\$Converter->{@{[literal '/'.$def{Name}.'/'.$def{Version}.$flag]}} = \$Converter->{@{[literal $def{Type}.$flag]}};\n}
285 if $def{Name};
286 } elsif ($def{Name}) {
287 $r = qq{\$Converter->{@{[literal '/'.$def{Name}.'/'.$def{Version}.$flag]}} = {$r};\n};
288 $r
289 }
290 $r;
291 }
292
293 sub make_function ($$) {
294 my ($src, $Info) = @_;
295 ## TODO: support of ARGV property
296 my $name;
297 my $r = <<EOH;
298 @{[change_package $Info, $Info->{module_name}]}
299 sub @{[$name = $src->get_attribute_value ('Name')]} {
300 @{[line $Info, node_path => "Function[Name='$name']/Main"]}@{[
301 code $Info, $src->get_attribute_value ('Main')
302 ]}@{[line $Info, reset => 1]}
303 }
304 EOH
305 }
306
307 sub register_plugin_const ($$) {
308 my ($src, $Info) = @_;
309 for (@{$src->child_nodes}) {
310 $Info->{const}->{$_->local_name} = $_->value;
311 }
312 }
313
314 sub make_resdef ($$) {
315 my ($src, $Info) = @_;
316 my $r = change_package $Info, 'SuikaWiki::Plugin::Resource';
317 $r .= qq{our \$BaseResource;\n};
318 for (@{$src->child_nodes}) {
319 if ($_->node_type eq '#element') {
320 my $lang = literal ($_->get_attribute_value ('lang') || 'und');
321 my $script = literal $_->get_attribute_value ('script');
322 my $name = literal $_->local_name;
323 my $val = literal n11n $_->value;
324 $r .= qq{\$BaseResource->{$lang}->{$script}->{$name} = $val;\n};
325 }
326 }
327 $r;
328 }
329
330 sub make_viewfragment ($$) {
331 my ($src, $Info) = @_;
332 my $r = '';
333 my $body = <<EOH;
334 {
335 Main => @{[literal $src->get_attribute_value ('Formatting')]},
336 Order => @{[0+$src->get_attribute_value ('Order')]},
337 Description => [@{[m13ed_val_list $src, 'Description']}],
338 };
339 EOH
340 ## Recommended format
341 my $name = $src->get_attribute_value ('Template');
342 if (ref ($name) and @$name > 1) {
343 $r .= qq({my \$def = $body;\n);
344 for (@$name) {
345 my $name = $_; $name =~ tr/-/_/;
346 $r .= qq(push \@{\$SuikaWiki::View::Implementation::TemplateFragment{@{[literal $name]}}}, \$def;\n);
347 push @{$Info->{provide}->{viewfragment}}, {Name => $name};
348 }
349 $r .= qq(}\n);
350 } else { ## Obsoleted format
351 $name = ref $name ? $name->[0] : $src->get_attribute_value ('Name');
352 $name =~ tr/-/_/;
353 $r .= qq(push \@{\$SuikaWiki::View::Implementation::TemplateFragment{@{[literal $name]}}}, $body);
354 push @{$Info->{provide}->{viewfragment}}, {Name => $name};
355 }
356 $r;
357 }
358
359 sub make_viewdef ($$) {
360 my ($src, $Info) = @_;
361 my $ViewProp = {};
362 my $r = '';
363 $ViewProp->{Name} = n11n $src->get_attribute_value ('Mode');
364 $ViewProp->{pack_name} = random_module_name ($Info, $ViewProp->{Name});
365
366 $ViewProp->{condition_stringified} = hash
367 mode => $ViewProp->{Name},
368 map {($_->local_name => $_->value)}
369 @{$src->get_attribute ('Condition',make_new_node=>1)->child_nodes};
370
371 $r .= <<EOH;
372 push \@SuikaWiki::View::Implementation::CommonViewDefs, {
373 condition => {$ViewProp->{condition_stringified}},
374 object_class => q#$ViewProp->{pack_name}#,
375 };
376 @{[change_package $Info, $ViewProp->{pack_name}]}
377 our \@ISA = q#SuikaWiki::View::template#;
378 EOH
379 for (@{$src->child_nodes}) {
380 if ($_->local_name eq 'template') {
381 $r .= make_view_template_method ($_, $Info);
382 } elsif ($_->local_name eq 'method') {
383 my $method_name = $_->get_attribute_value ('Name');
384 $r .= ({
385 main => q(sub main ($$) {)."\n".q(my ($self, $opt) = @_;)."\n",
386 main_pre => q(sub main_pre ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",
387 main_post => q(sub main_post ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",
388 }->{$method_name}
389 ||qq(sub @{[$method_name]} {\n))
390 . line ($Info, node_path => "ViewDefinition[Mode='$ViewProp->{Name}']/method[Name='$method_name']")
391 . code ($Info, $_->value)
392 . line ($Info, reset => 1)
393 . qq(}\n);
394 }
395 }
396 my $prop = {Name => $ViewProp->{Name},
397 Description => barecode m13ed_val_list $_, 'Description'};
398 push @{$Info->{provide}->{viewdef}}, $prop;
399 $r;
400 }
401
402 sub make_view_template_method ($$) {
403 my ($src, $info) = @_;
404 my $r = <<EOH;
405
406 sub main (\$\$\$) {
407 my (\$self, \$opt, \$opt2) = \@_;
408 require SuikaWiki::Output::HTTP;
409 \$opt2->{output} = SuikaWiki::Output::HTTP->new
410 (wiki => \$self->{view}->{wiki},
411 view => \$self->{view}, viewobj => \$self);
412 for (\@{\$self->{view}->{wiki}->{var}->{client}->{used_for_negotiate}},
413 'Accept-Language') {
414 \$opt2->{output}->add_negotiate_header_field (\$_);
415 }
416
417 \$opt2->{template} = @{[literal $src->get_attribute ('body', make_new_node => 1)->inner_text]};
418 \$opt2->{o} = bless {
419 ## Compatible options for SuikaWiki 2 WikiPlugin interface
420 param => \\\%main::form,
421 page => \$main::form{mypage},
422 toc => [],
423 #magic
424 #content
425 #use_anchor_name
426 media => {@{[hash
427 type => ($src->get_attribute ('media-type',make_new_node=>1)->inner_text
428 || 'application/octet-stream'),
429 charset => ($src->get_attribute ('use-media-type-charset',make_new_node=>1)
430 ->inner_text || 0),
431 ## In fact, this value is not referred from any SuikaWiki 2 WikiPlugin rule.
432 #expires => ($src->get_attribute ('expires',make_new_node=>1)->inner_text
433 # || 0)
434 ]}},
435 ## SuikaWiki 3 WikiPlugin interface
436 wiki => \$self->{view}->{wiki},
437 plugin => \$self->{view}->{wiki}->{plugin},
438 var => {},
439 }, 'SuikaWiki::Plugin';
440 @{[do{my $x=$src->get_attribute('http-status-code',make_new_node=>1)->inner_text;
441 $x?q{$opt2->{output}->{status_code} = }.(0 + $x).q{;}:q{}}]}
442 @{[do{my $x=$src->get_attribute('http-status-phrase',make_new_node=>1)->inner_text;
443 $x?q{$opt2->{output}->{reason_phrase} = }.literal($x).q{;}:q{}}]}
444 \$opt2->{output}->{entity}->{media_type} = @{[literal
445 $src->get_attribute ('media-type',make_new_node=>1)
446 ->inner_text || 'application/octet-stream']};
447 @{[($src->get_attribute ('use-media-type-charset',make_new_node=>1)
448 ->inner_text || 0) ?
449 q{$opt2->{output}->{entity}->{charset} = $self->{view}->{wiki}->{config}->{charset}->{output};}:
450 q{}]}
451 @{[do{my $x = $src->get_attribute ('expires',make_new_node=>1)->inner_text;
452 if ($x =~ /%%(\w+)%%/) {
453 qq{\$opt2->{output}->set_expires (%{\$self->{view}->{wiki}->{config}->{entity}->{expires}->{@{[literal $1]}}});};
454 } else {
455 qq{\$opt2->{output}->set_expires (delta => @{[0 + $x]});};
456 }
457 }]}
458
459 \$self->{view}->{wiki}->init_db;
460 \$self->main_pre (\$opt, \$opt2);
461
462 my \$fmt = SuikaWiki::Plugin->formatter ('view');
463 \$opt2->{output}->{entity}->{body}
464 = \$fmt->replace (\$opt2->{template}, param => \$opt2->{o});
465 \$opt2->{output}->output (output => 'http-cgi');
466
467 \$self->main_post (\$opt, \$opt2);
468 }
469 EOH
470 }
471
472 sub make_rule ($$) {
473 my ($src, $Info) = @_;
474 my $type = $src->get_attribute ('Category', make_new_node => 1)->value || [];
475 my $name = $src->get_attribute ('Name', make_new_node => 1)->value;
476 $name =~ s/(?<=.)-/_/g;
477 my $main = line ($Info, node_path => "FormattingRule[name()='@{[list $type]}/$name']/Formatting")
478 . code ($Info, $src->get_attribute_value ('Formatting'))
479 . line ($Info, reset => 1);
480
481 my $reg_block;
482 $reg_block = qr/[^{}]*(?>[^{}]+|{(??{$reg_block})})*/;
483 my $reg_attr = qr/__ATTR(TEXT|NODE)?:%(\w+)(?:->{($reg_block)})?__;/;
484
485 $main = q{my ($f, $rule_name, $p, $o, %opt) = @_;}."\n".$main
486 if $main =~ /\$f\b/
487 or $main =~ /\$rule_name\b/
488 or $main =~ /\$[opr]\b/
489 or $main =~ /[%\$]opt\b/;
490 if ($main =~ /\$r\b/) {
491 warn qq(Rule @{[list $type]}/$name: Use of \$r is deprecated);
492 $main = q{my $r = '';} . "\n" . $main . "\n"
493 . q{$p->{-parent}->append_node ($r, node_or_text => 1);};
494 }
495 $main =~ s{$reg_attr}
496 {($1 eq 'TEXT' ? '$p->{'.literal($2).'} = do { my $r = ' : '')
497 .'$f->parse_attr ($p=>'.literal($2).', $o, '
498 .($3?'-parent => '.$3.', ':'')
499 .($1?'-non_parsed_to_node => 1, ':'')
500 .'%opt)'
501 .($1 eq 'TEXT' ? '; ref $r?$r->inner_text:$r}' : '')
502 .';'}ge;
503
504 my $main = <<EOH;
505 {
506 main => sub {$main},
507 Description => [@{[m13ed_val_list $src, 'Description']}],
508 Parameter => {@{[do{
509 my @r;
510 for (@{$src->child_nodes}) {
511 if ($_->local_name eq 'Parameter') {
512 push @r, $_->get_attribute_value ('Name')
513 => {Type => $_->get_attribute_value ('Type'),
514 Default => $_->get_attribute_value ('Default'),
515 Description => [barecode m13ed_val_list $_, 'Description']};
516 }
517 }
518 list @r;
519 }]}},
520 }
521 EOH
522 my $r = change_package $Info, $Info->{module_name};
523 if (@$type == 1) {
524 $type->[0] =~ tr/-/_/;
525 $r .= qq{\$SuikaWiki::Plugin::Rule{$type->[0]}->{$name} = $main;\n};
526 push @{$Info->{provide}->{rule}->{$type->[0]}}, $name;
527 } else {
528 $r .= qq({my \$def = $main;\n);
529 for my $type (@$type) {
530 $type =~ tr/-/_/;
531 $r .= qq{\$SuikaWiki::Plugin::Rule{$type}->{$name} = \$def;\n};
532 push @{$Info->{provide}->{rule}->{$type}}, $name;
533 }
534 $r .= qq(};\n);
535 }
536 $r;
537 }
538
539
540 sub random_module_name ($;$) {
541 my ($Info, $subname) = @_;
542 $subname =~ s/[^0-9A-Za-z_:]//g;
543 my @date = gmtime;
544 my @rand = ('A'..'Z','a'..'z',0..9,'_');
545 sprintf '%s::%s%s%s', $Info->{module_name}, $subname,
546 sprintf ('%02d%02d%02d%02d%02d%02d', @date[5,4,3,2,1,0]),
547 join ('', @rand[rand@rand,rand@rand,rand@rand,rand@rand]);
548 }

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24