Class: Gollum::Filter::Macro

Inherits:
Gollum::Filter show all
Defined in:
lib/gollum-lib/filter/macro.rb

Overview

Replace specified tokens with dynamically generated content.

Instance Method Summary collapse

Methods inherited from Gollum::Filter

#initialize

Methods included from Helpers

#trim_leading_slash

Constructor Details

This class inherits a constructor from Gollum::Filter

Instance Method Details

#extract(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gollum-lib/filter/macro.rb', line 5

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
    id = Digest::SHA1.hexdigest($1 + $2)
    macro = $1
    argstr = $2
    args = []
    opts = {}
    
    argstr.scan /,?\s*(#{arg})\s*/ do |arg|
    	# Stabstabstab
    	arg = arg.first
    	
    	if arg =~ /^([a-z0-9_]+)="(.*?)"/
    		opts[$1] = $2
	elsif arg =~ /^"(.*)"$/
    		args << $1
	else
		args << arg
	end
end

args << opts unless opts.empty?
    
    @map[id] = { :macro => macro, :args => args }
    id
  end
end

#process(data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gollum-lib/filter/macro.rb', line 40

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