Sha256: 7fe1cbab26a8aaeaa376f9e9d44b0784075cbbc81a291dba4ac952b90e44972c

Contents?: true

Size: 1.5 KB

Versions: 39

Compression:

Stored size: 1.5 KB

Contents

# ~*~ encoding: utf-8 ~*~

# Replace specified tokens with dynamically generated content.
class Gollum::Filter::Macro < Gollum::Filter
  def extract(data)
    quoted_arg   = %r{".*?"}
    unquoted_arg = %r{[^,)]+}
    named_arg    = %r{[a-z0-9_]+=".*?"}
    
    arg = %r{(?:#{quoted_arg}|#{unquoted_arg}|#{named_arg})}
    arg_list = %r{(\s*|#{arg}(?:\s*,\s*#{arg})*)}

    data.gsub(/('?)\<\<\s*([A-Z][A-Za-z0-9]*)\s*\(#{arg_list}\)\s*\>\>/) do
      next CGI.escape_html($&[1..-1]) unless Regexp.last_match[1].empty?
      id = Digest::SHA1.hexdigest(Regexp.last_match[2] + Regexp.last_match[3])
      macro = Regexp.last_match[2]
      argstr = Regexp.last_match[3]
      args = []
      opts = {}
      
      argstr.scan(/,?\s*(#{arg})\s*/) do |arguments|
      	# Stabstabstab
      	argument = arguments.first
      	
        if argument =~ /^([a-z0-9_]+)="(.*?)"/
      		opts[Regexp.last_match[1]] = Regexp.last_match[2]
			  elsif argument =~ /^"(.*)"$/
      		args << Regexp.last_match[1]
			  else
				  args << argument
        end
      end
		  
		  args << opts unless opts.empty?
      
      @map[id] = { :macro => macro, :args => args }
      id
    end
  end

  def process(data)
    @map.each do |id, spec|
      macro = spec[:macro]
      args  = spec[:args]

      data.gsub!(id) do
        begin
          Gollum::Macro.instance(macro, @markup.wiki, @markup.page).render(*args)
        rescue StandardError => e
          "!!!Macro Error: #{e.message}!!!"
        end
      end
    end

    data
  end
end

Version data entries

39 entries across 39 versions & 2 rubygems

Version Path
gitlab-gollum-lib-4.2.7.10.gitlab.2 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.10.gitlab.1 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.9 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.8 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.7 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.6 lib/gollum-lib/filter/macro.rb
gollum-lib-4.2.10-java lib/gollum-lib/filter/macro.rb
gollum-lib-4.2.10 lib/gollum-lib/filter/macro.rb
gollum-lib-4.2.9-java lib/gollum-lib/filter/macro.rb
gollum-lib-4.2.9 lib/gollum-lib/filter/macro.rb
gollum-lib-5.0.a.4-java lib/gollum-lib/filter/macro.rb
gollum-lib-5.0.a.4 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.5 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.4 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.3 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.2 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7.1 lib/gollum-lib/filter/macro.rb
gitlab-gollum-lib-4.2.7 lib/gollum-lib/filter/macro.rb
gollum-lib-4.2.7-java lib/gollum-lib/filter/macro.rb
gollum-lib-4.2.7 lib/gollum-lib/filter/macro.rb