Merge pull request #2020 from jekyll/refactor-layout-reading
This commit is contained in:
commit
8f3e3e04d8
|
@ -48,6 +48,7 @@ require 'jekyll/errors'
|
||||||
require 'jekyll/related_posts'
|
require 'jekyll/related_posts'
|
||||||
require 'jekyll/cleaner'
|
require 'jekyll/cleaner'
|
||||||
require 'jekyll/entry_filter'
|
require 'jekyll/entry_filter'
|
||||||
|
require 'jekyll/layout_reader'
|
||||||
|
|
||||||
# extensions
|
# extensions
|
||||||
require 'jekyll/plugin'
|
require 'jekyll/plugin'
|
||||||
|
|
|
@ -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
|
|
@ -127,27 +127,11 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
def read
|
def read
|
||||||
self.read_layouts
|
self.layouts = LayoutReader.new(self).read
|
||||||
self.read_directories
|
self.read_directories
|
||||||
self.read_data(config['data_source'])
|
self.read_data(config['data_source'])
|
||||||
end
|
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
|
# Recursively traverse directories to find posts, pages and static files
|
||||||
# that will become part of the site according to the rules in
|
# that will become part of the site according to the rules in
|
||||||
# filter_entries.
|
# filter_entries.
|
||||||
|
|
|
@ -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
|
|
@ -161,11 +161,6 @@ class TestSite < Test::Unit::TestCase
|
||||||
assert_equal sorted_pages, @site.pages.map(&:name)
|
assert_equal sorted_pages, @site.pages.map(&:name)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "read layouts" do
|
|
||||||
@site.read_layouts
|
|
||||||
assert_equal ["default", "simple", "post/simple"].sort, @site.layouts.keys.sort
|
|
||||||
end
|
|
||||||
|
|
||||||
should "read posts" do
|
should "read posts" do
|
||||||
@site.read_posts('')
|
@site.read_posts('')
|
||||||
posts = Dir[source_dir('_posts', '**', '*')]
|
posts = Dir[source_dir('_posts', '**', '*')]
|
||||||
|
|
Loading…
Reference in New Issue