octopress-hooks 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8146d664fb709b511c92e4ae185dcc904896b395
4
- data.tar.gz: 0842bc67f1fafbf5f853c9bc88a5a79ae91f6312
3
+ metadata.gz: 3169a133f7c3a17900e54db52932fb84a202ec66
4
+ data.tar.gz: 104ff2a28bcce7a7bea13e51acd7b3585025bbfe
5
5
  SHA512:
6
- metadata.gz: df5cdfe5412c3a0e089b3d2d30f332283a8554477c0a0be790cb2dbc88466fd2e44963b398d66bd5e77b07689b79afb2d704cb4d3f31fcbdd192a02aad58d80b
7
- data.tar.gz: 34191d047548eca5d6a081be03b59e9351450ba59b53872ceae5a08d7a45f0ed43fca2a47c883e895140d1e6942616e61c1344fb7908a474ecac57b1b336f15f
6
+ metadata.gz: 9985494e92eb14430704b568b60beb58fd14e5043779f76eb0ed87bac8a23abc1da593128b30514b50c7091b7aea3e0565aacb186d38371d78e58d2da1ae8884
7
+ data.tar.gz: e4cb99f4706e85ac8d39b0fb8da9bfb006b21c7afaf286a7e38de1bb8ff9d5c267ede3b3e1c6eeeb984fdfcc26a19a06926cd159ec6834d93111966bd76731be
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ### 2.1.0
4
+ - New: Added Post hooks.
5
+ - Change: Page hooks now only target pages.
6
+ - New: Added post_init hooks for pages and posts which is triggered after initialization.
7
+ - Gone: Removed the ConvertiblePartial thing. It was unnecessary. Extending hooks is easy.
8
+
3
9
  ### 2.0.0
4
10
  - Added support for Site hooks: pre_render, post_write and a way to patch the site payload.
5
11
  - Changed name to octopress-hooks and moved repository to octopress/hooks.
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Octopress Hooks
2
2
 
