Skip to content

Commit

Permalink
Allow excerpts to be generated for Page objects (#7642)
Browse files Browse the repository at this point in the history
Merge pull request 7642
  • Loading branch information
ashmaroli committed May 21, 2020
1 parent 673f3d2 commit 1ec3843
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/jekyll.rb
Expand Up @@ -50,6 +50,7 @@ module Jekyll
autoload :EntryFilter, "jekyll/entry_filter"
autoload :Errors, "jekyll/errors"
autoload :Excerpt, "jekyll/excerpt"
autoload :PageExcerpt, "jekyll/page_excerpt"
autoload :External, "jekyll/external"
autoload :FrontmatterDefaults, "jekyll/frontmatter_defaults"
autoload :Hooks, "jekyll/hooks"
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/drops/page_drop.rb
Expand Up @@ -7,7 +7,7 @@ class PageDrop < Drop

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

def title
Expand Down
11 changes: 11 additions & 0 deletions lib/jekyll/page.rb
Expand Up @@ -15,6 +15,7 @@ class Page
ATTRIBUTES_FOR_LIQUID = %w(
content
dir
excerpt
name
path
url
Expand Down Expand Up @@ -214,5 +215,15 @@ def trigger_hooks(hook_name, *args)
def write?
true
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
26 changes: 26 additions & 0 deletions lib/jekyll/page_excerpt.rb
@@ -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
28 changes: 27 additions & 1 deletion test/test_page.rb
Expand Up @@ -125,7 +125,7 @@ def do_render(page)
attrs = {
:content => "All the properties.\n",
:dir => "/properties/",
:excerpt => nil,
:excerpt => "All the properties.\n",
:foo => "bar",
:layout => "default",
:name => "properties.html",
Expand Down Expand Up @@ -403,6 +403,32 @@ def do_render(page)
assert_exist dest_dir("contacts", "bar", "index.html")
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
4 changes: 2 additions & 2 deletions test/test_page_without_a_file.rb
Expand Up @@ -49,9 +49,9 @@ def render_and_write
assert_equal "All the properties.\n", regular_page["content"]
assert_equal "properties.html", regular_page["name"]

basic_attrs = %w(dir name path url)
basic_attrs = %w(dir name path url excerpt)
attrs = {
"content" => "All the properties.\n",
"content" => nil,
"dir" => "/",
"excerpt" => nil,
"foo" => "bar",
Expand Down

0 comments on commit 1ec3843

Please sign in to comment.