Permalink
Browse files

Getting there, prefixes work great

  • Loading branch information...
qrush committed Apr 25, 2009
1 parent 288d504 commit 9da140fcb2277c6201e50980a8a6f774dac4cf29
Showing with 65 additions and 33 deletions.
  1. +3 −3 features/permalinks.feature
  2. +1 −1 features/support/env.rb
  3. +19 −24 lib/jekyll/post.rb
  4. +42 −5 test/test_post.rb
@@ -31,7 +31,7 @@ Feature: Fancy permalinks
And I have a configuration file with "permalink" set to "blog/:year/:month/:day/:title"
When I run jekyll
Then the _site directory should exist
- And I should see "Totally custom." in "_site/posts/2009/03/27/custom-permalink-scheme/index.html"
+ And I should see "Totally custom." in "_site/blog/2009/03/27/custom-permalink-schema/index.html"
Scenario: Use custom permalink schema with category
Given I have a _posts directory
@@ -41,7 +41,7 @@ Feature: Fancy permalinks
And I have a configuration file with "permalink" set to ":category/:title.html"
When I run jekyll
Then the _site directory should exist
- And I should see "Totally custom." in "_site/stuff/custom-permalink-scheme.html"
+ And I should see "Totally custom." in "_site/stuff/custom-permalink-schema.html"
Scenario: Use custom permalink schema with squished date
Given I have a _posts directory
@@ -51,4 +51,4 @@ Feature: Fancy permalinks
And I have a configuration file with "permalink" set to ":month-:day-:year/:title.html"
When I run jekyll
Then the _site directory should exist
- And I should see "Totally custom." in "_site/03-27-2009/custom-permalink-scheme.html"
+ And I should see "Totally custom." in "_site/03-27-2009/custom-permalink-schema.html"
View
@@ -14,5 +14,5 @@ def run_jekyll(opts = {})
bg = '&'
end
- system "#{JEKYLL_PATH} >> /dev/null"
+ system "#{JEKYLL_PATH}" #>> /dev/null"
end
View
@@ -18,9 +18,12 @@ def self.valid?(name)
name =~ MATCHER
end
- attr_accessor :site
- attr_accessor :date, :slug, :ext, :categories, :topics, :published
- attr_accessor :data, :content, :output
+ attr_accessor :site, :date, :slug, :ext, :topics, :published, :data, :content, :output
+ attr_writer :categories
+
+ def categories
+ @categories ||= []
+ end
# Initialize this Post instance.
# +site+ is the Site
@@ -88,16 +91,7 @@ def process(name)
#
# Returns <String>
def dir
- if permalink
- permalink.to_s.split("/")[0..-2].join("/") + '/'
- else
- prefix = self.categories.empty? ? '' : '/' + self.categories.join('/')
- if [:date, :pretty].include?(self.site.permalink_style)
- prefix + date.strftime("/%Y/%m/%d/")
- else
- prefix + '/'
- end
- end
+ File.dirname(url)
end
# The full path and filename of the post.
@@ -112,13 +106,13 @@ def permalink
def template
case self.site.permalink_style
when :pretty
- "/:year/:month/:day/:title"
+ "/:categories/:year/:month/:day/:title"
when :none
- "/:title.html"
+ "/:categories/:title.html"
when :date
- "/:year/:month/:day/:title.html"
+ "/:categories/:year/:month/:day/:title.html"
else
- self.site.permalink_style
+ self.site.permalink_style.to_s
end
end
@@ -130,21 +124,22 @@ def url
return permalink if permalink
{
- "year" => date.strftime("%Y"),
- "month" => date.strftime("%m"),
- "day" => date.strftime("%d"),
- "title" => slug
+ "year" => date.strftime("%Y"),
+ "month" => date.strftime("%m"),
+ "day" => date.strftime("%d"),
+ "title" => slug,
+ "categories" => categories.sort.join('/')
}.inject(template) { |result, token|
result.gsub(/:#{token.first}/, token.last)
- }
+ }.gsub(/\/\//, "/")
end
# The UID for this post (useful in feeds)
# e.g. /2008/11/05/my-awesome-post
#
# Returns <String>
def id
- self.dir + self.slug
+ File.join(self.dir, self.slug)
end
# Calculate related posts.
@@ -195,7 +190,7 @@ def write(dest)
path = File.join(dest, self.url)
- if self.site.permalink_style == :pretty
+ if template[/\.html$/].nil?
FileUtils.mkdir_p(path)
path = File.join(path, "index.html")
end
View
@@ -25,6 +25,7 @@ def do_render(post)
assert !Post.valid?("blah")
end
+
context "processing posts" do
setup do
@post = Post.allocate
@@ -41,6 +42,8 @@ def do_render(post)
assert_equal Time.parse("2008-10-19"), @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
end
should "create url based on date and title" do
@@ -55,11 +58,10 @@ def do_render(post)
@post.read_yaml(@source, file)
assert_equal "my_category/permalinked-post", @post.permalink
- assert_equal "my_category/", @post.dir
+ assert_equal "my_category", @post.dir
assert_equal "my_category/permalinked-post", @post.url
end
-
context "with site wide permalink" do
setup do
@post.categories = []
@@ -71,19 +73,44 @@ def do_render(post)
end
should "process the url correctly" do
- assert_equal "/:year/:month/:day/:title.html", @post.template
+ assert_equal "/:categories/:year/:month/:day/:title.html", @post.template
assert_equal "/2008/10/19/foo-bar.html", @post.url
end
end
+ context "with unspecified (date) style and a category" do
+ setup do
+ @post.categories << "beer"
+ @post.process(@fake_file)
+ end
+
+ 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
+ end
+ end
+
+ context "with unspecified (date) style and categories" do
+ setup do
+ @post.categories << "food"
+ @post.categories << "beer"
+ @post.process(@fake_file)
+ end
+
+ should "process the url correctly" do
+ assert_equal "/:categories/:year/:month/:day/:title.html", @post.template
+ assert_equal "/beer/food/2008/10/19/foo-bar.html", @post.url
+ end
+ end
+
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 "/:categories/:title.html", @post.template
assert_equal "/foo-bar.html", @post.url
end
end
@@ -95,7 +122,7 @@ def do_render(post)
end
should "process the url correctly" do
- assert_equal "/:year/:month/:day/:title", @post.template
+ assert_equal "/:categories/:year/:month/:day/:title", @post.template
assert_equal "/2008/10/19/foo-bar", @post.url
end
end
@@ -198,6 +225,16 @@ def do_render(post)
assert File.exists?(File.join(dest_dir, '2008', '10', '18', 'foo-bar.html'))
end
+ should "write properly without html extension" do
+ post = setup_post("2008-10-18-foo-bar.textile")
+ post.site.permalink_style = ":title"
+ do_render(post)
+ post.write(dest_dir)
+
+ assert File.directory?(dest_dir)
+ assert File.exists?(File.join(dest_dir, 'foo-bar', 'index.html'))
+ end
+
should "insert data" do
post = setup_post("2008-11-21-complex.textile")
do_render(post)

0 comments on commit 9da140f

Please sign in to comment.