diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-08-08 16:28:33 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-08-08 16:28:33 +0000 |
commit | e2d6f0af4e49c60f5876aa087873650e1ab9b095 (patch) | |
tree | 0bee0c8195f5496ef95170f48cd9f0a1ec76817d | |
parent | 405bcc10c0861a861d506b3cf15d8d32c870bf96 (diff) | |
download | redmine-e2d6f0af4e49c60f5876aa087873650e1ab9b095.tar.gz redmine-e2d6f0af4e49c60f5876aa087873650e1ab9b095.zip |
Adds an option to macro definition to disable arguments parsing (#11578).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10174 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r-- | app/helpers/application_helper.rb | 3 | ||||
-rw-r--r-- | lib/redmine/wiki_formatting/macros.rb | 38 | ||||
-rw-r--r-- | test/unit/lib/redmine/wiki_formatting/macros_test.rb | 20 |
3 files changed, 49 insertions, 12 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b41faedbc..2245944c8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -860,8 +860,7 @@ module ApplicationHelper # Macros substitution def parse_macros(text, project, obj, attr, only_path, options) text.gsub!(MACROS_RE) do - esc, all, macro = $1, $2, $3.downcase - args = ($5 || '').split(',').each(&:strip) + esc, all, macro, args = $1, $2, $3.downcase, $5.to_s if esc.nil? begin exec_macro(macro, obj, args) diff --git a/lib/redmine/wiki_formatting/macros.rb b/lib/redmine/wiki_formatting/macros.rb index 81e5999f8..7647c7a93 100644 --- a/lib/redmine/wiki_formatting/macros.rb +++ b/lib/redmine/wiki_formatting/macros.rb @@ -20,7 +20,13 @@ module Redmine module Macros module Definitions def exec_macro(name, obj, args) + macro_options = Redmine::WikiFormatting::Macros.available_macros[name.to_sym] + return unless macro_options + method_name = "macro_#{name}" + unless macro_options[:parse_args] == false + args = args.split(',').map(&:strip) + end send(method_name, obj, args) if respond_to?(method_name) end @@ -35,6 +41,7 @@ module Redmine end @@available_macros = {} + mattr_accessor :available_macros class << self # Called with a block to define additional macros. @@ -54,11 +61,28 @@ module Redmine class_eval(&block) if block_given? end - private - # Defines a new macro with the given name and block. - def macro(name, &block) + # Defines a new macro with the given name, options and block. + # + # Options: + # * :parse_args => false - Disables arguments parsing (the whole arguments string + # is passed to the macro) + # + # Examples: + # By default, when the macro is invoked, the coma separated list of arguments + # is parsed and passed to the macro block as an array: + # + # macro :my_macro do |obj, args| + # # args is an array + # end + # + # You can disable arguments parsing with the :parse_args => false option: + # + # macro :my_macro, :parse_args => false do |obj, args| + # # args is a string + # end + def macro(name, options={}, &block) name = name.to_sym if name.is_a?(String) - @@available_macros[name] = @@desc || '' + @@available_macros[name] = {:desc => @@desc || ''}.merge(options) @@desc = nil raise "Can not create a macro without a block!" unless block_given? Definitions.send :define_method, "macro_#{name}".downcase, &block @@ -79,9 +103,9 @@ module Redmine desc "Displays a list of all available macros, including description if available." macro :macro_list do |obj, args| out = ''.html_safe - @@available_macros.keys.collect(&:to_s).sort.each do |macro| - out << content_tag('dt', content_tag('code', macro)) - out << content_tag('dd', textilizable(@@available_macros[macro.to_sym])) + @@available_macros.each do |macro, options| + out << content_tag('dt', content_tag('code', macro.to_s)) + out << content_tag('dd', textilizable(options[:desc])) end content_tag('dl', out) end diff --git a/test/unit/lib/redmine/wiki_formatting/macros_test.rb b/test/unit/lib/redmine/wiki_formatting/macros_test.rb index 4bea9a57d..4583d3e9b 100644 --- a/test/unit/lib/redmine/wiki_formatting/macros_test.rb +++ b/test/unit/lib/redmine/wiki_formatting/macros_test.rb @@ -43,12 +43,26 @@ class Redmine::WikiFormatting::MacrosTest < ActionView::TestCase def test_macro_registration Redmine::WikiFormatting::Macros.register do macro :foo do |obj, args| - "Foo macro output" + "Foo: #{args.size} (#{args.join(',')}) (#{args.class.name})" end end - text = "{{foo}}" - assert_equal '<p>Foo macro output</p>', textilizable(text) + assert_equal '<p>Foo: 0 () (Array)</p>', textilizable("{{foo}}") + assert_equal '<p>Foo: 0 () (Array)</p>', textilizable("{{foo()}}") + assert_equal '<p>Foo: 1 (arg1) (Array)</p>', textilizable("{{foo(arg1)}}") + assert_equal '<p>Foo: 2 (arg1,arg2) (Array)</p>', textilizable("{{foo(arg1, arg2)}}") + end + + def test_macro_registration_parse_args_set_to_false_should_disable_arguments_parsing + Redmine::WikiFormatting::Macros.register do + macro :bar, :parse_args => false do |obj, args| + "Bar: (#{args}) (#{args.class.name})" + end + end + + assert_equal '<p>Bar: (args, more args) (String)</p>', textilizable("{{bar(args, more args)}}") + assert_equal '<p>Bar: () (String)</p>', textilizable("{{bar}}") + assert_equal '<p>Bar: () (String)</p>', textilizable("{{bar()}}") end def test_macro_hello_world |