Merge pull request #2020 from jekyll/refactor-layout-reading

This commit is contained in:
Parker Moore 2014-02-08 00:31:41 -05:00
commit 8f3e3e04d8
5 changed files with 59 additions and 22 deletions

View File

@ -48,6 +48,7 @@ require 'jekyll/errors'
require 'jekyll/related_posts'
require 'jekyll/cleaner'
require 'jekyll/entry_filter'
require 'jekyll/layout_reader'
# extensions
require 'jekyll/plugin'

View File

@ -0,0 +1,40 @@
module Jekyll
class LayoutReader
attr_reader :site
def initialize(site)
@site = site
@layouts = {}
end
def read
layout_entries.each do |f|
@layouts[layout_name(f)] = Layout.new(site, layout_directory, f)
end
@layouts
end
private
def layout_entries
entries = []
within(layout_directory) do
entries = EntryFilter.new(site).filter(Dir['**/*.*'])
end
entries
end
def layout_name(file)
file.split(".")[0..-2].join(".")
end
def within(directory)
return unless File.exists?(directory)
Dir.chdir(directory) { yield }
end
def layout_directory
File.join(site.source, site.config['layouts'])
end
end
end

View File

@ -127,27 +127,11 @@ module Jekyll
#
# Returns nothing.
def read
self.read_layouts
self.layouts = LayoutReader.new(self).read
self.read_directories
self.read_data(config['data_source'])
end
# Read all the files in <source>/<layouts> and create a new Layout object
# with each one.
#
# Returns nothing.
def read_layouts
base = File.join(self.source, self.config['layouts'])
return unless File.exists?(base)
entries = []
Dir.chdir(base) { entries = filter_entries(Dir['**/*.*'], base) }
entries.each do |f|
name = f.split(".")[0..-2].join(".")
self.layouts[name] = Layout.new(self, base, f)
end
end
# Recursively traverse directories to find posts, pages and static files
# that will become part of the site according to the rules in
# filter_entries.

View File

@ -0,0 +1,17 @@
require 'helper'
class TestLayoutReader < Test::Unit::TestCase
context "reading layouts" do
setup do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir})
end
@site = Site.new(Jekyll.configuration)
end
should "read layouts" do
layouts = LayoutReader.new(@site).read
assert_equal ["default", "simple", "post/simple"].sort, layouts.keys.sort
end
end
end

View File

@ -161,11 +161,6 @@ class TestSite < Test::Unit::TestCase
assert_equal sorted_pages, @site.pages.map(&:name)
end
should "read layouts" do
@site.read_layouts
assert_equal ["default", "simple", "post/simple"].sort, @site.layouts.keys.sort
end
should "read posts" do
@site.read_posts('')
posts = Dir[source_dir('_posts', '**', '*')]