From ee0167d7060bd967204d30fb9736a23d3d134974 Mon Sep 17 00:00:00 2001 From: eugenebolshakov Date: Fri, 8 May 2009 10:40:11 +0400 Subject: [PATCH] Made pages respect permalinks style and permalinks in yml front matter --- lib/jekyll/page.rb | 51 ++++++++++++++++++++---- test/source/about.html | 6 +++ test/source/contacts.html | 5 +++ test/test_generated_site.rb | 5 +++ test/test_page.rb | 79 +++++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 test/source/about.html create mode 100644 test/source/contacts.html create mode 100644 test/test_page.rb diff --git a/lib/jekyll/page.rb b/lib/jekyll/page.rb index d1411e3d834..8cd0b2c8ba5 100644 --- a/lib/jekyll/page.rb +++ b/lib/jekyll/page.rb @@ -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. @@ -17,7 +17,7 @@ class Page def initialize(site, base, dir, name) @site = site @base = base - @dir = dir + @dir = dir @name = name self.data = {} @@ -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 + 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 + 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 + 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 # @@ -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 \ No newline at end of file +end diff --git a/test/source/about.html b/test/source/about.html new file mode 100644 index 00000000000..a6a79f932db --- /dev/null +++ b/test/source/about.html @@ -0,0 +1,6 @@ +--- +title: About +permalink: /about/ +--- + +About the site diff --git a/test/source/contacts.html b/test/source/contacts.html new file mode 100644 index 00000000000..1615afe2d85 --- /dev/null +++ b/test/source/contacts.html @@ -0,0 +1,5 @@ +--- +title: Contact Information +--- + +Contact Information diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 9b5dd6f0ff1..b9eeee244f2 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -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 diff --git a/test/test_page.rb b/test/test_page.rb new file mode 100644 index 00000000000..331e868b91b --- /dev/null +++ b/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