diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 2ca23ffe..e570baaf 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -55,13 +55,27 @@ module Jekyll end # The generated directory into which the post will be placed - # upon generation. e.g. "/2008/11/05/" + # upon generation. This is derived from the permalink or, if + # permalink is absent, set to the default date + # e.g. "/2008/11/05/" # # Returns def dir - self.date.strftime("/%Y/%m/%d/") + permalink ? + permalink.to_s.split("/")[0..-2].join("/") : + date.strftime("/%Y/%m/%d/") end + # The full path and filename of the post. + # Defined in the YAML of the post body + # (Optional) + # + # Returns + def permalink + self.data && self.data['permalink'] + end + + # The generated relative url of this post # e.g. /2008/11/05/my-awesome-post.html # @@ -111,7 +125,7 @@ module Jekyll # # Returns nothing def write(dest) - FileUtils.mkdir_p(File.join(dest, self.dir)) + FileUtils.mkdir_p(File.join(dest, dir)) path = File.join(dest, self.url) File.open(path, 'w') do |f| diff --git a/test/source/posts/2008-12-03-permalinked-post.textile b/test/source/posts/2008-12-03-permalinked-post.textile new file mode 100644 index 00000000..88a89ada --- /dev/null +++ b/test/source/posts/2008-12-03-permalinked-post.textile @@ -0,0 +1,9 @@ +--- +title: Post with Permalink +permalink: my_category/permalinked-post +--- + +h1. {{ page.title }} + + +

Best post ever

\ No newline at end of file diff --git a/test/test_post.rb b/test/test_post.rb index 76c8e649..2424939d 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -26,6 +26,22 @@ class TestPost < Test::Unit::TestCase assert_equal "/2008/10/19/foo-bar.html", p.url end + def test_permalink + p = Post.allocate + p.process("2008-12-03-permalinked-post.textile") + p.read_yaml(File.join(File.dirname(__FILE__), *%w[source posts]), "2008-12-03-permalinked-post.textile") + + assert_equal "my_category/permalinked-post", p.permalink + end + + def test_dir_respects_permalink + p = Post.allocate + p.process("2008-12-03-permalinked-post.textile") + p.read_yaml(File.join(File.dirname(__FILE__), *%w[source posts]), "2008-12-03-permalinked-post.textile") + + assert_equal "my_category", p.dir + end + def test_read_yaml p = Post.allocate p.read_yaml(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-10-18-foo-bar.textile")