Sha256: a5360b3a9b77872e76751764ec4eb6ad74ef93c6b35fb19ddac238a9c86a0ed2

Contents?: true

Size: 1.79 KB

Versions: 14

Compression:

Stored size: 1.79 KB

Contents

# 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)

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
link-checker-0.7.2 spec/test-site/plugins/include_array.rb
link-checker-0.7.1 spec/test-site/plugins/include_array.rb
link-checker-0.6.0 spec/test-site/plugins/include_array.rb
link-checker-0.5.2 spec/test-site/plugins/include_array.rb
link-checker-0.5.1 spec/test-site/plugins/include_array.rb
link-checker-0.5.0 spec/test-site/plugins/include_array.rb
link-checker-0.4.0 spec/test-site/plugins/include_array.rb
link-checker-0.3.1 spec/test-site/plugins/include_array.rb
link-checker-0.3.0 spec/test-site/plugins/include_array.rb
link-checker-0.2.0 spec/test-site/plugins/include_array.rb
link-checker-0.1.2 spec/test-site/plugins/include_array.rb
link-checker-0.1.1 spec/test-site/plugins/include_array.rb
link-checker-0.1.0 spec/test-site/plugins/include_array.rb
link-checker-0.0.0 spec/test-site/plugins/include_array.rb