octopress-hooks 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +72 -52
- data/lib/octopress-hooks.rb +100 -99
- data/lib/octopress-hooks/version.rb +1 -1
- data/test/_expected/2014/05/21/hi-guys.html +1 -0
- data/test/_plugins/test.rb +7 -1
- data/test/_posts/2014-05-21-hi-guys.md +3 -0
- data/test/pre_render_page.md +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3169a133f7c3a17900e54db52932fb84a202ec66
|
4
|
+
data.tar.gz: 104ff2a28bcce7a7bea13e51acd7b3585025bbfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
3
|
+
Modify Jekyll's Site, Pages and Posts at different points during the site processing stream.
|
4
|
+
|
5
|
+
[](https://travis-ci.org/octopress/hooks)
|
6
|
+
[](https://rubygems.org/gems/octopress-hooks)
|
7
|
+
[](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
|
25
|
+
First extend the appropriate Hook class.
|
22
26
|
|
23
|
-
|
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
|
-
|
26
|
-
require 'octopress-hooks'
|
31
|
+
Then add a method based on when you want to trigger your hooks.
|
27
32
|
|
28
|
-
|
33
|
+
#### Site Hooks
|
29
34
|
|
30
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
```
|
38
|
+
class MySiteHook < Octopress::Hooks::Site
|
39
|
+
|
40
|
+
def pre_render(site)
|
41
41
|
end
|
42
42
|
|
43
|
-
|
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
|
-
|
51
|
+
Use the `pre_render` hook to modify the site instance before posts and pages are rendered.
|
52
52
|
|
53
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
86
|
+
With `post_write` you can execute a code block after a page or post has been successfully written to disk.
|
79
87
|
|
80
|
-
|
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
|
-
|
90
|
+
```ruby
|
91
|
+
module MyModule
|
92
|
+
def self.do_awesome(document)
|
93
|
+
# something awesome
|
94
|
+
end
|
87
95
|
|
88
|
-
|
89
|
-
|
90
|
-
|
96
|
+
MyPostHook < Octopress::Hooks::Post
|
97
|
+
def pre_render(post)
|
98
|
+
do_awesome(post)
|
99
|
+
end
|
100
|
+
end
|
91
101
|
|
92
|
-
|
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
|
-
|
95
|
-
You could use this to modify these objects or even add to them.
|
110
|
+
### Hook timeline
|
96
111
|
|
97
|
-
|
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
|
-
|
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
|
|
data/lib/octopress-hooks.rb
CHANGED
@@ -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
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
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
|
-
#
|
158
|
+
# Monkey patch Jekyll's Page class
|
141
159
|
#
|
142
|
-
class
|
143
|
-
|
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
|
165
|
-
|
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
|
169
|
-
self.
|
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
|
-
|
178
|
-
|
179
|
-
|
173
|
+
# Monkey patch Jekyll's Post class
|
174
|
+
#
|
175
|
+
class Post
|
176
|
+
alias_method :old_initialize, :initialize
|
180
177
|
|
181
|
-
|
182
|
-
|
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
|
-
|
194
|
-
|
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']
|
256
|
+
File.join(self.site.config['url'], self.url)
|
256
257
|
end
|
257
258
|
end
|
258
259
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<p>Hi guys awesome</p>
|
data/test/_plugins/test.rb
CHANGED
@@ -23,7 +23,13 @@ module TestingHooks
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
class
|
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
|
|
data/test/pre_render_page.md
CHANGED
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.
|
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-
|
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
|