Next and previous posts

Signed-off-by: Nick Quaranto <nick@quaran.to>
This commit is contained in:
Juan Lupion 2009-04-22 23:32:56 +02:00 committed by Nick Quaranto
parent 3c9ed56129
commit 2e187864cf
3 changed files with 61 additions and 0 deletions

View File

@ -139,3 +139,17 @@ Feature: Post data
When I run jekyll
Then the _site directory should exist
And I should see "Post author: Darth Vader" in "_site/2009/03/27/star-wars.html"
Scenario: Previous and next posts title
Given I have a _posts directory
And I have a _layouts directory
And I have the following posts:
| title | date | layout | author | content |
| Star Wars | 3/27/2009 | ordered | Darth Vader | Luke, I am your father. |
| Some like it hot | 4/27/2009 | ordered | Osgood | Nobody is perfect. |
| Terminator | 5/27/2009 | ordered | Arnold | Sayonara, baby |
And I have a ordered layout that contains "Previous post: {{ page.previous.title }} and next post: {{ page.next.title }}"
When I run jekyll
Then the _site directory should exist
And I should see "next post: Some like it hot" in "_site/2009/03/27/star-wars.html"
And I should see "Previous post: Some like it hot" in "_site/2009/05/27/terminator.html"

View File

@ -194,12 +194,33 @@ module Jekyll
"id" => self.id,
"topics" => self.topics,
"categories" => self.categories,
"next" => self.next,
"previous" => self.previous,
"content" => self.content }.deep_merge(self.data)
end
def inspect
"<Post: #{self.id}>"
end
def next
pos = self.site.posts.index(self)
if pos && pos < self.site.posts.length-1
self.site.posts[pos+1]
else
nil
end
end
def previous
pos = self.site.posts.index(self)
if pos && pos > 0
self.site.posts[pos-1]
else
nil
end
end
end
end

View File

@ -75,6 +75,32 @@ class TestPost < Test::Unit::TestCase
end
end
context "when in a site" do
setup do
clear_dest
stub(Jekyll).configuration { Jekyll::DEFAULTS }
@site = Site.new(Jekyll.configuration)
@site.posts = [setup_post('2008-02-02-published.textile'),
setup_post('2009-01-27-categories.textile')]
end
should "have next post" do
assert_equal(@site.posts.last, @site.posts.first.next)
end
should "have previous post" do
assert_equal(@site.posts.first, @site.posts.last.previous)
end
should "not have previous post if first" do
assert_equal(nil, @site.posts.first.previous)
end
should "not have next post if last" do
assert_equal(nil, @site.posts.last.next)
end
end
context "initializing posts" do
should "publish when published yaml is no specified" do
post = setup_post("2008-02-02-published.textile")