3
- This plugin isn't useful on its own. It monkeypatches Jekyll's Site, Post, Page and Convertible classes to allow plugin authors to access page and post data before and after render, and after write.
3
+ Modify Jekyll's Site, Pages and Posts at different points during the site processing stream.
4
+
5
+ [![Build Status](https://travis-ci.org/octopress/hooks.svg)](https://travis-ci.org/octopress/hooks)
6
+ [![Gem Version](http://img.shields.io/gem/v/octopress-hooks.svg)](https://rubygems.org/gems/octopress-hooks)
7
+ [![License](http://img.shields.io/:license-mit-blue.svg)](http://octopress.mit-license.org)
4
8
 
5
9
  ## Installation
6
10
 
@@ -18,86 +22,102 @@ Or install it yourself as:
18
22
 
19
23
  ## Usage
20
24
 
21
- First require this plugin at the top of a plugin file, inside of Jekyll's plugin directory. Then if your plugin class inherits the PageHooks class, the methods, `pre_render`, `post_render`, `post_write` will execute automatically in turn.
25
+ First extend the appropriate Hook class.
22
26
 
23
- Here's an example.
27
+ - `Octopress::Hooks::Site` - access Jekyll's Site instance.
28
+ - `Octopress::Hooks::Page` - access to each of Jekyll's Page instances.
29
+ - `Octopress::Hooks::Post` - access to each of Jekyll's Post instances.
24
30
 
25
- ```ruby
26
- require 'octopress-hooks'
31
+ Then add a method based on when you want to trigger your hooks.
27
32
 
28
- class YourPageHooks < Octopress::Hooks::Page
33
+ #### Site Hooks
29
34
 
30
- # Manipulate page/post data before it has been processed with Liquid or
31
- # Converters like Markdown or Textile.
32
- #
33
- def pre_render(page)
34
- page.content = highlight_code(page.content)
35
- end
35
+ The Site class has three methods. Here's an example.
36
36
 
37
- # Manipulate page/post data after content has been processed to html.
38
- #
39
- def post_render(page)
40
- page.content = link_headings(page.content)
37
+ ```
38
+ class MySiteHook < Octopress::Hooks::Site
39
+
40
+ def pre_render(site)
41
41
  end
42
42
 
43
- # Access page/post data after it has been succesfully written to disk.
44
- #
45
- def post_write(page)
46
- log_something(page.title)
43
+ def merge_payload(payload, site)
47
44
  end
48
45
 
46
+ def post_write(site)
47
+ end
49
48
  end
49
+ ```
50
50
 
51
- class YourSiteHooks < Octopress::Hooks::Site
51
+ Use the `pre_render` hook to modify the site instance before posts and pages are rendered.
52
52
 
53
- # Get access to the site before the render process
54
- #
55
- def pre_render(site)
56
- # do something interesting
57
- end
53
+ Use the `merge_paylod` hook to modify the site payload or merge custom data into it. This data will be available to all documents when they are rendered. This method must return a hash.
58
54
 
59
- # Return a hash to be merged into the site payload
60
- #
61
- def merge_payload(payload, site)
62
- { 'awesome' => true }
55
+ Use the `post_write` to trigger and action after all documents have been written to disk.
56
+
57
+ #### Post/Page hooks
58
+
59
+ The Page and Post hooks have four methods and are identical except that Post hooks only operate on posts, and Page hooks only operate on
60
+ pages. Here's an example of a Page hook.
61
+
62
+ ```
63
+ class MySiteHook < Octopress::Hooks::Page
64
+
65
+ def post_init(page)
66
+ end
67
+
68
+ def pre_render(page)
63
69
  end
64
70
 
65
- # Trigger some action after the site has been written
66
- #
67
- def post_write(site)
68
- # do something interesting
71
+ def post_render(page)
69
72
  end
70
73
 
74
+ def post_write(page)
75
+ end
71
76
  end
72
77
  ```
73
78
 
74
- For a more complete example, check out [test.rb](test/_plugins/test.rb).
79
+ The `post_init` method lets you access the post or page class instance immediately after it has been initialized. This allows you to
80
+ modify the instance before the Site compiles its payload, which includes arrays of each page and post.
81
+
82
+ With `pre_render` you can parse and modify page contents before it is processed by Liquid, Markdown, Textile and the like, and rendered to HTML.
75
83
 
76
- ### When to use what
84
+ With `post_render` you can access pages and posts after it has been converted into HTML. You might use this option if you want to modify generated HTML.
77
85
 
78
- #### For posts/pages
86
+ With `post_write` you can execute a code block after a page or post has been successfully written to disk.
79
87
 
80
- With `pre_render` you can access page and post data before it has been
81
- processed by Liquid, Markdown, Textile, etc. You might want to do this if your
82
- plugin requires text which conflicts with some content convertors. This way
83
- you can replace that content with the correctly generated HTML before Liquid
84
- or other convertors sees it.
88
+ To run an action on both posts and pages, you'd do something like this.
85
89
 
86
- With `post_render` you can access pages and posts after it has been proccessed into HTML. You might use this option if you want to modify generated HTML, for example adding anchors for each heading element.
90
+ ```ruby
91
+ module MyModule
92
+ def self.do_awesome(document)
93
+ # something awesome
94
+ end
87
95
 
88
- With `post_write` you can execute a code block after a page or post has been
89
- successfully written to disk. You might use this for logging or triggering
90
- some external process.
96
+ MyPostHook < Octopress::Hooks::Post
97
+ def pre_render(post)
98
+ do_awesome(post)
99
+ end
100
+ end
91
101
 
92
- #### For site
102
+ MyPostHook < Octopress::Hooks::Page
103
+ def pre_render(post)
104
+ MyModule.do_awesome(post)
105
+ end
106
+ end
107
+ end
108
+ ```
93
109
 
94
- Use the `pre_render` hook to get access to the site class and modify it as necessary before posts and pages are rendered.
95
- You could use this to modify these objects or even add to them.
110
+ ### Hook timeline
96
111
 
97
- Use the `merge_paylod` hook to add data that all documents will have access to when they are rendered, or modify the contents
98
- of the site payload. Be sure to return a hash that can be merged.
112
+ Just to be clear, this is the order in which these hooks are triggered.
99
113
 
100
- Use the `post_write` to trigger and action after all documents have been written. With this you could gzip assets, or trigger a shell command.
114
+ 1. Post/Page `post_init`
115
+ 2. Site `pre_render`
116
+ 3. Site `merge_payload`
117
+ 4. Post/Page `pre_render`
118
+ 5. Post/Page `post_render`
119
+ 6. Post/Page `post_write`
120
+ 7. Site `post_write`
101
121
 
102
122
  ## Contributing
103
123
 
@@ -3,12 +3,38 @@ require 'jekyll'
3
3
 
4
4
  module Octopress
5
5
  module Hooks
6
+
7
+ class Site < Jekyll::Plugin
8
+
9
+ # Called before Jekyll renders posts and pages
10
+ # Returns nothing
11
+ #
12
+ def pre_render(site)
13
+ end
14
+
15
+ # Merges hash into site_payload
16
+ # Returns hash to be merged
17
+ #
18
+ def merge_payload(payload, site)
19
+ payload
20
+ end
21
+
22
+ # Called after Jekyll writes site files
23
+ # Returns nothing
24
+ #
25
+ def post_write(site)
26
+ end
27
+ end
6
28
 
7
- # Extended plugin type that allows the plugin
8
- # to be called on varous callback methods.
9
- #
10
29
  class Page < Jekyll::Plugin
11
30
 
31
+ # Called after Page is initialized
32
+ # allows you to modify a # page object before it is
33
+ # added to the Jekyll pages array
34
+ #
35
+ def post_init(post)
36
+ end
37
+
12
38
  # Called before post is sent to the converter. Allows
13
39
  # you to modify the post object before the converter
14
40
  # does it's thing
@@ -31,28 +57,13 @@ module Octopress
31
57
  end
32
58
  end
33
59
 
34
- class Site < Jekyll::Plugin
35
-
36
- # Called before Jekyll renders posts and pages
37
- # Returns nothing
38
- #
39
- def pre_render(site)
40
- end
41
-
42
- # Merges hash into site_payload
43
- # Returns hash to be merged
44
- #
45
- def merge_payload(payload, site)
46
- payload
47
- end
48
-
49
- # Called after Jekyll writes site files
50
- # Returns nothing
51
- #
52
- def post_write(site)
53
- end
54
-
60
+ class Post < Jekyll::Plugin
61
+ def post_init(post); end
62
+ def pre_render(post); end
63
+ def post_render(post); end
64
+ def post_write(post); end
55
65
  end
66
+
56
67
  end
57
68
  end
58
69
 
@@ -62,34 +73,42 @@ module Jekyll
62
73
 
63
74
  # For compatibilty with old jekyll-page-hooks gem
64
75
  #
65
- class PageHooks < Octopress::Hooks::Page; end
76
+ PageHooks = Class.new(Octopress::Hooks::Page)
66
77
 
67
78
  # Monkey patch for the Jekyll Site class.
68
79
  class Site
69
80
 
70
81
  # Instance variable to store the various page_hook
71
82
  # plugins that are loaded.
72
- attr_accessor :page_hooks, :site_hooks
83
+ attr_accessor :page_hooks, :post_hooks, :site_hooks
73
84
 
74
85
  # Instantiates all of the hook plugins. This is basically
75
86
  # a duplication of the other loaders in Site#setup.
76
87
  def load_hooks
77
88
  self.site_hooks = instantiate_subclasses(Octopress::Hooks::Site)
78
89
  self.page_hooks = instantiate_subclasses(Octopress::Hooks::Page)
90
+ self.post_hooks = instantiate_subclasses(Octopress::Hooks::Post)
79
91
  end
80
92
 
81
93
 
82
94
  alias_method :old_site_payload, :site_payload
83
95
  alias_method :old_render, :render
84
96
  alias_method :old_write, :write
97
+ alias_method :old_read, :read
98
+
99
+ # Load hooks before read to ensure that Post and Page hooks
100
+ # can be triggered during initialization
101
+ #
102
+ def read
103
+ self.load_hooks
104
+ old_read
105
+ end
85
106
 
86
107
  # Allows site hooks to get access to the site before
87
108
  # the render method is called
88
109
  #
89
110
  # Returns nothing
90
111
  def render
91
- self.load_hooks
92
-
93
112
  if self.site_hooks
94
113
  self.site_hooks.each do |hook|
95
114
  hook.pre_render(self)
@@ -133,87 +152,41 @@ module Jekyll
133
152
  end
134
153
  end
135
154
  end
136
-
137
155
  end
138
156
 
139
157
 
140
- # Create a new page class to allow partials to trigger Jekyll Page Hooks.
158
+ # Monkey patch Jekyll's Page class
141
159
  #
142
- class ConvertiblePartial
143
- include Convertible
144
-
145
- attr_accessor :name, :content, :site, :ext, :output, :data
146
-
147
- def initialize(site, name, content)
148
- @site = site
149
- @name = name
150
- @ext = File.extname(name)
151
- @content = content
152
- @data = { layout: "no_layout" } # hack
153
-
154
- end
155
-
156
- def render(payload)
157
- do_layout(payload, { no_layout: nil })
158
- end
159
- end
160
-
161
- # Monkey patch for the Jekyll Convertible module.
162
- module Convertible
160
+ class Page
161
+ alias_method :old_initialize, :initialize
163
162
 
164
- def is_post?
165
- self.is_a? Jekyll::Post
163
+ def initialize(*args)
164
+ old_initialize(*args)
165
+ post_init if respond_to?(:post_init) && self.hooks
166
166
  end
167
167
 
168
- def is_page?
169
- self.is_a? Jekyll::Page ||
170
- self.class.to_s == 'Octopress::Ink::Page'
171
- end
172
-
173
- def is_convertible_partial?
174
- self.is_a? Jekyll::ConvertiblePartial
168
+ def hooks
169
+ self.site.page_hooks
175
170
  end
171
+ end
176
172
 
177
- def is_filterable?
178
- is_post? or is_page? or is_convertible_partial?
179
- end
173
+ # Monkey patch Jekyll's Post class
174
+ #
175
+ class Post
176
+ alias_method :old_initialize, :initialize
180
177
 
181
- # Call the #pre_render methods on all of the loaded
182
- # page_hook plugins.
183
- #
184
- # Returns nothing
185
- def pre_render
186
- if self.site.page_hooks and is_filterable?
187
- self.site.page_hooks.each do |filter|
188
- filter.pre_render(self)
189
- end
190
- end
178
+ def initialize(*args)
179
+ old_initialize(*args)
180
+ post_init if respond_to?(:post_init) && self.hooks
191
181
  end
192
182
 
193
- # Call the #post_render methods on all of the loaded
194
- # page_hook plugins.
195
- #
196
- # Returns nothing
197
- def post_render
198
- if self.site.page_hooks and is_filterable?
199
- self.site.page_hooks.each do |filter|
200
- filter.post_render(self)
201
- end
202
- end
203
- end
204
-
205
- # Call the #post_write methods on all of the loaded
206
- # page_hook plugins.
207
- #
208
- # Returns nothing
209
- def post_write
210
- if self.site.page_hooks and is_filterable?
211
- self.site.page_hooks.each do |filter|
212
- filter.post_write(self)
213
- end
214
- end
183
+ def hooks
184
+ self.site.post_hooks
215
185
  end
186
+ end
216
187
 
188
+ # Monkey patch for the Jekyll Convertible module.
189
+ module Convertible
217
190
  alias_method :old_transform, :transform
218
191
  alias_method :old_do_layout, :do_layout
219
192
  alias_method :old_write, :write
@@ -224,7 +197,7 @@ module Jekyll
224
197
  # Returns nothing.
225
198
  def transform
226
199
  old_transform
227
- post_render if respond_to?(:post_render)
200
+ post_render if respond_to?(:post_render) && self.hooks
228
201
  end
229
202
 
230
203
  # Calls the pre_render method if it exists and then adds any necessary
@@ -235,7 +208,7 @@ module Jekyll
235
208
  #
236
209
  # Returns nothing.
237
210
  def do_layout(payload, layouts)
238
- pre_render if respond_to?(:pre_render)
211
+ pre_render if respond_to?(:pre_render) && self.hooks
239
212
  old_do_layout(payload, layouts)
240
213
  end
241
214
 
@@ -246,13 +219,41 @@ module Jekyll
246
219
  # Returns nothing
247
220
  def write(dest)
248
221
  old_write(dest)
249
- post_write if respond_to?(:post_write)
222
+ post_write if respond_to?(:post_write) && self.hooks
223
+ end
224
+
225
+ def hooks
226
+ []
227
+ end
228
+
229
+ def post_init
230
+ self.hooks.each do |hook|
231
+ hook.post_init(self)
232
+ end
233
+ end
234
+
235
+ def pre_render
236
+ self.hooks.each do |hook|
237
+ hook.pre_render(self)
238
+ end
239
+ end
240
+
241
+ def post_render
242
+ self.hooks.each do |hook|
243
+ hook.post_render(self)
244
+ end
245
+ end
246
+
247
+ def post_write
248
+ self.hooks.each do |hook|
249
+ hook.post_write(self)
250
+ end
250
251
  end
251
252
 
252
253
  # Returns the full url of the post, including the configured url
253
254
  #
254
255
  def full_url
255
- self.site.config['url'] + self.url
256
+ File.join(self.site.config['url'], self.url)
256
257
  end
257
258
  end
258
259
  end
@@ -1,5 +1,5 @@
1
1
  module Octopress
2
2
  module Hooks
3
- VERSION = "2.0.0"
3
+ VERSION = "2.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1 @@
1
+ <p>Hi guys awesome</p>
@@ -23,7 +23,13 @@ module TestingHooks
23
23
  end
24
24
  end
25
25
 
26
- class PageHooksTest < Jekyll::PageHooks
26
+ class PostHooksTest < Octopress::Hooks::Post
27
+ def post_init(post)
28
+ post.data['injected_data'] = 'awesome'
29
+ end
30
+ end
31
+
32
+ class PageHooksTest < Octopress::Hooks::Page
27
33
 
28
34
  # Inherited methods from PageHooks
29
35
 
@@ -0,0 +1,3 @@
1
+ ---
2
+ ---
3
+ Hi guys {{ page.injected_data }}
@@ -1,3 +1,3 @@
1
1
  ---
2
2
  ---
3
- Snatch this *cupcake* from my hand.
3
+ Snatch this *cupcake* from my hand.{{ page.injected_data }}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octopress-hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Mathis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-25 00:00:00.000000000 Z
11
+ date: 2014-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -60,6 +60,7 @@ files:
60
60
  - test/.gitignore
61
61
  - test/Gemfile
62
62
  - test/_config.yml
63
+ - test/_expected/2014/05/21/hi-guys.html
63
64
  - test/_expected/boom
64
65
  - test/_expected/magic
65
66
  - test/_expected/merge_payload.html
@@ -67,6 +68,7 @@ files:
67
68
  - test/_expected/post_write_page.html
68
69
  - test/_expected/pre_render_page.html
69
70
  - test/_plugins/test.rb
71
+ - test/_posts/2014-05-21-hi-guys.md
70
72
  - test/merge_payload.md
71
73
  - test/post_render_page.md
72
74
  - test/post_write_page.md