diff --git a/features/hooks.feature b/features/hooks.feature index 6562a90a..b0846bab 100644 --- a/features/hooks.feature +++ b/features/hooks.feature @@ -2,11 +2,11 @@ Feature: Hooks As a plugin author I want to be able to run code during various stages of the build process - Scenario: Run some code on site reset + Scenario: Run some code after site reset Given I have a _plugins directory And I have a "_plugins/ext.rb" file with content: """ - Jekyll::Hooks.register :site, :reset do |site| + Jekyll::Hooks.register :site, :after_reset do |site| pageklass = Class.new(Jekyll::Page) do def initialize(site, base) @site = site diff --git a/lib/jekyll/hooks.rb b/lib/jekyll/hooks.rb index b660bd6f..d01cae4c 100644 --- a/lib/jekyll/hooks.rb +++ b/lib/jekyll/hooks.rb @@ -20,7 +20,7 @@ module Jekyll # initial empty hooks @registry = { :site => { - reset: [], + after_reset: [], post_read: [], pre_render: [], post_write: [], @@ -48,23 +48,23 @@ module Jekyll @hook_priority = {} NotAvailable = Class.new(RuntimeError) + Uncallable = Class.new(RuntimeError) - # register hook(s) to be called later - def self.register(owners, event, priority: nil, &block) + # register hook(s) to be called later, public API + def self.register(owners, event, priority: DEFAULT_PRIORITY, &block) Array(owners).each do |owner| - register_one(owner, event, priority: priority_value(priority), &block) + register_one(owner, event, priority_value(priority), &block) end end # Ensure the priority is a Fixnum - def self.priority_value(priority=nil) - return DEFAULT_PRIORITY unless priority + def self.priority_value(priority) return priority if priority.is_a?(Fixnum) PRIORITY_MAP[priority] || DEFAULT_PRIORITY end - # register a single hook to be called later - def self.register_one(owner, event, priority: nil, &block) + # register a single hook to be called later, internal API + def self.register_one(owner, event, priority, &block) unless @registry[owner] raise NotAvailable, "Hooks are only available for the following " << "classes: #{@registry.keys.inspect}" @@ -75,6 +75,10 @@ module Jekyll "following hooks #{@registry[owner].keys.inspect}" end + unless block.respond_to? :call + raise Uncallable, "Hooks must respond to :call" + end + insert_hook owner, event, priority, &block end diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 53ac028b..89b9c28e 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -76,7 +76,7 @@ module Jekyll raise ArgumentError, "limit_posts must be a non-negative number" end - Jekyll::Hooks.trigger self, :reset + Jekyll::Hooks.trigger self, :after_reset end # Load necessary libraries, plugins, converters, and generators. diff --git a/site/_docs/plugins.md b/site/_docs/plugins.md index b000c4dc..fd59e4f8 100644 --- a/site/_docs/plugins.md +++ b/site/_docs/plugins.md @@ -520,7 +520,7 @@ The complete list of available hooks is below:
:site
:reset
:after_reset
Just after site reset