wrap in layout

This commit is contained in:
Tom Preston-Werner 2008-10-19 21:44:27 -07:00
parent ecda27488c
commit 84b00cc63e
4 changed files with 32 additions and 6 deletions

View File

@ -9,6 +9,9 @@ require 'time'
# stdlib
# 3rd party
require 'liquid'
# internal requires
require 'autoblog/site'
require 'autoblog/post'

View File

@ -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

View File

@ -45,7 +45,7 @@ module AutoBlog
def write_posts
self.posts.each do |post|
post.add_layout(self.layouts)
end
end
end

View File

@ -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