| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,176 @@ |
| 0 |
+module Jekyll |
|
| 1 |
+ |
|
| 2 |
+ # Extended plugin type that allows the plugin |
|
| 3 |
+ # to be called on varous callback methods. |
|
| 4 |
+ # |
|
| 5 |
+ # Examples: |
|
| 6 |
+ # https://github.com/tedkulp/octopress/blob/master/plugins/post_metaweblog.rb |
|
| 7 |
+ # https://github.com/tedkulp/octopress/blob/master/plugins/post_twitter.rb |
|
| 8 |
+ class PostFilter < Plugin |
|
| 9 |
+ |
|
| 10 |
+ #Called before post is sent to the converter. Allows |
|
| 11 |
+ #you to modify the post object before the converter |
|
| 12 |
+ #does it's thing |
|
| 13 |
+ def pre_render(post) |
|
| 14 |
+ end |
|
| 15 |
+ |
|
| 16 |
+ #Called after the post is rendered with the converter. |
|
| 17 |
+ #Use the post object to modify it's contents before the |
|
| 18 |
+ #post is inserted into the template. |
|
| 19 |
+ def post_render(post) |
|
| 20 |
+ end |
|
| 21 |
+ |
|
| 22 |
+ #Called after the post is written to the disk. |
|
| 23 |
+ #Use the post object to read it's contents to do something |
|
| 24 |
+ #after the post is safely written. |
|
| 25 |
+ def post_write(post) |
|
| 26 |
+ end |
|
| 27 |
+ end |
|
| 28 |
+ |
|
| 29 |
+ # Monkey patch for the Jekyll Site class. For the original class, |
|
| 30 |
+ # see: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/site.rb |
|
| 31 |
+ class Site |
|
| 32 |
+ |
|
| 33 |
+ # Instance variable to store the various post_filter |
|
| 34 |
+ # plugins that are loaded. |
|
| 35 |
+ attr_accessor :post_filters |
|
| 36 |
+ |
|
| 37 |
+ # Instantiates all of the post_filter plugins. This is basically |
|
| 38 |
+ # a duplication of the other loaders in Site#setup. |
|
| 39 |
+ def load_post_filters |
|
| 40 |
+ self.post_filters = Jekyll::PostFilter.subclasses.select do |c| |
|
| 41 |
+ !self.safe || c.safe |
|
| 42 |
+ end.map do |c| |
|
| 43 |
+ c.new(self.config) |
|
| 44 |
+ end |
|
| 45 |
+ end |
|
| 46 |
+ end |
|
| 47 |
+ |
|
| 48 |
+ # Monkey patch for the Jekyll Post class. For the original class, |
|
| 49 |
+ # see: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/post.rb |
|
| 50 |
+ class Post |
|
| 51 |
+ |
|
| 52 |
+ # Copy the #write method to #old_write, so we can redefine #write |
|
| 53 |
+ # method. |
|
| 54 |
+ alias_method :old_write, :write |
|
| 55 |
+ |
|
| 56 |
+ # Write the generated post file to the destination directory. It |
|
| 57 |
+ # then calls any post_write methods that may exist. |
|
| 58 |
+ # +dest+ is the String path to the destination dir |
|
| 59 |
+ # |
|
| 60 |
+ # Returns nothing |
|
| 61 |
+ def write(dest) |
|
| 62 |
+ old_write(dest) |
|
| 63 |
+ post_write if respond_to?(:post_write) |
|
| 64 |
+ end |
|
| 65 |
+ end |
|
| 66 |
+ |
|
| 67 |
+ # Monkey patch for the Jekyll Page class. For the original class, |
|
| 68 |
+ # see: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/page.rb |
|
| 69 |
+ class Page |
|
| 70 |
+ |
|
| 71 |
+ # Copy the #write method to #old_write, so we can redefine #write |
|
| 72 |
+ # method. |
|
| 73 |
+ alias_method :old_write, :write |
|
| 74 |
+ |
|
| 75 |
+ # Write the generated post file to the destination directory. It |
|
| 76 |
+ # then calls any post_write methods that may exist. |
|
| 77 |
+ # +dest+ is the String path to the destination dir |
|
| 78 |
+ # |
|
| 79 |
+ # Returns nothing |
|
| 80 |
+ def write(dest) |
|
| 81 |
+ old_write(dest) |
|
| 82 |
+ post_write if respond_to?(:post_write) |
|
| 83 |
+ end |
|
| 84 |
+ end |
|
| 85 |
+ |
|
| 86 |
+ # Monkey patch for the Jekyll Convertible module. For the original class, |
|
| 87 |
+ # see: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb |
|
| 88 |
+ module Convertible |
|
| 89 |
+ |
|
| 90 |
+ def is_post? |
|
| 91 |
+ self.class.to_s == 'Jekyll::Post' |
|
| 92 |
+ end |
|
| 93 |
+ |
|
| 94 |
+ def is_page? |
|
| 95 |
+ self.class.to_s == 'Jekyll::Page' |
|
| 96 |
+ end |
|
| 97 |
+ |
|
| 98 |
+ def is_filterable? |
|
| 99 |
+ is_post? or is_page? |
|
| 100 |
+ end |
|
| 101 |
+ |
|
| 102 |
+ # Call the #pre_render methods on all of the loaded |
|
| 103 |
+ # post_filter plugins. |
|
| 104 |
+ # |
|
| 105 |
+ # Returns nothing |
|
| 106 |
+ def pre_render |
|
| 107 |
+ self.site.load_post_filters unless self.site.post_filters |
|
| 108 |
+ |
|
| 109 |
+ if self.site.post_filters and is_filterable? |
|
| 110 |
+ self.site.post_filters.each do |filter| |
|
| 111 |
+ filter.pre_render(self) |
|
| 112 |
+ end |
|
| 113 |
+ end |
|
| 114 |
+ end |
|
| 115 |
+ |
|
| 116 |
+ # Call the #post_render methods on all of the loaded |
|
| 117 |
+ # post_filter plugins. |
|
| 118 |
+ # |
|
| 119 |
+ # Returns nothing |
|
| 120 |
+ def post_render |
|
| 121 |
+ if self.site.post_filters and is_filterable? |
|
| 122 |
+ self.site.post_filters.each do |filter| |
|
| 123 |
+ filter.post_render(self) |
|
| 124 |
+ end |
|
| 125 |
+ end |
|
| 126 |
+ end |
|
| 127 |
+ |
|
| 128 |
+ # Call the #post_write methods on all of the loaded |
|
| 129 |
+ # post_filter plugins. |
|
| 130 |
+ # |
|
| 131 |
+ # Returns nothing |
|
| 132 |
+ def post_write |
|
| 133 |
+ if self.site.post_filters and is_filterable? |
|
| 134 |
+ self.site.post_filters.each do |filter| |
|
| 135 |
+ filter.post_write(self) |
|
| 136 |
+ end |
|
| 137 |
+ end |
|
| 138 |
+ end |
|
| 139 |
+ |
|
| 140 |
+ # Copy the #transform method to #old_transform, so we can |
|
| 141 |
+ # redefine #transform method. |
|
| 142 |
+ alias_method :old_transform, :transform |
|
| 143 |
+ |
|
| 144 |
+ # Transform the contents based on the content type. Then calls the |
|
| 145 |
+ # #post_render method if it exists |
|
| 146 |
+ # |
|
| 147 |
+ # Returns nothing. |
|
| 148 |
+ def transform |
|
| 149 |
+ old_transform |
|
| 150 |
+ post_render if respond_to?(:post_render) |
|
| 151 |
+ end |
|
| 152 |
+ |
|
| 153 |
+ # Copy the #do_layout method to #old_do_layout, so we can |
|
| 154 |
+ # redefine #do_layout method. |
|
| 155 |
+ alias_method :old_do_layout, :do_layout |
|
| 156 |
+ |
|
| 157 |
+ # Calls the pre_render method if it exists and then adds any necessary |
|
| 158 |
+ # layouts to this convertible document. |
|
| 159 |
+ # |
|
| 160 |
+ # payload - The site payload Hash. |
|
| 161 |
+ # layouts - A Hash of {"name" => "layout"}.
|
|
| 162 |
+ # |
|
| 163 |
+ # Returns nothing. |
|
| 164 |
+ def do_layout(payload, layouts) |
|
| 165 |
+ pre_render if respond_to?(:pre_render) |
|
| 166 |
+ old_do_layout(payload, layouts) |
|
| 167 |
+ end |
|
| 168 |
+ |
|
| 169 |
+ # Returns the full url of the post, including the |
|
| 170 |
+ # configured url |
|
| 171 |
+ def full_url |
|
| 172 |
+ self.site.config['url'] + self.url |
|
| 173 |
+ end |
|
| 174 |
+ end |
|
| 175 |
+end |