自作プラグイン第2弾
昨日のと似たようなので、設定画面からプラグインが書ける。ラベルの書き換えや、他のプラグインの動作の一部修正、自作プラグインを FTP 無しにレンタルサーバーで運用中の tDiary に適用することなどを想定している。(最近 FTP に不自由してるものですから (;^_^A
ソースは以下の通り。コピーして misc/plugin/ ディレクトリに「~chotto_plugin.rb」として EUC で保存するとよい。ファイル名の頭の ~ は他のプラグインの後に読み込まれるように付けてある。もちろん、その後で設定画面からこのプラグインを使用可能な状態にすることを忘れてはいけない。
# ~chotto_plugin.rb
#
# 設定画面からプラグインを定義/上書きできる。
# ラベルの書き換え、プラグインの動作の一部修正など、
# ちょっとしたことに使う。
if(@conf['~chotto_plugin.script'])
begin
instance_eval(@conf['~chotto_plugin.script']);
rescue Exception
end
end
add_conf_proc('~chotto_plugin', 'プラグインの微調整') {
if(@mode == 'saveconf')
if(@cgi.params['~chotto_plugin.script'].empty? || @cgi.params['~chotto_plugin.script'][0].empty?)
@conf.delete('~chotto_plugin.script');
else
@conf['~chotto_plugin.script'] = @cgi.params['~chotto_plugin.script'][0];
end
end
<<-HTML
<h3>ちょっとしたプラグインスクリプト</h3>
<p>入力したスクリプトが読み込み時に実行されます。普通のプラグインと同じ内容のものを書きます。他のプラグインのちょっとした修正や、自作プラグインの試用に使って下さい。</p>
<p><textarea name="~chotto_plugin.script" cols="70" rows="15">#{CGI::escapeHTML(@conf['~chotto_plugin.script'].to_s)}</textarea></p>
HTML
}