*** lib/redmine/wiki_formatting.rb.orig	2010-08-05 15:31:18.000000000 -0500
--- lib/redmine/wiki_formatting.rb	2010-08-05 15:16:19.000000000 -0500
***************
*** 15,20 ****
--- 15,22 ----
  # along with this program; if not, write to the Free Software
  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  
+ require 'digest/md5'
+ 
  module Redmine
    module WikiFormatting
      @@formatters = {}
***************
*** 48,58 ****
            # Text retrieved from the cache store may be frozen
            # We need to dup it so we can do in-place substitutions with gsub!
            cache_store.fetch cache_key do
!             formatter_for(format).new(text).to_html
            end.dup
          else
!           formatter_for(format).new(text).to_html
          end
          if block_given?
            execute_macros(text, block)
          end
--- 50,67 ----
            # Text retrieved from the cache store may be frozen
            # We need to dup it so we can do in-place substitutions with gsub!
            cache_store.fetch cache_key do
!             formatter_for(format).new(text)
            end.dup
          else
!           formatter_for(format).new(text)
          end
+ 
+ 	@inline_macros_grabbed = {}
+ 
+         preprocess_inline_macros(text)
+ 
+         text = text.to_html
+ 
          if block_given?
            execute_macros(text, block)
          end
***************
*** 76,91 ****
                      (
                      \{\{                        # opening tag
                      ([\w]+)                     # macro name
!                     (\(([^\}]*)\))?             # optional arguments
                      \}\}                        # closing tag
                      )
!                   /x unless const_defined?(:MACROS_RE)
        
        # Macros substitution
        def execute_macros(text, macros_runner)
          text.gsub!(MACROS_RE) do
            esc, all, macro = $1, $2, $3.downcase
            args = ($5 || '').split(',').each(&:strip)
            if esc.nil?
              begin
                macros_runner.call(macro, args)
--- 85,120 ----
                      (
                      \{\{                        # opening tag
                      ([\w]+)                     # macro name
!                     (\((.*?)\))?             # optional arguments
                      \}\}                        # closing tag
                      )
!                   /xm unless const_defined?(:MACROS_RE)
        
        # Macros substitution
+ 
+       # grab raw text for wiki_external_filter macro
+ 
+       def preprocess_inline_macros(text)
+ 	text.gsub!(MACROS_RE) do |s|
+ 	  esc, all, macro = $1, $2, $3.downcase
+ 	  if esc.nil? and (WikiExternalFilterHelper.has_macro macro rescue false)
+ 	    args = $5
+ 	    key = Digest::MD5.hexdigest("#{macro}:#{args}")
+ 	    @inline_macros_grabbed[key] = {:macro => macro, :args => args}
+ 	    "{{_inline_macros_grabbed(#{key})}}"
+ 	  else
+ 	    s
+ 	  end
+ 	end
+       end
+ 
        def execute_macros(text, macros_runner)
          text.gsub!(MACROS_RE) do
            esc, all, macro = $1, $2, $3.downcase
            args = ($5 || '').split(',').each(&:strip)
+           if macro == '_inline_macros_grabbed' and @inline_macros_grabbed.member? args.first
+              macro, args = @inline_macros_grabbed[args.first].values_at(:macro, :args)
+           end
            if esc.nil?
              begin
                macros_runner.call(macro, args)
***************
*** 97,102 ****
--- 126,133 ----
            end
          end
        end
+     ensure
+       @inline_macros_grabbed && @inline_macros_grabbed.clear
      end
      
      # Default formatter module
