OK, so I was ready to give Jekyll another shot today. But one thing I’m not that happy with is the need for specifying the YAML front matter on every post. At this point, I’m not really using it for any practical matter. So I went looking for ways to achieve the following:
- Defining a default template in _config.yml All my posts are, at least for now, using the same template. I want to be able to skip defining this in every post, but be able to use it if I want to override the default.
- Using the first headline for page title As it seems, Jekyll doesn’t require you to define a title in the front matter. If you skip it, it will just generate the title from the filename. Not an ideal solution, but OK by me for now.
[bjornad]
Default template
I didn’t have to search long to find others that wanted the same as I did. Luckily, Nick Markwell and Philip Durbin already made a plugin that I just needed to drop into the _plugins folder.
This actually solved issue #1, but when combined with issue #2 it would barf when the front matter was empty. I tested this with just making up some foobar keys, which made it work again. Also, when I removed the front matter block altogether, it would seemingly execute just fine, but the default template wouldn’t work.
*sigh*
So, with my not-so-extensive knowledge of Ruby, I dived into the plugin code – which was very short and easy to understand. So I tried a small modification, and to my delight, it works!
I am fully aware of how bad this code is (any coder should see that from following the logic), but it does the job I need it to and I don’t know enough Ruby to fix it. BTW: It still barfs on an empty front matter block.
# Created by Nick Markwell, based on code by Philip Durbin from:
# http://stackoverflow.com/questions/8490528/how-can-i-make-jekyll-use-a-layout-without-specifying-it
#
# Small modification by Bjørn Johansen (@bjornjohansen) to workaround when there is no front matter at all
#
# This provides the ability to specify the following in _config.yml:
# layouts:
# default: DEFAULT_LAYOUT_NAME # everything that doesn't have another default specified
# _posts: DEFAULT_POST_LAYOUT_NAME # all posts
# magic: DEFAULT_MAGICAL_LAYOUT # everything in magic/
#
# To use the plugin, just drop this file in _plugins.
#
# Repository: https://github.com/duckinator/jekyll-default-layout
#
module Jekyll
module Convertible
# TODO: Find where this is already stored so it's less hacktastic
def __get_config_yml
@__config ||= YAML.load(File.open(File.join(File.dirname(__FILE__), '..', '_config.yml')))
@__layouts ||= @__config['layouts']
end
def read_yaml(base, name)
__get_config_yml
dir = base.split('/')[-1] # TODO: Remove hackiness :(
self.content = File.read(File.join(base, name))
if self.content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.content = $POSTMATCH
begin
self.data = YAML.load($1)
if @__layouts.keys.include?(dir)
self.data['layout'] ||= @__layouts[dir]
elsif @__layouts.keys.include?('default')
self.data['layout'] ||= @__layouts['default']
end
rescue => e
puts "YAML Exception reading #{name}: #{e.message}"
end
else
self.data = {}
if @__layouts.keys.include?(dir)
self.data['layout'] ||= @__layouts[dir]
elsif @__layouts.keys.include?('default')
self.data['layout'] ||= @__layouts['default']
end
end
self.data ||= {}
end
end
end