Skip to content

Commit

Permalink
Made pages respect permalinks style and permalinks in yml front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenebolshakov committed May 10, 2009
1 parent 605adf8 commit ee0167d
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 7 deletions.
51 changes: 44 additions & 7 deletions lib/jekyll/page.rb
Expand Up @@ -4,7 +4,7 @@ class Page
include Convertible

attr_accessor :site
attr_accessor :ext
attr_accessor :name, :ext
attr_accessor :data, :content, :output

# Initialize a new Page.
Expand All @@ -17,7 +17,7 @@ class Page
def initialize(site, base, dir, name)
@site = site
@base = base
@dir = dir
@dir = dir
@name = name

self.data = {}
Expand All @@ -27,6 +27,42 @@ def initialize(site, base, dir, name)
#self.transform
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
# +name+ is the String filename of the page file
#
Expand Down Expand Up @@ -55,16 +91,17 @@ def write(dest_prefix, dest_suffix = nil)
dest = File.join(dest, dest_suffix) if dest_suffix
FileUtils.mkdir_p(dest)

name = @name
if self.ext != ""
name = @name.split(".")[0..-2].join('.') + self.ext
# The url needs to be unescaped in order to preserve the correct filename
path = File.join(dest, CGI.unescape(self.url))
if self.url[/\.html$/].nil?
FileUtils.mkdir_p(path)
path = File.join(path, "index.html")
end

path = File.join(dest, name)
File.open(path, 'w') do |f|
f.write(self.output)
end
end
end

end
end
6 changes: 6 additions & 0 deletions test/source/about.html
@@ -0,0 +1,6 @@
---
title: About
permalink: /about/
---

About the site
5 changes: 5 additions & 0 deletions test/source/contacts.html
@@ -0,0 +1,5 @@
---
title: Contact Information
---

Contact Information
5 changes: 5 additions & 0 deletions test/test_generated_site.rb
Expand Up @@ -34,5 +34,10 @@ class TestGeneratedSite < Test::Unit::TestCase
should "not copy _posts directory" do
assert !File.exist?(dest_dir('_posts'))
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
79 changes: 79 additions & 0 deletions test/test_page.rb
@@ -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

0 comments on commit ee0167d

Please sign in to comment.