From d8b17814e0d388c208dd5cabcad878093dbd4ec8 Mon Sep 17 00:00:00 2001 From: Jason Woodward Date: Thu, 28 Jul 2011 12:02:08 -0400 Subject: [PATCH] allow select and order of sidebar components to be configurable in jekyll's _config.yml --- .themes/classic/source/_includes/sidebar.html | 5 +- _config.yml | 3 + plugins/include_array.rb | 58 +++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 plugins/include_array.rb diff --git a/.themes/classic/source/_includes/sidebar.html b/.themes/classic/source/_includes/sidebar.html index f2e0734..2e738c7 100644 --- a/.themes/classic/source/_includes/sidebar.html +++ b/.themes/classic/source/_includes/sidebar.html @@ -1,4 +1 @@ -{% include asides/recent_posts.html %} -{% include asides/twitter.html %} -{% include asides/delicious.html %} -{% include asides/pinboard.html %} +{% include_array asides %} diff --git a/_config.yml b/_config.yml index 25783c8..38d5bdb 100644 --- a/_config.yml +++ b/_config.yml @@ -33,6 +33,9 @@ pygments: false paginate: 10 # Posts per page on the blog index recent_posts: 5 # Posts in the sidebar Recent Posts section +# list each of the sidebar modules you want to include, in the order you want, here, adding your own in asides/custom/... if you'd like +asides: [asides/recent_posts.html, asides/twitter.html, asides/delicious.html, asides/pinboard.html] + # ----------------------- # # 3rd Party Settings # # ----------------------- # diff --git a/plugins/include_array.rb b/plugins/include_array.rb new file mode 100644 index 0000000..000040f --- /dev/null +++ b/plugins/include_array.rb @@ -0,0 +1,58 @@ +# Title: Include Array Tag for Jekyll +# Author: Jason Woodward http://www.woodwardjd.com +# Description: Import files on your filesystem as specified in a configuration variable in _config.yml. Mostly cribbed from Jekyll's include tag. +# Syntax: {% include_array variable_name_from_config.yml %} +# +# Example 1: +# {% include_array asides %} +# +# _config.yml snippet: +# asides: [asides/twitter.html, asides/custom/my_picture.html] +# +module Jekyll + + class IncludeArrayTag < Liquid::Tag + Syntax = /(#{Liquid::QuotedFragment}+)/ + def initialize(tag_name, markup, tokens) + if markup =~ Syntax + @array_name = $1 + else + raise SyntaxError.new("Error in tag 'include_array' - Valid syntax: include_array [array from _config.yml]") + end + + super + end + + def render(context) + includes_dir = File.join(context.registers[:site].source, '_includes') + + if File.symlink?(includes_dir) + return "Includes directory '#{includes_dir}' cannot be a symlink" + end + + rtn = '' + (context.environments.first['site'][@array_name] || []).each do |file| + if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./ + rtn = rtn + "Include file '#{file}' contains invalid characters or sequences" + end + + Dir.chdir(includes_dir) do + choices = Dir['**/*'].reject { |x| File.symlink?(x) } + if choices.include?(file) + source = File.read(file) + partial = Liquid::Template.parse(source) + context.stack do + rtn = rtn + partial.render(context) + end + else + rtn = rtn + "Included file '#{file}' not found in _includes directory" + end + end + end + rtn + end + end + +end + +Liquid::Template.register_tag('include_array', Jekyll::IncludeArrayTag)