Allow excerpts to be generated for Page objects (#7642)

Merge pull request 7642
This commit is contained in:
Ashwin Maroli 2020-05-21 14:14:14 +05:30 committed by GitHub
parent 673f3d20ba
commit 1ec3843130
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 4 deletions

View File

@ -50,6 +50,7 @@ module Jekyll
autoload :EntryFilter, "jekyll/entry_filter" autoload :EntryFilter, "jekyll/entry_filter"
autoload :Errors, "jekyll/errors" autoload :Errors, "jekyll/errors"
autoload :Excerpt, "jekyll/excerpt" autoload :Excerpt, "jekyll/excerpt"
autoload :PageExcerpt, "jekyll/page_excerpt"
autoload :External, "jekyll/external" autoload :External, "jekyll/external"
autoload :FrontmatterDefaults, "jekyll/frontmatter_defaults" autoload :FrontmatterDefaults, "jekyll/frontmatter_defaults"
autoload :Hooks, "jekyll/hooks" autoload :Hooks, "jekyll/hooks"

View File

@ -7,7 +7,7 @@ module Jekyll
mutable false mutable false
def_delegators :@obj, :content, :dir, :name, :path, :url def_delegators :@obj, :content, :dir, :name, :path, :url, :excerpt
private def_delegator :@obj, :data, :fallback_data private def_delegator :@obj, :data, :fallback_data
def title def title

View File

@ -15,6 +15,7 @@ module Jekyll
ATTRIBUTES_FOR_LIQUID = %w( ATTRIBUTES_FOR_LIQUID = %w(
content content
dir dir
excerpt
name name
path path
url url
@ -214,5 +215,15 @@ module Jekyll
def write? def write?
true true
end end
def excerpt_separator
@excerpt_separator ||= (data["excerpt_separator"] || site.config["excerpt_separator"]).to_s
end
def excerpt
return data["excerpt"] unless self.class == Jekyll::Page
data["excerpt"] ||= Jekyll::PageExcerpt.new(self).to_liquid unless excerpt_separator.empty?
end
end end
end end

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
module Jekyll
class PageExcerpt < Excerpt
attr_reader :output, :doc
alias_method :id, :relative_path
# The Liquid representation of this instance is simply the rendered output string.
alias_method :to_liquid, :output
def initialize(doc)
super
self.output = Renderer.new(site, self, site.site_payload).run
end
def render_with_liquid?
return false if data["render_with_liquid"] == false
Jekyll::Utils.has_liquid_construct?(content)
end
def inspect
"#<#{self.class} id=#{id.inspect}>"
end
end
end

View File

@ -125,7 +125,7 @@ class TestPage < JekyllUnitTest
attrs = { attrs = {
:content => "All the properties.\n", :content => "All the properties.\n",
:dir => "/properties/", :dir => "/properties/",
:excerpt => nil, :excerpt => "All the properties.\n",
:foo => "bar", :foo => "bar",
:layout => "default", :layout => "default",
:name => "properties.html", :name => "properties.html",
@ -403,6 +403,32 @@ class TestPage < JekyllUnitTest
assert_exist dest_dir("contacts", "bar", "index.html") assert_exist dest_dir("contacts", "bar", "index.html")
end end
end end
context "read-in by default" do
should "expose an excerpt to Liquid templates" do
page = setup_page("/contacts", "bar.html")
assert_equal "Contact Information\n", page.to_liquid["excerpt"]
end
end
context "generated via plugin" do
setup do
PageSubclass = Class.new(Jekyll::Page)
@test_page = PageSubclass.new(@site, source_dir, "/contacts", "bar.html")
@test_page.data.clear
end
should "not expose an excerpt to Liquid templates by default" do
assert_equal "Contact Information\n", @test_page.content
assert_nil @test_page.to_liquid["excerpt"]
end
should "expose an excerpt to Liquid templates if hardcoded" do
@test_page.data["excerpt"] = "Test excerpt."
assert_equal "Contact Information\n", @test_page.content
assert_equal "Test excerpt.", @test_page.to_liquid["excerpt"]
end
end
end end
end end
end end

View File

@ -49,9 +49,9 @@ class TestPageWithoutAFile < JekyllUnitTest
assert_equal "All the properties.\n", regular_page["content"] assert_equal "All the properties.\n", regular_page["content"]
assert_equal "properties.html", regular_page["name"] assert_equal "properties.html", regular_page["name"]
basic_attrs = %w(dir name path url) basic_attrs = %w(dir name path url excerpt)
attrs = { attrs = {
"content" => "All the properties.\n", "content" => nil,
"dir" => "/", "dir" => "/",
"excerpt" => nil, "excerpt" => nil,
"foo" => "bar", "foo" => "bar",