From 924ca62bd2cc24414e8d033e5f36b08f0ae2701e Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Mon, 5 Nov 2018 00:32:00 +0530 Subject: [PATCH 1/3] Re-implement handling Liquid blocks in excerpts (#7250) Merge pull request 7250 --- lib/jekyll/excerpt.rb | 52 +++++++++++-------- ...01-28-closed-liquid-block-excerpt.markdown | 23 ++++++-- ...8-01-28-open-liquid-block-excerpt.markdown | 20 +++++-- test/test_excerpt.rb | 25 ++++++--- 4 files changed, 81 insertions(+), 39 deletions(-) diff --git a/lib/jekyll/excerpt.rb b/lib/jekyll/excerpt.rb index 1df15e09a53..f12cec5ace7 100644 --- a/lib/jekyll/excerpt.rb +++ b/lib/jekyll/excerpt.rb @@ -128,36 +128,45 @@ def render_with_liquid? # # Returns excerpt String - LIQUID_TAG_REGEX = %r!{%-?\s*(\w+).+\s*-?%}!m - MKDWN_LINK_REF_REGEX = %r!^ {0,3}\[[^\]]+\]:.+$! + LIQUID_TAG_REGEX = %r!{%-?\s*(\w+)\s*.*?-?%}!m.freeze + MKDWN_LINK_REF_REGEX = %r!^ {0,3}\[[^\]]+\]:.+$!.freeze 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. + # append appropriate closing tag(s) (for each Liquid block), to the `head` if the + # partitioning resulted in leaving the closing tag somewhere in the `tail` partition. if head.include?("{%") - head =~ LIQUID_TAG_REGEX - tag_name = Regexp.last_match(1) - - if liquid_block?(tag_name) && head.match(%r!{%-?\s*end#{tag_name}\s*-?%}!).nil? - print_build_warning + modified = false + tag_names = head.scan(LIQUID_TAG_REGEX) + tag_names.flatten! + tag_names.reverse_each do |tag_name| + next unless liquid_block?(tag_name) + next if head =~ endtag_regex_stash(tag_name) + + modified = true head << "\n{% end#{tag_name} %}" end + print_build_warning if modified end - if tail.empty? - head - else - head.to_s.dup << "\n\n" << tail.scan(MKDWN_LINK_REF_REGEX).join("\n") - end + return head if tail.empty? + + head << "\n\n" << tail.scan(MKDWN_LINK_REF_REGEX).join("\n") end private + def endtag_regex_stash(tag_name) + @endtag_regex_stash ||= {} + @endtag_regex_stash[tag_name] ||= %r!{%-?\s*end#{tag_name}.*?\s*-?%}!m + end + def liquid_block?(tag_name) - Liquid::Template.tags[tag_name].superclass == Liquid::Block + return false unless tag_name.is_a?(String) + return false if tag_name.start_with?("end") + + Liquid::Template.tags[tag_name].ancestors.include?(Liquid::Block) rescue NoMethodError Jekyll.logger.error "Error:", "A Liquid tag in the excerpt of #{doc.relative_path} couldn't be " \ @@ -167,12 +176,11 @@ def liquid_block?(tag_name) 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." + Jekyll.logger.warn "", "Found a Liquid block containing the excerpt separator" \ + " #{doc.excerpt_separator.inspect}. " + Jekyll.logger.warn "", "The block has been modified with the appropriate closing tag." + Jekyll.logger.warn "", "Feel free to define a custom excerpt or excerpt_separator in the" + Jekyll.logger.warn "", "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 index 991ebf84e5b..a2c9fe56d5a 100644 --- a/test/source/_posts/2018-01-28-closed-liquid-block-excerpt.markdown +++ b/test/source/_posts/2018-01-28-closed-liquid-block-excerpt.markdown @@ -2,10 +2,23 @@ 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 %} +{% + highlight + ruby +%} +{% assign foo = 'foobar' %} +{% raw +%} +def print_hi(name) + puts "Hi, #{name}" +end +print_hi('Tom') +#=> prints 'Hi, Tom' to STDOUT. +{% + endraw +%} +{% + endhighlight +%} 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 index bffe45f0b7b..24c2e0eb04d 100644 --- a/test/source/_posts/2018-01-28-open-liquid-block-excerpt.markdown +++ b/test/source/_posts/2018-01-28-open-liquid-block-excerpt.markdown @@ -2,10 +2,20 @@ layout: post --- -{% if page.layout == "post" %} - You’ll find this post in your `_posts` directory. +{% + highlight + ruby +%} +{% assign foo = 'foobar' %} +{% raw +%} +def print_hi(name) + puts "Hi, #{name}" +end -{% else %} +print_hi('Tom') +#=> prints 'Hi, Tom' to STDOUT. +{% endraw %} +{% endhighlight %} - To add new posts, simply add a file in the `_posts` directory. -{% endif %} +So let's talk business. diff --git a/test/test_excerpt.rb b/test/test_excerpt.rb index 24647decada..020b6f86dbe 100644 --- a/test/test_excerpt.rb +++ b/test/test_excerpt.rb @@ -185,12 +185,17 @@ def do_render(document) @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 %}" + head = @post.content.split("\n\n")[0] + + assert_includes @post.content, "{%\n highlight\n" + assert_includes @post.content, "{% raw" + refute_includes head, "{% endraw %}" + refute_includes head, "{% endhighlight %}" end should "be appended to as necessary and generated" do - assert_includes @excerpt.content, "{% endif %}" + assert_includes @excerpt.content, "{% endraw %}" + assert_includes @excerpt.content, "{% endhighlight %}" assert_equal true, @excerpt.is_a?(Jekyll::Excerpt) end end @@ -202,13 +207,19 @@ def do_render(document) @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 %}" + head = @post.content.split("\n\n")[0] + + assert_includes @post.content, "{%\n highlight\n" + assert_includes @post.content, "{% raw" + assert_includes head, "{%\n endraw\n%}" + assert_includes head, "{%\n endhighlight\n%}" 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_includes @excerpt.content, "{%\n endraw\n%}" + assert_includes @excerpt.content, "{%\n endhighlight\n%}" + refute_includes @excerpt.content, "{%\n endraw\n%}\n\n{% endraw %}" + refute_includes @excerpt.content, "{%\n endhighlight\n%}\n\n{% endhighlight %}" assert_equal true, @excerpt.is_a?(Jekyll::Excerpt) end end From aedb403afd5de0363b53bc892ba69fa9db5a1cdf Mon Sep 17 00:00:00 2001 From: Frank Taillandier Date: Sun, 4 Nov 2018 20:45:28 +0100 Subject: [PATCH 2/3] style: fix offenses --- lib/jekyll/excerpt.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/jekyll/excerpt.rb b/lib/jekyll/excerpt.rb index f12cec5ace7..7242c6c5a67 100644 --- a/lib/jekyll/excerpt.rb +++ b/lib/jekyll/excerpt.rb @@ -128,14 +128,16 @@ def render_with_liquid? # # Returns excerpt String - LIQUID_TAG_REGEX = %r!{%-?\s*(\w+)\s*.*?-?%}!m.freeze - MKDWN_LINK_REF_REGEX = %r!^ {0,3}\[[^\]]+\]:.+$!.freeze + LIQUID_TAG_REGEX = %r!{%-?\s*(\w+)\s*.*?-?%}!m + MKDWN_LINK_REF_REGEX = %r!^ {0,3}\[[^\]]+\]:.+$! def extract_excerpt(doc_content) head, _, tail = doc_content.to_s.partition(doc.excerpt_separator) - # append appropriate closing tag(s) (for each Liquid block), to the `head` if the - # partitioning resulted in leaving the closing tag somewhere in the `tail` partition. + # append appropriate closing tag(s) (for each Liquid block), to the `head` + # if the partitioning resulted in leaving the closing tag somewhere + # in the `tail` partition. + if head.include?("{%") modified = false tag_names = head.scan(LIQUID_TAG_REGEX) @@ -178,9 +180,12 @@ def print_build_warning Jekyll.logger.warn "Warning:", "Excerpt modified in #{doc.relative_path}!" Jekyll.logger.warn "", "Found a Liquid block containing the excerpt separator" \ " #{doc.excerpt_separator.inspect}. " - Jekyll.logger.warn "", "The block has been modified with the appropriate closing tag." - Jekyll.logger.warn "", "Feel free to define a custom excerpt or excerpt_separator in the" - Jekyll.logger.warn "", "document's Front Matter if the generated excerpt is unsatisfactory." + Jekyll.logger.warn "", "The block has been modified with the appropriate" \ + " closing tag." + Jekyll.logger.warn "", "Feel free to define a custom excerpt or" \ + "excerpt_separator in the" + Jekyll.logger.warn "", "document's Front Matter if the generated excerpt" \ + " is unsatisfactory." end end end From a4171db345b47c5bd63c38d28fb593c2708e0e22 Mon Sep 17 00:00:00 2001 From: Frank Taillandier Date: Sun, 4 Nov 2018 20:52:35 +0100 Subject: [PATCH 3/3] style: Metrics/LineLength --- lib/jekyll/excerpt.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/jekyll/excerpt.rb b/lib/jekyll/excerpt.rb index 7242c6c5a67..202c9bb3d4b 100644 --- a/lib/jekyll/excerpt.rb +++ b/lib/jekyll/excerpt.rb @@ -183,9 +183,8 @@ def print_build_warning Jekyll.logger.warn "", "The block has been modified with the appropriate" \ " closing tag." Jekyll.logger.warn "", "Feel free to define a custom excerpt or" \ - "excerpt_separator in the" - Jekyll.logger.warn "", "document's Front Matter if the generated excerpt" \ - " is unsatisfactory." + " excerpt_separator in the document's front matter" \ + " if the generated excerpt is unsatisfactory." end end end