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

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

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

revision 1.7 by wakaba, Thu Nov 27 05:50:02 2003 UTC revision 1.12 by wakaba, Sun Feb 8 08:58:24 2004 UTC
# Line 56  sub barecode ($) { Line 56  sub barecode ($) {
56  sub code ($$) {  sub code ($$) {
57    my ($Info, $code) = @_;    my ($Info, $code) = @_;
58    for (keys %{$Info->{const}}) {    for (keys %{$Info->{const}}) {
59      $code =~ s/\$$_\b/literal $Info->{const}->{$_}/ge;      $code =~ s/\$$_\b/$Info->{const}->{$_}/ge;
60    }    }
61    $code =~ s/__FUNCPACK__/$Info->{module_name}/g;    $code =~ s/__FUNCPACK__/$Info->{module_name}/g;
62    $code;    $code;
# Line 97  sub line ($;%) { Line 97  sub line ($;%) {
97    $opt{file} =~ s/"/''/g;    $opt{file} =~ s/"/''/g;
98    sprintf '%s#line %d "%s"%s', "\n", $opt{line_no} || 1, $opt{file}, "\n";    sprintf '%s#line %d "%s"%s', "\n", $opt{line_no} || 1, $opt{file}, "\n";
99  }  }
100    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    
106  my $parser = Message::Markup::SuikaWikiConfig20::Parser->new;  my $parser = Message::Markup::SuikaWikiConfig20::Parser->new;
107  my $plugins = $parser->parse_text ($src);  my $plugins = $parser->parse_text ($src);
# Line 176  for (@{$plugins->child_nodes}) { Line 181  for (@{$plugins->child_nodes}) {
181      register_plugin_const ($_, \%Info);      register_plugin_const ($_, \%Info);
182    } elsif ($_->local_name eq 'Format') {    } elsif ($_->local_name eq 'Format') {
183      print "\n", make_format ($_, \%Info);      print "\n", make_format ($_, \%Info);
184    # Parameter
185    # PluginCategory
186    }    }
187  }  }
188    
# Line 203  sub make_format ($$) { Line 210  sub make_format ($$) {
210      $r .= qq{\$SuikaWiki::Format::Definition::Class{@{[literal '/'.$name.'/'.$src->get_attribute_value ('Version', default => '').'//']}} = '$module_name';\n};      $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')) {    if (my $type = $src->get_attribute_value ('Type')) {
213        $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      $r .= qq{\$SuikaWiki::Format::Definition::Class{@{[literal $type.'//']}} = '$module_name';\n};      $r .= qq{\$SuikaWiki::Format::Definition::Class{@{[literal $type.'//']}} = '$module_name';\n};
219    }    }
220        
221    $r .= line $Info, line_no => __LINE__ + 2, realfile => __FILE__;    my $convert = line $Info, line_no => __LINE__ + 2, realfile => __FILE__;
222    $r .= <<'EOH';    $convert .= <<'EOH';
223  our $Converter;  our $Converter;
224  sub convert ($$;%) {  sub convert ($$;%) {
225    my ($self, $source, %opt) = @_;    my ($self, $source, %opt) = @_;
# Line 215  sub convert ($$;%) { Line 227  sub convert ($$;%) {
227    my $flag = '//';    my $flag = '//';
228    $flag .= 'f' if $opt{IsFragment};    $flag .= 'f' if $opt{IsFragment};
229    $flag .= 'p' if $opt{IsPlaceholder};    $flag .= 'p' if $opt{IsPlaceholder};
230    if ($Converter->{$opt{Type}.$flag}) {    my $type = $opt{Type} ?
231      $converter = $Converter->{$opt{Type}.$flag};                  $opt{Type} .
232                    SuikaWiki::Format::Definition->__get_param_string
233                      ($opt{Type_param}) : undef;
234      if ($Converter->{$type.$flag}) {
235        $converter = $Converter->{$type.$flag};
236    } elsif ($Converter->{$opt{Name}.'/'.$opt{Version}.$flag}) {    } elsif ($Converter->{$opt{Name}.'/'.$opt{Version}.$flag}) {
237      $converter = $Converter->{'/'.$opt{Name}.'/'.$opt{Version}.$flag};      $converter = $Converter->{'/'.$opt{Name}.'/'.$opt{Version}.$flag};
238    }    }
239    return $converter->{Main}->($self, $source, \%opt) if $converter;    return ($converter->{$opt{return_type} or 'Main'} or
240              CORE::die "Buggy implementation: $type $opt{Name}/$opt{Version}$flag/@{[$opt{return_type} or 'Main']} not defined")
241             ->($self, $source, \%opt)
242        if $converter;
243    $self->SUPER::convert ($source, %opt);    $self->SUPER::convert ($source, %opt);
244  }  }
245  EOH  EOH
246        
   my $reset = 0;  
