Moving data around to make sure excerpts have no layouts but that they are still converted with liquid and the proper converter

This commit is contained in:
Parker Moore 2013-07-22 11:31:15 +02:00
parent e359472870
commit 26dc14881c
2 changed files with 39 additions and 28 deletions

View File

@ -13,27 +13,41 @@ module Jekyll
# #
# Returns the new Post. # Returns the new Post.
def initialize(post) def initialize(post)
@post = post self.post = post
@content = extract_excerpt(post.content) self.content = extract_excerpt(post.content)
end end
%w[site name data ext].each do |meth| %w[site name ext].each do |meth|
define_method(meth) do define_method(meth) do
post.send(meth) post.send(meth)
end end
end end
def to_liquid
post.to_liquid(Post::EXCERPT_ATTRIBUTES_FOR_LIQUID)
end
# Fetch YAML front-matter data from related post, without layout key
#
# Returns Hash of post data
def data
@data ||= post.data.dup
@data.delete("layout") if @data.has_key?("layout")
@data
end
# 'Path' of the excerpt.
#
# Returns the path for the post this excerpt belongs to with #excerpt appended
def path def path
File.join(post.path, "#excerpt") File.join(post.path, "#excerpt")
end end
# Check if excerpt includes a string
#
# Returns true if the string passed in
def include?(something) def include?(something)
(output && output.include?(something)) || content.include?(something) (self.output && self.output.include?(something)) || self.content.include?(something)
end
def render_all_layouts(layouts, payload, info)
output = content
Jekyll.logger.debug "Output of", "#{self.path} => '#{self.output}'"
end end
# The UID for this post (useful in feeds). # The UID for this post (useful in feeds).
@ -44,16 +58,9 @@ module Jekyll
File.join(post.dir, post.slug, "#excerpt") File.join(post.dir, post.slug, "#excerpt")
end end
# Convert this post into a Hash for use in Liquid templates.
#
# Returns the representative Hash.
def to_liquid
post.to_liquid
end
def to_s def to_s
Jekyll.logger.debug "Excerpt#to_s:", "#{output} || #{content}" Jekyll.logger.debug "Excerpt#to_s:", "#{self.output} || #{content}"
output || content self.output || self.content
end end
# Returns the shorthand String identifier of this Post. # Returns the shorthand String identifier of this Post.

View File

@ -10,8 +10,7 @@ module Jekyll
# Valid post name regex. # Valid post name regex.
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/ MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
# Attributes for Liquid templates EXCERPT_ATTRIBUTES_FOR_LIQUID = %w[
ATTRIBUTES_FOR_LIQUID = %w[
title title
url url
date date
@ -20,11 +19,15 @@ module Jekyll
next next
previous previous
tags tags
content
excerpt
path path
] ]
# Attributes for Liquid templates
ATTRIBUTES_FOR_LIQUID = EXCERPT_ATTRIBUTES_FOR_LIQUID.concat(%w[
content
excerpt
])
# Post name validator. Post filenames must be like: # Post name validator. Post filenames must be like:
# 2008-11-05-my-awesome-post.textile # 2008-11-05-my-awesome-post.textile
# #
@ -109,7 +112,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.output || self.extracted_excerpt.to_s self.extracted_excerpt.to_s
end end
end end
@ -249,12 +252,13 @@ module Jekyll
# construct payload # construct payload
payload = { payload = {
"site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) }, "site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) },
"page" => self.to_liquid "page" => self.to_liquid(EXCERPT_ATTRIBUTES_FOR_LIQUID)
}.deep_merge(site_payload) }.deep_merge(site_payload)
self.extracted_excerpt.do_layout(payload, layouts) self.extracted_excerpt.do_layout(payload, {})
Jekyll.logger.info("", "#{self.excerpt}".green)
do_layout(payload, layouts) do_layout(payload.merge({"page" => self.to_liquid}), layouts)
end end
# Obtain destination path. # Obtain destination path.
@ -272,8 +276,8 @@ module Jekyll
# Convert this post into a Hash for use in Liquid templates. # Convert this post into a Hash for use in Liquid templates.
# #
# Returns the representative Hash. # Returns the representative Hash.
def to_liquid def to_liquid(attrs = ATTRIBUTES_FOR_LIQUID)
further_data = Hash[ATTRIBUTES_FOR_LIQUID.map { |attribute| further_data = Hash[attrs.map { |attribute|
[attribute, send(attribute)] [attribute, send(attribute)]
}] }]
data.deep_merge(further_data) data.deep_merge(further_data)