From c444b3f23201fbd2e51c7517b3a22adcc079ba12 Mon Sep 17 00:00:00 2001 From: yterajima Date: Wed, 6 May 2015 16:16:30 +0900 Subject: [PATCH] fixed 'after_render' hook is not work. - 'before_render' hook is called twice. - add simple cucumber test about some hooks. --- .../features/extension_hooks.feature | 13 +++++++ .../fixtures/extension-hooks-app/config.rb | 39 +++++++++++++++++++ .../extension-hooks-app/source/index.html.erb | 9 +++++ .../lib/middleman-core/file_renderer.rb | 2 +- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 middleman-core/features/extension_hooks.feature create mode 100644 middleman-core/fixtures/extension-hooks-app/config.rb create mode 100644 middleman-core/fixtures/extension-hooks-app/source/index.html.erb diff --git a/middleman-core/features/extension_hooks.feature b/middleman-core/features/extension_hooks.feature new file mode 100644 index 00000000..1dedd9a9 --- /dev/null +++ b/middleman-core/features/extension_hooks.feature @@ -0,0 +1,13 @@ +Feature: Extension author could use some hooks + + Scenario: When build + Given a fixture app "extension-hooks-app" + When I run `middleman build` + Then the exit status should be 0 + And the output should contain "/// after_configuration ///" + And the output should contain "/// ready ///" + And the output should contain "/// before_build ///" + And the output should contain "/// before ///" + And the output should contain "/// before_render ///" + And the output should contain "/// after_render ///" + And the output should contain "/// after_build ///" diff --git a/middleman-core/fixtures/extension-hooks-app/config.rb b/middleman-core/fixtures/extension-hooks-app/config.rb new file mode 100644 index 00000000..dfedb14f --- /dev/null +++ b/middleman-core/fixtures/extension-hooks-app/config.rb @@ -0,0 +1,39 @@ +set :layout, false + +class MyFeature < Middleman::Extension + def initialize(app, options_hash = {}, &block) + super + + app.before do + puts '/// before ///' + end + + app.ready do + puts '/// ready ///' + end + + app.before_render do |body, path, locs, template_class| + puts "/// before_render ///" + end + + app.after_render do |content, path, locs, template_class| + puts "/// after_render ///" + end + + app.before_build do |builder| + puts "/// before_build ///" + end + + app.after_build do |builder| + puts "/// after_build ///" + end + end + + def after_configuration + puts '/// after_configuration ///' + end +end + +::Middleman::Extensions.register(:my_feature, MyFeature) + +activate :my_feature diff --git a/middleman-core/fixtures/extension-hooks-app/source/index.html.erb b/middleman-core/fixtures/extension-hooks-app/source/index.html.erb new file mode 100644 index 00000000..fed8030e --- /dev/null +++ b/middleman-core/fixtures/extension-hooks-app/source/index.html.erb @@ -0,0 +1,9 @@ + + + + extension-hooks-app + + +

extension-hooks-app

+ + diff --git a/middleman-core/lib/middleman-core/file_renderer.rb b/middleman-core/lib/middleman-core/file_renderer.rb index eb98e657..f9087004 100644 --- a/middleman-core/lib/middleman-core/file_renderer.rb +++ b/middleman-core/lib/middleman-core/file_renderer.rb @@ -76,7 +76,7 @@ module Middleman end # Allow hooks to manipulate the result after render - content = @app.callbacks_for(:before_render).reduce(content) do |sum, callback| + content = @app.callbacks_for(:after_render).reduce(content) do |sum, callback| callback.call(sum, path, locs, template_class) || sum end