247    for (@{$src->child_nodes}) {    for (@{$src->child_nodes}) {
248      if ($_->local_name eq 'Converter') {      if ($_->local_name eq 'Converter') {
249        $r .= line $Info, reset => 1 unless $reset;        if ($convert) {
250            $r .= $convert;
251            $r .= line $Info, reset => 1;
252            undef $convert;
253          }
254        $r .= make_format_converter ($_, $Info);        $r .= make_format_converter ($_, $Info);
255        $reset = 1;      } elsif ($_->local_name eq 'WikiForm') {
256          $r .= q(sub wikiform {)."\n".q(my ($self, $source, %opt) = @_;);
257          $r .= line $Info, node_path => qq(Format[module-name()=$module_name]/WikiForm);
258          $r .= code $Info, $_->get_attribute_value ('Main');
259          $r .= line $Info, reset => 1;
260          $r .= qq(}\n);
261        } elsif ($_->local_name eq 'HeadSummary') {
262          $r .= q(sub headsummary {)."\n".q(my ($self, $source, %opt) = @_;);
263          $r .= line $Info, node_path => qq(Format[module-name()=$module_name]/HeadSummary);
264          $r .= code $Info, $_->get_attribute_value ('Main');
265          $r .= line $Info, reset => 1;
266          $r .= qq(}\n);
267        } elsif ($_->local_name eq 'NextIndex') {
268          my $name = $_->get_attribute_value ('Name', default => '');
269          $r .= q(sub next_index_for_).$name
270             .  q( {)."\n".q(my ($self, $source, %opt) = @_;)
271             .  line $Info, node_path => qq(Format[module-name()=$module_name]/NextIndex[Name=$name]);
272          $r .= code $Info, $_->get_attribute_value ('Main');
273          $r .= line $Info, reset => 1;
274          $r .= qq(}\n);
275      } elsif ($_->local_name eq 'Use') {      } elsif ($_->local_name eq 'Use') {
276        $r .= line $Info, node_path => qq(Format[module-name()=$module_name]/Use);        $r .= line $Info, node_path => qq(Format[module-name()=$module_name]/Use);
277        $r .= $_->inner_text;        $r .= $_->inner_text;
       $reset = 0;  
278      }      }
279    }    }
   $r .= line $Info, reset => 1 unless $reset;  
     
