diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9738f29..03e3467 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1407,6 +1407,9 @@ module ApplicationHelper def filter_html(html, source) if @plugins html = convert_macro(html, source) + #TODO This parse should be done through the macro infra, but since there + # are old things that do not support it we are keeping this hot spot. + html = @plugins.pipeline(:parse_content, html, source).first end html end diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index 3b51b14..84bbc4f 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -252,8 +252,8 @@ class Noosfero::Plugin # -> Parse and possibly make changes of content (article, block, etc) during HTML rendering # returns = content as string after parser and changes - def parse_content(args) - args + def parse_content(html, source) + [html, source] end # -> Adds links to the admin panel diff --git a/lib/noosfero/plugin/manager.rb b/lib/noosfero/plugin/manager.rb index f82f5d7..d13fab8 100644 --- a/lib/noosfero/plugin/manager.rb +++ b/lib/noosfero/plugin/manager.rb @@ -53,6 +53,17 @@ class Noosfero::Plugin::Manager result end + + def pipeline(event, *args) + each do |plugin| + result = plugin.send(event, *args) + result = result.kind_of?(Array) ? result : [result] + raise ArgumentError, "Pipeline broken by #{plugin.class.name} on #{event} with #{result.length} arguments instead of #{args.length}." if result.length != args.length + args = result + end + args.length < 2 ? args.first : args + end + def parse_macro(macro_name, macro, source = nil) macro_instance = enabled_macros[macro_name] || default_macro macro_instance.convert(macro, source) diff --git a/test/unit/plugin_manager_test.rb b/test/unit/plugin_manager_test.rb index 47e9678..88fc0d5 100644 --- a/test/unit/plugin_manager_test.rb +++ b/test/unit/plugin_manager_test.rb @@ -194,4 +194,67 @@ class PluginManagerTest < ActiveSupport::TestCase assert_equal 'My name is Macro2!', manager.parse_macro(Plugin1::Macro2.identifier, macro) end + should 'dispatch event in a pipeline sequence' do + class Plugin1 < Noosfero::Plugin + def transform(v1, v2) + v = 2 + [v1 * v, v2 * v] + end + end + + class Plugin2 < Noosfero::Plugin + def transform(v1, v2) + v = 5 + [v1 * v, v2 * v] + end + end + + environment.enable_plugin(Plugin1) + environment.enable_plugin(Plugin2) + + assert_equal [10, 20], manager.pipeline(:transform, 1, 2) + end + + should 'be able to pipeline with single arguments' do + class Plugin1 < Noosfero::Plugin + def transform(value) + value * 2 + end + end + + class Plugin2 < Noosfero::Plugin + def transform(value) + value * 5 + end + end + + environment.enable_plugin(Plugin1) + environment.enable_plugin(Plugin2) + + assert_equal 10, manager.pipeline(:transform, 1) + end + + should 'raise if pipeline is broken' do + class Plugin1 < Noosfero::Plugin + def transform(v1, v2) + v = 2 + [v1 * v, v2 * v] + end + end + + class Plugin2 < Noosfero::Plugin + def transform(v1, v2) + v = 5 + [v1 * v, v2 * v, 666] + end + end + + environment.enable_plugin(Plugin1) + environment.enable_plugin(Plugin2) + + assert_raise ArgumentError do + manager.pipeline(:transform, 1, 2) + end + end + end -- libgit2 0.21.2