Skip to content
Browse files

Convert {%include%}d files

  • Loading branch information...
1 parent e3c7bd6 commit 3947512d4a86ebe4a98797142f51eb078da02ee9 Jan-Yves Ruzicka committed Mar 29, 2013
Showing with 66 additions and 2 deletions.
  1. +25 −2 lib/jekyll/tags/include.rb
  2. +41 −0 test/test_tags.rb
View
27 lib/jekyll/tags/include.rb
@@ -7,7 +7,8 @@ def initialize(tag_name, file, tokens)
end
def render(context)
- includes_dir = File.join(context.registers[:site].source, '_includes')
+ @site = context.registers[:site]
+ includes_dir = File.join(@site.source, '_includes')
if File.symlink?(includes_dir)
return "Includes directory '#{includes_dir}' cannot be a symlink"
@@ -20,8 +21,11 @@ def render(context)
Dir.chdir(includes_dir) do
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
if choices.include?(@file)
- source = File.read(@file)
+
+ source = File.read(@file)
+ source = convert_source(source)
partial = Liquid::Template.parse(source)
+
context.stack do
partial.render(context)
end
@@ -30,6 +34,25 @@ def render(context)
end
end
end
+
+ # Converts the passed text using the first available converter that
+ # matches its extension
+ #
+ # source - the source text to convert
+ #
+ # Returns the converted text
+ def convert_source(source)
+ extension = File.extname(@file)
+ converter = @site.converters.find{ |c| c.matches(extension) }
+
+ unless converter.is_a?(Jekyll::Converters::Identity)
+ encoder = Jekyll::LiquidEncoder.new(source)
+ source = converter.convert(encoder.encoded_content)
+ source = encoder.decode(source)
+ end
+ puts "Reached end" if $debug
+ source
+ end
end
end
end
View
41 test/test_tags.rb
@@ -277,4 +277,45 @@ def fill_post(code, override = {})
end
end
end
+
+ context "{% include %}" do
+
+ setup do
+ @tag = Jekyll::Tags::IncludeTag.new("include", "sample.md", nil)
+ end
+
+ def site_with_converter c
+ stub!.converters{ [c] }.subject
+ end
+
+ context "when including a file with a recognised extension" do
+ setup do
+ @sample_converter = mock!.matches(".md"){ true }.subject
+ mock(@sample_converter).is_a?(Jekyll::Converters::Identity){ false }
+ mock(@sample_converter).convert('Sample text'){ 'Converted' }
+ end
+
+ should "try to convert the included file" do
+ @tag.instance_variable_set("@site", site_with_converter(@sample_converter))
+
+ converted_source = @tag.convert_source('Sample text')
+ assert_equal('Converted', converted_source)
+ end
+ end
+
+ context "when including a file without a recognised extension" do
+ setup do
+ @sample_converter = Jekyll::Converters::Identity.new
+ end
+
+ should "not try to convert the included file" do
+ mock(@sample_converter).convert.never
+
+ @tag.instance_variable_set("@site", site_with_converter(@sample_converter))
+
+ converted_source = @tag.convert_source('Sample text')
+ assert_equal('Sample text', converted_source)
+ end
+ end
+ end
end

0 comments on commit 3947512

Please sign in to comment.
Something went wrong with that request. Please try again.