wrap in layout
This commit is contained in:
parent
ecda27488c
commit
84b00cc63e
|
@ -9,6 +9,9 @@ require 'time'
|
|||
|
||||
# stdlib
|
||||
|
||||
# 3rd party
|
||||
require 'liquid'
|
||||
|
||||
# internal requires
|
||||
require 'autoblog/site'
|
||||
require 'autoblog/post'
|
||||
|
|
|
@ -8,7 +8,7 @@ module AutoBlog
|
|||
end
|
||||
|
||||
attr_accessor :date, :slug, :ext
|
||||
attr_accessor :data, :contents
|
||||
attr_accessor :data, :content
|
||||
|
||||
def initialize(base, name)
|
||||
@base = base
|
||||
|
@ -16,6 +16,7 @@ module AutoBlog
|
|||
|
||||
self.process(name)
|
||||
self.read_yaml(base, name)
|
||||
self.set_defaults
|
||||
end
|
||||
|
||||
def process(name)
|
||||
|
@ -30,14 +31,28 @@ module AutoBlog
|
|||
end
|
||||
|
||||
def read_yaml(base, name)
|
||||
self.contents = File.read(File.join(base, name))
|
||||
self.content = File.read(File.join(base, name))
|
||||
|
||||
if self.contents =~ /^(---\n.*?)\n---\n/
|
||||
self.contents = self.contents[($1.size + 5)..-1]
|
||||
if self.content =~ /^(---\n.*?)\n---\n/
|
||||
self.content = self.content[($1.size + 5)..-1]
|
||||
|
||||
self.data = YAML.load($1)
|
||||
end
|
||||
end
|
||||
|
||||
def set_defaults
|
||||
self.data["layout"] ||= "default"
|
||||
end
|
||||
|
||||
def add_layout(layouts)
|
||||
payload = {"page" => self.data}
|
||||
self.content = Liquid::Template.parse(self.content).render(payload)
|
||||
|
||||
layout = layouts[self.data["layout"]] || self.content
|
||||
payload = {"content" => self.content, "page" => self.data}
|
||||
|
||||
self.content = Liquid::Template.parse(layout).render(payload)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -45,7 +45,7 @@ module AutoBlog
|
|||
|
||||
def write_posts
|
||||
self.posts.each do |post|
|
||||
|
||||
post.add_layout(self.layouts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,6 +31,14 @@ class TestPost < Test::Unit::TestCase
|
|||
p.read_yaml(File.join(File.dirname(__FILE__), *%w[source posts]), "2008-10-18-foo-bar.textile")
|
||||
|
||||
assert_equal({"title" => "Foo Bar"}, p.data)
|
||||
assert_equal "\nh1. {{ page.title }}\n\nBest post ever", p.contents
|
||||
assert_equal "\nh1. {{ page.title }}\n\nBest post ever", p.content
|
||||
end
|
||||
|
||||
def test_add_layout
|
||||
p = Post.new(File.join(File.dirname(__FILE__), *%w[source posts]), "2008-10-18-foo-bar.textile")
|
||||
layouts = {"default" => "<<< {{ content }} >>>"}
|
||||
p.add_layout(layouts)
|
||||
|
||||
assert_equal "<<< \nh1. Foo Bar\n\nBest post ever >>>", p.content
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue