parse out yaml from posts

This commit is contained in:
Tom Preston-Werner 2008-10-19 21:18:58 -07:00
parent bb0fb49aee
commit ecda27488c
3 changed files with 26 additions and 0 deletions

View File

@ -8,12 +8,14 @@ module AutoBlog
end
attr_accessor :date, :slug, :ext
attr_accessor :data, :contents
def initialize(base, name)
@base = base
@name = name
self.process(name)
self.read_yaml(base, name)
end
def process(name)
@ -26,6 +28,16 @@ module AutoBlog
def url
self.date.strftime("/%Y/%m/%d/") + self.slug
end
def read_yaml(base, name)
self.contents = File.read(File.join(base, name))
if self.contents =~ /^(---\n.*?)\n---\n/
self.contents = self.contents[($1.size + 5)..-1]
self.data = YAML.load($1)
end
end
end
end

View File

@ -42,6 +42,12 @@ module AutoBlog
rescue Errno::ENOENT => e
# ignore missing layout dir
end
def write_posts
self.posts.each do |post|
end
end
end
end

View File

@ -25,4 +25,12 @@ class TestPost < Test::Unit::TestCase
assert_equal "/2008/10/19/foo-bar", p.url
end
def test_read_yaml
p = Post.allocate
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
end
end