From 0675f2a4237887b8d0176b082cd93ab9690376a5 Mon Sep 17 00:00:00 2001 From: Garen Torikian Date: Sun, 7 Sep 2014 21:23:45 -0700 Subject: [PATCH 1/3] Update include_relative tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This verifies you can’t go “up” in the include --- lib/jekyll/tags/include.rb | 14 ++++++++++---- test/test_tags.rb | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index 724b33c0..5edd33de 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -13,8 +13,6 @@ module Jekyll class IncludeTag < Liquid::Tag - SYNTAX_EXAMPLE = "{% include file.ext param='value' param2='value' %}" - VALID_SYNTAX = /([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/ VARIABLE_SYNTAX = /(?[^{]*\{\{\s*(?[\w\-\.]+)\s*(\|.*)?\}\}[^\s}]*)(?.*)/ @@ -30,6 +28,10 @@ module Jekyll validate_params if @params end + def syntax_example + "{% include file.ext param='value' param2='value' %}" + end + def parse_params(context) params = {} markup = @params @@ -59,7 +61,7 @@ Invalid syntax for include tag. File contains invalid characters or sequences: Valid syntax: - #{SYNTAX_EXAMPLE} + #{syntax_example} eos end @@ -75,7 +77,7 @@ Invalid syntax for include tag: Valid syntax: - #{SYNTAX_EXAMPLE} + #{syntax_example} eos end @@ -154,6 +156,10 @@ eos page_path = context.registers[:page].nil? ? includes_dir : File.dirname(context.registers[:page]["path"]) Jekyll.sanitized_path(context.registers[:site].source, page_path) end + + def syntax_example + "{% include_relative file.ext param='value' param2='value' %}" + end end end end diff --git a/test/test_tags.rb b/test/test_tags.rb index 756bdcb5..a47ca897 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -568,6 +568,25 @@ CONTENT assert_equal 'Included file \'./missing.html\' not found', exception.message end end + + context "include existing file above you" do + setup do + @content = < 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true}) + end + assert_equal "Invalid syntax for include tag. File contains invalid characters or sequences:\n\n ../README.markdown\n\nValid syntax:\n\n {% include_relative file.ext param='value' param2='value' %}\n\n", exception.message + end + end end context "with symlink'd include" do From 5fe73a6671547739fb9c7e4a46a67e672be68d6c Mon Sep 17 00:00:00 2001 From: Garen Torikian Date: Sun, 7 Sep 2014 21:24:05 -0700 Subject: [PATCH 2/3] Add some more docs on `include_relative` --- site/_docs/templates.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/site/_docs/templates.md b/site/_docs/templates.md index 580d3724..cca7c87b 100644 --- a/site/_docs/templates.md +++ b/site/_docs/templates.md @@ -292,16 +292,22 @@ These parameters are available via Liquid in the include: {% raw %}{{ include.param }}{% endraw %} {% endhighlight %} -### Including files relative to another file +#### Including files relative to another file -You can also choose to include files relative to the current file: +You can also choose to include file fragments relative to the current file: {% highlight ruby %} {% raw %}{% include_relative somedir/footer.html %}{% endraw %} {% endhighlight %} -You won't need to place your included content within the `_includes` directory. -All the other capaibilities of the `include` tag are available to the `include_relative` tag. +You won't need to place your included content within the `_includes` directory. Instead, +the inclusion is specifically relative to the file where the tag is being used. For example, +if `_posts/2014-09-03-my-file.markdown` uses the `include_relative` tag, the included file +must be within the `_posts` directory, or one of it's subdirectories. You cannot include +files in other locations. + +All the other capaibilities of the `include` tag are available to the `include_relative` tag, +such as using variables. ### Code snippet highlighting From deb4e0d24afb76dbe96f9090aa2e3ffa5736d8f2 Mon Sep 17 00:00:00 2001 From: Garen Torikian Date: Mon, 8 Sep 2014 14:27:51 -0700 Subject: [PATCH 3/3] Set tag name as ivar This lets us use just one syntax_example method for both classes. --- lib/jekyll/tags/include.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index 5edd33de..87583e95 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -26,10 +26,11 @@ module Jekyll @file, @params = markup.strip.split(' ', 2); end validate_params if @params + @tag_name = tag_name end def syntax_example - "{% include file.ext param='value' param2='value' %}" + "{% #{@tag_name} file.ext param='value' param2='value' %}" end def parse_params(context) @@ -156,10 +157,6 @@ eos page_path = context.registers[:page].nil? ? includes_dir : File.dirname(context.registers[:page]["path"]) Jekyll.sanitized_path(context.registers[:site].source, page_path) end - - def syntax_example - "{% include_relative file.ext param='value' param2='value' %}" - end end end end