Compare commits

...

1 Commits

Author SHA1 Message Date
Parker Moore
2db51e6464 Beginning work on class-based hooks. 2014-06-23 19:35:29 -04:00
3 changed files with 65 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ require 'toml'
# internal requires
require 'jekyll/version'
require 'jekyll/utils'
require 'jekyll/hooks'
require 'jekyll/log_adapter'
require 'jekyll/stevenson'
require 'jekyll/deprecator'

62
lib/jekyll/hooks.rb Normal file
View File

@@ -0,0 +1,62 @@
module Jekyll
module Hooks
class HookCollection
include Enumerable
def each(&block)
if block.nil?
hook_methods
else
hook_methods.each &block
end
end
def hook_methods
@hook_methods ||= Array.new
end
def add_hook(proc)
hook_methods << proc
end
def exec(*args)
if args.empty?
hook_methods.each { |hook| hook.call }
else
hook_methods.each do |hook|
args.each { |arg| hook.call(arg) }
end
end
end
end
%w[
post_reset
pre_read
post_read
pre_generate
post_generate
pre_render
post_render
pre_cleanup
post_cleanup
pre_write
post_write
].each do |method|
declaration = <<-METH
def #{method}(method_name, *args)
cr = caller
((@hooks ||= Hash.new).fetch(method_name.to_s, HookCollection.new)).add_hook -> { cr.send(method_name.to_sym, args) }
end
def #{method}_exec(*args)
((@hooks ||= Hash.new).fetch(method_name.to_s, HookCollection.new)).exec(*args)
end
METH
puts declaration
class_eval declaration
end
end
end

View File

@@ -8,6 +8,8 @@ module Jekyll
attr_accessor :converters, :generators
include Jekyll::Hooks
# Public: Initialize a new Site.
#
# config - A Hash containing site configuration details.