Browse code

allow select and order of sidebar components to be configurable in jekyll's _config.yml

Jason Woodward authored on 28/07/2011 at 16:02:08
Showing 3 changed files
... ...
@@ -1,4 +1 @@
1
-{% include asides/recent_posts.html %}
2
-{% include asides/twitter.html %}
3
-{% include asides/delicious.html %}
4
-{% include asides/pinboard.html %}
1
+{% include_array asides %}
... ...
@@ -33,6 +33,9 @@ pygments: false
33 33
 paginate: 10 # Posts per page on the blog index
34 34
 recent_posts: 5 # Posts in the sidebar Recent Posts section
35 35
 
36
+# 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
37
+asides: [asides/recent_posts.html, asides/twitter.html, asides/delicious.html, asides/pinboard.html]
38
+
36 39
 # ----------------------- #
37 40
 #   3rd Party Settings    #
38 41
 # ----------------------- #
39 42
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+# Title: Include Array Tag for Jekyll
1
+# Author: Jason Woodward http://www.woodwardjd.com
2
+# Description: Import files on your filesystem as specified in a configuration variable in _config.yml.  Mostly cribbed from Jekyll's include tag.
3
+# Syntax: {% include_array variable_name_from_config.yml %}
4
+#
5
+# Example 1:
6
+# {% include_array asides  %}
7
+#
8
+# _config.yml snippet:
9
+# asides: [asides/twitter.html, asides/custom/my_picture.html]
10
+#
11
+module Jekyll
12
+
13
+  class IncludeArrayTag < Liquid::Tag
14
+    Syntax = /(#{Liquid::QuotedFragment}+)/
15
+    def initialize(tag_name, markup, tokens)
16
+      if markup =~ Syntax
17
+        @array_name = $1
18
+      else
19
+        raise SyntaxError.new("Error in tag 'include_array' - Valid syntax: include_array [array from _config.yml]")
20
+      end
21
+
22
+      super
23
+    end
24
+
25
+    def render(context)
26
+      includes_dir = File.join(context.registers[:site].source, '_includes')
27
+
28
+      if File.symlink?(includes_dir)
29
+        return "Includes directory '#{includes_dir}' cannot be a symlink"
30
+      end
31
+
32
+      rtn = ''
33
+      (context.environments.first['site'][@array_name] || []).each do |file|
34
+        if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./
35
+          rtn = rtn + "Include file '#{file}' contains invalid characters or sequences"
36
+        end
37
+
38
+        Dir.chdir(includes_dir) do
39
+          choices = Dir['**/*'].reject { |x| File.symlink?(x) }
40
+          if choices.include?(file)
41
+            source = File.read(file)
42
+            partial = Liquid::Template.parse(source)
43
+            context.stack do
44
+              rtn = rtn + partial.render(context)
45
+            end
46
+          else
47
+            rtn = rtn + "Included file '#{file}' not found in _includes directory"
48
+          end
49
+        end
50
+      end
51
+      rtn
52
+    end
53
+  end
54
+
55
+end
56
+
57
+Liquid::Template.register_tag('include_array', Jekyll::IncludeArrayTag)