280    $r;    $r;
281  }  }
282    
# Line 268  sub make_format_converter ($$) { Line 306  sub make_format_converter ($$) {
306    $flag .= 'p' and $def{IsPlaceholder} = 1    $flag .= 'p' and $def{IsPlaceholder} = 1
307      if $src->get_attribute_value ('IsPlaceholder');      if $src->get_attribute_value ('IsPlaceholder');
308        
309    $def{Main} = $src->get_attribute_value ('Main');    for (qw/Main ToString ToOctetStream/) {
310    $def{Main} = line ($Info, node_path => '//Converter/Main')      my $def = $src->get_attribute_value ($_);
311               . $def{Main}      next unless $def;
312               . line ($Info, reset => 1);      $def{$_} = line ($Info, node_path => '//Converter/'.$_)
313    if ($def{Main} =~ /\$r\b/) {                 . $def
314      $def{Main} = 'my $r;'."\n".$def{Main}."\n".'$r';                 . line ($Info, reset => 1);
315    }      if ($def{$_} =~ /\$r\b/) {
316    $def{Main} = barecode code $Info,        $def{$_} = 'my $r;'."\n".$def{$_}."\n".'$r';
317                 'sub {my ($self, $source, $opt) = @_;'      }
318               . $def{Main} . '}';      $def{$_} = barecode code $Info,
319                     'sub {my ($self, $source, $opt) = @_;'
320                   . $def{$_} . '}';
321      }
322        
323    my $r = list %def;    my $r = list %def;
324    if ($def{Type}) {    if ($def{Type}) {
# Line 286  sub make_format_converter ($$) { Line 327  sub make_format_converter ($$) {
327        if $def{Name};        if $def{Name};
328    } elsif ($def{Name}) {    } elsif ($def{Name}) {
329      $r = qq{\$Converter->{@{[literal '/'.$def{Name}.'/'.$def{Version}.$flag]}} = {$r};\n};      $r = qq{\$Converter->{@{[literal '/'.$def{Name}.'/'.$def{Version}.$flag]}} = {$r};\n};
330      $r    } else {
331        $r = 'BEGIN { die "Invalid Syntax of Converter: Type or Name property required" }';
332    }    }
333    $r;    $r;
334  }  }
# Line 300  sub make_function ($$) { Line 342  sub make_function ($$) {
342  sub @{[$name = $src->get_attribute_value ('Name')]} {  sub @{[$name = $src->get_attribute_value ('Name')]} {
343  @{[line $Info, node_path => "Function[Name='$name']/Main"]}@{[  @{[line $Info, node_path => "Function[Name='$name']/Main"]}@{[
344    code $Info, $src->get_attribute_value ('Main')    code $Info, $src->get_attribute_value ('Main')
345  ]}@{[line $Info, reset => 1]}  ]}
346  }  }
347    @{[line $Info, reset => 1]}
348  EOH  EOH
349  }  }
350    
351  sub register_plugin_const ($$) {  sub register_plugin_const ($$) {
352    my ($src, $Info) = @_;    my ($src, $Info) = @_;
353    for (@{$src->child_nodes}) {    for (@{$src->child_nodes}) {
354      $Info->{const}->{$_->local_name} = $_->value;      $Info->{const}->{$_->local_name} = literal_or_code $Info, $_->value;
355    }    }
356  }  }
357    
# Line 362  sub make_viewdef ($$) { Line 405  sub make_viewdef ($$) {
405    my $ViewProp = {};    my $ViewProp = {};
406    my $r = '';    my $r = '';
407    $ViewProp->{Name} = n11n $src->get_attribute_value ('Mode');    $ViewProp->{Name} = n11n $src->get_attribute_value ('Mode');
408      $ViewProp->{Name} =~ s/(?<=.)-/_/g;
409    $ViewProp->{pack_name} = random_module_name ($Info, $ViewProp->{Name});    $ViewProp->{pack_name} = random_module_name ($Info, $ViewProp->{Name});
410        
411    $ViewProp->{condition_stringified} = hash    $ViewProp->{condition_stringified} = hash
# Line 377  push \@SuikaWiki::View::Implementation:: Line 421  push \@SuikaWiki::View::Implementation::
421  @{[change_package $Info, $ViewProp->{pack_name}]}  @{[change_package $Info, $ViewProp->{pack_name}]}
422  our \@ISA = q#SuikaWiki::View::template#;  our \@ISA = q#SuikaWiki::View::template#;
423  EOH  EOH
424      
425      my $use = $src->get_attribute ('Use');
426      if (ref $use) {
427        $r .= line $Info, node_path => "ViewDefinition[Mode='$ViewProp->{Name}']/Use";
428        $r .= $use->inner_text . "\n\n";
429      }
430      
431    for (@{$src->child_nodes}) {    for (@{$src->child_nodes}) {
432      if ($_->local_name eq 'template') {      if ($_->local_name eq 'template') {
433        $r .= make_view_template_method ($_, $Info);        $r .= make_view_template_method ($_, $Info, $ViewProp);
434      } elsif ($_->local_name eq 'method') {      } elsif ($_->local_name eq 'method') {
435        my $method_name = $_->get_attribute_value ('Name');        my $method_name = $_->get_attribute_value ('Name');
436        $r .= ({        $r .= ({
437                main => q(sub main ($$) {)."\n".q(my ($self, $opt) = @_;)."\n",                main => q(sub main ($$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",
438                main_pre => q(sub main_pre ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",                main_pre => q(sub main_pre ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",
439                main_post => q(sub main_post ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",                                    main_post => q(sub main_post ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n",                    
440               }->{$method_name}               }->{$method_name}
441               ||qq(sub @{[$method_name]} {\n))               ||qq(sub @{[$method_name]} {\n))
442           . line ($Info, node_path => "ViewDefinition[Mode='$ViewProp->{Name}']/method[Name='$method_name']")           . line ($Info, node_path => "ViewDefinition[Mode='$ViewProp->{Name}']/method[Name='$method_name']")
443           . code ($Info, $_->value)           . code ($Info, $_->value)
444           . line ($Info, reset => 1)           . qq(}\n)
445           . qq(}\n);           . line ($Info, reset => 1);
446      }      }
447    }    }
448    my $prop = {Name => $ViewProp->{Name},    my $prop = {Name => $ViewProp->{Name},
# Line 401  EOH Line 452  EOH
452  }  }
453    
454  sub make_view_template_method ($$) {  sub make_view_template_method ($$) {
455    my ($src, $info) = @_;    my ($src, $Info, $ViewProp) = @_;
456    my $r = <<EOH;    my $r = <<EOH;
457    
458  sub main (\$\$\$) {  sub main (\$\$\$) {
# Line 417  sub main (\$\$\$) { Line 468  sub main (\$\$\$) {
468        
469    \$opt2->{template} = @{[literal $src->get_attribute ('body', make_new_node => 1)->inner_text]};    \$opt2->{template} = @{[literal $src->get_attribute ('body', make_new_node => 1)->inner_text]};
470    \$opt2->{o} = bless {    \$opt2->{o} = bless {
                      ## Compatible options for SuikaWiki 2 WikiPlugin interface  
                        param => \\\%main::form,  
                        page => \$main::form{mypage},  
                        toc => [],  
                        #magic  
                        #content  
                        #use_anchor_name  
                        media => {@{[hash  
     type => ($src->get_attribute ('media-type',make_new_node=>1)->inner_text  
              || 'application/octet-stream'),  
     charset => ($src->get_attribute ('use-media-type-charset',make_new_node=>1)  
                     ->inner_text || 0),  
     ## In fact, this value is not referred from any SuikaWiki 2 WikiPlugin rule.  
     #expires => ($src->get_attribute ('expires',make_new_node=>1)->inner_text  
     #                             || 0)  
     ]}},  
471                        ## SuikaWiki 3 WikiPlugin interface                        ## SuikaWiki 3 WikiPlugin interface
472                          wiki => \$self->{view}->{wiki},                          wiki => \$self->{view}->{wiki},
473                          plugin => \$self->{view}->{wiki}->{plugin},                          plugin => \$self->{view}->{wiki}->{plugin},
# Line 441  sub main (\$\$\$) { Line 476  sub main (\$\$\$) {
476    @{[do{my $x=$src->get_attribute('http-status-code',make_new_node=>1)->inner_text;    @{[do{my $x=$src->get_attribute('http-status-code',make_new_node=>1)->inner_text;
477       $x?q{$opt2->{output}->{status_code} = }.(0 + $x).q{;}:q{}}]}       $x?q{$opt2->{output}->{status_code} = }.(0 + $x).q{;}:q{}}]}
478    @{[do{my $x=$src->get_attribute('http-status-phrase',make_new_node=>1)->inner_text;    @{[do{my $x=$src->get_attribute('http-status-phrase',make_new_node=>1)->inner_text;
479       $x?q{$opt2->{output}->{reason_phrase} = }.literal($x).q{;}:q{}}]}       $x?q{$opt2->{output}->{status_phrase} = }.literal($x).q{;}:q{}}]}
480    \$opt2->{output}->{entity}->{media_type} = @{[literal    \$opt2->{output}->{entity}->{media_type} = @{[literal
481                             $src->get_attribute ('media-type',make_new_node=>1)                             $src->get_attribute ('media-type',make_new_node=>1)
482                                 ->inner_text || 'application/octet-stream']};                                 ->inner_text || 'application/octet-stream']};
# Line 456  sub main (\$\$\$) { Line 491  sub main (\$\$\$) {
491              qq{\$opt2->{output}->set_expires (delta => @{[0 + $x]});};              qq{\$opt2->{output}->set_expires (delta => @{[0 + $x]});};
492            }            }
493        }]}        }]}
494      \$opt2->{output}->{entity}->{body_is_octet_stream} = @{[
495        $src->get_attribute ('body')->get_attribute_value ('is-octet-stream', default_value => 0)
496        or 0
497      ]};
498        
499    \$self->{view}->{wiki}->init_db;    \$self->{view}->{wiki}->init_db;
500    \$self->main_pre (\$opt, \$opt2);    \$self->main_pre (\$opt, \$opt2);
501        
502    my \$fmt = SuikaWiki::Plugin->formatter ('view');    use Message::Util::Error;
503    \$opt2->{output}->{entity}->{body}    try {
504      = \$fmt->replace (\$opt2->{template}, param => \$opt2->{o});      \$opt2->{output}->{entity}->{body}
505          = SuikaWiki::Plugin->formatter ('view')
506            ->replace (\$opt2->{template}, param => \$opt2->{o});
507      } \$self->{view}->{wiki}->{config}->{catch}->{ @{[
508           $ViewProp->{Name} eq '-error' ? 'formatter_view_error'
509                                         : 'formatter_view' ]} };
510    \$opt2->{output}->output (output => 'http-cgi');    \$opt2->{output}->output (output => 'http-cgi');
511        
512    \$self->main_post (\$opt, \$opt2);    \$self->main_post (\$opt, \$opt2);
# Line 475  sub make_rule ($$) { Line 519  sub make_rule ($$) {
519    my $type = $src->get_attribute ('Category', make_new_node => 1)->value || [];    my $type = $src->get_attribute ('Category', make_new_node => 1)->value || [];
520    my $name = $src->get_attribute ('Name', make_new_node => 1)->value;    my $name = $src->get_attribute ('Name', make_new_node => 1)->value;
521    $name =~ s/(?<=.)-/_/g;    $name =~ s/(?<=.)-/_/g;
   my $main = line ($Info, node_path => "FormattingRule[name()='@{[list $type]}/$name']/Formatting")  
            . code ($Info, $src->get_attribute_value ('Formatting'))  
            . line ($Info, reset => 1);  
522        
523    my $reg_block;    my $reg_block;
524    $reg_block = qr/[^{}]*(?>[^{}]+|{(??{$reg_block})})*/;    $reg_block = qr/[^{}]*(?>[^{}]+|{(??{$reg_block})})*/;
525    my $reg_attr = qr/__ATTR(TEXT|NODE)?:%(\w+)(?:->{($reg_block)})?__;/;    my %code;
526        for my $codename ([qw/Formatting main/], [qw/After after/],
527    $main = q{my ($f, $rule_name, $p, $o, %opt) = @_;}."\n".$main                      [qw/Before before/], [qw/Pre pre/], [qw/Post post/],
528      if $main =~ /\$f\b/                      [qw/Attribute attr/]) {
529      or $main =~ /\$rule_name\b/      my $main = code $Info, $src->get_attribute_value ($codename->[0]);
530      or $main =~ /\$[opr]\b/      next unless $main;
531      or $main =~ /[%\$]opt\b/;      $main = line ($Info, node_path =>
532    if ($main =~ /\$r\b/) {                "FormattingRule[name()='@{[list $type]}/$name']/".$codename->[0])
533      warn qq(Rule @{[list $type]}/$name: Use of \$r is deprecated);            . $main;
534      $main = q{my $r = '';} . "\n" . $main . "\n"      
535            . q{$p->{-parent}->append_node ($r, node_or_text => 1);};      if ( $main =~ /\$f\b/
536    }        or $main =~ /\$rule_name\b/
537    $main =~ s{$reg_attr}        or $main =~ /\$[opr]\b/
538              {($1 eq 'TEXT' ? '$p->{'.literal($2).'} = do { my $r = ' : '')        or $main =~ /[%\$]opt\b/
539               .'$f->parse_attr ($p=>'.literal($2).', $o, '        or $main =~ /\$param_(?:name|value)\n/) {
540                               .($3?'-parent => '.$3.', ':'')        if ($codename->[0] ne 'Attribute') {
541                               .($1?'-non_parsed_to_node => 1, ':'')          $main = q{my ($f, $rule_name, $p, $o, %opt) = @_;}."\n".$main;
542                               .'%opt)'        } else {
543                               .($1 eq 'TEXT' ? '; ref $r?$r->inner_text:$r}' : '')          $main = q{my ($f, $rule_name, $p, $o, $param_name => $param_value, %opt) = @_;}."\n".$main;
544                               .';'}ge;        }
545          }
546    my $main = <<EOH;      if ($main =~ /\$r\b/) {
547          warn qq(Rule @{[list $type]}/$name: Use of \$r is deprecated);
548          $main = q{my $r = '';} . "\n" . $main . "\n"
549                . q{$p->{-parent}->append_node ($r, node_or_text => 1);};
550        }
551        $main =~ s{__ATTR(TEXT|NODE)?:%(\w+|{$reg_block})(?:->{($reg_block)})?__;}
552                  {($1 eq 'TEXT' ? '$p->{'.literal_or_code($Info, $2)
553                                          .'} = do { my $r = ' : '')
554                   .'$f->parse_attr ($p=>'.literal_or_code($Info, $2).', $o, '
555                                   .($3?'-parent => '.$3.', ':'')
556                                   .($1?'-non_parsed_to_node => 1, ':'')
557                                   .'%opt)'
558                                   .($1 eq 'TEXT' ? '; ref $r?$r->inner_text:$r}'
559                                                  : '')
560                                   .';'}ge;
561        $code{$codename->[1]} = barecode "sub {$main}";
562      }
563      
564      my $main = literal {
565        Description => [barecode m13ed_val_list $src, 'Description'],
566        Parameter => {do {
567          my @r;
568          for (@{$src->child_nodes}) {
569            if ($_->local_name eq 'Parameter') {
570              push @r, $_->get_attribute_value ('Name')
571                       => {Type => $_->get_attribute_value ('Type'),
572                           Default => $_->get_attribute_value ('Default'),
573                           Description => [barecode m13ed_val_list $_, 'Description']};
574            }
575          }
576          @r;
577        }},
578        %code,
579      };
580      $main .= line $Info, reset => 1;
581    
582    
583    my  $amain = <<EOH;
584  {  {
585    main => sub {$main},    main => sub {$main},
586    @{[line ($Info, reset => 1)]}
587    Description => [@{[m13ed_val_list $src, 'Description']}],    Description => [@{[m13ed_val_list $src, 'Description']}],
588    Parameter => {@{[do{    Parameter => {@{[do{
     my @r;  
     for (@{$src->child_nodes}) {  
       if ($_->local_name eq 'Parameter') {  
         push @r, $_->get_attribute_value ('Name')  
                  => {Type => $_->get_attribute_value ('Type'),  
                      Default => $_->get_attribute_value ('Default'),  
                      Description => [barecode m13ed_val_list $_, 'Description']};  
       }  
     }  
     list @r;  
589    }]}},    }]}},
590  }  }
591  EOH  EOH

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.12

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24