forked from hoffmanc/jekyll-slim
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added liquid tag for slim to render as _include
- Loading branch information
Sunny Juneja
committed
Apr 11, 2013
1 parent
e61c1d2
commit ef63338
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |