Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow excerpts to be generated for Page objects #7642

Merged
merged 6 commits into from May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
end
end
Expand Down
9 changes: 9 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,13 @@ 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
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
2 changes: 1 addition & 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
6 changes: 3 additions & 3 deletions test/test_page_without_a_file.rb
Expand Up @@ -49,11 +49,11 @@ 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,
"excerpt" => "",
"foo" => "bar",
"layout" => "default",
"name" => "properties.html",
Expand Down