Sha256: 2dc319c71c9c3f2d5629819b9ad88f17c74b972eeffd80a3c02a96be4d49276c

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Jekyll
  
  class IncludeTag < Liquid::Tag
    Syntax = /from\s+(#{Liquid::VariableSignature}+)/
    
    def initialize(tag_name, markup, tokens)
      super
      if markup =~ Syntax
        @variable = $1
        @use_context = 1
      else
        @file = markup.strip
        @use_context = 0
      end
    end
    
    def render(context)
      if @use_context == 1
        @file = context[@variable]
      end

      if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./
        return "Include file '#{@file}' contains invalid characters or sequences"
      end

      path = File.join(Jekyll.source, '_includes')

      # this doesn't ever seem to work right. seems to be a variable scoping issue
      if Jekyll.site && Jekyll.site.options && Jekyll.site.options['include_path']
         puts "Using options"
         path = Jekyll.site.options['include_path']
      end

      Dir.chdir(path) do
        choices = Dir['**/*'].reject { |x| File.symlink?(x) }
        if choices.include?(@file)
          source = File.read(@file)
          partial = Liquid::Template.parse(source)
          context.stack do
            partial.render(context, [Jekyll::Filters])
          end
        else
          "Included file '#{@file}' not found in _includes directory"
        end
      end
    end
  end
  
end

Liquid::Template.register_tag('include', Jekyll::IncludeTag)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jsjohnst-jekyll-0.4.1.999.3 lib/jekyll/tags/include.rb
jsjohnst-jekyll-0.4.1.999.4 lib/jekyll/tags/include.rb
jsjohnst-jekyll-0.4.1.999.6 lib/jekyll/tags/include.rb