diff --git a/lib/autoblog.rb b/lib/autoblog.rb index b573b96e..7a16dc0e 100644 --- a/lib/autoblog.rb +++ b/lib/autoblog.rb @@ -5,6 +5,7 @@ require 'rubygems' # core require 'fileutils' +require 'time' # stdlib diff --git a/lib/autoblog/post.rb b/lib/autoblog/post.rb index d6535503..4b494abf 100644 --- a/lib/autoblog/post.rb +++ b/lib/autoblog/post.rb @@ -1,5 +1,27 @@ module AutoBlog + class Post + MATCHER = /^(\d+-\d+-\d+)-(.*)\.([^.]+)$/ + def self.valid?(name) + name =~ MATCHER + end + + attr_accessor :date, :slug, :ext + + def initialize(base, name) + @base = base + @name = name + + self.process(name) + end + + def process(name) + m, date, slug, ext = *name.match(MATCHER) + self.date = Time.parse(date) + self.slug = slug + self.ext = ext + end end + end \ No newline at end of file diff --git a/lib/autoblog/site.rb b/lib/autoblog/site.rb index 3a788d68..c42bf5b3 100644 --- a/lib/autoblog/site.rb +++ b/lib/autoblog/site.rb @@ -2,16 +2,18 @@ module AutoBlog class Site attr_accessor :source, :dest - attr_accessor :layouts + attr_accessor :layouts, :posts def initialize(source, dest) self.source = source self.dest = dest self.layouts = {} + self.posts = [] end def process self.read_layouts + self.read_posts end def read_layouts @@ -26,6 +28,20 @@ module AutoBlog rescue Errno::ENOENT => e # ignore missing layout dir end + + def read_posts + base = File.join(self.source, "posts") + entries = Dir.entries(base) + entries = entries.reject { |e| File.directory?(e) } + + entries.each do |f| + self.posts << Post.new(base, f) if Post.valid?(f) + end + + self.posts.sort! + rescue Errno::ENOENT => e + # ignore missing layout dir + end end end \ No newline at end of file diff --git a/test/test_post.rb b/test/test_post.rb new file mode 100644 index 00000000..7fdd24a0 --- /dev/null +++ b/test/test_post.rb @@ -0,0 +1,21 @@ +require File.dirname(__FILE__) + '/helper' + +class TestPost < Test::Unit::TestCase + def setup + + end + + def test_valid + assert Post.valid?("2008-10-19-foo-bar.textile") + assert !Post.valid?("blah") + end + + def test_site_init + p = Post.allocate + p.process("2008-10-19-foo-bar.textile") + + assert_equal Time.parse("2008-10-19"), p.date + assert_equal "foo-bar", p.slug + assert_equal "textile", p.ext + end +end \ No newline at end of file diff --git a/test/test_site.rb b/test/test_site.rb index 8984728b..119317ea 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -15,4 +15,10 @@ class TestSite < Test::Unit::TestCase assert_equal ["default"], @s.layouts.keys end + + def test_read_posts + @s.read_posts + + assert_equal 1, @s.posts.size + end end \ No newline at end of file