1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
our $VERSION = do{my @r=(q$Revision: 1.7 $=~/\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 source %s, block %s)', |
92 |
$Info->{source_file}, |
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{source_file} = $srcfile; |
108 |
$Info{name_literal} = literal $Info{Name}; |
109 |
my @date = gmtime; |
110 |
$Info{LastModified} = sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', |
111 |
$date[5] + 1900, $date[4] + 1, @date[3,2,1,0]; |
112 |
$Info{Version} = sprintf '%04d.%02d%02d.%02d%02d', |
113 |
$date[5] + 1900, $date[4] + 1, @date[3,2,1]; |
114 |
$Info{InterfaceVersion} = '2.9.1'; |
115 |
$Info{mkpluginVersion} = '2.'.$VERSION; |
116 |
$Info{module_name} = q#SuikaWiki::Plugin::plugin#; |
117 |
$Info{module_name} = random_module_name (\%Info, $Info{Name}); |
118 |
|
119 |
print <<EOH; |
120 |
use strict; |
121 |
@{[change_package \%Info, 'SuikaWiki::Plugin::Registry']} |
122 |
our \%Info; |
123 |
\$Info{$Info{name_literal}}->{Name} = $Info{name_literal}; |
124 |
EOH |
125 |
for (qw/Version InterfaceVersion mkpluginVersion module_name/) { |
126 |
print qq{\$Info{$Info{name_literal}}->{$_} = @{[literal $Info{$_}]};\n}; |
127 |
} |
128 |
for (qw/LastModified/) { |
129 |
$Info{$_} = n11n $meta->get_attribute ($_,make_new_node=>1)->value; |
130 |
next unless length $Info{$_}; |
131 |
print qq{\$Info{$Info{name_literal}}->{$_} = } . literal $Info{$_}; |
132 |
print ";\n"; |
133 |
} |
134 |
for (qw/RequiredPlugin RequiredModule/) { |
135 |
$Info{$_} = $meta->get_attribute ($_); |
136 |
next unless ref $Info{$_}; $Info{$_} = $Info{$_}->value; |
137 |
print qq{\$Info{$Info{name_literal}}->{$_} = [}; |
138 |
print join ', ', map {literal $_} @{$Info{$_}}; |
139 |
print "];\n"; |
140 |
} |
141 |
for (qw/Description License RelatedWikiPage RelatedURI/) { |
142 |
my $r = m13ed_val_list $meta, $_; |
143 |
next unless $r; |
144 |
print qq{\$Info{$Info{name_literal}}->{$_} = [$r];\n}; |
145 |
} |
146 |
|
147 |
print qq{\$Info{$Info{name_literal}}->{Author} = [} .( list map { |
148 |
[ |
149 |
[ barecode m13ed_val_list ($_, 'Name') ], |
150 |
[ $_->get_attribute ('Mail', make_new_node => 1)->value ], |
151 |
[ $_->get_attribute ('URI', make_new_node => 1)->value ], |
152 |
] |
153 |
} grep { $_->local_name eq 'Author' } @{$meta->child_nodes} |
154 |
). qq{];\n}; |
155 |
|
156 |
my $use = $meta->get_attribute ('Use'); |
157 |
if (ref $use) { |
158 |
print change_package \%Info, $Info{module_name}; |
159 |
print line \%Info, node_path => 'Plugin/Use'; |
160 |
print $use->inner_text, "\n"; |
161 |
print line \%Info, reset => 1; |
162 |
} |
163 |
|
164 |
for (@{$plugins->child_nodes}) { |
165 |
if ($_->local_name eq 'FormattingRule') { |
166 |
print "\n", make_rule ($_, \%Info); |
167 |
} elsif ($_->local_name eq 'ViewDefinition') { |
168 |
print "\n", make_viewdef ($_, \%Info); |
169 |
} elsif ($_->local_name eq 'ViewFragment') { |
170 |
print "\n", make_viewfragment ($_, \%Info); |
171 |
} elsif ($_->local_name eq 'Function') { |
172 |
print "\n", make_function ($_, \%Info); |
173 |
} elsif ($_->local_name eq 'Resource') { |
174 |
print "\n", make_resdef ($_, \%Info); |
175 |
} elsif ($_->local_name eq 'PluginConst') { |
176 |
register_plugin_const ($_, \%Info); |
177 |
} elsif ($_->local_name eq 'Format') { |
178 |
print "\n", make_format ($_, \%Info); |
179 |
} |
180 |
} |
181 |
|
182 |
print change_package \%Info, q(SuikaWiki::Plugin::Registry); |
183 |
print qq{\$Info{$Info{name_literal}}->{provide} = } . literal $Info{provide}; |
184 |
print qq{;\n}; |
185 |
|
186 |
print "\n1;\n"; |
187 |
exit; |
188 |
} |
189 |
|
190 |
sub make_format ($$) { |
191 |
my ($src, $Info) = @_; |
192 |
my $module_name = 'SuikaWiki::Format::Definition::'.$src->get_attribute_value ('ModuleName'); |
193 |
my $r = change_package $Info, $module_name; |
194 |
$r .= qq{our \@ISA;\n}; |
195 |
if (my $isa = $src->get_attribute_value ('Inherit')) { |
196 |
for (@$isa) { |
197 |
$r .= qq{push \@ISA, @{[literal 'SuikaWiki::Format::Definition::'.$_]};\n}; |
198 |
} |
199 |
} else { |
200 |
$r .= qq{push \@ISA, 'SuikaWiki::Format::Definition::template';\n}; |
201 |
} |
202 |
if (my $name = $src->get_attribute_value ('Name')) { |
203 |
$r .= qq{\$SuikaWiki::Format::Definition::Class{@{[literal '/'.$name.'/'.$src->get_attribute_value ('Version', default => '').'//']}} = '$module_name';\n}; |
204 |
} |
205 |
if (my $type = $src->get_attribute_value ('Type')) { |
206 |
$r .= qq{\$SuikaWiki::Format::Definition::Class{@{[literal $type.'//']}} = '$module_name';\n}; |
207 |
} |
208 |
|
209 |
my $convert = line $Info, line_no => __LINE__ + 2, realfile => __FILE__; |
210 |
$convert .= <<'EOH'; |
211 |
our $Converter; |
212 |
sub convert ($$;%) { |
213 |
my ($self, $source, %opt) = @_; |
214 |
my $converter; |
215 |
my $flag = '//'; |
216 |
$flag .= 'f' if $opt{IsFragment}; |
217 |
$flag .= 'p' if $opt{IsPlaceholder}; |
218 |
if ($Converter->{$opt{Type}.$flag}) { |
219 |
$converter = $Converter->{$opt{Type}.$flag}; |
220 |
} elsif ($Converter->{$opt{Name}.'/'.$opt{Version}.$flag}) { |
221 |
$converter = $Converter->{'/'.$opt{Name}.'/'.$opt{Version}.$flag}; |
222 |
} |
223 |
return $converter->{Main}->($self, $source, \%opt) if $converter; |
224 |
$self->SUPER::convert ($source, %opt); |
225 |
} |
226 |
EOH |
227 |
|
228 |
for (@{$src->child_nodes}) { |
229 |
if ($_->local_name eq 'Converter') { |
230 |
if ($convert) { |
231 |
$r .= $convert; |
232 |
$r .= line $Info, reset => 1; |
233 |
undef $convert; |
234 |
} |
235 |
$r .= make_format_converter ($_, $Info); |
236 |
} elsif ($_->local_name eq 'Use') { |
237 |
$r .= line $Info, node_path => qq(Format[module-name()=$module_name]/Use); |
238 |
$r .= $_->inner_text; |
239 |
} |
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, $ViewProp); |
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, $opt2) = @_;)."\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, $ViewProp) = @_; |
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}->{status_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 |
use Message::Util::Error; |
463 |
try { |
464 |
\$opt2->{output}->{entity}->{body} |
465 |
= SuikaWiki::Plugin->formatter ('view') |
466 |
->replace (\$opt2->{template}, param => \$opt2->{o}); |
467 |
} \$self->{view}->{wiki}->{config}->{catch}->{ @{[ |
468 |
$ViewProp->{Name} eq '-error' ? 'formatter_view_error' |
469 |
: 'formatter_view' ]} }; |
470 |
\$opt2->{output}->output (output => 'http-cgi'); |
471 |
|
472 |
\$self->main_post (\$opt, \$opt2); |
473 |
} |
474 |
EOH |
475 |
} |
476 |
|
477 |
sub make_rule ($$) { |
478 |
my ($src, $Info) = @_; |
479 |
my $type = $src->get_attribute ('Category', make_new_node => 1)->value || []; |
480 |
my $name = $src->get_attribute ('Name', make_new_node => 1)->value; |
481 |
$name =~ s/(?<=.)-/_/g; |
482 |
my $main = line ($Info, node_path => "FormattingRule[name()='@{[list $type]}/$name']/Formatting") |
483 |
. code ($Info, $src->get_attribute_value ('Formatting')) |
484 |
. line ($Info, reset => 1); |
485 |
|
486 |
my $reg_block; |
487 |
$reg_block = qr/[^{}]*(?>[^{}]+|{(??{$reg_block})})*/; |
488 |
my $reg_attr = qr/__ATTR(TEXT|NODE)?:%(\w+)(?:->{($reg_block)})?__;/; |
489 |
|
490 |
$main = q{my ($f, $rule_name, $p, $o, %opt) = @_;}."\n".$main |
491 |
if $main =~ /\$f\b/ |
492 |
or $main =~ /\$rule_name\b/ |
493 |
or $main =~ /\$[opr]\b/ |
494 |
or $main =~ /[%\$]opt\b/; |
495 |
if ($main =~ /\$r\b/) { |
496 |
warn qq(Rule @{[list $type]}/$name: Use of \$r is deprecated); |
497 |
$main = q{my $r = '';} . "\n" . $main . "\n" |
498 |
. q{$p->{-parent}->append_node ($r, node_or_text => 1);}; |
499 |
} |
500 |
$main =~ s{$reg_attr} |
501 |
{($1 eq 'TEXT' ? '$p->{'.literal($2).'} = do { my $r = ' : '') |
502 |
.'$f->parse_attr ($p=>'.literal($2).', $o, ' |
503 |
.($3?'-parent => '.$3.', ':'') |
504 |
.($1?'-non_parsed_to_node => 1, ':'') |
505 |
.'%opt)' |
506 |
.($1 eq 'TEXT' ? '; ref $r?$r->inner_text:$r}' : '') |
507 |
.';'}ge; |
508 |
|
509 |
my $main = <<EOH; |
510 |
{ |
511 |
main => sub {$main}, |
512 |
Description => [@{[m13ed_val_list $src, 'Description']}], |
513 |
Parameter => {@{[do{ |
514 |
my @r; |
515 |
for (@{$src->child_nodes}) { |
516 |
if ($_->local_name eq 'Parameter') { |
517 |
push @r, $_->get_attribute_value ('Name') |
518 |
=> {Type => $_->get_attribute_value ('Type'), |
519 |
Default => $_->get_attribute_value ('Default'), |
520 |
Description => [barecode m13ed_val_list $_, 'Description']}; |
521 |
} |
522 |
} |
523 |
list @r; |
524 |
}]}}, |
525 |
} |
526 |
EOH |
527 |
my $r = change_package $Info, $Info->{module_name}; |
528 |
if (@$type == 1) { |
529 |
$type->[0] =~ tr/-/_/; |
530 |
$r .= qq{\$SuikaWiki::Plugin::Rule{$type->[0]}->{$name} = $main;\n}; |
531 |
push @{$Info->{provide}->{rule}->{$type->[0]}}, $name; |
532 |
} else { |
533 |
$r .= qq({my \$def = $main;\n); |
534 |
for my $type (@$type) { |
535 |
$type =~ tr/-/_/; |
536 |
$r .= qq{\$SuikaWiki::Plugin::Rule{$type}->{$name} = \$def;\n}; |
537 |
push @{$Info->{provide}->{rule}->{$type}}, $name; |
538 |
} |
539 |
$r .= qq(};\n); |
540 |
} |
541 |
$r; |
542 |
} |
543 |
|
544 |
|
545 |
sub random_module_name ($;$) { |
546 |
my ($Info, $subname) = @_; |
547 |
$subname =~ s/[^0-9A-Za-z_:]//g; |
548 |
my @date = gmtime; |
549 |
my @rand = ('A'..'Z','a'..'z',0..9,'_'); |
550 |
sprintf '%s::%s%s%s', $Info->{module_name}, $subname, |
551 |
sprintf ('%02d%02d%02d%02d%02d%02d', @date[5,4,3,2,1,0]), |
552 |
join ('', @rand[rand@rand,rand@rand,rand@rand,rand@rand]); |
553 |
} |