Skip to content

Commit

Permalink
Added liquid tag for slim to render as _include
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Juneja committed Apr 11, 2013
1 parent e61c1d2 commit ef63338
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/jekyll-slim.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'slim'
require 'jekyll-slim/version'
require 'jekyll-slim/tags/slim_partial'
require 'jekyll-slim/ext/convertible'

module Jekyll
Expand Down
50 changes: 50 additions & 0 deletions lib/jekyll-slim/tags/slim_partial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'slim'

module Jekyll
class SlimPartialTag < 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 \".slim\" extension" if @file !~ /\.slim$/

Dir.chdir(includes_dir) do
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
if choices.include?(@file)
source = File.read(@file)
conversion = ::Slim::Template.new{ source }.render
partial = Liquid::Template.parse(conversion)
begin
return partial.render!(context)
rescue => e
puts "Liquid Exception: #{e.message} in #{self.data["layout"]}"
e.backtrace.each do |backtrace|
puts backtrace
end
abort("Build Failed")
end

context.stack do
return partial.render(context)
end
else
"Included file '#{@file}' not found in _includes directory"
end
end
end
end
end

Liquid::Template.register_tag('slim', Jekyll::SlimPartialTag)

0 comments on commit ef63338

Please sign in to comment.