From a662bc24aa4164d6e8a486e18ee81e93ea27e049 Mon Sep 17 00:00:00 2001 From: ashmaroli Date: Tue, 20 Feb 2018 20:17:56 +0530 Subject: [PATCH] Append appropriate closing tag to Liquid block in an excerpt (#6724) Merge pull request 6724 --- lib/jekyll/excerpt.rb | 29 +++++++++++++++ ...01-28-closed-liquid-block-excerpt.markdown | 10 ++++++ ...8-01-28-open-liquid-block-excerpt.markdown | 11 ++++++ test/test_excerpt.rb | 35 +++++++++++++++++++ test/test_generated_site.rb | 2 +- 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 test/source/_posts/2018-01-28-closed-liquid-block-excerpt.markdown create mode 100644 test/source/_posts/2018-01-28-open-liquid-block-excerpt.markdown diff --git a/lib/jekyll/excerpt.rb b/lib/jekyll/excerpt.rb index 7adfec98..bac91ede 100644 --- a/lib/jekyll/excerpt.rb +++ b/lib/jekyll/excerpt.rb @@ -116,11 +116,40 @@ module Jekyll def extract_excerpt(doc_content) head, _, tail = doc_content.to_s.partition(doc.excerpt_separator) + # append appropriate closing tag (to a Liquid block), to the "head" if the + # partitioning resulted in leaving the closing tag somewhere in the "tail" + # partition. + if head.include?("{%") + head =~ %r!{%\s*(\w+).+\s*%}! + tag_name = Regexp.last_match(1) + + if liquid_block?(tag_name) && head.match(%r!{%\s*end#{tag_name}\s*%}!).nil? + print_build_warning + head << "\n{% end#{tag_name} %}" + end + end + if tail.empty? head else head.to_s.dup << "\n\n" << tail.scan(%r!^ {0,3}\[[^\]]+\]:.+$!).join("\n") end end + + private + + def liquid_block?(tag_name) + Liquid::Template.tags[tag_name].superclass == Liquid::Block + end + + def print_build_warning + Jekyll.logger.warn "Warning:", "Excerpt modified in #{doc.relative_path}!" + Jekyll.logger.warn "", + "Found a Liquid block containing separator '#{doc.excerpt_separator}' and has " \ + "been modified with the appropriate closing tag." + Jekyll.logger.warn "", + "Feel free to define a custom excerpt or excerpt_separator in the document's " \ + "Front Matter if the generated excerpt is unsatisfactory." + end end end diff --git a/test/source/_posts/2018-01-28-closed-liquid-block-excerpt.markdown b/test/source/_posts/2018-01-28-closed-liquid-block-excerpt.markdown new file mode 100644 index 00000000..599a0194 --- /dev/null +++ b/test/source/_posts/2018-01-28-closed-liquid-block-excerpt.markdown @@ -0,0 +1,10 @@ +--- +layout: post +--- + +{% if page.layout == "post" %} +You’ll find this post in your `_posts` directory. +To add new posts, simply add a file in the `_posts` directory. +{% endif %} + +So let's talk business. diff --git a/test/source/_posts/2018-01-28-open-liquid-block-excerpt.markdown b/test/source/_posts/2018-01-28-open-liquid-block-excerpt.markdown new file mode 100644 index 00000000..bffe45f0 --- /dev/null +++ b/test/source/_posts/2018-01-28-open-liquid-block-excerpt.markdown @@ -0,0 +1,11 @@ +--- +layout: post +--- + +{% if page.layout == "post" %} + You’ll find this post in your `_posts` directory. + +{% else %} + + To add new posts, simply add a file in the `_posts` directory. +{% endif %} diff --git a/test/test_excerpt.rb b/test/test_excerpt.rb index 2bcf3ba8..96cd60cf 100644 --- a/test/test_excerpt.rb +++ b/test/test_excerpt.rb @@ -168,4 +168,39 @@ class TestExcerpt < JekyllUnitTest end end end + + context "An excerpt with non-closed but valid Liquid block tag" do + setup do + clear_dest + @site = fixture_site + @post = setup_post("2018-01-28-open-liquid-block-excerpt.markdown") + @excerpt = @post.data["excerpt"] + + assert_includes @post.content, "{% if" + refute_includes @post.content.split("\n\n")[0], "{% endif %}" + end + + should "be appended to as necessary and generated" do + assert_includes @excerpt.content, "{% endif %}" + assert_equal true, @excerpt.is_a?(Jekyll::Excerpt) + end + end + + context "An excerpt with valid closed Liquid block tag" do + setup do + clear_dest + @site = fixture_site + @post = setup_post("2018-01-28-closed-liquid-block-excerpt.markdown") + @excerpt = @post.data["excerpt"] + + assert_includes @post.content, "{% if" + assert_includes @post.content.split("\n\n")[0], "{% endif %}" + end + + should "not be appended to but generated as is" do + assert_includes @excerpt.content, "{% endif %}" + refute_includes @excerpt.content, "{% endif %}\n\n{% endif %}" + assert_equal true, @excerpt.is_a?(Jekyll::Excerpt) + end + end end diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 136f5fc9..4eaddb29 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -16,7 +16,7 @@ class TestGeneratedSite < JekyllUnitTest end should "ensure post count is as expected" do - assert_equal 52, @site.posts.size + assert_equal 54, @site.posts.size end should "insert site.posts into the index" do