diff --git a/lib/jekyll.rb b/lib/jekyll.rb index caa45e50..803b6fa4 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -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' diff --git a/lib/jekyll/layout_reader.rb b/lib/jekyll/layout_reader.rb new file mode 100644 index 00000000..1b07c724 --- /dev/null +++ b/lib/jekyll/layout_reader.rb @@ -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 diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 7457ca6d..ed55598f 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -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 / 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. diff --git a/test/test_layout_reader.rb b/test/test_layout_reader.rb new file mode 100644 index 00000000..eead655a --- /dev/null +++ b/test/test_layout_reader.rb @@ -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 diff --git a/test/test_site.rb b/test/test_site.rb index 5e639f50..b1cf01d1 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -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', '**', '*')]