1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
require SuikaWiki::Markup::SuikaWikiConfig20::Parser; |
4 |
|
5 |
my $src = ''; |
6 |
my $srcfile = shift; |
7 |
open SRC, $srcfile or die "$0: $!"; { |
8 |
local $/ = undef; |
9 |
$src = <SRC>; |
10 |
} close SRC; |
11 |
|
12 |
sub literal ($) { |
13 |
my $s = shift; |
14 |
$s =~ s/([#\\])/\\$1/g; |
15 |
q<q#> . $s . q<#>; |
16 |
} |
17 |
sub n11n ($) { |
18 |
my $s = shift; |
19 |
$s =~ s/\s+/ /g; |
20 |
$s; |
21 |
} |
22 |
|
23 |
my $parser = SuikaWiki::Markup::SuikaWikiConfig20::Parser->new; |
24 |
my $plugins = $parser->parse_text ($src); |
25 |
my $meta = $plugins->get_attribute ('Plugin') |
26 |
or die "$0: Required 'Plugin' section not found"; |
27 |
my %Info = (Name => n11n $meta->get_attribute ('Name')->value); |
28 |
$Info{name_literal} = literal $Info{Name}; |
29 |
my @date = gmtime; |
30 |
$Info{LastModified} = sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', |
31 |
$date[5] + 1900, $date[4] + 1, @date[3,2,1,0]; |
32 |
$Info{Version} = sprintf '%04d.%02d%02d.%02d%02d', |
33 |
$date[5] + 1900, $date[4] + 1, @date[3,2,1]; |
34 |
$Info{module_name} = q#SuikaWiki::Plugin::plugin#; |
35 |
$Info{module_name} = random_module_name (\%Info, $Info{Name}); |
36 |
|
37 |
print <<EOH; |
38 |
use strict; |
39 |
package SuikaWiki::Plugin::Registry; |
40 |
our \%Info; |
41 |
\$Info{$Info{name_literal}}->{Name} = $Info{name_literal}; |
42 |
\$Info{$Info{name_literal}}->{Version} = q#$Info{Version}#; |
43 |
EOH |
44 |
for (qw/Description LastModified License/) { |
45 |
$Info{$_} = n11n $meta->get_attribute ($_,make_new_node=>1)->value; |
46 |
next unless length $Info{$_}; |
47 |
print qq{\$Info{$Info{name_literal}}->{$_} = } . literal $Info{$_}; |
48 |
print ";\n"; |
49 |
} |
50 |
for (qw/Author RelatedURI RelatedWikiPage RequiredPlugin RequiredModule/) { |
51 |
$Info{$_} = $meta->get_attribute ($_); |
52 |
next unless ref $Info{$_}; $Info{$_} = $Info{$_}->value; |
53 |
print qq{\$Info{$Info{name_literal}}->{$_} = [}; |
54 |
print join ', ', map {literal $_} @{$Info{$_}}; |
55 |
print "];\n"; |
56 |
} |
57 |
|
58 |
for (@{$plugins->child_nodes}) { |
59 |
if ($_->local_name eq 'ViewDefinition') { |
60 |
print "\n", make_viewdef ($_, \%Info); |
61 |
} elsif ($_->local_name eq 'ViewFragment') { |
62 |
print "\n", make_viewfragment ($_, \%Info); |
63 |
} |
64 |
} |
65 |
|
66 |
print "\n1;\n"; |
67 |
exit; |
68 |
|
69 |
sub make_viewfragment ($$) { |
70 |
my ($src, $Info) = @_; |
71 |
my $r = ''; |
72 |
for (@{$src->child_nodes}) { |
73 |
## TODO: use SuikaWiki2 interface |
74 |
$r .= qq(SuikaWiki::View->template (@{[literal $_->local_name]})->add_line (@{[literal $_->value]});\n); |
75 |
} |
76 |
$r; |
77 |
} |
78 |
|
79 |
sub make_viewdef ($$) { |
80 |
my ($src, $Info) = @_; |
81 |
my $ViewProp = {}; |
82 |
my $r = ''; |
83 |
$ViewProp->{Name} = n11n $src->get_attribute ('Mode')->value; |
84 |
$ViewProp->{pack_name} = random_module_name ($Info, $ViewProp->{Name}); |
85 |
|
86 |
$ViewProp->{condition_stringified} = join ', ', map {literal $_} |
87 |
mode => $ViewProp->{Name}, |
88 |
map {($_->local_name => $_->value)} |
89 |
@{$src->get_attribute ('Condition',make_new_node=>1)->child_nodes}; |
90 |
|
91 |
$r .= <<EOH; |
92 |
push \@SuikaWiki::View::Implementation::CommonViewDefs, { |
93 |
condition => {$ViewProp->{condition_stringified}}, |
94 |
object_class => q#$ViewProp->{pack_name}#, |
95 |
}; |
96 |
package $ViewProp->{pack_name}; |
97 |
our \@ISA = q#SuikaWiki::View::template#; |
98 |
EOH |
99 |
for (@{$src->child_nodes}) { |
100 |
if ($_->local_name eq 'method') { |
101 |
$r .= ({ |
102 |
main => q(sub main ($$) {)."\n".q(my ($self, $opt) = @_;)."\n", |
103 |
main_pre => q(sub main_pre ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n", |
104 |
main_post => q(sub main_post ($$$) {)."\n".q(my ($self, $opt, $opt2) = @_;)."\n", |
105 |
}->{$_->get_attribute ('Name')->value} |
106 |
||qq(sub @{[$_->get_attribute ('Name')->value]} {\n)) |
107 |
. $_->inner_text |
108 |
. qq(\n}\n); |
109 |
} |
110 |
} |
111 |
$r; |
112 |
} |
113 |
|
114 |
|
115 |
sub random_module_name ($;$) { |
116 |
my ($Info, $subname) = @_; |
117 |
$subname =~ s/[^0-9A-Za-z_:]//g; |
118 |
my @date = gmtime; |
119 |
my @rand = ('A'..'Z','a'..'z',0..9,'_'); |
120 |
sprintf '%s::%s%s%s', $Info{module_name}, $subname, |
121 |
sprintf ('%02d%02d%02d%02d%02d%02d', @date[5,4,3,2,1,0]), |
122 |
join ('', @rand[rand@rand,rand@rand,rand@rand,rand@rand]); |
123 |
} |