1
0
Fork 0
docsearch/docs/_plugins/haml.rb
Sylvain UTARD e59d132c2d feat(website): ensure the inputs are mandatory
Move the forms to a partial (had a hard time with HAML-based partials...)
Small box-shadow if not specified
Add a "I'm the owner of that website" checkbox
2015-12-28 14:53:54 +01:00

40 lines
1.1 KiB
Ruby

require 'jekyll-haml'
module Jekyll
class HamlPartialTag < Liquid::Tag
def initialize(tag_name, file, tokens)
super
@file = file.strip
end
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
return "File must have \".haml\" extension" if @file !~ /\.haml$/
Dir.chdir(includes_dir) do
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
if choices.include?(@file)
source = File.read(@file)
conversion = ::Haml::Engine.new(source).render
partial = Liquid::Template.parse(conversion)
return partial.render!(context).gsub(/\s+/, ' ')
else
"Included file '#{@file}' not found in _includes directory"
end
end
end
end
end
Liquid::Template.register_tag('haml', Jekyll::HamlPartialTag)