simplify and correct rendering pipeline
This commit is contained in:
parent
2074f92ed6
commit
c46ea4096d
|
@ -19,7 +19,7 @@ module Jekyll
|
|||
self.data = YAML.load($1)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Transform the contents based on the file extension.
|
||||
#
|
||||
# Returns nothing
|
||||
|
@ -39,10 +39,8 @@ module Jekyll
|
|||
# +site_payload+ is the site payload hash
|
||||
#
|
||||
# Returns nothing
|
||||
def do_layout(payload, layouts, site_payload)
|
||||
# construct payload
|
||||
payload = payload.merge(site_payload)
|
||||
# render content
|
||||
def do_layout(payload, layouts)
|
||||
# render and transform content (this becomes the final content of the object)
|
||||
self.content = Liquid::Template.parse(self.content).render(payload, [Jekyll::Filters])
|
||||
self.transform
|
||||
|
||||
|
|
|
@ -37,9 +37,9 @@ module Jekyll
|
|||
# +site_payload+ is the site payload hash
|
||||
#
|
||||
# Returns nothing
|
||||
def add_layout(layouts, site_payload)
|
||||
payload = {"page" => self.data}
|
||||
do_layout(payload, layouts, site_payload)
|
||||
def render(layouts, site_payload)
|
||||
payload = {"page" => self.data}.merge(site_payload)
|
||||
do_layout(payload, layouts)
|
||||
end
|
||||
|
||||
# Write the generated page file to the destination directory.
|
||||
|
|
|
@ -123,11 +123,16 @@ module Jekyll
|
|||
# +site_payload+ is the site payload hash
|
||||
#
|
||||
# Returns nothing
|
||||
def add_layout(layouts, site_payload)
|
||||
# construct post payload
|
||||
related = related_posts(site_payload["site"]["posts"])
|
||||
payload = {"page" => self.to_liquid.merge(self.data)}
|
||||
do_layout(payload, layouts, site_payload.merge({"site" => {"related_posts" => related}}))
|
||||
def render(layouts, site_payload)
|
||||
# construct payload
|
||||
payload =
|
||||
{
|
||||
"site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) },
|
||||
"page" => self.to_liquid
|
||||
}
|
||||
payload = payload.merge(site_payload)
|
||||
|
||||
do_layout(payload, layouts)
|
||||
end
|
||||
|
||||
# Write the generated post file to the destination directory.
|
||||
|
@ -147,11 +152,11 @@ module Jekyll
|
|||
#
|
||||
# Returns <Hash>
|
||||
def to_liquid
|
||||
self.data.merge({ "title" => self.data["title"] || "",
|
||||
{ "title" => self.data["title"] || "",
|
||||
"url" => self.url,
|
||||
"date" => self.date,
|
||||
"id" => self.id,
|
||||
"content" => self.content })
|
||||
"content" => self.content }.merge(self.data)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -53,15 +53,19 @@ module Jekyll
|
|||
entries = Dir.entries(base)
|
||||
entries = entries.reject { |e| File.directory?(File.join(base, e)) }
|
||||
|
||||
# first pass processes, but does not yet render post content
|
||||
entries.each do |f|
|
||||
if Post.valid?(f)
|
||||
post = Post.new(base, f)
|
||||
post.content = Liquid::Template.parse(post.content).render(site_payload, [Jekyll::Filters])
|
||||
post.transform
|
||||
post = Post.new(base, f)
|
||||
self.posts << post
|
||||
end
|
||||
end
|
||||
|
||||
# second pass renders each post now that full site payload is available
|
||||
self.posts.each do |post|
|
||||
post.render(self.layouts, site_payload)
|
||||
end
|
||||
|
||||
self.posts.sort!
|
||||
rescue Errno::ENOENT => e
|
||||
# ignore missing layout dir
|
||||
|
@ -72,7 +76,6 @@ module Jekyll
|
|||
# Returns nothing
|
||||
def write_posts
|
||||
self.posts.each do |post|
|
||||
post.add_layout(self.layouts, site_payload)
|
||||
post.write(self.dest)
|
||||
end
|
||||
end
|
||||
|
@ -87,14 +90,14 @@ module Jekyll
|
|||
def transform_pages(dir = '')
|
||||
base = File.join(self.source, dir)
|
||||
entries = Dir.entries(base)
|
||||
entries = entries.reject { |e|
|
||||
(e != '_posts') and ['.', '_'].include?(e[0..0])
|
||||
}
|
||||
entries = entries.reject do |e|
|
||||
(e != '_posts') and ['.', '_'].include?(e[0..0])
|
||||
end
|
||||
|
||||
# we need to make sure to process _posts *first* otherwise they
|
||||
# might not be available yet to other templates as {{ site.posts }}
|
||||
if entries.include? '_posts'
|
||||
entries.delete '_posts'
|
||||
if entries.include?('_posts')
|
||||
entries.delete('_posts')
|
||||
read_posts(File.join(base, '_posts'))
|
||||
end
|
||||
|
||||
|
@ -105,13 +108,13 @@ module Jekyll
|
|||
else
|
||||
first3 = File.open(File.join(self.source, dir, f)) { |fd| fd.read(3) }
|
||||
|
||||
# if the file appears to have a YAML header then process it as a page
|
||||
if first3 == "---"
|
||||
# file appears to have a YAML header so process it as a page
|
||||
page = Page.new(self.source, dir, f)
|
||||
page.add_layout(self.layouts, site_payload)
|
||||
page.render(self.layouts, site_payload)
|
||||
page.write(self.dest)
|
||||
# otherwise copy the file without transforming it
|
||||
else
|
||||
# otherwise copy the file without transforming it
|
||||
FileUtils.mkdir_p(File.join(self.dest, dir))
|
||||
FileUtils.cp(File.join(self.source, dir, f), File.join(self.dest, dir, f))
|
||||
end
|
||||
|
|
|
@ -59,10 +59,10 @@ class TestPost < Test::Unit::TestCase
|
|||
assert_equal "<h1>{{ page.title }}</h1>\n<p>Best <strong>post</strong> ever</p>", p.content
|
||||
end
|
||||
|
||||
def test_add_layout
|
||||
def test_render
|
||||
p = Post.new(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-10-18-foo-bar.textile")
|
||||
layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")}
|
||||
p.add_layout(layouts, {"site" => {"posts" => []}})
|
||||
p.render(layouts, {"site" => {"posts" => []}})
|
||||
|
||||
assert_equal "<<< <h1>Foo Bar</h1>\n<p>Best <strong>post</strong> ever</p> >>>", p.output
|
||||
end
|
||||
|
@ -72,14 +72,14 @@ class TestPost < Test::Unit::TestCase
|
|||
|
||||
p = Post.new(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-10-18-foo-bar.textile")
|
||||
layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")}
|
||||
p.add_layout(layouts, {"site" => {"posts" => []}})
|
||||
p.render(layouts, {"site" => {"posts" => []}})
|
||||
p.write(dest_dir)
|
||||
end
|
||||
|
||||
def test_data
|
||||
p = Post.new(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-11-21-complex.textile")
|
||||
layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")}
|
||||
p.add_layout(layouts, {"site" => {"posts" => []}})
|
||||
p.render(layouts, {"site" => {"posts" => []}})
|
||||
|
||||
assert_equal "<<< <p>url: /test/source/2008/11/21/complex.html<br />\ndate: #{Time.parse("2008-11-21")}<br />\nid: /test/source/2008/11/21/complex</p> >>>", p.output
|
||||
end
|
||||
|
@ -88,7 +88,7 @@ class TestPost < Test::Unit::TestCase
|
|||
Jekyll.source = File.join(File.dirname(__FILE__), *%w[source])
|
||||
p = Post.new(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-12-13-include.markdown")
|
||||
layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")}
|
||||
p.add_layout(layouts, {"site" => {"posts" => []}})
|
||||
p.render(layouts, {"site" => {"posts" => []}})
|
||||
|
||||
assert_equal "<<< <hr />\n<p>Tom Preston-Werner github.com/mojombo</p>\n\n<p>This <em>is</em> cool</p> >>>", p.output
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue