lib/jekyll/assets/hook.rb in jekyll-assets-2.4.0 vs lib/jekyll/assets/hook.rb in jekyll-assets-3.0.0

- old
+ new

@@ -1,78 +1,145 @@ -# ---------------------------------------------------------------------------- # Frozen-string-literal: true -# Copyright: 2012 - 2016 - MIT License +# Copyright: 2012 - 2017 - MIT License # Encoding: utf-8 -# ---------------------------------------------------------------------------- module Jekyll module Assets class Hook class UnknownHookError < RuntimeError - def initialize(base: nil, point: nil) - super "Unknown hook #{base ? "base" : "point"} '#{base || point}'" + def initialize(point) + super "Unknown hook point `#{point}'" end end - # ---------------------------------------------------------------------- + # -- + class << self + attr_reader :points + end - HOOK_ALIASES = { - :env => { - :post_init => :init, - :pre_init => :init - } - } + # -- + @points = { + env: { + before_init: { + 1 => [], + 2 => [], + 3 => [], + }, - # ---------------------------------------------------------------------- + after_init: { + 1 => [], + 2 => [], + 3 => [], + }, + }, - HOOK_POINTS = { - :env => [ - :init - ] + config: { + before_merge: { + 1 => [], + 2 => [], + 3 => [], + }, + }, + + asset: { + before_compile: { + 1 => [], + 2 => [], + 3 => [], + }, + }, + + liquid: { + pre_render: { + 1 => [], + 2 => [], + 3 => [], + }, + }, } - # ---------------------------------------------------------------------- + # -- + # Create a hook point to attach hooks to. + # @param [Array<String,Symbol>] point the parent and child. + # @note plugins can create their own points if wished. + # @return [Hash<Hash<Array>>] + # -- + def self.add_point(*point) + raise ArgumentError, "only give 2 points" if point.count > 2 - def self.all - @_all ||= {} + @points[point[0]] ||= {} + @points[point[0]][point[1]] ||= { + # + } + + 1.upto(3).each do |i| + @points[point[0]][point[1]][i] ||= [ + # + ] + end + + @points end - # ---------------------------------------------------------------------- - # Trigger a hook, giving an optional block where we pass you the, - # proc we got and then you can do as you please (such as instance eval) - # but if you do not give us one then we simply pass the args. - # ---------------------------------------------------------------------- + # -- + # @return [Array<Proc>] + # @param [Array<String,Symbol>] point the parent and child. + # @note this is really internal. + # Get a hook point. + # -- + def self.get_point(*point) + check_point(*point) - def self.trigger(base, point_, *args, &block) - raise ArgumentError, "Do not give args with a block" if !args.empty? && block_given? - if all.key?(base) && all[base].key?(point_) - Set.new.merge(point(base, point_, :early)).merge(point(base, point_)).map do |v| - block_given?? block.call(v) : v.call(*args) + @points[point[0]][point[1]] + .each_with_object([]) do |(_, v), a| + a.concat(v) end + end + + # -- + # Trigger a hook point. + # @note plugins can trigger their own hooks. + # @param [Array<String,Symbol>] point the parent and child. + # @param [Proc{}] block the code to run. + # @see self.add_point + # @return [nil] + # -- + def self.trigger(*point, &block) + get_point(*point).map do |v| + block.call(v) end end - # ---------------------------------------------------------------------- + # -- + # Register a hook on a hook point. + # @param [Array<String,Symbol>] point the parent and child. + # @param [Integer] priority your priority. + # @note this is what plugins should use. + # @return [nil] + # -- + def self.register(*point, priority: 2, &block) + if priority > 3 + raise ArgumentError, + "priority must be between 1 and 3" + end - def self.point(base, point, when_ = :late) - point = all[base][point] ||= { - :early => Set.new, - :late => Set.new - } - - point[when_] + check_point(*point) + out = @points[point[0]] + out = out[point[1]] + out = out[priority] + out << block end - # ---------------------------------------------------------------------- - - def self.register(base, point, when_ = :late, &block) - raise UnknownHookError, base: base unless HOOK_POINTS.key?(base) - point = HOOK_ALIASES[base][point] if HOOK_ALIASES.fetch(base, {}).key?(point) - raise UnknownHookError, point: point unless HOOK_POINTS[base].include?(point) - all[base] ||= {} - - point(base, point, when_) \ - .add(block) + # -- + # @param point the points to check. + # Checks that a point exists or raises an error. + # @return [nil] + # -- + def self.check_point(*point) + raise ArgumentError, "only give 2 points" if point.count > 2 + if !@points.key?(point[0]) || !@points[point[0]].key?(point[1]) + raise ArgumentError, "Unknown hook #{point}" + end end end end end