From e8d119ef0a38024a7188e4a96b8d23fbf8574ecb Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 31 May 2010 23:28:39 -0700 Subject: [PATCH] add :i_day and :i_month permalink values so you can get urls like /2010/6/1/title --- lib/jekyll/post.rb | 4 +++- test/test_post.rb | 35 +++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 1e7238b4..275f3c23 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -124,9 +124,11 @@ module Jekyll "month" => date.strftime("%m"), "day" => date.strftime("%d"), "title" => CGI.escape(slug), + "i_day" => date.strftime("%d").to_i.to_s, + "i_month" => date.strftime("%m").to_i.to_s, "categories" => categories.join('/') }.inject(template) { |result, token| - result.gsub(/:#{token.first}/, token.last) + result.gsub(/:#{Regexp.escape token.first}/, token.last) }.gsub(/\/\//, "/") end diff --git a/test/test_post.rb b/test/test_post.rb index f57f530d..b7d1b305 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -18,10 +18,10 @@ class TestPost < Test::Unit::TestCase end should "ensure valid posts are valid" do - assert Post.valid?("2008-10-19-foo-bar.textile") - assert Post.valid?("foo/bar/2008-10-19-foo-bar.textile") + assert Post.valid?("2008-09-09-foo-bar.textile") + assert Post.valid?("foo/bar/2008-09-09-foo-bar.textile") - assert !Post.valid?("lol2008-10-19-foo-bar.textile") + assert !Post.valid?("lol2008-09-09-foo-bar.textile") assert !Post.valid?("blah") end @@ -31,7 +31,7 @@ class TestPost < Test::Unit::TestCase @post.site = @site @real_file = "2008-10-18-foo-bar.textile" - @fake_file = "2008-10-19-foo-bar.textile" + @fake_file = "2008-09-09-foo-bar.textile" @source = source_dir('_posts') end @@ -39,17 +39,17 @@ class TestPost < Test::Unit::TestCase @post.categories = [] @post.process(@fake_file) - assert_equal Time.parse("2008-10-19"), @post.date + assert_equal Time.parse("2008-09-09"), @post.date assert_equal "foo-bar", @post.slug assert_equal ".textile", @post.ext - assert_equal "/2008/10/19", @post.dir - assert_equal "/2008/10/19/foo-bar", @post.id + assert_equal "/2008/09/09", @post.dir + assert_equal "/2008/09/09/foo-bar", @post.id end should "create url based on date and title" do @post.categories = [] @post.process(@fake_file) - assert_equal "/2008/10/19/foo-bar.html", @post.url + assert_equal "/2008/09/09/foo-bar.html", @post.url end should "CGI escape urls" do @@ -106,7 +106,7 @@ class TestPost < Test::Unit::TestCase should "process the url correctly" do assert_equal "/:categories/:year/:month/:day/:title.html", @post.template - assert_equal "/2008/10/19/foo-bar.html", @post.url + assert_equal "/2008/09/09/foo-bar.html", @post.url end end @@ -118,7 +118,7 @@ class TestPost < Test::Unit::TestCase should "process the url correctly" do assert_equal "/:categories/:year/:month/:day/:title.html", @post.template - assert_equal "/beer/2008/10/19/foo-bar.html", @post.url + assert_equal "/beer/2008/09/09/foo-bar.html", @post.url end end @@ -131,7 +131,7 @@ class TestPost < Test::Unit::TestCase should "process the url correctly" do assert_equal "/:categories/:year/:month/:day/:title.html", @post.template - assert_equal "/food/beer/2008/10/19/foo-bar.html", @post.url + assert_equal "/food/beer/2008/09/09/foo-bar.html", @post.url end end @@ -155,7 +155,18 @@ class TestPost < Test::Unit::TestCase should "process the url correctly" do assert_equal "/:categories/:year/:month/:day/:title/", @post.template - assert_equal "/2008/10/19/foo-bar/", @post.url + assert_equal "/2008/09/09/foo-bar/", @post.url + end + end + + context "with custom date permalink" do + setup do + @post.site.permalink_style = '/:categories/:year/:i_month/:i_day/:title/' + @post.process(@fake_file) + end + + should "process the url correctly" do + assert_equal "/2008/9/9/foo-bar/", @post.url end end