Made pages respect permalinks style and permalinks in yml front matter

This commit is contained in:
eugenebolshakov 2009-05-08 10:40:11 +04:00
parent 605adf88d5
commit ee0167d706
5 changed files with 139 additions and 7 deletions

View File

@ -4,7 +4,7 @@ module Jekyll
include Convertible include Convertible
attr_accessor :site attr_accessor :site
attr_accessor :ext attr_accessor :name, :ext
attr_accessor :data, :content, :output attr_accessor :data, :content, :output
# Initialize a new Page. # Initialize a new Page.
@ -17,7 +17,7 @@ module Jekyll
def initialize(site, base, dir, name) def initialize(site, base, dir, name)
@site = site @site = site
@base = base @base = base
@dir = dir @dir = dir
@name = name @name = name
self.data = {} self.data = {}
@ -27,6 +27,42 @@ module Jekyll
#self.transform #self.transform
end end
# The generated directory into which the page will be placed
# upon generation. This is derived from the permalink or, if
# permalink is absent, set to '/'
#
# Returns <String>
def dir
url[-1, 1] == '/' ? url : File.dirname(url)
end
# The full path and filename of the post.
# Defined in the YAML of the post body
# (Optional)
#
# Returns <String>
def permalink
self.data && self.data['permalink']
end
def template
if self.site.permalink_style == :pretty
"/:name/"
else
"/:name.html"
end
end
# The generated relative url of this page
# e.g. /about.html
#
# Returns <String>
def url
return permalink if permalink
@url ||= template.gsub(':name', name.split('.')[0..-2].first)
end
# Extract information from the page filename # Extract information from the page filename
# +name+ is the String filename of the page file # +name+ is the String filename of the page file
# #
@ -55,16 +91,17 @@ module Jekyll
dest = File.join(dest, dest_suffix) if dest_suffix dest = File.join(dest, dest_suffix) if dest_suffix
FileUtils.mkdir_p(dest) FileUtils.mkdir_p(dest)
name = @name # The url needs to be unescaped in order to preserve the correct filename
if self.ext != "" path = File.join(dest, CGI.unescape(self.url))
name = @name.split(".")[0..-2].join('.') + self.ext if self.url[/\.html$/].nil?
FileUtils.mkdir_p(path)
path = File.join(path, "index.html")
end end
path = File.join(dest, name)
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write(self.output) f.write(self.output)
end end
end end
end end
end end

6
test/source/about.html Normal file
View File

@ -0,0 +1,6 @@
---
title: About
permalink: /about/
---
About the site

View File

@ -0,0 +1,5 @@
---
title: Contact Information
---
Contact Information

View File

@ -34,5 +34,10 @@ class TestGeneratedSite < Test::Unit::TestCase
should "not copy _posts directory" do should "not copy _posts directory" do
assert !File.exist?(dest_dir('_posts')) assert !File.exist?(dest_dir('_posts'))
end end
should "process other static files and generate correct permalinks" do
assert File.exists?(dest_dir('/about/index.html'))
assert File.exists?(dest_dir('/contacts.html'))
end
end end
end end

79
test/test_page.rb Normal file
View File

@ -0,0 +1,79 @@
require File.dirname(__FILE__) + '/helper'
class TestPage < Test::Unit::TestCase
def setup_page(file)
@page = Page.new(@site, source_dir, '', file)
end
def do_render(page)
layouts = { "default" => Layout.new(@site, source_dir('_layouts'), "simple.html")}
page.render(layouts, {"site" => {"posts" => []}})
end
context "A Page" do
setup do
clear_dest
stub(Jekyll).configuration { Jekyll::DEFAULTS }
@site = Site.new(Jekyll.configuration)
end
context "processing pages" do
should "create url based on filename" do
@page = setup_page('contacts.html')
assert_equal "/contacts.html", @page.url
end
context "with pretty url style" do
should "return dir correctly" do
@site.permalink_style = :pretty
@page = setup_page('contacts.html')
assert_equal '/contacts/', @page.dir
end
end
context "with any other url style" do
should "return dir correctly" do
@site.permalink_style = nil
@page = setup_page('contacts.html')
assert_equal '/', @page.dir
end
end
should "respect permalink in yaml front matter" do
file = "about.html"
@page = setup_page(file)
assert_equal "/about/", @page.permalink
assert_equal @page.permalink, @page.url
assert_equal "/about/", @page.dir
end
end
context "rendering" do
setup do
clear_dest
end
should "write properly" do
page = setup_page('contacts.html')
do_render(page)
page.write(dest_dir)
assert File.directory?(dest_dir)
assert File.exists?(File.join(dest_dir, 'contacts.html'))
end
should "write properly without html extension" do
page = setup_page('contacts.html')
page.site.permalink_style = :pretty
do_render(page)
page.write(dest_dir)
assert File.directory?(dest_dir)
assert File.exists?(File.join(dest_dir, 'contacts', 'index.html'))
end
end
end
end