Shuffle around rendering of extracted excerpt so it can be liquified.
This commit is contained in:
parent
9f867c8014
commit
015ace6773
|
@ -36,6 +36,7 @@ require 'jekyll/convertible'
|
||||||
require 'jekyll/layout'
|
require 'jekyll/layout'
|
||||||
require 'jekyll/page'
|
require 'jekyll/page'
|
||||||
require 'jekyll/post'
|
require 'jekyll/post'
|
||||||
|
require 'jekyll/excerpt'
|
||||||
require 'jekyll/draft'
|
require 'jekyll/draft'
|
||||||
require 'jekyll/filters'
|
require 'jekyll/filters'
|
||||||
require 'jekyll/static_file'
|
require 'jekyll/static_file'
|
||||||
|
|
|
@ -0,0 +1,140 @@
|
||||||
|
module Jekyll
|
||||||
|
class Excerpt
|
||||||
|
|
||||||
|
# Internal: Extract excerpt from the content
|
||||||
|
#
|
||||||
|
# By default excerpt is your first paragraph of a post: everything before
|
||||||
|
# the first two new lines:
|
||||||
|
#
|
||||||
|
# ---
|
||||||
|
# title: Example
|
||||||
|
# ---
|
||||||
|
#
|
||||||
|
# First paragraph with [link][1].
|
||||||
|
#
|
||||||
|
# Second paragraph.
|
||||||
|
#
|
||||||
|
# [1]: http://example.com/
|
||||||
|
#
|
||||||
|
# This is fairly good option for Markdown and Textile files. But might cause
|
||||||
|
# problems for HTML posts (which is quite unusual for Jekyll). If default
|
||||||
|
# excerpt delimiter is not good for you, you might want to set your own via
|
||||||
|
# configuration option `excerpt_separator`. For example, following is a good
|
||||||
|
# alternative for HTML posts:
|
||||||
|
#
|
||||||
|
# # file: _config.yml
|
||||||
|
# excerpt_separator: "<!-- more -->"
|
||||||
|
#
|
||||||
|
# Notice that all markdown-style link references will be appended to the
|
||||||
|
# excerpt. So the example post above will have this excerpt source:
|
||||||
|
#
|
||||||
|
# First paragraph with [link][1].
|
||||||
|
#
|
||||||
|
# [1]: http://example.com/
|
||||||
|
#
|
||||||
|
# Excerpts are rendered same time as content is rendered.
|
||||||
|
#
|
||||||
|
# Returns excerpt String
|
||||||
|
|
||||||
|
include Convertible
|
||||||
|
|
||||||
|
attr_accessor :post
|
||||||
|
attr_accessor :content, :output, :ext
|
||||||
|
|
||||||
|
# Initialize this Post instance.
|
||||||
|
#
|
||||||
|
# site - The Site.
|
||||||
|
# base - The String path to the dir containing the post file.
|
||||||
|
# name - The String filename of the post file.
|
||||||
|
#
|
||||||
|
# Returns the new Post.
|
||||||
|
def initialize(post)
|
||||||
|
@post = post
|
||||||
|
@content = extract_excerpt(post.content)
|
||||||
|
end
|
||||||
|
|
||||||
|
%w[site name data ext].each do |meth|
|
||||||
|
define_method(meth) do
|
||||||
|
post.send(meth)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def path
|
||||||
|
File.join(post.path, "#excerpt")
|
||||||
|
end
|
||||||
|
|
||||||
|
def include?(something)
|
||||||
|
(output && output.include?(something)) || content.include?(something)
|
||||||
|
end
|
||||||
|
|
||||||
|
# The UID for this post (useful in feeds).
|
||||||
|
# e.g. /2008/11/05/my-awesome-post
|
||||||
|
#
|
||||||
|
# Returns the String UID.
|
||||||
|
def id
|
||||||
|
File.join(post.dir, post.slug, "#excerpt")
|
||||||
|
end
|
||||||
|
|
||||||
|
# Convert this post into a Hash for use in Liquid templates.
|
||||||
|
#
|
||||||
|
# Returns the representative Hash.
|
||||||
|
def to_liquid
|
||||||
|
further_data = Hash[ATTRIBUTES_FOR_LIQUID.map { |attribute|
|
||||||
|
[attribute, post.send(attribute)]
|
||||||
|
}]
|
||||||
|
further_data
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
output || content
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the shorthand String identifier of this Post.
|
||||||
|
def inspect
|
||||||
|
"<Excerpt: #{self.id}>"
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
# Internal: Extract excerpt from the content
|
||||||
|
#
|
||||||
|
# By default excerpt is your first paragraph of a post: everything before
|
||||||
|
# the first two new lines:
|
||||||
|
#
|
||||||
|
# ---
|
||||||
|
# title: Example
|
||||||
|
# ---
|
||||||
|
#
|
||||||
|
# First paragraph with [link][1].
|
||||||
|
#
|
||||||
|
# Second paragraph.
|
||||||
|
#
|
||||||
|
# [1]: http://example.com/
|
||||||
|
#
|
||||||
|
# This is fairly good option for Markdown and Textile files. But might cause
|
||||||
|
# problems for HTML posts (which is quite unusual for Jekyll). If default
|
||||||
|
# excerpt delimiter is not good for you, you might want to set your own via
|
||||||
|
# configuration option `excerpt_separator`. For example, following is a good
|
||||||
|
# alternative for HTML posts:
|
||||||
|
#
|
||||||
|
# # file: _config.yml
|
||||||
|
# excerpt_separator: "<!-- more -->"
|
||||||
|
#
|
||||||
|
# Notice that all markdown-style link references will be appended to the
|
||||||
|
# excerpt. So the example post above will have this excerpt source:
|
||||||
|
#
|
||||||
|
# First paragraph with [link][1].
|
||||||
|
#
|
||||||
|
# [1]: http://example.com/
|
||||||
|
#
|
||||||
|
# Excerpts are rendered same time as content is rendered.
|
||||||
|
#
|
||||||
|
# Returns excerpt String
|
||||||
|
def extract_excerpt(post_content)
|
||||||
|
separator = site.config['excerpt_separator']
|
||||||
|
head, _, tail = post_content.partition(separator)
|
||||||
|
|
||||||
|
"" << head << "\n\n" << tail.scan(/^\[[^\]]+\]:.+$/).join("\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -109,7 +109,7 @@ module Jekyll
|
||||||
if self.data.has_key? 'excerpt'
|
if self.data.has_key? 'excerpt'
|
||||||
self.data['excerpt']
|
self.data['excerpt']
|
||||||
else
|
else
|
||||||
self.extracted_excerpt
|
self.extracted_excerpt.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -158,14 +158,6 @@ module Jekyll
|
||||||
raise FatalException.new("Post #{name} does not have a valid date.")
|
raise FatalException.new("Post #{name} does not have a valid date.")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Transform the contents and excerpt based on the content type.
|
|
||||||
#
|
|
||||||
# Returns nothing.
|
|
||||||
def transform
|
|
||||||
super
|
|
||||||
self.extracted_excerpt = converter.convert(self.extracted_excerpt)
|
|
||||||
end
|
|
||||||
|
|
||||||
# The generated directory into which the post will be placed
|
# The generated directory into which the post will be placed
|
||||||
# upon generation. This is derived from the permalink or, if
|
# upon generation. This is derived from the permalink or, if
|
||||||
# permalink is absent, set to the default date
|
# permalink is absent, set to the default date
|
||||||
|
@ -260,6 +252,8 @@ module Jekyll
|
||||||
"page" => self.to_liquid
|
"page" => self.to_liquid
|
||||||
}.deep_merge(site_payload)
|
}.deep_merge(site_payload)
|
||||||
|
|
||||||
|
self.extracted_excerpt.do_layout(payload, layouts)
|
||||||
|
|
||||||
do_layout(payload, layouts)
|
do_layout(payload, layouts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -311,45 +305,8 @@ module Jekyll
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
# Internal: Extract excerpt from the content
|
|
||||||
#
|
|
||||||
# By default excerpt is your first paragraph of a post: everything before
|
|
||||||
# the first two new lines:
|
|
||||||
#
|
|
||||||
# ---
|
|
||||||
# title: Example
|
|
||||||
# ---
|
|
||||||
#
|
|
||||||
# First paragraph with [link][1].
|
|
||||||
#
|
|
||||||
# Second paragraph.
|
|
||||||
#
|
|
||||||
# [1]: http://example.com/
|
|
||||||
#
|
|
||||||
# This is fairly good option for Markdown and Textile files. But might cause
|
|
||||||
# problems for HTML posts (which is quite unusual for Jekyll). If default
|
|
||||||
# excerpt delimiter is not good for you, you might want to set your own via
|
|
||||||
# configuration option `excerpt_separator`. For example, following is a good
|
|
||||||
# alternative for HTML posts:
|
|
||||||
#
|
|
||||||
# # file: _config.yml
|
|
||||||
# excerpt_separator: "<!-- more -->"
|
|
||||||
#
|
|
||||||
# Notice that all markdown-style link references will be appended to the
|
|
||||||
# excerpt. So the example post above will have this excerpt source:
|
|
||||||
#
|
|
||||||
# First paragraph with [link][1].
|
|
||||||
#
|
|
||||||
# [1]: http://example.com/
|
|
||||||
#
|
|
||||||
# Excerpts are rendered same time as content is rendered.
|
|
||||||
#
|
|
||||||
# Returns excerpt String
|
|
||||||
def extract_excerpt
|
def extract_excerpt
|
||||||
separator = self.site.config['excerpt_separator']
|
Jekyll::Excerpt.new(self)
|
||||||
head, _, tail = self.content.partition(separator)
|
|
||||||
|
|
||||||
"" << head << "\n\n" << tail.scan(/^\[[^\]]+\]:.+$/).join("\n")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue