Module: Redmine::WikiFormatting::Macros::Definitions

Included in:
ApplicationHelper
Defined in:
lib/redmine/wiki_formatting/macros.rb

Instance Method Summary collapse

Instance Method Details

#exec_macro(name, obj, args, text, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/redmine/wiki_formatting/macros.rb', line 29

def exec_macro(name, obj, args, text, options={})
  macro_options = Redmine::WikiFormatting::Macros.available_macros[name.to_sym]
  return unless macro_options

  if options[:inline_attachments] == false
    Redmine::WikiFormatting::Macros.inline_attachments = false
  else
    Redmine::WikiFormatting::Macros.inline_attachments = true
  end

  method_name = "macro_#{name}"
  unless macro_options[:parse_args] == false
    # Split the arguments by commas, but only if the commas
    # are not within double quotes
    args = args.split(/\s*,\s*(?=(?:[^"]*"[^"]*")*[^"]*$)/)
               .map {|i| i.gsub(/^"(.*)"$/, '\1').gsub('""', '"')}
  end

  begin
    if self.class.instance_method(method_name).arity == 3
      send(method_name, obj, args, text)
    elsif text
      raise t(:error_macro_does_not_accept_block)
    else
      send(method_name, obj, args)
    end
  rescue => e
    %|<div class="flash error">#{::I18n.t(:error_can_not_execute_macro_html, :name => name, :error => e.to_s)}</div>|.html_safe
  end
end

#extract_macro_options(args, *keys) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/redmine/wiki_formatting/macros.rb', line 60

def extract_macro_options(args, *keys)
  options = {}
  while args.last.to_s.strip =~ %r{^(.+?)\=(.+)$} && keys.include?($1.downcase.to_sym)
    options[$1.downcase.to_sym] = $2.gsub(/^"(.*)"$/, '\1')
    args.pop
  end
  return [args, options]
end

#macro_exists?(name) ⇒ Boolean

Returns true if name is the name of an existing macro

Returns:

  • (Boolean)


25
26
27
# File 'lib/redmine/wiki_formatting/macros.rb', line 25

def macro_exists?(name)
  Redmine::WikiFormatting::Macros.available_macros.key?(name.to_sym)
end