Class: Moft::Tags::IncludeTag

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

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, file, tokens) ⇒ IncludeTag

Returns a new instance of IncludeTag.



4
5
6
7
# File 'lib/moft/tags/include.rb', line 4

def initialize(tag_name, file, tokens)
  super
  @file = file.strip
end

Instance Method Details

#render(context) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/moft/tags/include.rb', line 9

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

  if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./
    return "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
        partial.render(context)
      end
    else
      "Included file '#{@file}' not found in _includes directory"
    end
  end
end