(メッセージはありません)
@@ -0,0 +1,1154 @@ | ||
1 | +#!/usr/bin/perl | |
2 | +# $Id$ | |
3 | +# | |
4 | +# MTPlugin-Starter | |
5 | +# | |
6 | +# Based on plugin-start.pl(Plagger). | |
7 | +# | |
8 | +# Copyright 2007-2009 by M-Logic, Inc. | |
9 | +# | |
10 | +# This is free software; you can redistribute it and/or modify it | |
11 | +# under the same terms as Perl itself. | |
12 | + | |
13 | +use strict; | |
14 | +use warnings; | |
15 | + | |
16 | +use Config; | |
17 | +use FindBin; | |
18 | +use ExtUtils::MakeMaker; | |
19 | +use File::Basename; | |
20 | +use File::Path; | |
21 | +use YAML; | |
22 | +use Template; | |
23 | + | |
24 | +use vars qw( $VERSION ); | |
25 | +$VERSION = '0.90'; | |
26 | + | |
27 | +use Getopt::Long; | |
28 | +my %opts = (help => 0); | |
29 | +GetOptions( | |
30 | + \%opts, | |
31 | + 'name|n:s', | |
32 | + 'doc|d', | |
33 | + 'object|o', | |
34 | + 'config|c', | |
35 | + 'task|s', | |
36 | + 'transform|r', | |
37 | + 'tags|t', | |
38 | + 'filter|f', | |
39 | + 'hook|k', | |
40 | + 'method|m', | |
41 | + 'athena|a', | |
42 | + 'help|h', | |
43 | + 'version|v', | |
44 | + 'init', | |
45 | +); | |
46 | + | |
47 | +usage() if $opts{help}; | |
48 | +version() if $opts{version}; | |
49 | + | |
50 | +my $cfg_name = '.mt-plugin.yml'; | |
51 | +my $cfg_file; | |
52 | +if (($^O eq 'MSWin32') and $ENV{APPDATA}) { | |
53 | + $cfg_file = "$ENV{APPDATA}/M-Logic/MTPlugin-Starter/$cfg_name"; | |
54 | +} else { | |
55 | + $cfg_file = ($ENV{HOME}) ? "$ENV{HOME}/$cfg_name" | |
56 | + : "$cfg_name" | |
57 | + ; | |
58 | +} | |
59 | + | |
60 | +my $cfg_dir = File::Basename::dirname($cfg_file); | |
61 | +File::Path::mkpath($cfg_dir, 1, 0777) unless (-e $cfg_dir); | |
62 | +my $config = eval { YAML::LoadFile($cfg_file) }; | |
63 | + | |
64 | +if ($opts{init}) { | |
65 | + init_config(\$config); | |
66 | + YAML::DumpFile($cfg_file, $config); | |
67 | + exit; | |
68 | +} | |
69 | + | |
70 | +if (!$config) { | |
71 | + init_config(\$config); | |
72 | +} | |
73 | + | |
74 | +get_opt_dialogical(\%opts, $config->{opt_history}) unless $opts{name}; | |
75 | + | |
76 | +my $module = $opts{name}; | |
77 | +write_plugin_files($module, $config); | |
78 | + | |
79 | +print "...............finished!\n"; | |
80 | + | |
81 | +$config->{opt_history} = \%opts; | |
82 | +YAML::DumpFile($cfg_file, $config); | |
83 | + | |
84 | + | |
85 | +sub init_config { | |
86 | + my $config = shift; | |
87 | + $config ||= {}; | |
88 | + my @cfg_settings = ( | |
89 | + { name => 'author', | |
90 | + description => 'Your name', }, | |
91 | + { name => 'link', | |
92 | + description => 'Your site URL', }, | |
93 | + { name => 'baseversion', | |
94 | + description => 'Plugin\'s version at start', | |
95 | + default => '0.1', }, | |
96 | + ); | |
97 | + | |
98 | + foreach my $cfg (@cfg_settings) { | |
99 | + my $cfg_name = $cfg->{name}; | |
100 | + my $description = $cfg->{description}; | |
101 | + my $default_val = $$config->{$cfg_name} || $cfg->{default}; | |
102 | + my $val = prompt( "$description: ", $default_val ); | |
103 | + $val =~ s/^"(.*)"$/$1/; | |
104 | + $$config->{$cfg_name} = $val; | |
105 | + } | |
106 | + | |
107 | + $$config->{link} .= '/' if $$config->{link} !~ m!/$!; | |
108 | +} | |
109 | + | |
110 | +sub get_opt_dialogical { | |
111 | + my ($opts, $history) = @_; | |
112 | + my @opt_settings = ( | |
113 | + { name => 'name', | |
114 | + description => 'Plugin Name ', | |
115 | + ask => 'Enter your new plugin\'s name', }, | |
116 | + { name => 'doc', | |
117 | + bool => 1, | |
118 | + description => 'include documentation link ', | |
119 | + ask => 'Do you want to include URL of documentation?', }, | |
120 | + { name => 'object', | |
121 | + bool => 1, | |
122 | + description => 'using MT::Object ', | |
123 | + ask => 'Does your plugin have an original MT::Object?', }, | |
124 | + { name => 'config', | |
125 | + bool => 1, | |
126 | + description => 'using config template ', | |
127 | + ask => 'Does your plugin have configure templates?', }, | |
128 | + { name => 'task', | |
129 | + bool => 1, | |
130 | + description => 'using scheduled tasks ', | |
131 | + ask => 'Does your plugin have scheduled tasks?', }, | |
132 | + { name => 'transform', | |
133 | + bool => 1, | |
134 | + description => 'using transformer ', | |
135 | + ask => 'Does your plugin have transformer?', }, | |
136 | + { name => 'tags', | |
137 | + bool => 1, | |
138 | + description => 'using template tags ', | |
139 | + ask => 'Does your plugin have template tags?', }, | |
140 | + { name => 'filter', | |
141 | + bool => 1, | |
142 | + description => 'using text filters ', | |
143 | + ask => 'Does your plugin have text filters?', }, | |
144 | + { name => 'hook', | |
145 | + bool => 1, | |
146 | + description => 'using app callbacks ', | |
147 | + ask => 'Does your plugin use any application callbacks?', }, | |
148 | + { name => 'method', | |
149 | + bool => 1, | |
150 | + description => 'using methods ', | |
151 | + ask => 'Does your plugin use any methods?', }, | |
152 | + { name => 'athena', | |
153 | + bool => 1, | |
154 | + description => 'using MT4 structure ', | |
155 | + ask => 'Does your plugin use MT4 structure?', }, | |
156 | + ); | |
157 | + do { | |
158 | + foreach my $opt (@opt_settings) { | |
159 | + my $opt_name = $opt->{name}; | |
160 | + my $ask = $opt->{ask}; | |
161 | + my $default_val = $opts->{$opt_name} || $history->{$opt_name} || $opt->{default}; | |
162 | + my $val; | |
163 | + if ( $opt->{bool} ) { | |
164 | + $val = prompt_get_bool($ask, $default_val); | |
165 | + } | |
166 | + else { | |
167 | + $val = prompt( "$ask: ", $default_val ); | |
168 | + $val =~ s/^"(.*)"$/$1/; | |
169 | + } | |
170 | + $opts->{$opt_name} = $val; | |
171 | + } | |
172 | + | |
173 | + print "Your new plugin settings are...\n"; | |
174 | + foreach my $opt (@opt_settings) { | |
175 | + my $opt_name = $opt->{name}; | |
176 | + my $description = $opt->{description}; | |
177 | + my $val = $opts->{$opt_name}; | |
178 | + $val = $val ? 'yes' : 'no' if $opt->{bool}; | |
179 | + print " $description : $val\n"; | |
180 | + } | |
181 | + } while !prompt_get_bool('EVERYTHING IS OK?', 0); | |
182 | +} | |
183 | + | |
184 | +sub prompt_get_bool { | |
185 | + my ($description, $default) = @_; | |
186 | + $default = $default ? 'y' : 'n'; | |
187 | + my $val; | |
188 | + while (1) { | |
189 | + $val = prompt( "$description (y/n): ", $default ); | |
190 | + if ( $val =~ /y(es)?/i ) { | |
191 | + $val = 1; | |
192 | + last; | |
193 | + } | |
194 | + elsif ( $val =~ /no?/i ) { | |
195 | + $val = 0; | |
196 | + last; | |
197 | + } | |
198 | + print "Enter Y(es) or N(o)\n"; | |
199 | + } | |
200 | + return $val; | |
201 | +} | |
202 | + | |
203 | +sub usage { | |
204 | + my $myname = $0; | |
205 | + $myname =~ s|(.*/)?||; | |
206 | + | |
207 | + print STDERR <<END; | |
208 | +Usage: perl $myname -n <PluginName> [Options] | |
209 | + % perl $myname -n TestPlugin | |
210 | + % perl $myname -n TestPlugin -t -c | |
211 | + | |
212 | + without -n option, run with dialog mode. | |
213 | + | |
214 | + -d, --doc : Link for document | |
215 | + -o, --object : Object classes | |
216 | + -c, --config : Plugin settings | |
217 | + -s, --task : Schedule tasks | |
218 | + -r, --transform : Transformer | |
219 | + -t, --tags : Tags | |
220 | + -f, --filter : Global filter | |
221 | + -k, --hook : Hook | |
222 | + -m, --method : Methods | |
223 | + -a, --athena : Use MT4 style structure | |
224 | + | |
225 | + --init : Re-configure environment values | |
226 | + | |
227 | + -h, --help : Help | |
228 | + -v, --version : Version | |
229 | + | |
230 | +END | |
231 | + | |
232 | + exit; | |
233 | +} | |
234 | + | |
235 | +sub version { | |
236 | + my $myname = $0; | |
237 | + $myname =~ s|(.*/)?||; | |
238 | + | |
239 | + print "MTPlugin-Starter $VERSION\n"; | |
240 | + | |
241 | + exit; | |
242 | +} | |
243 | + | |
244 | +sub write_plugin_files { | |
245 | + my($module, $config) = @_; | |
246 | + | |
247 | + # $module = "FooBar" | |
248 | + # $file = "foo_bar" | |
249 | + (my $file = $module) =~ s!([a-z0-9])([A-Z])!$1_$2!g; | |
250 | + $file =~ s!^_!!g; | |
251 | + $file = lc $file; | |
252 | + | |
253 | + my $upper = uc $file; | |
254 | + my $lower = lc $module; | |
255 | + | |
256 | + my $template = YAML::Load(join '', <DATA>); | |
257 | + my $vars = { | |
258 | + module => $module, | |
259 | + file => $file, | |
260 | + upper => $upper, | |
261 | + lower => $lower, | |
262 | + vc_id => '$' . 'Id' . '$', | |
263 | + }; | |
264 | + | |
265 | + foreach my $key ( keys %$config ) { | |
266 | + $vars->{$key} = $config->{$key}; | |
267 | + } | |
268 | + | |
269 | + foreach my $key ( keys %opts ) { | |
270 | + $vars->{$key} = $opts{$key}; | |
271 | + } | |
272 | + | |
273 | + my $module_path = "$module/plugins/$module/"; | |
274 | + my $lib_path = $module_path . "lib/$module/"; | |
275 | + my $tmpl_path = $module_path . "tmpl/"; | |
276 | + my $doc_path = "$module/docs/"; | |
277 | + my $php_path = $opts{athena} ? "$module/plugins/$module/php/" : "$module/php/plugins/"; | |
278 | + write_file( | |
279 | + $module_path . "mt-$file.pl", | |
280 | + $opts{athena} ? $template->{plugin_athena} | |
281 | + : $template->{plugin}, | |
282 | + $vars | |
283 | + ); | |
284 | + write_file($lib_path . 'L10N.pm' , $template->{l10n} , $vars); | |
285 | + write_file($lib_path . 'L10N/en_us.pm' , $template->{en_us} , $vars); | |
286 | + write_file($lib_path . 'L10N/ja.pm' , $template->{ja} , $vars); | |
287 | + | |
288 | + if ($opts{object}) { | |
289 | + write_file($lib_path . "Object.pm", $template->{object}, $vars) ; | |
290 | + } | |
291 | + if ($opts{filter}) { | |
292 | + write_file($php_path . "modifier.$file.php", $template->{php_filter}, $vars) ; | |
293 | + } | |
294 | + if ($opts{method}) { | |
295 | + if ($opts{athena}) { | |
296 | + write_file($lib_path . "CMS.pm", $template->{cms_athena}, $vars) ; | |
297 | + write_file($tmpl_path . "view.tmpl", $template->{tmpl_view_athena}, $vars) ; | |
298 | + } else { | |
299 | + write_file($module_path . "mt-$file.cgi", $template->{cgi}, $vars) ; | |
300 | + write_file($lib_path . "App.pm", $template->{app}, $vars) ; | |
301 | + write_file($tmpl_path . "view.tmpl", $template->{tmpl_view}, $vars) ; | |
302 | + } | |
303 | + } | |
304 | + if ($opts{tags}) { | |
305 | + my $php_container_file = $opts{athena} ? 'block.mtcontainertag.php' | |
306 | + : 'block.MTContainerTag.php'; | |
307 | + my $php_condition_file = $opts{athena} ? 'block.mtconditiontag.php' | |
308 | + : 'block.MTConditionTag.php'; | |
309 | + my $php_function_file = $opts{athena} ? 'function.mttag.php' | |
310 | + : 'function.MTTag.php'; | |
311 | + write_file($php_path . $php_container_file, $template->{php_container_tag}, $vars) ; | |
312 | + write_file($php_path . $php_condition_file, $template->{php_if_tag}, $vars) ; | |
313 | + write_file($php_path . $php_function_file , $template->{php_var_tag}, $vars) ; | |
314 | + } | |
315 | + write_file($doc_path . "mt-$file.html", $template->{doc}, $vars); | |
316 | + write_file($doc_path . "mtdocs.css", $template->{css}, $vars); | |
317 | +} | |
318 | + | |
319 | +sub write_file { | |
320 | + my($path, $template, $vars) = @_; | |
321 | + | |
322 | + if (-e $path) { | |
323 | + return unless prompt_get_bool("$path exists. Override?", 0); | |
324 | + } | |
325 | + | |
326 | + my $dir = File::Basename::dirname($path); | |
327 | + unless (-e $dir) { | |
328 | + warn "Creating directory $dir\n"; | |
329 | + File::Path::mkpath($dir, 1, 0777); | |
330 | + } | |
331 | + | |
332 | + my $tt = Template->new(TRIM =>1,); | |
333 | + $tt->process(\$template, $vars, \my $content); | |
334 | + | |
335 | + warn "Creating $path\n"; | |
336 | + open my $out, ">", $path or die "$path: $!"; | |
337 | + print $out $content; | |
338 | + close $out; | |
339 | + | |
340 | + return $path; | |
341 | +} | |
342 | + | |
343 | +__DATA__ | |
344 | +plugin: | | |
345 | + # [% vc_id %] | |
346 | + | |
347 | + package MT::Plugin::[% module %]; | |
348 | + | |
349 | + use strict; | |
350 | + use MT::Template::Context; | |
351 | + use MT::Plugin; | |
352 | + @MT::Plugin::[% module %]::ISA = qw(MT::Plugin); | |
353 | + | |
354 | + use vars qw($PLUGIN_NAME $VERSION); | |
355 | + $PLUGIN_NAME = '[% module %]'; | |
356 | + $VERSION = '[% baseversion %]'; | |
357 | + | |
358 | + use MT; | |
359 | + my $plugin = new MT::Plugin::[% module %]({ | |
360 | + name => $PLUGIN_NAME, | |
361 | + version => $VERSION, | |
362 | + description => "<MT_TRANS phrase='description of [% module %]'>", | |
363 | + [%- IF doc %] | |
364 | + doc_link => '[% link %]mt-plugins/mt-[% file %]/docs/mt-[% file %].html', | |
365 | + [%- END %] | |
366 | + author_name => '[% author %]', | |
367 | + author_link => '[% link %]', | |
368 | + l10n_class => '[% module %]::L10N', | |
369 | + [%- IF object %] | |
370 | + schema_version => 0.01, | |
371 | + object_classes => [ '[% module %]::Object', ], | |
372 | + [%- END -%] | |
373 | + [%- IF config %] | |
374 | + config_template => \&config_template, | |
375 | + # blog_config_template => \&blog_config_template, | |
376 | + # system_config_template => \&system_config_template, | |
377 | + settings => new MT::PluginSettings([ | |
378 | + ['[% file %]_pref', { Default => 1 }], | |
379 | + ]), | |
380 | + [%- END %] | |
381 | + }); | |
382 | + | |
383 | + MT->add_plugin($plugin); | |
384 | + [%- IF task %] | |
385 | + | |
386 | + #----- Task | |
387 | + MT->add_task({ | |
388 | + key => '[% module %]', | |
389 | + name => '[% module %]', | |
390 | + frequency => 1 * 60 * 60, # no more than every 1 hours | |
391 | + code => sub { | |
392 | + $plugin->do_[% file %]; | |
393 | + } | |
394 | + }); | |
395 | + [%- END -%] | |
396 | + [%- IF transform %] | |
397 | + | |
398 | + #----- Transformer | |
399 | + #----- Transformer(MT4) | |
400 | + MT->add_callback('template_source.edit_entry', 9, $plugin, \&hdlr_mt4_source); | |
401 | + MT->add_callback('template_output.edit_entry', 9, $plugin, \&hdlr_mt4_output); | |
402 | + MT->add_callback('template_param.edit_entry', 9, $plugin, \&hdlr_mt4_param); | |
403 | + | |
404 | + #----- Transformer(MT3.3) | |
405 | + MT->add_callback('MT::App::CMS::AppTemplateSource.edit_entry', 9, $plugin, \&hdlr_source); | |
406 | + MT->add_callback('MT::App::CMS::AppTemplateOutput.edit_entry', 9, $plugin, \&hdlr_output); | |
407 | + MT->add_callback('MT::App::CMS::AppTemplateParam.edit_entry', 9, $plugin, \&hdlr_param); | |
408 | + [%- END -%] | |
409 | + [%- IF tags %] | |
410 | + | |
411 | + #----- Tags | |
412 | + MT::Template::Context->add_container_tag('ContainerTag' => sub{&hdlr_container;}); | |
413 | + MT::Template::Context->add_tag('Tag' => sub{&hdlr_tag;}); | |
414 | + MT::Template::Context->add_conditional_tag('IfTag' => sub{&hdlr_if;}); | |
415 | + [%- END -%] | |
416 | + [%- IF filter %] | |
417 | + | |
418 | + #----- Global filter | |
419 | + MT::Template::Context->add_global_filter([% file %] => \&[% file %] ); | |
420 | + [%- END -%] | |
421 | + [%- IF hook %] | |
422 | + | |
423 | + #----- Hook | |
424 | + MT->add_callback('CMSPreSave.entry', 9, $plugin, \&hdlr_cms_pre_save_entry); | |
425 | + MT->add_callback('CMSPostSave.entry', 9, $plugin, \&hdlr_cms_post_save_entry); | |
426 | + MT->add_callback("BuildPage", 9, $plugin, \&hdlr_cb_build_page); | |
427 | + [%- END %] | |
428 | + | |
429 | + | |
430 | + sub instance { $plugin; } | |
431 | + | |
432 | + sub doLog { | |
433 | + my ($msg) = @_; | |
434 | + return unless defined($msg); | |
435 | + | |
436 | + require MT::Log; | |
437 | + my $log = MT::Log->new; | |
438 | + $log->message($msg) ; | |
439 | + $log->save or die $log->errstr; | |
440 | + } | |
441 | + [%- IF config %] | |
442 | + | |
443 | + sub config_template { | |
444 | + my $tmpl = <<'EOT'; | |
445 | + <div class="setting"> | |
446 | + <div class="label"> | |
447 | + <label for="[% file %]_pref"><MT_TRANS phrase="[% module %] pref:"></label> | |
448 | + </div> | |
449 | + <div class="field"> | |
450 | + <p> | |
451 | + <input type="text" name="[% file %]_pref" id="[% file %]_pref" value="<TMPL_VAR NAME=[% upper %]_PREF ESCAPE=HTML>" /> | |
452 | + </p> | |
453 | + <p><MT_TRANS phrase="[% module %] pref"></p> | |
454 | + </div> | |
455 | + </div> | |
456 | + EOT | |
457 | + } | |
458 | + | |
459 | + sub [% file %]_pref { | |
460 | + my $plugin = shift; | |
461 | + my ($blog_id) = @_; | |
462 | + my %plugin_param; | |
463 | + | |
464 | + $plugin->load_config(\%plugin_param, 'blog:'.$blog_id); | |
465 | + my $value = $plugin_param{[% file %]_pref}; | |
466 | + unless ($value) { | |
467 | + $plugin->load_config(\%plugin_param, 'system'); | |
468 | + $value = $plugin_param{[% file %]_pref}; | |
469 | + } | |
470 | + $value; | |
471 | + } | |
472 | + [%- END -%] | |
473 | + [%- IF task %] | |
474 | + | |
475 | + #----- Task | |
476 | + sub do_[% file %] { | |
477 | + | |
478 | + 1 | |
479 | + } | |
480 | + [%- END -%] | |
481 | + [%- IF transform %] | |
482 | + | |
483 | + #----- Transformer(MT4) | |
484 | + sub hdlr_mt4_source { | |
485 | + my ($cb, $app, $tmpl_ref) = @_; | |
486 | + | |
487 | + 1; | |
488 | + } | |
489 | + | |
490 | + sub hdlr_mt4_output { | |
491 | + my ($cb, $app, $tmpl_str_ref, $param, $tmpl) = @_; | |
492 | + | |
493 | + 1; | |
494 | + } | |
495 | + | |
496 | + sub hdlr_mt4_param { | |
497 | + my ($cb, $app, $param, $tmpl) = @_; | |
498 | + | |
499 | + 1; | |
500 | + } | |
501 | + | |
502 | + #----- Transformer(MT3.3) | |
503 | + sub hdlr_source { | |
504 | + my ($cb, $app, $tmpl_ref) = @_; | |
505 | + | |
506 | + 1; | |
507 | + } | |
508 | + | |
509 | + sub hdlr_output { | |
510 | + my ($cb, $app, $tmpl_str_ref, $param, $tmpl) = @_; | |
511 | + | |
512 | + 1; | |
513 | + } | |
514 | + | |
515 | + sub hdlr_param { | |
516 | + my ($cb, $app, $param, $tmpl) = @_; | |
517 | + | |
518 | + 1; | |
519 | + } | |
520 | + [%- END -%] | |
521 | + [%- IF tags %] | |
522 | + | |
523 | + #----- Tags | |
524 | + sub hdlr_container { | |
525 | + my ($ctx, $args, $cond) = @_; | |
526 | + my $builder = $ctx->stash('builder'); | |
527 | + my $tokens = $ctx->stash('tokens'); | |
528 | + | |
529 | + defined(my $out = $builder->build($ctx, $tokens, $cond)) | |
530 | + || return $ctx->error($ctx->errstr); | |
531 | + | |
532 | + return $out; | |
533 | + } | |
534 | + | |
535 | + sub hdlr_tag { | |
536 | + my ($ctx, $args) = @_; | |
537 | + | |
538 | + 1; | |
539 | + } | |
540 | + | |
541 | + sub hdlr_if { | |
542 | + my ($ctx, $args, $cond) = @_; | |
543 | + | |
544 | + 1; | |
545 | + } | |
546 | + [%- END -%] | |
547 | + [%- IF filter %] | |
548 | + | |
549 | + #----- Global filter | |
550 | + sub [% file %] { | |
551 | + my ($text, $arg, $ctx) = @_; | |
552 | + $arg or return $text; | |
553 | + | |
554 | + $text; | |
555 | + } | |
556 | + [%- END -%] | |
557 | + [%- IF hook %] | |
558 | + | |
559 | + #----- Hook | |
560 | + sub hdlr_cms_pre_save_entry { | |
561 | + my ($cb, $app, $obj, $original) = @_; | |
562 | + | |
563 | + 1; | |
564 | + } | |
565 | + | |
566 | + sub hdlr_cms_post_save_entry { | |
567 | + my ($cb, $app, $obj) = @_; | |
568 | + | |
569 | + 1; | |
570 | + } | |
571 | + | |
572 | + sub hdlr_cb_build_page { | |
573 | + my ($cb, %args) = @_; | |
574 | + my $content_ref = $args{Content}; | |
575 | + | |
576 | + 1; | |
577 | + } | |
578 | + [%- END %] | |
579 | + | |
580 | + 1; | |
581 | + | |
582 | +plugin_athena: | | |
583 | + # [% vc_id %] | |
584 | + | |
585 | + package MT::Plugin::[% module %]; | |
586 | + | |
587 | + use strict; | |
588 | + use MT::Plugin; | |
589 | + use base qw( MT::Plugin ); | |
590 | + | |
591 | + use vars qw($PLUGIN_NAME $VERSION); | |
592 | + $PLUGIN_NAME = '[% module %]'; | |
593 | + $VERSION = '[% baseversion %]'; | |
594 | + | |
595 | + use MT; | |
596 | + my $plugin = MT::Plugin::[% module %]->new({ | |
597 | + id => '[% lower %]', | |
598 | + key => __PACKAGE__, | |
599 | + name => $PLUGIN_NAME, | |
600 | + version => $VERSION, | |
601 | + description => "<MT_TRANS phrase='description of [% module %]'>", | |
602 | + [%- IF doc %] | |
603 | + doc_link => '[% link %]mt-plugins/mt-[% file %]/docs/mt-[% file %].html', | |
604 | + [%- END %] | |
605 | + author_name => '[% author %]', | |
606 | + author_link => '[% link %]', | |
607 | + l10n_class => '[% module %]::L10N', | |
608 | + [%- IF object %] | |
609 | + schema_version => 0.01, | |
610 | + object_classes => [ '[% module %]::Object', ], | |
611 | + [%- END -%] | |
612 | + [%- IF config %] | |
613 | + config_template => \&config_template, | |
614 | + # blog_config_template => \&blog_config_template, | |
615 | + # system_config_template => \&system_config_template, | |
616 | + settings => new MT::PluginSettings([ | |
617 | + ['[% file %]_pref', { Default => 1 }], | |
618 | + ]), | |
619 | + [%- END %] | |
620 | + registry => { | |
621 | + [%- IF object %] | |
622 | + object_types => { | |
623 | + '[% file %]' => '[% module %]::Object', | |
624 | + }, | |
625 | + [%- END -%] | |
626 | + [%- IF method %] | |
627 | + applications => { | |
628 | + cms => { | |
629 | + methods => { | |
630 | + [% file %]_view => '$[% module %]::[% module %]::CMS::view', | |
631 | + }, | |
632 | + }, | |
633 | + }, | |
634 | + [%- END -%] | |
635 | + [%- IF task %] | |
636 | + tasks => { | |
637 | + '[% module %]' => { | |
638 | + label => '[% module %]', | |
639 | + frequency => 1 * 60 * 60, # no more than every 1 hours | |
640 | + code => \&do_[% file %], | |
641 | + }, | |
642 | + }, | |
643 | + [%- END -%] | |
644 | + [%- IF tags || filter %] | |
645 | + tags => { | |
646 | + [%- END -%] | |
647 | + [%- IF tags %] | |
648 | + function => { | |
649 | + 'Tag' => \&hdlr_tag, | |
650 | + }, | |
651 | + block => { | |
652 | + 'ContainerTag' => \&hdlr_container, | |
653 | + 'IfTag?' => \&hdlr_if, | |
654 | + }, | |
655 | + [%- END -%] | |
656 | + [%- IF filter %] | |
657 | + modifier => { | |
658 | + '[% file %]' => \&[% file %], | |
659 | + }, | |
660 | + [%- END %] | |
661 | + [%- IF tags || filter %] | |
662 | + }, | |
663 | + [%- END %] | |
664 | + [%- IF transform || hook %] | |
665 | + callbacks => { | |
666 | + [%- END %] | |
667 | + [%- IF transform %] | |
668 | + #----- Transformer | |
669 | + 'template_source.edit_entry' | |
670 | + => \&hdlr_mt4_source, | |
671 | + 'template_output.edit_entry' | |
672 | + => \&hdlr_mt4_output, | |
673 | + 'template_param.edit_entry' | |
674 | + => \&hdlr_mt4_param, | |
675 | + [%- END -%] | |
676 | + [%- IF hook %] | |
677 | + #----- Hook | |
678 | + 'cms_pre_save.entry' | |
679 | + => \&hdlr_cms_pre_save_entry, | |
680 | + 'cms_post_save.entry' | |
681 | + => \&hdlr_cms_post_save_entry, | |
682 | + 'build_page' | |
683 | + => \&hdlr_cb_build_page, | |
684 | + [%- END %] | |
685 | + [%- IF transform || hook %] | |
686 | + }, | |
687 | + [%- END %] | |
688 | + }, | |
689 | + }); | |
690 | + | |
691 | + MT->add_plugin($plugin); | |
692 | + | |
693 | + sub instance { $plugin; } | |
694 | + | |
695 | + sub doLog { | |
696 | + my ($msg) = @_; | |
697 | + return unless defined($msg); | |
698 | + | |
699 | + require MT::Log; | |
700 | + my $log = MT::Log->new; | |
701 | + $log->message($msg) ; | |
702 | + $log->save or die $log->errstr; | |
703 | + } | |
704 | + [%- IF config %] | |
705 | + | |
706 | + sub config_template { | |
707 | + my $tmpl = <<'EOT'; | |
708 | + <mtapp:setting | |
709 | + id="[% file %]_pref" | |
710 | + label="<__trans phrase="[% module %] pref:">" | |
711 | + hint="<__trans phrase="Hint">"> | |
712 | + <input type="text" name="[% file %]_pref" id="[% file %]_pref" value="<mt:var name="[% file %]_pref" escape="html">" /> | |
713 | + </mtapp:setting> | |
714 | + EOT | |
715 | + } | |
716 | + | |
717 | + sub [% file %]_pref { | |
718 | + my $plugin = shift; | |
719 | + my ($blog_id) = @_; | |
720 | + my %plugin_param; | |
721 | + | |
722 | + $plugin->load_config(\%plugin_param, 'blog:'.$blog_id); | |
723 | + my $value = $plugin_param{[% file %]_pref}; | |
724 | + unless ($value) { | |
725 | + $plugin->load_config(\%plugin_param, 'system'); | |
726 | + $value = $plugin_param{[% file %]_pref}; | |
727 | + } | |
728 | + $value; | |
729 | + } | |
730 | + [%- END -%] | |
731 | + [%- IF task %] | |
732 | + | |
733 | + #----- Task | |
734 | + sub do_[% file %] { | |
735 | + | |
736 | + 1 | |
737 | + } | |
738 | + [%- END -%] | |
739 | + [%- IF transform %] | |
740 | + | |
741 | + #----- Transformer | |
742 | + sub hdlr_mt4_source { | |
743 | + my ($cb, $app, $tmpl_ref) = @_; | |
744 | + | |
745 | + 1; | |
746 | + } | |
747 | + | |
748 | + sub hdlr_mt4_output { | |
749 | + my ($cb, $app, $tmpl_str_ref, $param, $tmpl) = @_; | |
750 | + | |
751 | + 1; | |
752 | + } | |
753 | + | |
754 | + sub hdlr_mt4_param { | |
755 | + my ($cb, $app, $param, $tmpl) = @_; | |
756 | + | |
757 | + 1; | |
758 | + } | |
759 | + [%- END -%] | |
760 | + [%- IF tags %] | |
761 | + | |
762 | + #----- Tags | |
763 | + sub hdlr_container { | |
764 | + my ($ctx, $args, $cond) = @_; | |
765 | + my $builder = $ctx->stash('builder'); | |
766 | + my $tokens = $ctx->stash('tokens'); | |
767 | + | |
768 | + defined(my $out = $builder->build($ctx, $tokens, $cond)) | |
769 | + || return $ctx->error($ctx->errstr); | |
770 | + | |
771 | + return $out; | |
772 | + } | |
773 | + | |
774 | + sub hdlr_tag { | |
775 | + my ($ctx, $args) = @_; | |
776 | + | |
777 | + 1; | |
778 | + } | |
779 | + | |
780 | + sub hdlr_if { | |
781 | + my ($ctx, $args, $cond) = @_; | |
782 | + | |
783 | + 1; | |
784 | + } | |
785 | + [%- END -%] | |
786 | + [%- IF filter %] | |
787 | + | |
788 | + #----- Global filter | |
789 | + sub [% file %] { | |
790 | + my ($text, $arg, $ctx) = @_; | |
791 | + $arg or return $text; | |
792 | + | |
793 | + $text; | |
794 | + } | |
795 | + [%- END -%] | |
796 | + [%- IF hook %] | |
797 | + | |
798 | + #----- Hook | |
799 | + sub hdlr_cms_pre_save_entry { | |
800 | + my ($cb, $app, $obj, $original) = @_; | |
801 | + | |
802 | + 1; | |
803 | + } | |
804 | + | |
805 | + sub hdlr_cms_post_save_entry { | |
806 | + my ($cb, $app, $obj) = @_; | |
807 | + | |
808 | + 1; | |
809 | + } | |
810 | + | |
811 | + sub hdlr_cb_build_page { | |
812 | + my ($cb, %args) = @_; | |
813 | + my $content_ref = $args{Content}; | |
814 | + | |
815 | + 1; | |
816 | + } | |
817 | + [%- END %] | |
818 | + | |
819 | + 1; | |
820 | + | |
821 | +l10n: | | |
822 | + # [% vc_id %] | |
823 | + | |
824 | + package [% module %]::L10N; | |
825 | + | |
826 | + use strict; | |
827 | + use base 'MT::Plugin::L10N'; | |
828 | + | |
829 | + 1; | |
830 | + | |
831 | +en_us: | | |
832 | + # [% vc_id %] | |
833 | + | |
834 | + package [% module %]::L10N::en_us; | |
835 | + | |
836 | + use strict; | |
837 | + use base '[% module %]::L10N'; | |
838 | + use vars qw( %Lexicon ); | |
839 | + | |
840 | + %Lexicon = ( | |
841 | + ); | |
842 | + | |
843 | + 1; | |
844 | + | |
845 | +ja: | | |
846 | + # [% vc_id %] | |
847 | + | |
848 | + package [% module %]::L10N::ja; | |
849 | + | |
850 | + use strict; | |
851 | + use base '[% module %]::L10N::en_us'; | |
852 | + use vars qw( %Lexicon ); | |
853 | + | |
854 | + ## The following is the translation table. | |
855 | + | |
856 | + %Lexicon = ( | |
857 | + 'description of [% module %]' => '[% module %]の説明', | |
858 | + ); | |
859 | + | |
860 | + 1; | |
861 | + | |
862 | +doc: | | |
863 | + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
864 | + <html> | |
865 | + <head> | |
866 | + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
867 | + <title>[% module %]プラグイン</title> | |
868 | + <link href="mtdocs.css" rel="stylesheet" type="text/css"> | |
869 | + </head> | |
870 | + | |
871 | + <body> | |
872 | + <h1>[% module %]プラグイン</h1> | |
873 | + | |
874 | + <h2>はじめに</h2> | |
875 | + <p> | |
876 | + </p> | |
877 | + | |
878 | + <h2>インストール</h2> | |
879 | + <p> | |
880 | + </p> | |
881 | + | |
882 | + <h2>使い方</h2> | |
883 | + <p> | |
884 | + </p> | |
885 | + | |
886 | + <h2>更新履歴</h2> | |
887 | + <ul> | |
888 | + <li>[200*-**-**:v0.50]</li> | |
889 | + <ul> | |
890 | + <li>最初のリリース</li> | |
891 | + </ul> | |
892 | + </ul> | |
893 | + | |
894 | + <h2>このプラグインの利用、及び著作権や保証について</h2> | |
895 | + | |
896 | + <p> | |
897 | + このプラグインの著作権は[% author %]が所有しています。著作権者の許可なく本プラグインを使用、改変、再配布することはできません。 | |
898 | + </p> | |
899 | + | |
900 | + <h2>連絡先</h2> | |
901 | + | |
902 | + <p>作者:<a href="[% link %]">[% author %]</a></p> | |
903 | + | |
904 | + </body> | |
905 | + </html> | |
906 | + | |
907 | +css: | | |
908 | + body { | |
909 | + margin-right: 20px; | |
910 | + margin-left: 20px; | |
911 | + } | |
912 | + | |
913 | + p { | |
914 | + margin-right: 5px; | |
915 | + margin-left: 5px; | |
916 | + } | |
917 | + | |
918 | + h1 { | |
919 | + border-bottom-width: 0; | |
920 | + } | |
921 | + | |
922 | + h2 { | |
923 | + font-size: medium; | |
924 | + padding-left: 5px; | |
925 | + padding-top: 3px; | |
926 | + padding-bottom: 3px; | |
927 | + border-top: 1px solid #039; | |
928 | + border-bottom: 1px solid #039; | |
929 | + border-left: 10px solid #039; | |
930 | + } | |
931 | + | |
932 | + h3 { | |
933 | + font-size: medium; | |
934 | + padding-left: 5px; | |
935 | + padding-top: 3px; | |
936 | + padding-bottom: 3px; | |
937 | + border-bottom: 1px solid #aaa; | |
938 | + border-left: 10px solid #aaa; | |
939 | + } | |
940 | + | |
941 | + dt { | |
942 | + font-weight: bold; | |
943 | + margin-top: 15px; | |
944 | + margin-bottom: 5px; | |
945 | + } | |
946 | + | |
947 | + dd { | |
948 | + margin-top: 5px; | |
949 | + margin-bottom: 5px; | |
950 | + } | |
951 | + | |
952 | + strong { | |
953 | + font-weight: bold; | |
954 | + } | |
955 | + | |
956 | + .sample { | |
957 | + margin-right: 15px; | |
958 | + margin-left: 15px; | |
959 | + background: #E6E6FF; | |
960 | + padding: 5px; | |
961 | + } | |
962 | + | |
963 | +object: | | |
964 | + # [% vc_id %] | |
965 | + | |
966 | + package [% module %]::Object; | |
967 | + use warnings; | |
968 | + use strict; | |
969 | + use Carp; | |
970 | + | |
971 | + use MT::Object; | |
972 | + @[% module %]::Object::ISA = qw( MT::Object ); | |
973 | + | |
974 | + __PACKAGE__->install_properties ({ | |
975 | + column_defs => { | |
976 | + 'id' => 'integer not null auto_increment', | |
977 | + }, | |
978 | + indexes => { | |
979 | + }, | |
980 | + datasource => '[% file %]', | |
981 | + #audit => 1, | |
982 | + primary_key => 'id', | |
983 | + }); | |
984 | + | |
985 | + 1; | |
986 | + | |
987 | + __END__ | |
988 | + | |
989 | +cgi: | | |
990 | + #!/usr/bin/perl -w | |
991 | + use strict; | |
992 | + use lib 'lib', ($ENV{MT_HOME} ? "$ENV{MT_HOME}/lib" : '../../lib/'); | |
993 | + use MT::Bootstrap App => '[% module %]::App'; | |
994 | + | |
995 | + __END__ | |
996 | + | |
997 | +app: | | |
998 | + # [% vc_id %] | |
999 | + | |
1000 | + package [% module %]::App; | |
1001 | + | |
1002 | + use strict; | |
1003 | + use base qw( MT::App::CMS ); | |
1004 | + | |
1005 | + sub init { | |
1006 | + my $app = shift; | |
1007 | + my %param = @_; | |
1008 | + $app->SUPER::init(%param) or return; | |
1009 | + $app->add_methods( | |
1010 | + 'view' => \&view, | |
1011 | + ); | |
1012 | + $app->{default_mode} = 'view'; | |
1013 | + $app->{plugin_template_path} = 'tmpl'; | |
1014 | + $app->{requires_login} = 1; | |
1015 | + $app; | |
1016 | + } | |
1017 | + | |
1018 | + sub plugin { | |
1019 | + MT::Plugin::[% module %]->instance; | |
1020 | + } | |
1021 | + | |
1022 | + sub translate { | |
1023 | + my $app = shift; | |
1024 | + $app->plugin->translate(@_); | |
1025 | + } | |
1026 | + | |
1027 | + sub translate_templatized { | |
1028 | + my $app = shift; | |
1029 | + $app->plugin->translate_templatized(@_); | |
1030 | + } | |
1031 | + | |
1032 | + sub build_page { | |
1033 | + my $app = shift; | |
1034 | + my ($page, $param) = @_; | |
1035 | + my $q = $app->param; | |
1036 | + my $uri = $app->static_path; | |
1037 | + $uri .= '/' unless $uri =~ m!/$!; | |
1038 | + $uri .= $app->plugin->envelope . '/'; | |
1039 | + $uri = $app->base . $uri if $uri =~ m!^/!; | |
1040 | + $param->{plugin_static_uri} = $uri; | |
1041 | + my $blog_id = $q->param('blog_id'); | |
1042 | + $param->{system_overview_nav} = 1 unless $blog_id; | |
1043 | + $param->{plugin_name} = $MT::Plugin::[% module %]::PLUGIN_NAME; | |
1044 | + $param->{plugin_version} = $MT::Plugin::[% module %]::VERSION; | |
1045 | + $app->SUPER::build_page($page, $param); | |
1046 | + } | |
1047 | + | |
1048 | + sub view { | |
1049 | + my $app = shift; | |
1050 | + my $q = $app->param; | |
1051 | + my %param = (); | |
1052 | + $app->add_breadcrumb($app->translate('[% module %]')); | |
1053 | + $app->build_page('view.tmpl', \%param); | |
1054 | + } | |
1055 | + | |
1056 | + 1; | |
1057 | + | |
1058 | +tmpl_view: | | |
1059 | + <TMPL_INCLUDE NAME="header.tmpl"> | |
1060 | + | |
1061 | + <TMPL_INCLUDE NAME="footer.tmpl"> | |
1062 | + | |
1063 | + | |
1064 | +cms_athena: | | |
1065 | + # [% vc_id %] | |
1066 | + | |
1067 | + package [% module %]::CMS; | |
1068 | + | |
1069 | + use strict; | |
1070 | + | |
1071 | + sub view { | |
1072 | + my $app = shift; | |
1073 | + my (%opt) = @_; | |
1074 | + | |
1075 | + my $q = $app->{query}; | |
1076 | + my $blog_id = scalar $q->param('blog_id'); | |
1077 | + | |
1078 | + my $tmpl = $app->load_tmpl('view.tmpl'); | |
1079 | + $tmpl->param('blog_id' => $blog_id); | |
1080 | + $app->add_breadcrumb($app->translate("Main Menu"),$app->{mtscript_url}); | |
1081 | + require MT::Blog; | |
1082 | + my $blog = MT::Blog->load ($blog_id); | |
1083 | + $app->add_breadcrumb($blog->name,$app->mt_uri( mode => 'menu', args => { blog_id => $blog_id })); | |
1084 | + $app->add_breadcrumb($app->translate("[% module %]")); | |
1085 | + | |
1086 | + $tmpl->param(breadcrumbs => $app->{breadcrumbs}); | |
1087 | + return $app->build_page($tmpl); | |
1088 | + } | |
1089 | + | |
1090 | + 1; | |
1091 | + | |
1092 | +tmpl_view_athena: | | |
1093 | + <mt:setvarblock name="page_title"><__trans phrase="[% module %]"></mt:setvarblock> | |
1094 | + <mt:include name="include/header.tmpl"> | |
1095 | + | |
1096 | + <mt:include name="include/footer.tmpl"> | |
1097 | + | |
1098 | +php_filter: | | |
1099 | + <?php | |
1100 | + function smarty_modifier_[% file %]($text) { | |
1101 | + return $text; | |
1102 | + } | |
1103 | + ?> | |
1104 | + | |
1105 | +php_container_tag: | | |
1106 | + <?php | |
1107 | + function smarty_block_[% IF athena %]mtcontainertag[% ELSE %]MTContainerTag[% END %]($args, $content, &$ctx, &$repeat) { | |
1108 | + $localvars = array('_items', '_items_counter', 'item'); | |
1109 | + if(!isset($content)) { | |
1110 | + $ctx->localize($localvars); | |
1111 | + // load items here... | |
1112 | + $items = array(); | |
1113 | + $ctx->stash('_items', $items); | |
1114 | + $counter = 0; | |
1115 | + } | |
1116 | + else { | |
1117 | + $items = $ctx->stash('_items'); | |
1118 | + $counter = $ctx->stash('_items_counter'); | |
1119 | + } | |
1120 | + if($counter < count($items)) { | |
1121 | + $item = $items[$counter]; | |
1122 | + $ctx->stash('item', $item); | |
1123 | + $ctx->stash('_items_counter', $counter + 1); | |
1124 | + $repeat = true; | |
1125 | + } | |
1126 | + else { | |
1127 | + $ctx->restore($localvars); | |
1128 | + $repeat = false; | |
1129 | + } | |
1130 | + return $content; | |
1131 | + } | |
1132 | + ?> | |
1133 | + | |
1134 | +php_var_tag: | | |
1135 | + <?php | |
1136 | + function smarty_function_[% IF athena %]mttag[% ELSE %]MTTag[% END %]($args, &$ctx) { | |
1137 | + return ''; | |
1138 | + } | |
1139 | + ?> | |
1140 | + | |
1141 | +php_if_tag: | | |
1142 | + <?php | |
1143 | + function smarty_block_[% IF athena %]mtiftag[% ELSE %]MTIfTag[% END %]($args, $content, &$ctx, &$repeat) { | |
1144 | + if(!isset($content)) { | |
1145 | + // set condition here... | |
1146 | + $condition = false; | |
1147 | + return $ctx->_hdlr_if($args, $content, $ctx, $repeat, $condition); | |
1148 | + } | |
1149 | + else { | |
1150 | + return $ctx->_hdlr_if($args, $content, $ctx, $repeat); | |
1151 | + } | |
1152 | + } | |
1153 | + ?> | |
1154 | + |