jekyll-page-hooks 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jekyll-pagehooks.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brandon Mathis
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Jekyll Page Hooks
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.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'jekyll-page-hooks'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jekyll-page-hooks
18
+
19
+ ## Usage
20
+
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.
22
+
23
+ Here's an example.
24
+
25
+ ```ruby
26
+ require 'jeykll-page-hooks'
27
+
28
+ module Jekyll
29
+ class YourFancyPlugin < PageHooks
30
+
31
+ # Manipulate page/post data before it has been processed with Liquid or
32
+ # Converters like Markdown or Textile.
33
+ #
34
+ def pre_render(page)
35
+ page.content = highlight_code(page.content)
36
+ end
37
+
38
+ # Manipulate page/post data after content has been processed to html.
39
+ #
40
+ def post_render(page)
41
+ page.content = link_headings(page.content)
42
+ end
43
+
44
+ # Access page/post data after it has been succesfully written to disk.
45
+ #
46
+ def post_write(page)
47
+ log_something(page.title)
48
+ end
49
+
50
+ end
51
+ end
52
+ ```
53
+
54
+ ### When to use what
55
+
56
+ With `pre_render` you can access page and post data before it has been
57
+ processed by Liquid, Markdown, Textile, etc. You might want to do this if your
58
+ plugin requires text which conflicts with some content convertors. This way
59
+ you can replace that content with the correctly generated HTML before Liquid
60
+ or other convertors sees it.
61
+
62
+ 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.
63
+
64
+ With `post_write` you can execute a code block after a page or post has been
65
+ successfully written to disk. You might use this for logging or triggering
66
+ some external process.
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create new Pull Request
75
+
76
+ ## Lisence
77
+
78
+ Copyright (c) 2013 Brandon Mathis
79
+
80
+ MIT License
81
+
82
+ Permission is hereby granted, free of charge, to any person obtaining
83
+ a copy of this software and associated documentation files (the
84
+ "Software"), to deal in the Software without restriction, including
85
+ without limitation the rights to use, copy, modify, merge, publish,
86
+ distribute, sublicense, and/or sell copies of the Software, and to
87
+ permit persons to whom the Software is furnished to do so, subject to
88
+ the following conditions:
89
+
90
+ The above copyright notice and this permission notice shall be
91
+ included in all copies or substantial portions of the Software.
92
+
93
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
94
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
95
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
96
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
97
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
98
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
99
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
100
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jekyll-page-hooks/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "jekyll-page-hooks"
8
+ gem.version = Jekyll::PageHooksVersion::VERSION
9
+ gem.authors = ["Brandon Mathis"]
10
+ gem.email = ["brandon@imathis.com"]
11
+ gem.description = %q{Monkeypatches Jekyll's Site, Post, Page and Convertible classes to allow other plugins to access page/post content before and after render, and after write.}
12
+ gem.summary = %q{Allows other plugins to access page/post content before and after render, and after write.}
13
+ gem.homepage = "http://github.com/octopress/jekyll-page-hooks"
14
+ gem.license = "MIT"
15
+
16
+ gem.add_dependency 'jekyll', '>= 1.0.0'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module PageHooksVersion
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,174 @@
1
+ module Jekyll
2
+
3
+ # Extended plugin type that allows the plugin
4
+ # to be called on varous callback methods.
5
+ #
6
+ class PageHooks < Plugin
7
+
8
+ #Called before post is sent to the converter. Allows
9
+ #you to modify the post object before the converter
10
+ #does it's thing
11
+ def pre_render(post)
12
+ end
13
+
14
+ #Called after the post is rendered with the converter.
15
+ #Use the post object to modify it's contents before the
16
+ #post is inserted into the template.
17
+ def post_render(post)
18
+ end
19
+
20
+ #Called after the post is written to the disk.
21
+ #Use the post object to read it's contents to do something
22
+ #after the post is safely written.
23
+ def post_write(post)
24
+ end
25
+ end
26
+
27
+ # Monkey patch for the Jekyll Site class. For the original class,
28
+ # see: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/site.rb
29
+ class Site
30
+
31
+ # Instance variable to store the various post_filter
32
+ # plugins that are loaded.
33
+ attr_accessor :post_filters
34
+
35
+ # Instantiates all of the post_filter plugins. This is basically
36
+ # a duplication of the other loaders in Site#setup.
37
+ def load_post_filters
38
+ self.post_filters = Jekyll::PageHooks.subclasses.select do |c|
39
+ !self.safe || c.safe
40
+ end.map do |c|
41
+ c.new(self.config)
42
+ end
43
+ end
44
+ end
45
+
46
+ # Monkey patch for the Jekyll Post class. For the original class,
47
+ # see: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/post.rb
48
+ class Post
49
+
50
+ # Copy the #write method to #old_write, so we can redefine #write
51
+ # method.
52
+ alias_method :old_write, :write
53
+
54
+ # Write the generated post file to the destination directory. It
55
+ # then calls any post_write methods that may exist.
56
+ # +dest+ is the String path to the destination dir
57
+ #
58
+ # Returns nothing
59
+ def write(dest)
60
+ old_write(dest)
61
+ post_write if respond_to?(:post_write)
62
+ end
63
+ end
64
+
65
+ # Monkey patch for the Jekyll Page class. For the original class,
66
+ # see: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/page.rb
67
+ class Page
68
+
69
+ # Copy the #write method to #old_write, so we can redefine #write
70
+ # method.
71
+ alias_method :old_write, :write
72
+
73
+ # Write the generated post file to the destination directory. It
74
+ # then calls any post_write methods that may exist.
75
+ # +dest+ is the String path to the destination dir
76
+ #
77
+ # Returns nothing
78
+ def write(dest)
79
+ old_write(dest)
80
+ post_write if respond_to?(:post_write)
81
+ end
82
+ end
83
+
84
+ # Monkey patch for the Jekyll Convertible module. For the original class,
85
+ # see: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb
86
+ module Convertible
87
+
88
+ def is_post?
89
+ self.class.to_s == 'Jekyll::Post'
90
+ end
91
+
92
+ def is_page?
93
+ self.class.to_s == 'Jekyll::Page'
94
+ end
95
+
96
+ def is_filterable?
97
+ is_post? or is_page?
98
+ end
99
+
100
+ # Call the #pre_render methods on all of the loaded
101
+ # post_filter plugins.
102
+ #
103
+ # Returns nothing
104
+ def pre_render
105
+ self.site.load_post_filters unless self.site.post_filters
106
+
107
+ if self.site.post_filters and is_filterable?
108
+ self.site.post_filters.each do |filter|
109
+ filter.pre_render(self)
110
+ end
111
+ end
112
+ end
113
+
114
+ # Call the #post_render methods on all of the loaded
115
+ # post_filter plugins.
116
+ #
117
+ # Returns nothing
118
+ def post_render
119
+ if self.site.post_filters and is_filterable?
120
+ self.site.post_filters.each do |filter|
121
+ filter.post_render(self)
122
+ end
123
+ end
124
+ end
125
+
126
+ # Call the #post_write methods on all of the loaded
127
+ # post_filter plugins.
128
+ #
129
+ # Returns nothing
130
+ def post_write
131
+ if self.site.post_filters and is_filterable?
132
+ self.site.post_filters.each do |filter|
133
+ filter.post_write(self)
134
+ end
135
+ end
136
+ end
137
+
138
+ # Copy the #transform method to #old_transform, so we can
139
+ # redefine #transform method.
140
+ alias_method :old_transform, :transform
141
+
142
+ # Transform the contents based on the content type. Then calls the
143
+ # #post_render method if it exists
144
+ #
145
+ # Returns nothing.
146
+ def transform
147
+ old_transform
148
+ post_render if respond_to?(:post_render)
149
+ end
150
+
151
+ # Copy the #do_layout method to #old_do_layout, so we can
152
+ # redefine #do_layout method.
153
+ alias_method :old_do_layout, :do_layout
154
+
155
+ # Calls the pre_render method if it exists and then adds any necessary
156
+ # layouts to this convertible document.
157
+ #
158
+ # payload - The site payload Hash.
159
+ # layouts - A Hash of {"name" => "layout"}.
160
+ #
161
+ # Returns nothing.
162
+ def do_layout(payload, layouts)
163
+ pre_render if respond_to?(:pre_render)
164
+ old_do_layout(payload, layouts)
165
+ end
166
+
167
+ # Returns the full url of the post, including the
168
+ # configured url
169
+ def full_url
170
+ self.site.config['url'] + self.url
171
+ end
172
+ end
173
+ end
174
+
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-page-hooks
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brandon Mathis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jekyll
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ description: Monkeypatches Jekyll's Site, Post, Page and Convertible classes to allow
31
+ other plugins to access page/post content before and after render, and after write.
32
+ email:
33
+ - brandon@imathis.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - jekyll-page-hooks.gemspec
44
+ - lib/jekyll-page-hooks.rb
45
+ - lib/jekyll-page-hooks/version.rb
46
+ homepage: http://github.com/octopress/jekyll-page-hooks
47
+ licenses:
48
+ - MIT
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.23
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Allows other plugins to access page/post content before and after render,
71
+ and after write.
72
+ test_files: []