Class: Jekyll::IncludeTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/tags/include.rb

Constant Summary collapse

Syntax =
/from\s+(#{Liquid::VariableSignature}+)/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ IncludeTag

Returns a new instance of IncludeTag.



6
7
8
9
10
11
12
13
14
15
# File 'lib/jekyll/tags/include.rb', line 6

def initialize(tag_name, markup, tokens)
  super
  if markup =~ Syntax
    @variable = $1
    @use_context = 1
  else
    @file = markup.strip
    @use_context = 0
  end
end

Instance Method Details

#render(context) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jekyll/tags/include.rb', line 17

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

  if Jekyll.site && Jekyll.site.options
     path = Jekyll.site.options['include_path']
  else
     path = File.join(Jekyll.source, '_includes')
  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