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