Re-implement handling Liquid blocks in excerpts (#7250)
Merge pull request 7250
This commit is contained in:
parent
b586bed0ee
commit
592b530de1
|
@ -131,36 +131,45 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns excerpt String
|
# Returns excerpt String
|
||||||
|
|
||||||
LIQUID_TAG_REGEX = %r!{%-?\s*(\w+).+\s*-?%}!m.freeze
|
LIQUID_TAG_REGEX = %r!{%-?\s*(\w+)\s*.*?-?%}!m.freeze
|
||||||
MKDWN_LINK_REF_REGEX = %r!^ {0,3}\[[^\]]+\]:.+$!.freeze
|
MKDWN_LINK_REF_REGEX = %r!^ {0,3}\[[^\]]+\]:.+$!.freeze
|
||||||
|
|
||||||
def extract_excerpt(doc_content)
|
def extract_excerpt(doc_content)
|
||||||
head, _, tail = doc_content.to_s.partition(doc.excerpt_separator)
|
head, _, tail = doc_content.to_s.partition(doc.excerpt_separator)
|
||||||
|
|
||||||
# append appropriate closing tag (to a Liquid block), to the "head" if the
|
# 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"
|
# partitioning resulted in leaving the closing tag somewhere in the `tail` partition.
|
||||||
# partition.
|
|
||||||
if head.include?("{%")
|
if head.include?("{%")
|
||||||
head =~ LIQUID_TAG_REGEX
|
modified = false
|
||||||
tag_name = Regexp.last_match(1)
|
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)
|
||||||
|
|
||||||
if liquid_block?(tag_name) && head.match(%r!{%-?\s*end#{tag_name}\s*-?%}!).nil?
|
modified = true
|
||||||
print_build_warning
|
|
||||||
head << "\n{% end#{tag_name} %}"
|
head << "\n{% end#{tag_name} %}"
|
||||||
end
|
end
|
||||||
|
print_build_warning if modified
|
||||||
end
|
end
|
||||||
|
|
||||||
if tail.empty?
|
return head if tail.empty?
|
||||||
head
|
|
||||||
else
|
head << "\n\n" << tail.scan(MKDWN_LINK_REF_REGEX).join("\n")
|
||||||
head.to_s.dup << "\n\n" << tail.scan(MKDWN_LINK_REF_REGEX).join("\n")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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)
|
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
|
rescue NoMethodError
|
||||||
Jekyll.logger.error "Error:",
|
Jekyll.logger.error "Error:",
|
||||||
"A Liquid tag in the excerpt of #{doc.relative_path} couldn't be parsed."
|
"A Liquid tag in the excerpt of #{doc.relative_path} couldn't be parsed."
|
||||||
|
@ -169,12 +178,11 @@ module Jekyll
|
||||||
|
|
||||||
def print_build_warning
|
def print_build_warning
|
||||||
Jekyll.logger.warn "Warning:", "Excerpt modified in #{doc.relative_path}!"
|
Jekyll.logger.warn "Warning:", "Excerpt modified in #{doc.relative_path}!"
|
||||||
Jekyll.logger.warn "",
|
Jekyll.logger.warn "", "Found a Liquid block containing the excerpt separator" \
|
||||||
"Found a Liquid block containing separator '#{doc.excerpt_separator}'" \
|
" #{doc.excerpt_separator.inspect}. "
|
||||||
" and has been modified with the appropriate closing tag."
|
Jekyll.logger.warn "", "The block has been modified with the appropriate closing tag."
|
||||||
Jekyll.logger.warn "",
|
Jekyll.logger.warn "", "Feel free to define a custom excerpt or excerpt_separator in the"
|
||||||
"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."
|
||||||
" document's Front Matter if the generated excerpt is unsatisfactory."
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,10 +2,23 @@
|
||||||
layout: post
|
layout: post
|
||||||
---
|
---
|
||||||
|
|
||||||
{% if
|
{%
|
||||||
page.layout == "post" %}
|
highlight
|
||||||
You’ll find this post in your `_posts` directory.
|
ruby
|
||||||
To add new posts, simply add a file in the `_posts` directory.
|
%}
|
||||||
{% endif %}
|
{% 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.
|
So let's talk business.
|
||||||
|
|
|
@ -2,10 +2,20 @@
|
||||||
layout: post
|
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.
|
So let's talk business.
|
||||||
{% endif %}
|
|
||||||
|
|
|
@ -184,12 +184,17 @@ class TestExcerpt < JekyllUnitTest
|
||||||
@post = setup_post("2018-01-28-open-liquid-block-excerpt.markdown")
|
@post = setup_post("2018-01-28-open-liquid-block-excerpt.markdown")
|
||||||
@excerpt = @post.data["excerpt"]
|
@excerpt = @post.data["excerpt"]
|
||||||
|
|
||||||
assert_includes @post.content, "{% if"
|
head = @post.content.split("\n\n")[0]
|
||||||
refute_includes @post.content.split("\n\n")[0], "{% endif %}"
|
|
||||||
|
assert_includes @post.content, "{%\n highlight\n"
|
||||||
|
assert_includes @post.content, "{% raw"
|
||||||
|
refute_includes head, "{% endraw %}"
|
||||||
|
refute_includes head, "{% endhighlight %}"
|
||||||
end
|
end
|
||||||
|
|
||||||
should "be appended to as necessary and generated" do
|
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)
|
assert_equal true, @excerpt.is_a?(Jekyll::Excerpt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -201,13 +206,19 @@ class TestExcerpt < JekyllUnitTest
|
||||||
@post = setup_post("2018-01-28-closed-liquid-block-excerpt.markdown")
|
@post = setup_post("2018-01-28-closed-liquid-block-excerpt.markdown")
|
||||||
@excerpt = @post.data["excerpt"]
|
@excerpt = @post.data["excerpt"]
|
||||||
|
|
||||||
assert_includes @post.content, "{% if"
|
head = @post.content.split("\n\n")[0]
|
||||||
assert_includes @post.content.split("\n\n")[0], "{% endif %}"
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
should "not be appended to but generated as is" do
|
should "not be appended to but generated as is" do
|
||||||
assert_includes @excerpt.content, "{% endif %}"
|
assert_includes @excerpt.content, "{%\n endraw\n%}"
|
||||||
refute_includes @excerpt.content, "{% endif %}\n\n{% endif %}"
|
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)
|
assert_equal true, @excerpt.is_a?(Jekyll::Excerpt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue