refactor Include tag file validation

Split validation into a separate method, and give a more
descriptive error on symlinks.
This commit is contained in:
maul.esel 2013-07-23 14:23:33 +02:00
parent 86e007f970
commit ba5db5ad5b
1 changed files with 22 additions and 13 deletions

View File

@ -51,6 +51,21 @@ eos
def render(context)
includes_dir = File.join(context.registers[:site].source, '_includes')
error = self.validate_file(includes_dir)
unless error.nil?
return error
end
source = File.read(File.join(includes_dir, @file))
partial = Liquid::Template.parse(source)
context.stack do
context['include'] = parse_params(context) if @params
partial.render(context)
end
end
def validate_file(includes_dir)
if File.symlink?(includes_dir)
return "Includes directory '#{includes_dir}' cannot be a symlink"
end
@ -59,20 +74,14 @@ eos
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)
file = File.join(includes_dir, @file)
if !File.exists?(file)
return "Included file #{@file} not found in _includes directory"
elsif File.symlink?(file)
return "Included file #{@file} is a symlink"
end
context.stack do
context['include'] = parse_params(context) if @params
partial.render(context)
end
else
"Included file '#{@file}' not found in _includes directory"
end
end
nil
end
end
end