[Hiki-dev:00827] Re: ブロックプラグインとインラインプラグインについて

Back to archive index

Kenta MURATA murak****@compl*****
2005年 6月 20日 (月) 17:18:46 JST


むらけんです.

Kazuhiko wrote:
> def hoge(*args, &proc)
>   case yield(proc)
>   when true
>     # inline plugin
>   else
>     # block plugin
>   end
> end
> 
> みたいなことができますが、もっといいやりかたがあるのかなぁ。

こういうとき,キーワード引数が欲しくなりますよね.

という冗談はさておき,プラグインオブジェクトに状態を持たせるのが
手っ取り早い気がします.動作確認はしてないですが,以下のような
patch が分かりやすくて良いのではないでしょうか.プラグインの中で

  if block_context? then
    ブロックプラグイン
  elsif inline_context? then
    インラインプラグイン
  else
    その他
  end

という条件分岐が可能となるはずです.

# バグってたらごめんなさい.

Index: hiki/plugin.rb
===================================================================
RCS file: /cvsroot/hiki/hiki/hiki/plugin.rb,v
retrieving revision 1.22
diff -u -r1.22 plugin.rb
--- hiki/plugin.rb      17 Jun 2005 05:06:00 -0000      1.22
+++ hiki/plugin.rb      20 Jun 2005 08:14:33 -0000
@@ -40,6 +40,7 @@
       end

       @toc_f            = false
+      @context          = nil
       @plugin_command   = []
       @plugin_menu      = []
       @text             = ''
@@ -62,6 +63,22 @@
       end
     end

+    def block_context?
+      @context == :block
+    end
+
+    def inline_context?
+      @context == :inline
+    end
+
+    def block_context
+      in_context(:block)
+    end
+
+    def inline_context
+      in_context(:inline)
+    end
+
     def cookie_path
       ret = File::dirname( @options['cgi'].script_name )
       ret += '/' unless %r|/+$| =~ ret
@@ -295,6 +312,15 @@

     private

+    def in_context(context)
+      begin
+        @context = context
+        yield
+      ensure
+        @context = nil
+      end
+    end
+
     def export_plugin_methods(*names)
       @export_method_list = names.collect do |name|
         name = name.intern if name.is_a?(String)
Index: style/default/html_formatter.rb
===================================================================
RCS file: /cvsroot/hiki/hiki/style/default/html_formatter.rb,v
retrieving revision 1.31
diff -u -r1.31 html_formatter.rb
--- style/default/html_formatter.rb     16 Jun 2005 01:22:18 -0000      1.31
+++ style/default/html_formatter.rb     20 Jun 2005 08:14:33 -0000
@@ -258,7 +258,12 @@
     def call_plugin_method( t )
       return nil unles****@conf*****_plugin
       str = t[:method].gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/,
'&gt;')
-      return apply_plugin( str, @plugin, @conf )
+      case t[:e]
+      when :inline_plugin
+        @plugin.inline_context{ return apply_plugin( str, @plugin,
@conf ) }
+      else
+        @plugin.block_context{ return apply_plugin( str, @plugin, @conf ) }
+      end
     end

     def make_link(t, s)

--
Kenta MURATA



Hiki-dev メーリングリストの案内
Back to archive index