aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorBrandon Mathis <brandon@imathis.com>2011-07-30 12:05:34 -0400
committerBrandon Mathis <brandon@imathis.com>2011-07-30 12:05:34 -0400
commit141800087f728389ab3cd1ae266b2273a078073f (patch)
tree75eccaeda334db97a3d7ecdeca2e4dc2d0bcdb1f /plugins
parentcb6442e6249099ce708c0d26af84738eaef240a2 (diff)
parentffc730fc0d6d30f44ff8197343246e3b60293661 (diff)
downloadmy_new_personal_website-141800087f728389ab3cd1ae266b2273a078073f.tar.xz
my_new_personal_website-141800087f728389ab3cd1ae266b2273a078073f.zip
Merge branch 'woodwardjd-master'
Diffstat (limited to 'plugins')
-rw-r--r--plugins/include_array.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/plugins/include_array.rb b/plugins/include_array.rb
new file mode 100644
index 00000000..000040fb
--- /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)