Class: Jekyll::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/octopress-hooks.rb

Overview

Monkey patch for the Jekyll Site class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#page_hooksObject

Instance variable to store the various page_hook plugins that are loaded.



83
84
85
# File 'lib/octopress-hooks.rb', line 83

def page_hooks
  @page_hooks
end

#post_hooksObject

Instance variable to store the various page_hook plugins that are loaded.



83
84
85
# File 'lib/octopress-hooks.rb', line 83

def post_hooks
  @post_hooks
end

#site_hooksObject

Instance variable to store the various page_hook plugins that are loaded.



83
84
85
# File 'lib/octopress-hooks.rb', line 83

def site_hooks
  @site_hooks
end

Instance Method Details

#load_hooksObject

Instantiates all of the hook plugins. This is basically a duplication of the other loaders in Site#setup.



87
88
89
90
91
# File 'lib/octopress-hooks.rb', line 87

def load_hooks
  self.site_hooks = instantiate_subclasses(Octopress::Hooks::Site)
  self.page_hooks = instantiate_subclasses(Octopress::Hooks::Page)
  self.post_hooks = instantiate_subclasses(Octopress::Hooks::Post)
end

#old_readObject



97
# File 'lib/octopress-hooks.rb', line 97

alias_method :old_read, :read

#old_renderObject



95
# File 'lib/octopress-hooks.rb', line 95

alias_method :old_render, :render

#old_site_payloadObject



94
# File 'lib/octopress-hooks.rb', line 94

alias_method :old_site_payload, :site_payload

#old_writeObject



96
# File 'lib/octopress-hooks.rb', line 96

alias_method :old_write, :write

#readObject

Load hooks before read to ensure that Post and Page hooks can be triggered during initialization



102
103
104
105
# File 'lib/octopress-hooks.rb', line 102

def read
  self.load_hooks
  old_read
end

#renderObject

Allows site hooks to get access to the site before the render method is called

Returns nothing



111
112
113
114
115
116
117
118
119
# File 'lib/octopress-hooks.rb', line 111

def render
  if self.site_hooks
    self.site_hooks.each do |hook|
      hook.pre_render(self)
    end
  end

  old_render
end

#site_payloadObject

Allows site hooks to merge data into the site payload

Returns the patched site payload



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/octopress-hooks.rb', line 124

def site_payload
  unless @cached_payload
    payload = old_site_payload

    if self.site_hooks
      self.site_hooks.each do |hook|
        p = hook.merge_payload(payload, self) || {}
        if p != {}
          payload = Jekyll::Utils.deep_merge_hashes(payload, p)
        end
      end
    end

    @cached_payload = payload
  end

  @cached_payload
end

#writeObject

Trigger site hooks after site has been written

Returns nothing



146
147
148
149
150
151
152
153
154
# File 'lib/octopress-hooks.rb', line 146

def write
  old_write

  if self.site_hooks
    self.site_hooks.each do |hook|
      hook.post_write(self)
    end
  end
end