bjornjohansen

Jekyll default layout

Mar 28th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.96 KB | None | 0 0
  1. # Created by Nick Markwell, based on code by Philip Durbin from:
  2. # http://stackoverflow.com/questions/8490528/how-can-i-make-jekyll-use-a-layout-without-specifying-it
  3. #
  4. # Small modification by Bjørn Johansen (@bjornjohansen) to workaround when there is no front matter at all
  5. #
  6. # This provides the ability to specify the following in _config.yml:
  7. #    layouts:
  8. #      default: DEFAULT_LAYOUT_NAME     # everything that doesn't have another default specified
  9. #      _posts: DEFAULT_POST_LAYOUT_NAME # all posts
  10. #      magic: DEFAULT_MAGICAL_LAYOUT    # everything in magic/
  11. #
  12. # To use the plugin, just drop this file in _plugins.
  13. #
  14. # Repository: https://github.com/duckinator/jekyll-default-layout
  15. #
  16.  
  17. module Jekyll
  18.   module Convertible
  19.     # TODO: Find where this is already stored so it's less hacktastic
  20.     def __get_config_yml
  21.       @__config  ||= YAML.load(File.open(File.join(File.dirname(__FILE__), '..', '_config.yml')))
  22.       @__layouts ||= @__config['layouts']
  23.     end
  24.    
  25.     def read_yaml(base, name)
  26.       __get_config_yml
  27.       dir = base.split('/')[-1] # TODO: Remove hackiness :(
  28.      
  29.      
  30.       self.content = File.read(File.join(base, name))
  31.      
  32.       if self.content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
  33.         self.content = $POSTMATCH
  34.        
  35.         begin
  36.           self.data = YAML.load($1)
  37.          
  38.           if @__layouts.keys.include?(dir)
  39.             self.data['layout'] ||= @__layouts[dir]
  40.           elsif @__layouts.keys.include?('default')
  41.             self.data['layout'] ||= @__layouts['default']
  42.           end
  43.         rescue => e
  44.           puts "YAML Exception reading #{name}: #{e.message}"
  45.         end
  46.       else
  47.         self.data = {}
  48.         if @__layouts.keys.include?(dir)
  49.           self.data['layout'] ||= @__layouts[dir]
  50.         elsif @__layouts.keys.include?('default')
  51.           self.data['layout'] ||= @__layouts['default']
  52.         end
  53.       end
  54.      
  55.       self.data ||= {}
  56.     end
  57.   end
  58. end
Advertisement
Add Comment
Please, Sign In to add comment