allow nested layouts

This commit is contained in:
Tom Preston-Werner 2008-11-05 22:00:30 -05:00
parent 6980797d38
commit af673529cb
6 changed files with 82 additions and 18 deletions

View File

@ -16,6 +16,7 @@ require 'redcloth'
# internal requires
require 'autoblog/site'
require 'autoblog/convertible'
require 'autoblog/layout'
require 'autoblog/page'
require 'autoblog/post'
require 'autoblog/filters'

View File

@ -24,5 +24,30 @@ module AutoBlog
self.content = RedCloth.new(self.content).to_html
end
end
# Add any necessary layouts to this post
# +layouts+ is a Hash of {"name" => "layout"}
# +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
self.content = Liquid::Template.parse(self.content).render(payload, [AutoBlog::Filters])
# output keeps track of what will finally be written
self.output = self.content
# recursively render layouts
layout = layouts[self.data["layout"]]
while layout
payload = payload.merge({"content" => self.output, "page" => self.data})
self.output = Liquid::Template.parse(layout.content).render(payload, [AutoBlog::Filters])
layout = layouts[layout.data["layout"]]
end
end
end
end

48
lib/autoblog/layout.rb Normal file
View File

@ -0,0 +1,48 @@
module AutoBlog
class Layout
include Convertible
attr_accessor :ext
attr_accessor :data, :content
# Initialize a new Layout.
# +base+ is the String path to the <source>
# +name+ is the String filename of the post file
#
# Returns <Page>
def initialize(base, name)
@base = base
@name = name
self.data = {}
self.process(name)
self.read_yaml(base, name)
end
# Extract information from the layout filename
# +name+ is the String filename of the layout file
#
# Returns nothing
def process(name)
self.ext = File.extname(name)
end
# Add any necessary layouts to this post
# +layouts+ is a Hash of {"name" => "layout"}
# +site_payload+ is the site payload hash
#
# Returns nothing
def add_layout(layouts, site_payload)
payload = {"page" => self.data}.merge(site_payload)
self.content = Liquid::Template.parse(self.content).render(payload, [AutoBlog::Filters])
layout = layouts[self.data["layout"]] || self.content
payload = {"content" => self.content, "page" => self.data}
self.content = Liquid::Template.parse(layout).render(payload, [AutoBlog::Filters])
end
end
end

View File

@ -4,7 +4,7 @@ module AutoBlog
include Convertible
attr_accessor :ext
attr_accessor :data, :content
attr_accessor :data, :content, :output
# Initialize a new Page.
# +base+ is the String path to the <source>
@ -38,13 +38,8 @@ module AutoBlog
#
# Returns nothing
def add_layout(layouts, site_payload)
payload = {"page" => self.data}.merge(site_payload)
self.content = Liquid::Template.parse(self.content).render(payload, [AutoBlog::Filters])
layout = layouts[self.data["layout"]] || self.content
payload = {"content" => self.content, "page" => self.data}
self.content = Liquid::Template.parse(layout).render(payload, [AutoBlog::Filters])
payload = {"page" => self.data}
do_layout(payload, layouts, site_payload)
end
# Write the generated page file to the destination directory.
@ -61,7 +56,7 @@ module AutoBlog
path = File.join(dest, @dir, name)
File.open(path, 'w') do |f|
f.write(self.content)
f.write(self.output)
end
end
end

View File

@ -86,15 +86,10 @@ module AutoBlog
#
# Returns nothing
def add_layout(layouts, site_payload)
# construct post payload
related = related_posts(site_payload["site"]["posts"])
payload = {"page" => self.data, "related_posts" => related}.merge(site_payload)
self.content = Liquid::Template.parse(self.content).render(payload, [AutoBlog::Filters])
layout = layouts[self.data["layout"]] || self.content
payload = {"content" => self.content, "page" => self.data}
self.output = Liquid::Template.parse(layout).render(payload, [AutoBlog::Filters])
payload = {"page" => self.data, "related_posts" => related}
do_layout(payload, layouts, site_payload)
end
# Write the generated post file to the destination directory.

View File

@ -40,7 +40,7 @@ module AutoBlog
entries.each do |f|
name = f.split(".")[0..-2].join(".")
self.layouts[name] = File.read(File.join(base, f))
self.layouts[name] = Layout.new(base, f)
end
rescue Errno::ENOENT => e
# ignore missing layout dir