mirror of
https://github.com/jekyll/jekyll.git
synced 2026-01-30 09:18:11 -05:00
read posts
This commit is contained in:
@@ -5,6 +5,7 @@ require 'rubygems'
|
||||
|
||||
# core
|
||||
require 'fileutils'
|
||||
require 'time'
|
||||
|
||||
# stdlib
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
21
test/test_post.rb
Normal file
21
test/test_post.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user