forked from jekyll/jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.feature
360 lines (340 loc) · 12.6 KB
/
hooks.feature
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
Feature: Hooks
As a plugin author
I want to be able to run code during various stages of the build process
Scenario: Run some code after site reset
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :site, :after_reset do |site|
pageklass = Class.new(Jekyll::Page) do
def initialize(site, base)
@site = site
@base = base
@data = {}
@dir = '/'
@name = 'foo.html'
@content = 'mytinypage'
self.process(@name)
end
end
site.pages << pageklass.new(site, site.source)
end
"""
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "mytinypage" in "_site/foo.html"
Scenario: Modify the payload before rendering the site
Given I have a _plugins directory
And I have a "index.html" page that contains "{{ site.injected }}!"
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :site, :pre_render do |site, payload|
payload['site']['injected'] = 'myparam'
end
"""
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "myparam!" in "_site/index.html"
Scenario: Modify the site contents after reading
Given I have a _plugins directory
And I have a "page1.html" page that contains "page1"
And I have a "page2.html" page that contains "page2"
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :site, :post_read do |site|
site.pages.delete_if { |p| p.name == 'page1.html' }
end
"""
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And the "_site/page1.html" file should not exist
And I should see "page2" in "_site/page2.html"
Scenario: Work with the site files after they've been written to disk
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :site, :post_write do |site|
firstpage = site.pages.first
content = File.read firstpage.destination(site.dest)
File.write(File.join(site.dest, 'firstpage.html'), content)
end
"""
And I have a "page1.html" page that contains "page1"
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "page1" in "_site/firstpage.html"
Scenario: Alter a page right after it is initialized
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :pages, :post_init do |page|
page.name = 'renamed.html'
page.process(page.name)
end
"""
And I have a "page1.html" page that contains "page1"
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "page1" in "_site/renamed.html"
Scenario: Alter the payload for one page but not another
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :pages, :pre_render do |page, payload|
payload['page']['myparam'] = 'special' if page.name == 'page1.html'
end
"""
And I have a "page1.html" page that contains "{{ page.myparam }}"
And I have a "page2.html" page that contains "{{ page.myparam }}"
When I run jekyll build
Then I should see "special" in "_site/page1.html"
And I should not see "special" in "_site/page2.html"
Scenario: Modify page contents before writing to disk
Given I have a _plugins directory
And I have a "index.html" page that contains "WRAP ME"
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :pages, :post_render do |page|
page.output = "{{{{{ #{page.output.chomp} }}}}}"
end
"""
When I run jekyll build
Then I should see "{{{{{ WRAP ME }}}}}" in "_site/index.html"
Scenario: Work with a page after writing it to disk
Given I have a _plugins directory
And I have a "index.html" page that contains "HELLO FROM A PAGE"
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :pages, :post_write do |page|
require 'fileutils'
filename = page.destination(page.site.dest)
FileUtils.mv(filename, "#{filename}.moved")
end
"""
When I run jekyll build
Then I should see "HELLO FROM A PAGE" in "_site/index.html.moved"
Scenario: Alter a post right after it is initialized
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :posts, :post_init do |post|
post.data['harold'] = "content for entry1.".tr!('abcdefghijklmnopqrstuvwxyz',
'nopqrstuvwxyzabcdefghijklm')
end
"""
And I have a _posts directory
And I have the following posts:
| title | date | layout | content |
| entry1 | 2015-03-14 | nil | {{ page.harold }} |
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "pbagrag sbe ragel1." in "_site/2015/03/14/entry1.html"
Scenario: Alter the payload for certain posts
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
# Add myvar = 'old' to posts before 2015-03-15, and myvar = 'new' for
# others
Jekyll::Hooks.register :posts, :pre_render do |post, payload|
if post.date < Time.new(2015, 3, 15)
payload['myvar'] = 'old'
else
payload['myvar'] = 'new'
end
end
"""
And I have a _posts directory
And I have the following posts:
| title | date | layout | content |
| entry1 | 2015-03-14 | nil | {{ myvar }} post |
| entry2 | 2015-03-15 | nil | {{ myvar }} post |
When I run jekyll build
Then I should see "old post" in "_site/2015/03/14/entry1.html"
And I should see "new post" in "_site/2015/03/15/entry2.html"
Scenario: Modify post contents before writing to disk
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
# Replace content after rendering
Jekyll::Hooks.register :posts, :post_render do |post|
post.output.gsub! /42/, 'the answer to life, the universe and everything'
end
"""
And I have a _posts directory
And I have the following posts:
| title | date | layout | content |
| entry1 | 2015-03-14 | nil | {{ 6 \| times: 7 }} |
| entry2 | 2015-03-15 | nil | {{ 6 \| times: 8 }} |
When I run jekyll build
Then I should see "the answer to life, the universe and everything" in "_site/2015/03/14/entry1.html"
And I should see "48" in "_site/2015/03/15/entry2.html"
Scenario: Work with a post after writing it to disk
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
# Log all post filesystem writes
Jekyll::Hooks.register :posts, :post_write do |post|
filename = post.destination(post.site.dest)
open('_site/post-build.log', 'a') do |f|
f.puts "Wrote #{filename} at #{Time.now}"
end
end
"""
And I have a _posts directory
And I have the following posts:
| title | date | layout | content |
| entry1 | 2015-03-14 | nil | entry one |
| entry2 | 2015-03-15 | nil | entry two |
When I run jekyll build
Then I should see "_site/2015/03/14/entry1.html at" in "_site/post-build.log"
Then I should see "_site/2015/03/15/entry2.html at" in "_site/post-build.log"
Scenario: Register a hook on multiple owners at the same time
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register [:pages, :posts], :post_render do |owner|
owner.output = "{{{{{ #{owner.output.chomp} }}}}}"
end
"""
And I have a "index.html" page that contains "WRAP ME"
And I have a _posts directory
And I have the following posts:
| title | date | layout | content |
| entry1 | 2015-03-14 | nil | entry one |
When I run jekyll build
Then I should see "{{{{{ WRAP ME }}}}}" in "_site/index.html"
And I should see "{{{{{ <p>entry one</p> }}}}}" in "_site/2015/03/14/entry1.html"
Scenario: Allow hooks to have a named priority
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :pages, :post_render, priority: :normal do |owner|
# first normal runs second
owner.output = "1 #{owner.output.chomp}"
end
Jekyll::Hooks.register :pages, :post_render, priority: :high do |owner|
# high runs first
owner.output = "2 #{owner.output.chomp}"
end
Jekyll::Hooks.register :pages, :post_render do |owner|
# second normal runs third (normal is default)
owner.output = "3 #{owner.output.chomp}"
end
Jekyll::Hooks.register :pages, :post_render, priority: :low do |owner|
# low runs last
owner.output = "4 #{owner.output.chomp}"
end
"""
And I have a "index.html" page that contains "WRAP ME"
When I run jekyll build
Then I should see "4 3 1 2 WRAP ME" in "_site/index.html"
Scenario: Alter a document right after it is initialized
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :documents, :pre_render do |doc, payload|
doc.data['text'] = doc.data['text'] << ' are belong to us'
end
"""
And I have a "_config.yml" file that contains "collections: [ memes ]"
And I have a _memes directory
And I have a "_memes/doc1.md" file with content:
"""
---
text: all your base
---
"""
And I have an "index.md" file with content:
"""
---
---
{{ site.memes.first.text }}
"""
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "all your base are belong to us" in "_site/index.html"
Scenario: Update a document after rendering it, but before writing it to disk
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :documents, :post_render do |doc|
doc.output.gsub! /<p>/, '<p class="meme">'
end
"""
And I have a "_config.yml" file with content:
"""
collections:
memes:
output: true
"""
And I have a _memes directory
And I have a "_memes/doc1.md" file with content:
"""
---
text: all your base are belong to us
---
{{ page.text }}
"""
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "<p class=\"meme\">all your base are belong to us" in "_site/memes/doc1.html"
Scenario: Perform an action after every document is written
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :documents, :post_write do |doc|
open('_site/document-build.log', 'a') do |f|
f.puts "Wrote document #{doc.collection.docs.index doc} at #{Time.now}"
end
end
"""
And I have a "_config.yml" file with content:
"""
collections:
memes:
output: true
"""
And I have a _memes directory
And I have a "_memes/doc1.md" file with content:
"""
---
text: all your base are belong to us
---
{{ page.text }}
"""
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "Wrote document 0" in "_site/document-build.log"
Scenario: Set a custom payload['page'] property
Given I have a _plugins directory
And I have a "_plugins/ext.rb" file with content:
"""
Jekyll::Hooks.register :pages, :pre_render do |page, payload|
payload['page']['foo'] = "hello world"
end
"""
And I have a _layouts directory
And I have a "_layouts/custom.html" file with content:
"""
---
---
{{ content }} {% include foo.html %}
"""
And I have a _includes directory
And I have a "_includes/foo.html" file with content:
"""
{{page.foo}}
"""
And I have an "index.html" page with layout "custom" that contains "page content"
When I run jekyll build
Then the "_site/index.html" file should exist
And I should see "page content\n hello world" in "_site/index.html"