refactor Include tag file validation
Split validation into a separate method, and give a more descriptive error on symlinks.
This commit is contained in:
parent
86e007f970
commit
ba5db5ad5b
|
@ -51,6 +51,21 @@ eos
|
||||||
def render(context)
|
def render(context)
|
||||||
includes_dir = File.join(context.registers[:site].source, '_includes')
|
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)
|
if File.symlink?(includes_dir)
|
||||||
return "Includes directory '#{includes_dir}' cannot be a symlink"
|
return "Includes directory '#{includes_dir}' cannot be a symlink"
|
||||||
end
|
end
|
||||||
|
@ -59,20 +74,14 @@ eos
|
||||||
return "Include file '#{@file}' contains invalid characters or sequences"
|
return "Include file '#{@file}' contains invalid characters or sequences"
|
||||||
end
|
end
|
||||||
|
|
||||||
Dir.chdir(includes_dir) do
|
file = File.join(includes_dir, @file)
|
||||||
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
|
if !File.exists?(file)
|
||||||
if choices.include?(@file)
|
return "Included file #{@file} not found in _includes directory"
|
||||||
source = File.read(@file)
|
elsif File.symlink?(file)
|
||||||
partial = Liquid::Template.parse(source)
|
return "Included file #{@file} is a symlink"
|
||||||
|
|
||||||
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
|
end
|
||||||
|
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue