allow nested layouts
This commit is contained in:
parent
6980797d38
commit
af673529cb
|
@ -16,6 +16,7 @@ require 'redcloth'
|
||||||
# internal requires
|
# internal requires
|
||||||
require 'autoblog/site'
|
require 'autoblog/site'
|
||||||
require 'autoblog/convertible'
|
require 'autoblog/convertible'
|
||||||
|
require 'autoblog/layout'
|
||||||
require 'autoblog/page'
|
require 'autoblog/page'
|
||||||
require 'autoblog/post'
|
require 'autoblog/post'
|
||||||
require 'autoblog/filters'
|
require 'autoblog/filters'
|
||||||
|
|
|
@ -24,5 +24,30 @@ module AutoBlog
|
||||||
self.content = RedCloth.new(self.content).to_html
|
self.content = RedCloth.new(self.content).to_html
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
|
@ -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
|
|
@ -4,7 +4,7 @@ module AutoBlog
|
||||||
include Convertible
|
include Convertible
|
||||||
|
|
||||||
attr_accessor :ext
|
attr_accessor :ext
|
||||||
attr_accessor :data, :content
|
attr_accessor :data, :content, :output
|
||||||
|
|
||||||
# Initialize a new Page.
|
# Initialize a new Page.
|
||||||
# +base+ is the String path to the <source>
|
# +base+ is the String path to the <source>
|
||||||
|
@ -38,13 +38,8 @@ module AutoBlog
|
||||||
#
|
#
|
||||||
# Returns nothing
|
# Returns nothing
|
||||||
def add_layout(layouts, site_payload)
|
def add_layout(layouts, site_payload)
|
||||||
payload = {"page" => self.data}.merge(site_payload)
|
payload = {"page" => self.data}
|
||||||
self.content = Liquid::Template.parse(self.content).render(payload, [AutoBlog::Filters])
|
do_layout(payload, layouts, site_payload)
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
# Write the generated page file to the destination directory.
|
# Write the generated page file to the destination directory.
|
||||||
|
@ -61,7 +56,7 @@ module AutoBlog
|
||||||
|
|
||||||
path = File.join(dest, @dir, name)
|
path = File.join(dest, @dir, name)
|
||||||
File.open(path, 'w') do |f|
|
File.open(path, 'w') do |f|
|
||||||
f.write(self.content)
|
f.write(self.output)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -86,15 +86,10 @@ module AutoBlog
|
||||||
#
|
#
|
||||||
# Returns nothing
|
# Returns nothing
|
||||||
def add_layout(layouts, site_payload)
|
def add_layout(layouts, site_payload)
|
||||||
|
# construct post payload
|
||||||
related = related_posts(site_payload["site"]["posts"])
|
related = related_posts(site_payload["site"]["posts"])
|
||||||
|
payload = {"page" => self.data, "related_posts" => related}
|
||||||
payload = {"page" => self.data, "related_posts" => related}.merge(site_payload)
|
do_layout(payload, layouts, 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])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Write the generated post file to the destination directory.
|
# Write the generated post file to the destination directory.
|
||||||
|
|
|
@ -40,7 +40,7 @@ module AutoBlog
|
||||||
|
|
||||||
entries.each do |f|
|
entries.each do |f|
|
||||||
name = f.split(".")[0..-2].join(".")
|
name = f.split(".")[0..-2].join(".")
|
||||||
self.layouts[name] = File.read(File.join(base, f))
|
self.layouts[name] = Layout.new(base, f)
|
||||||
end
|
end
|
||||||
rescue Errno::ENOENT => e
|
rescue Errno::ENOENT => e
|
||||||
# ignore missing layout dir
|
# ignore missing layout dir
|
||||||
|
|
Loading…
Reference in New Issue