From 288d5045d2fbb278505d120259bc29a6b6b9b74f Mon Sep 17 00:00:00 2001 From: Nick Quaranto Date: Sat, 25 Apr 2009 00:17:10 -0400 Subject: [PATCH] Changing to the template permalink system, only test_post passing so far --- lib/jekyll/post.rb | 25 +++++++++++++++++-- test/test_post.rb | 61 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 5fac0988..4d439592 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -109,13 +109,34 @@ module Jekyll self.data && self.data['permalink'] end + def template + case self.site.permalink_style + when :pretty + "/:year/:month/:day/:title" + when :none + "/:title.html" + when :date + "/:year/:month/:day/:title.html" + else + self.site.permalink_style + end + end + # The generated relative url of this post # e.g. /2008/11/05/my-awesome-post.html # # Returns def url - ext = self.site.permalink_style == :pretty ? '' : '.html' - permalink || self.id + ext + return permalink if permalink + + { + "year" => date.strftime("%Y"), + "month" => date.strftime("%m"), + "day" => date.strftime("%d"), + "title" => slug + }.inject(template) { |result, token| + result.gsub(/:#{token.first}/, token.last) + } end # The UID for this post (useful in feeds) diff --git a/test/test_post.rb b/test/test_post.rb index c4e0c9db..e6d08747 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -49,8 +49,7 @@ class TestPost < Test::Unit::TestCase assert_equal "/2008/10/19/foo-bar.html", @post.url end - - should "respect permalink" do + should "respect permalink in yaml front matter" do file = "2008-12-03-permalinked-post.textile" @post.process(file) @post.read_yaml(@source, file) @@ -60,27 +59,57 @@ class TestPost < Test::Unit::TestCase assert_equal "my_category/permalinked-post", @post.url end - context "with permalink style of none" do + + context "with site wide permalink" do setup do - @post.site.permalink_style = :none @post.categories = [] - @post.process(@fake_file) end - should "process the url correctly" do - assert_equal "/foo-bar.html", @post.url - end - end + context "with unspecified (date) style" do + setup do + @post.process(@fake_file) + end - context "with permalink style of pretty" do - setup do - @post.site.permalink_style = :pretty - @post.categories = [] - @post.process(@fake_file) + should "process the url correctly" do + assert_equal "/:year/:month/:day/:title.html", @post.template + assert_equal "/2008/10/19/foo-bar.html", @post.url + end end - should "process the url correctly" do - assert_equal "/2008/10/19/foo-bar", @post.url + context "with none style" do + setup do + @post.site.permalink_style = :none + @post.process(@fake_file) + end + + should "process the url correctly" do + assert_equal "/:title.html", @post.template + assert_equal "/foo-bar.html", @post.url + end + end + + context "with pretty style" do + setup do + @post.site.permalink_style = :pretty + @post.process(@fake_file) + end + + should "process the url correctly" do + assert_equal "/:year/:month/:day/:title", @post.template + assert_equal "/2008/10/19/foo-bar", @post.url + end + end + + context "with prefix style and no extension" do + setup do + @post.site.permalink_style = "/prefix/:title" + @post.process(@fake_file) + end + + should "process the url correctly" do + assert_equal "/prefix/:title", @post.template + assert_equal "/prefix/foo-bar", @post.url + end end end