Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge commit 'qrush/permalinks'

  • Loading branch information...
commit ccbd2c39a1f2d90a78ef13f17998178a4e4bbcbd 2 parents 1324535 + 080108b
Nick Quaranto qrush authored
54 features/permalinks.feature
View
@@ -0,0 +1,54 @@
+Feature: Fancy permalinks
+ As a hacker who likes to blog
+ I want to be able to set permalinks
+ In order to make my blog URLs awesome
+
+ Scenario: Use none permalink schema
+ Given I have a _posts directory
+ And I have the following post:
+ | title | date | content |
+ | None Permalink Schema | 3/27/2009 | Totally nothing. |
+ And I have a configuration file with "permalink" set to "none"
+ When I run jekyll
+ Then the _site directory should exist
+ And I should see "Totally nothing." in "_site/none-permalink-schema.html"
+
+ Scenario: Use pretty permalink schema
+ Given I have a _posts directory
+ And I have the following post:
+ | title | date | content |
+ | Pretty Permalink Schema | 3/27/2009 | Totally wordpress. |
+ And I have a configuration file with "permalink" set to "pretty"
+ When I run jekyll
+ Then the _site directory should exist
+ And I should see "Totally wordpress." in "_site/2009/03/27/pretty-permalink-schema/index.html"
+
+ Scenario: Use custom permalink schema with prefix
+ Given I have a _posts directory
+ And I have the following post:
+ | title | category | date | content |
+ | Custom Permalink Schema | stuff | 3/27/2009 | Totally custom. |
+ 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/blog/2009/03/27/custom-permalink-schema/index.html"
+
+ Scenario: Use custom permalink schema with category
+ Given I have a _posts directory
+ And I have the following post:
+ | title | category | date | content |
+ | Custom Permalink Schema | stuff | 3/27/2009 | Totally custom. |
+ And I have a configuration file with "permalink" set to "/:categories/:title.html"
+ When I run jekyll
+ Then the _site directory should exist
+ 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
+ And I have the following post:
+ | title | category | date | content |
+ | Custom Permalink Schema | stuff | 3/27/2009 | Totally custom. |
+ 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-schema.html"
20 features/site_configuration.feature
View
@@ -32,26 +32,6 @@ Feature: Site configuration
Then the _site directory should exist
And I should see "<a href='http://google.com'>Google</a>" in "_site/index.html"
- Scenario: Use none permalink schema
- Given I have a _posts directory
- And I have the following post:
- | title | date | content |
- | None Permalink Schema | 3/27/2009 | Totally nothing. |
- And I have a configuration file with "permalink" set to "none"
- When I run jekyll
- Then the _site directory should exist
- And I should see "Totally nothing." in "_site/none-permalink-schema.html"
-
- Scenario: Use pretty permalink schema
- Given I have a _posts directory
- And I have the following post:
- | title | date | content |
- | Pretty Permalink Schema | 3/27/2009 | Totally wordpress. |
- And I have a configuration file with "permalink" set to "pretty"
- When I run jekyll
- Then the _site directory should exist
- And I should see "Totally wordpress." in "_site/2009/03/27/pretty-permalink-schema/index.html"
-
Scenario: Highlight code with pygments
Given I have an "index.html" file that contains "{% highlight ruby %} puts 'Hello world!' {% endhighlight %}"
And I have a configuration file with "pygments" set to "true"
50 lib/jekyll/post.rb
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.
@@ -109,13 +103,35 @@ def permalink
self.data && self.data['permalink']
end
+ def template
+ case self.site.permalink_style
+ when :pretty
+ "/:categories/:year/:month/:day/:title"
+ when :none
+ "/:categories/:title.html"
+ when :date
+ "/:categories/:year/:month/:day/:title.html"
+ else
+ self.site.permalink_style.to_s
+ end
+ end
+
# The generated relative url of this post
# e.g. /2008/11/05/my-awesome-post.html
#
# Returns <String>
def url
- ext = self.site.permalink_style == :pretty ? '' : '.html'
- permalink || self.id + ext
+ return permalink if permalink
+
+ @url ||= {
+ "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)
@@ -123,7 +139,7 @@ def url
#
# Returns <String>
def id
- self.dir + self.slug
+ File.join(self.dir, self.slug)
end
# Calculate related posts.
@@ -174,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
95 test/test_post.rb
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
@@ -49,16 +52,94 @@ def do_render(post)
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)
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 = []
+ end
+
+ context "with unspecified (date) style" do
+ setup do
+ @post.process(@fake_file)
+ end
+
+ 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
+ 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 "/:categories/: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 "/:categories/: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
+
should "read yaml front-matter" do
@post.read_yaml(@source, @real_file)
@@ -144,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)
Please sign in to comment.
Something went wrong with that request. Please try again.