diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index f90f8e8a6..6d9e31cf6 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -18,9 +18,15 @@ module Jekyll VALID_SYNTAX = /([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/ VARIABLE_SYNTAX = /(?[^{]*\{\{\s*(?[\w\-\.]+)\s*(\|.*)?\}\}[^\s}]*)(?.*)/ - INCLUDES_DIR = '_includes' - def initialize(tag_name, markup, tokens) + @tag_name = tag_name + case tag_name + when 'include' + @includes_dir = '_includes' + when 'relative_include' + @includes_dir = '' + end + super matched = markup.strip.match(VARIABLE_SYNTAX) if matched @@ -97,8 +103,12 @@ eos end def render(context) - dir = File.join(File.realpath(context.registers[:site].source), INCLUDES_DIR) - + case @tag_name + when 'include' + dir = File.join(File.realpath(context.registers[:site].source), @includes_dir) + when 'relative_include' + dir = File.join(File.realpath(context.registers[:site].source), File.dirname(context.registers[:page]["path"])) + end file = render_variable(context) || @file validate_file_name(file) @@ -113,7 +123,7 @@ eos partial.render!(context) end rescue => e - raise IncludeTagError.new e.message, File.join(INCLUDES_DIR, @file) + raise IncludeTagError.new e.message, File.join(@includes_dir, @file) end end @@ -126,7 +136,7 @@ eos end def path_relative_to_source(dir, path) - File.join(INCLUDES_DIR, path.sub(Regexp.new("^#{dir}"), "")) + File.join(@includes_dir, path.sub(Regexp.new("^#{dir}"), "")) end def realpath_prefixed_with?(path, dir) @@ -138,7 +148,11 @@ eos File.read(file, file_read_opts(context)) end end + + class RelativeIncludeTag < IncludeTag + end end end Liquid::Template.register_tag('include', Jekyll::Tags::IncludeTag) +Liquid::Template.register_tag('relative_include', Jekyll::Tags::RelativeIncludeTag) diff --git a/test/source/_posts/2014-09-02-relative-includes.markdown b/test/source/_posts/2014-09-02-relative-includes.markdown new file mode 100644 index 000000000..4afeffbcb --- /dev/null +++ b/test/source/_posts/2014-09-02-relative-includes.markdown @@ -0,0 +1,25 @@ +--- +title: Post +layout: post +include1: rel_include.html +include2: relative_includes/rel_include +include3: rel_INCLUDE +include4: params +include5: clude +--- + +Liquid tests +- 1 {% relative_include relative_includes/{{ page.include1 }} %} +- 2 {% relative_include {{ page.include2 | append: '.html' }} %} +- 3 {% relative_include relative_includes/{{ page.include3 | downcase | append: '.html' }} %} + +Whitespace tests +- 4 {% relative_include relative_includes/{{page.include1}} %} +- 5 {% relative_include relative_includes/{{ page.include1}} %} +- 6 {% relative_include relative_includes/{{ page.include3 | downcase | append: '.html'}} %} + +Parameters test +- 7 {% relative_include relative_includes/{{ page.include4 | append: '.html' }} var1='foo' var2='bar' %} + +Partial variable test +- 8 {% relative_include relative_includes/rel_in{{ page.include5 }}.html %} diff --git a/test/source/_posts/relative_includes/params.html b/test/source/_posts/relative_includes/params.html new file mode 100644 index 000000000..ae4c6cfa0 --- /dev/null +++ b/test/source/_posts/relative_includes/params.html @@ -0,0 +1,7 @@ +{{include.param}} + +
    + {% for param in include %} +
  • {{param[0]}} = {{param[1]}}
  • + {% endfor %} +
diff --git a/test/source/_posts/relative_includes/rel_include.html b/test/source/_posts/relative_includes/rel_include.html new file mode 100644 index 000000000..5fb677b54 --- /dev/null +++ b/test/source/_posts/relative_includes/rel_include.html @@ -0,0 +1 @@ +relative_included diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index ca3124c50..378187266 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -14,7 +14,7 @@ class TestGeneratedSite < Test::Unit::TestCase end should "ensure post count is as expected" do - assert_equal 42, @site.posts.size + assert_equal 43, @site.posts.size end should "insert site.posts into the index" do diff --git a/test/test_site.rb b/test/test_site.rb index 971119711..7c42862ad 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -48,7 +48,7 @@ class TestSite < Test::Unit::TestCase Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir}) end @site = Site.new(Jekyll.configuration) - @num_invalid_posts = 2 + @num_invalid_posts = 4 end should "have an empty tag hash by default" do diff --git a/test/test_tags.rb b/test/test_tags.rb index 12402c7fd..379208533 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -509,5 +509,40 @@ CONTENT assert_match %r{8 included}, @content end end + + context "relative include tag with variable and liquid filters" do + setup do + stub(Jekyll).configuration do + site_configuration({'pygments' => true}) + end + + site = Site.new(Jekyll.configuration) + post = Post.new(site, source_dir, '', "2014-09-02-relative-includes.markdown") + layouts = { "default" => Layout.new(site, source_dir('_layouts'), "simple.html")} + post.render(layouts, {"site" => {"posts" => []}}) + @content = post.content + end + + should "include file as variable with liquid filters" do + assert_match %r{1 relative_include}, @content + assert_match %r{2 relative_include}, @content + assert_match %r{3 relative_include}, @content + end + + should "include file as variable and liquid filters with arbitrary whitespace" do + assert_match %r{4 relative_include}, @content + assert_match %r{5 relative_include}, @content + assert_match %r{6 relative_include}, @content + end + + should "include file as variable and filters with additional parameters" do + assert_match '
  • var1 = foo
  • ', @content + assert_match '
  • var2 = bar
  • ', @content + end + + should "include file as partial variable" do + assert_match %r{8 relative_include}, @content + end + end end end