New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Re-implement handling Liquid blocks in excerpts #7250
Changes from 1 commit
d46fdd6
8f97bf5
e1d6db0
61831d4
55f164c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,36 +131,44 @@ def render_with_liquid? | |
# | ||
# Returns excerpt String | ||
|
||
LIQUID_TAG_REGEX = %r!{%-?\s*(\w+).+\s*-?%}!m | ||
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 (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) | ||
tag_names = head.scan(LIQUID_TAG_REGEX) | ||
tag_names.flatten! | ||
print_build_warning unless tag_names.empty? | ||
|
||
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? | ||
print_build_warning | ||
head << "\n{% end#{tag_name} %}" | ||
end | ||
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 parsed." | ||
|
@@ -169,12 +177,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 separator" \ | ||
" #{doc.excerpt_separator.inspect} " | ||
Jekyll.logger.warn "", "and has been modified with the appropriate closing tag." | ||
DirtyF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are all the same warning, not separate messages, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pathawks Yes. These calls are for the same warning. I can't use a heredoc to emulate the formatting because Jekyll's |
||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The capture for this regular expression isn't 100% correct. A liquid tag of
{%highlight%}
will lose the last letter, meaning that the capture group will containhighligh
instead ofhighlight
I wrote a quick test case for this on Rubular as well, as an extra example: http://rubular.com/r/umPU5g5PiH
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for catching this @mattr- 🙂