Test and fix the site.categories hash
This commit is contained in:
parent
340dd68ee5
commit
bac34fab3a
|
@ -2,7 +2,7 @@ module Jekyll
|
||||||
|
|
||||||
class Site
|
class Site
|
||||||
attr_accessor :source, :dest
|
attr_accessor :source, :dest
|
||||||
attr_accessor :layouts, :posts
|
attr_accessor :layouts, :posts, :categories
|
||||||
|
|
||||||
# Initialize the site
|
# Initialize the site
|
||||||
# +source+ is String path to the source directory containing
|
# +source+ is String path to the source directory containing
|
||||||
|
@ -16,6 +16,7 @@ module Jekyll
|
||||||
self.dest = dest
|
self.dest = dest
|
||||||
self.layouts = {}
|
self.layouts = {}
|
||||||
self.posts = []
|
self.posts = []
|
||||||
|
self.categories = Hash.new { |hash, key| hash[key] = Array.new }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Do the actual work of processing the site and generating the
|
# Do the actual work of processing the site and generating the
|
||||||
|
@ -63,6 +64,7 @@ module Jekyll
|
||||||
if Post.valid?(f)
|
if Post.valid?(f)
|
||||||
post = Post.new(self.source, dir, f)
|
post = Post.new(self.source, dir, f)
|
||||||
self.posts << post
|
self.posts << post
|
||||||
|
post.categories.each { |c| self.categories[c] << post }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -72,6 +74,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
self.posts.sort!
|
self.posts.sort!
|
||||||
|
self.categories.values.map { |cats| cats.sort! { |a, b| b <=> a} }
|
||||||
rescue Errno::ENOENT => e
|
rescue Errno::ENOENT => e
|
||||||
# ignore missing layout dir
|
# ignore missing layout dir
|
||||||
end
|
end
|
||||||
|
@ -133,12 +136,6 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns {"site" => {"time" => <Time>, "posts" => [<Post>]}}
|
# Returns {"site" => {"time" => <Time>, "posts" => [<Post>]}}
|
||||||
def site_payload
|
def site_payload
|
||||||
# Build the category hash map of category ( names => arrays of posts )
|
|
||||||
# then sort each array in reverse order
|
|
||||||
categories = Hash.new { |hash, key| hash[key] = Array.new }
|
|
||||||
self.posts.each { |p| p.categories.each { |c| categories[c] << p } }
|
|
||||||
categories.values.map { |cats| cats.sort! { |a, b| b <=> a} }
|
|
||||||
|
|
||||||
{"site" => {
|
{"site" => {
|
||||||
"time" => Time.now,
|
"time" => Time.now,
|
||||||
"posts" => self.posts.sort { |a,b| b <=> a },
|
"posts" => self.posts.sort { |a,b| b <=> a },
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
---
|
||||||
|
layout: default
|
||||||
|
title: Topical Post
|
||||||
|
---
|
||||||
|
|
||||||
|
h1. {{ page.title }}
|
||||||
|
|
||||||
|
This post has a topic.
|
|
@ -88,6 +88,12 @@ class TestPost < Test::Unit::TestCase
|
||||||
assert_equal "<<< <p>url: /2008/11/21/complex.html<br />\ndate: #{Time.parse("2008-11-21")}<br />\nid: /2008/11/21/complex</p> >>>", p.output
|
assert_equal "<<< <p>url: /2008/11/21/complex.html<br />\ndate: #{Time.parse("2008-11-21")}<br />\nid: /2008/11/21/complex</p> >>>", p.output
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_categories_and_topics
|
||||||
|
p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), 'foo', 'bar/2008-12-12-topical-post.textile')
|
||||||
|
assert_equal ['foo'], p.categories
|
||||||
|
assert_equal ['bar'], p.topics
|
||||||
|
end
|
||||||
|
|
||||||
def test_include
|
def test_include
|
||||||
Jekyll.source = File.join(File.dirname(__FILE__), *%w[source])
|
Jekyll.source = File.join(File.dirname(__FILE__), *%w[source])
|
||||||
p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-12-13-include.markdown")
|
p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-12-13-include.markdown")
|
||||||
|
|
|
@ -22,9 +22,12 @@ class TestSite < Test::Unit::TestCase
|
||||||
assert_equal 4, @s.posts.size
|
assert_equal 4, @s.posts.size
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_write_posts
|
def test_site_payload
|
||||||
clear_dest
|
clear_dest
|
||||||
|
|
||||||
@s.process
|
@s.process
|
||||||
|
|
||||||
|
assert_equal 5, @s.posts.length
|
||||||
|
assert_equal ['foo'], @s.categories.keys
|
||||||
|
assert_equal 1, @s.categories['foo'].length
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue